001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.visualweb.insync.java;
041:
042: import com.sun.rave.designtime.ContextMethod;
043: import java.lang.reflect.Modifier;
044: import junit.framework.Test;
045: import junit.framework.TestSuite;
046: import org.netbeans.junit.NbTestSuite;
047: import org.netbeans.modules.visualweb.insync.InsyncTestBase;
048: import org.netbeans.modules.visualweb.insync.ParserAnnotation;
049: import org.netbeans.modules.visualweb.insync.Unit;
050: import org.netbeans.modules.visualweb.insync.models.FacesModel;
051: import org.netbeans.modules.visualweb.insync.models.FacesModelSet;
052:
053: /**
054: *
055: * @author jdeva
056: */
057: public class JavaUnitTest extends InsyncTestBase {
058: public JavaUnitTest(String testName) {
059: super (testName);
060: }
061:
062: public static void main(java.lang.String[] args) {
063: junit.textui.TestRunner.run(suite());
064: }
065:
066: public static Test suite() {
067: TestSuite suite = new NbTestSuite(JavaUnitTest.class);
068: return suite;
069: }
070:
071: @Override
072: protected void setUp() throws Exception {
073: super .setUp();
074: }
075:
076: @Override
077: protected void tearDown() throws Exception {
078: super .tearDown();
079: }
080:
081: JavaUnit getJavaUnit() {
082: FacesModelSet set = createFacesModelSet();
083: FacesModel model = set
084: .getFacesModel(getJavaFile(getPageBeans()[0]));
085: model.sync();
086: return model.getJavaUnit();
087: }
088:
089: /**
090: * Test of destroy method, of class JavaUnit.
091: */
092: public void testDestroy() {
093: System.out.println("destroy");
094: JavaUnit instance = getJavaUnit();
095: instance.destroy();
096: }
097:
098: /**
099: * Test of sync method, of class JavaUnit.
100: */
101: public void testSync() {
102: System.out.println("sync");
103: JavaUnit instance = getJavaUnit();
104: boolean result = instance.sync();
105: //because the unit has been alreay synced because of getJavaUnit()
106: assertEquals(result, false);
107: assertEquals(instance.getState(), Unit.State.CLEAN);
108:
109: //Try syncing again
110: instance.setSourceDirty();
111: result = instance.sync();
112: assertEquals(result, true);
113: assertEquals(instance.getState(), Unit.State.CLEAN);
114:
115: //Introduce errors and sync again
116: ContextMethod cm = new ContextMethod(null, "foo",
117: Modifier.PUBLIC, Void.TYPE, new Class[] {},
118: new String[] {});
119: Method method = instance.getJavaClass().addMethod(cm);
120: method.replaceBody("int i\nin j;");
121: instance.setSourceDirty();
122: result = instance.sync();
123: assertEquals(result, true);
124: assertEquals(instance.getState(), Unit.State.BUSTED);
125: }
126:
127: /**
128: * Test of getJavaClass method, of class JavaUnit.
129: */
130: public void testGetJavaClass() {
131: System.out.println("getJavaClass");
132: JavaUnit instance = getJavaUnit();
133: JavaClass result = instance.getJavaClass();
134: assertNotNull(result);
135: }
136:
137: /**
138: * Test of getErrors method, of class JavaUnit.
139: */
140: public void testGetErrors() {
141: System.out.println("getErrors");
142: JavaUnit instance = getJavaUnit();
143: ParserAnnotation[] expResult = ParserAnnotation.EMPTY_ARRAY;
144: ParserAnnotation[] result = instance.getErrors();
145: assertEquals(expResult, result);
146:
147: //Introduce errors and check again
148: ContextMethod cm = new ContextMethod(null, "foo",
149: Modifier.PUBLIC, Void.TYPE, new Class[] {},
150: new String[] {});
151: Method method = instance.getJavaClass().addMethod(cm);
152: method.replaceBody("int i\nin j;");
153: instance.setSourceDirty();
154: instance.sync();
155: result = instance.getErrors();
156: assert (result.length > 0);
157: }
158:
159: /**
160: * Test of ensureImport method, of class JavaUnit.
161: */
162: public void testEnsureImport() {
163: System.out.println("ensureImport");
164: String fqn = "com.sun.webui.jsf.component.Button";
165: JavaUnit instance = getJavaUnit();
166: boolean result = instance.ensureImport(fqn);
167: assertEquals(true, result);
168:
169: //Try again after the import is added
170: result = instance.ensureImport(fqn);
171: assertEquals(true, result);
172:
173: //Try importing with same short name but different FQN
174: fqn = "test.Button";
175: result = instance.ensureImport(fqn);
176: assertEquals(false, result);
177: }
178:
179: // /**
180: // * Test of insertUpdate method, of class JavaUnit.
181: // */
182: // public void testInsertUpdate() {
183: // System.out.println("insertUpdate");
184: // DocumentEvent e = null;
185: // JavaUnit instance = null;
186: // instance.insertUpdate(e);
187: // // TODO review the generated test code and remove the default call to fail.
188: // fail("The test case is a prototype.");
189: // }
190: //
191: // /**
192: // * Test of removeUpdate method, of class JavaUnit.
193: // */
194: // public void testRemoveUpdate() {
195: // System.out.println("removeUpdate");
196: // DocumentEvent e = null;
197: // JavaUnit instance = null;
198: // instance.removeUpdate(e);
199: // // TODO review the generated test code and remove the default call to fail.
200: // fail("The test case is a prototype.");
201: // }
202:
203: }
|