InputTests.java :  » J2EE » Expresso » com » jcorporate » expresso » core » controller » tests » Java Open Source

Java Open Source » J2EE » Expresso 
Expresso » com » jcorporate » expresso » core » controller » tests » InputTests.java
/* ====================================================================
 * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
 *
 * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by Jcorporate Ltd.
 *        (http://www.jcorporate.com/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. "Jcorporate" and product names such as "Expresso" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written permission,
 *    please contact info@jcorporate.com.
 *
 * 5. Products derived from this software may not be called "Expresso",
 *    or other Jcorporate product names; nor may "Expresso" or other
 *    Jcorporate product names appear in their name, without prior
 *    written permission of Jcorporate Ltd.
 *
 * 6. No product derived from this software may compete in the same
 *    market space, i.e. framework, without prior written permission
 *    of Jcorporate Ltd. For written permission, please contact
 *    partners@jcorporate.com.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Jcorporate Ltd. Contributions back
 * to the project(s) are encouraged when you make modifications.
 * Please send them to support@jcorporate.com. For more information
 * on Jcorporate Ltd. and its products, please see
 * <http://www.jcorporate.com/>.
 *
 * Portions of this software are based upon other open source
 * products and are subject to their respective licenses.
 */

package com.jcorporate.expresso.core.controller.tests;

import com.jcorporate.expresso.core.controller.Input;
import com.jcorporate.expresso.core.dbobj.ValidValue;
import com.jcorporate.expresso.core.misc.StringDOMParser;
import com.jcorporate.expresso.kernel.util.FastStringBuffer;
import com.jcorporate.expresso.services.test.CommandLineParser;
import com.jcorporate.expresso.services.test.ExpressoTestCase;
import junit.framework.TestSuite;
import org.w3c.dom.Document;

import java.util.Enumeration;
import java.util.Vector;


/**
 * Unit test case for testing com.jcorporate.expresso.core.controller.Input
 * objects.
 *
 * @author Michael Rimov
 * @version $Revision: 1.2 $
 */
public class InputTests
        extends ExpressoTestCase {
    Input i = null;
    StringDOMParser parser = null;

    public InputTests(String testName)
            throws Exception {
        super(testName);
    }

    public static void main(String[] args)
            throws Exception {

        //Set the system properties we need
        CommandLineParser.parseCommandLine(args);
        junit.textui.TestRunner.run(suite());
    }

    public void setUp()
            throws Exception {
        i = new Input("Test Input", "This is a test Input");
        i.setLookup("com.jcorporate.expresso.services.dbobj.MimeTypes");
        i.setDescription("This is a test input description");
        i.setDisplayLength(30);
        i.setType("boolean");
        i.setAttribute("checkbox", "");
        parser = new StringDOMParser();
    }

    /**
     * Filler Function to conform to log4j test suites.
     */
    public static junit.framework.Test suite()
            throws Exception {
        return new TestSuite(InputTests.class);
    }

    /**
     * Helper function to set up the parser and parse the XML returned
     * from the input
     */
    private Document getXMLDoc()
            throws Exception {
        FastStringBuffer fsb = new FastStringBuffer(128);
        String result = i.toXML(fsb).toString();
        System.out.println("\nInput Before Serialization:");
        System.out.println(result);

        Document d = parser.parseString(result);

        if (d == null) {
            fail("Input returned mal-formed XML document");
        }

        return d;
    }

    /**
     * Helper to rebuild the input from XML
     */
    private Input getInput(Document d)
            throws Exception {
        Input anotherI = (Input) Input.fromXML(d);

        //        System.out.println("\nInput After De-Serialization:");
        //        System.out.println(anotherI.toXML(fsb).toString());
        return anotherI;
    }

    /**
     * Test basic Input->XML->Input Serialization
     */
    public void testXML()
            throws Exception {
        Document d = getXMLDoc();
        Input newInput = getInput(d);
        assertTrue("getInput returned null", newInput != null);
        assertTrue("Proper Display Length", newInput.getDisplayLength() == 30);
        assertTrue("Proper Lookup",
                newInput.getLookup().equals("com.jcorporate.expresso.services.dbobj.MimeTypes"));
        assertTrue("Proper Name", newInput.getName().equals("Test Input"));
        assertTrue("Proper Description",
                newInput.getDescription().equals("This is a test input description"));
        assertTrue("Proper Label",
                newInput.getLabel().equals("This is a test Input"));
        assertTrue("Proper Type", newInput.getType().equals("boolean"));
        assertTrue("Checkbox Attribute Exists",
                newInput.getAttribute("checkbox") != null);
    }

    /**
     * Same as testXML, but test MultiValues too
     */
    public void testXMLMultiValues()
            throws Exception {
        i.addValidValue("one", "One Value");
        i.addValidValue("two", "Two Values");
        i.addValidValue("three", "Three Values");
        i.setType("C");

        Document d = getXMLDoc();
        Input newInput = getInput(d);
        assertTrue("getInput returned null", newInput != null);
        assertTrue("Proper Display Length", newInput.getDisplayLength() == 30);
        assertTrue("Proper Lookup",
                newInput.getLookup().equals("com.jcorporate.expresso.services.dbobj.MimeTypes"));
        assertTrue("Proper Name", newInput.getName().equals("Test Input"));
        assertTrue("Proper Description",
                newInput.getDescription().equals("This is a test input description"));
        assertTrue("Proper Label",
                newInput.getLabel().equals("This is a test Input"));
        assertTrue("Proper Type", newInput.getType().equals("C"));
        assertTrue("Checkbox Attribute Exists",
                newInput.getAttribute("checkbox") != null);

        //Now check valid values
        Vector v = newInput.getValidValues();
        assertTrue("Returned Vector of valid values",
                (v != null && !v.isEmpty()));
        assertTrue("Returned proper number of valid values", v.size() == 3);

        for (Enumeration e = v.elements(); e.hasMoreElements();) {
            ValidValue vv = (ValidValue) e.nextElement();
            assertTrue("Valid Value != null", vv != null);

            String value = vv.getValue();
            String description = vv.getDescription();

            if (value.equals("one")) {
                assertTrue("Proper Description for one",
                        description.equals("One Value"));
            } else if (value.equals("two")) {
                assertTrue("Proper Description for two",
                        description.equals("Two Values"));
            } else if (value.equals("three")) {
                assertTrue("Proper Description for three",
                        description.equals("Three Values"));
            } else {
                fail("Got invalid valid value: " + value);
            }
        }
    }

    /**
     * Same as testXML, but test with nested inputs
     */
    public void testXMLWithNestedInput()
            throws Exception {
        Input nested = new Input("nested1", "label1");
        i.addNested(nested);
        nested = new Input("nested2", "label2");
        i.addNested(nested);

        Document d = getXMLDoc();
        Input newInput = getInput(d);
        assertTrue("getInput returned null", newInput != null);
        assertTrue("Proper Display Length", newInput.getDisplayLength() == 30);
        assertTrue("Proper Lookup",
                newInput.getLookup().equals("com.jcorporate.expresso.services.dbobj.MimeTypes"));
        assertTrue("Proper Name", newInput.getName().equals("Test Input"));
        assertTrue("Proper Description",
                newInput.getDescription().equals("This is a test input description"));
        assertTrue("Proper Label",
                newInput.getLabel().equals("This is a test Input"));
        assertTrue("Proper Type", newInput.getType().equals("boolean"));
        assertTrue("Checkbox Attribute Exists",
                newInput.getAttribute("checkbox") != null);
        nested = (Input) newInput.getNested("nested1");
        assertTrue("Got nested 1", nested != null);
        assertTrue("Proper Nested 1 Label", nested.getLabel().equals("label1"));
        nested = (Input) newInput.getNested("nested2");
        assertTrue("Got nested 2", nested != null);
        assertTrue("Proper Nested 2 Label", nested.getLabel().equals("label2"));
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.