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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.api.model;
042:
043: import junit.framework.Test;
044: import junit.framework.TestCase;
045: import junit.framework.TestSuite;
046:
047: import org.netbeans.modules.vmd.api.model.descriptors.FirstCD;
048: import org.netbeans.modules.vmd.api.model.descriptors.SecondCD;
049: import org.netbeans.modules.vmd.api.model.utils.ModelTestUtil;
050:
051: import static org.netbeans.modules.vmd.api.model.utils.TestTypes.*;
052:
053: /**
054: *
055: * @auth Karol Harezlak
056: */
057: public class DesignComponentTest extends TestCase {
058:
059: private DesignDocument document;
060:
061: private final long comp1ID = 0;
062: private final long comp2ID = 1;
063:
064: public static final String PROPERTY1_VALUE_STRING = "VALUE1"; // NOI18N
065: public static final Integer PROPERTY2_VALUE_INT = new Integer(2); // NOI18N
066: public static final String PROPERTY3_VALUE_STRING = "VALUE3"; // NOI18N
067: public static final String PROPERTY4_VALUE_STRING = "VALUE4"; // NOI18N
068: public static final String PROJECT_TYPE = "testProject"; //NOI18N
069:
070: public DesignComponentTest(String testName) {
071: super (testName);
072: }
073:
074: protected void setUp() throws Exception {
075: document = ModelTestUtil.createTestDesignDocument();
076: }
077:
078: protected void tearDown() throws Exception {
079: }
080:
081: public static Test suite() {
082: TestSuite suite = new TestSuite(DesignComponentTest.class);
083:
084: return suite;
085: }
086:
087: /**
088: * Test of getComponentDescriptor method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
089: */
090: public void testGetComponentDescriptor() {
091: System.out.println("getComponentDescriptor"); // NOI18N
092:
093: document.getTransactionManager().writeAccess(new Runnable() {
094: public void run() {
095: ComponentDescriptor result = document.createComponent(
096: FirstCD.TYPEID_CLASS).getComponentDescriptor();
097:
098: assertNotNull(result);
099: assertEquals(FirstCD.TYPEID_CLASS.toString(), result
100: .getTypeDescriptor().getThisType().toString());
101: }
102: });
103:
104: }
105:
106: /**
107: * Test of getDocument method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
108: */
109: public void testGetDocument() {
110: System.out.println("getDocument"); // NOI18N
111:
112: document.getTransactionManager().writeAccess(new Runnable() {
113: public void run() {
114: DesignDocument result = document.createComponent(
115: FirstCD.TYPEID_CLASS).getDocument();
116: DesignDocument expResult = document;
117: assertEquals(expResult, result);
118: }
119: });
120: }
121:
122: /**
123: * Test of getComponentID method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
124: */
125: public void testGetComponentID() {
126: System.out.println("getComponentID"); // NOI18N
127:
128: document.getTransactionManager().writeAccess(new Runnable() {
129: public void run() {
130: long result = document.createComponent(
131: FirstCD.TYPEID_CLASS).getComponentID();
132: long expResult = comp1ID;
133: assertEquals(expResult, result);
134: }
135: });
136: }
137:
138: /**
139: * Test of getType method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
140: */
141: public void testGetType() {
142: System.out.println("getType"); // NOI18N
143:
144: document.getTransactionManager().writeAccess(new Runnable() {
145: public void run() {
146: TypeID result = document.createComponent(
147: FirstCD.TYPEID_CLASS).getType();
148: TypeID expResult = FirstCD.TYPEID_CLASS;
149: assertEquals(expResult, result);
150: }
151: });
152: }
153:
154: /**
155: * Test of getParentComponent method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
156: */
157: public void testGetParentComponent() {
158: System.out.println("getParentComponent"); // NOI18N
159:
160: document.getTransactionManager().writeAccess(new Runnable() {
161: public void run() {
162: DesignComponent comp1 = document
163: .createComponent(FirstCD.TYPEID_CLASS);
164: DesignComponent comp2 = document
165: .createComponent(SecondCD.TYPEID_CLASS);
166: DesignComponent result = comp1.getParentComponent();
167:
168: assertNull(result);
169: comp1.addComponent(comp2);
170: result = comp2.getParentComponent();
171: DesignComponent expResult = comp1;
172: assertEquals(expResult, result);
173: }
174: });
175: }
176:
177: /**
178: * Test of removeComponent method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
179: */
180: //TODO Strange you can add component to itself !!
181: public void testAddtRemoveComponent() {
182: System.out.println("addComponent, removeComponent"); // NOI18N
183:
184: document.getTransactionManager().writeAccess(new Runnable() {
185: public void run() {
186: DesignComponent comp1 = document
187: .createComponent(FirstCD.TYPEID_CLASS);
188: DesignComponent comp2 = document
189: .createComponent(SecondCD.TYPEID_CLASS);
190:
191: //addComponent
192: comp1.addComponent(comp2);
193: DesignComponent result = comp2.getParentComponent();
194: DesignComponent expResult = comp1;
195: assertEquals(result, expResult);
196: comp1.removeComponent(comp2);
197: //Check if comp2 has been remeved from comp1 by checking if he has paerent componet.
198: result = comp2.getParentComponent();
199: assertNull(result);
200: //Check if comp1 has any components
201: assertTrue(comp1.getComponents().size() == 0);
202: }
203: });
204: }
205:
206: /**
207: * Test of getComponents method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
208: */
209: public void testGetComponents() {
210: System.out.println("getComponents"); // NOI18N
211:
212: document.getTransactionManager().writeAccess(new Runnable() {
213: public void run() {
214: final int numberOfComp = 1;
215:
216: DesignComponent comp1 = document
217: .createComponent(FirstCD.TYPEID_CLASS);
218: DesignComponent comp2 = document
219: .createComponent(SecondCD.TYPEID_CLASS);
220: comp1.addComponent(comp2);
221: //Check if comp1 has added component
222: assertTrue(comp1.getComponents().size() == numberOfComp);
223: }
224: });
225: }
226:
227: /**
228: * Test of writeProperty method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
229: */
230: public void testReadWriteProperty() {
231: System.out.println("readProperty, writeProperty"); // NOI18N
232:
233: document.getTransactionManager().writeAccess(new Runnable() {
234: public void run() {
235: DesignComponent comp1 = document
236: .createComponent(FirstCD.TYPEID_CLASS);
237: DesignComponent comp2 = document
238: .createComponent(SecondCD.TYPEID_CLASS);
239:
240: // writeProperty to Component
241: comp1
242: .writeProperty(
243: FirstCD.PROPERTY_TEST,
244: PropertyValue
245: .createValue(
246: PrimitiveDescriptorFactoryRegistry
247: .getDescriptor(
248: PROJECT_TYPE,
249: TYPEID_JAVA_LANG_STRING),
250: TYPEID_JAVA_LANG_STRING,
251: PROPERTY1_VALUE_STRING));
252: comp2.writeProperty(SecondCD.PROPERTY_INT,
253: PropertyValue.createValue(
254: PrimitiveDescriptorFactoryRegistry
255: .getDescriptor(PROJECT_TYPE,
256: TYPEID_INT),
257: TYPEID_INT, PROPERTY2_VALUE_INT));
258:
259: }
260: });
261:
262: document.getTransactionManager().readAccess(new Runnable() {
263: public void run() {
264: String result1 = (String) document.getComponentByUID(
265: comp1ID).readProperty(FirstCD.PROPERTY_TEST)
266: .getValue();
267: String expResult1 = PROPERTY1_VALUE_STRING;
268:
269: assertEquals(expResult1, result1);
270: Integer result2 = (Integer) document.getComponentByUID(
271: comp2ID).readProperty(SecondCD.PROPERTY_INT)
272: .getValue();
273: Integer expResult2 = PROPERTY2_VALUE_INT;
274: assertEquals(expResult2, result2);
275: }
276: });
277: }
278:
279: /**
280: * Test of isDefaultValue method, of class org.netbeans.modules.vmd.api.model.DesignComponent.
281: */
282: public void testIsDefaultValueResetToDefault() {
283: System.out.println("isDefaultValue, resetToDefault"); // NOI18N
284:
285: document.getTransactionManager().writeAccess(new Runnable() {
286: public void run() {
287: DesignComponent comp1 = document
288: .createComponent(FirstCD.TYPEID_CLASS);
289: comp1
290: .writeProperty(
291: FirstCD.PROPERTY_TEST,
292: PropertyValue
293: .createValue(
294: PrimitiveDescriptorFactoryRegistry
295: .getDescriptor(
296: PROJECT_TYPE,
297: TYPEID_JAVA_LANG_STRING),
298: TYPEID_JAVA_LANG_STRING,
299: "someDifferentValue")); // NOI18N
300:
301: boolean result = false;
302: try {
303: result = comp1
304: .isDefaultValue(FirstCD.PROPERTY_TEST);
305: } catch (Exception ex) {
306: ex.printStackTrace();
307: }
308: boolean expResult = false;
309:
310: assertEquals(expResult, result);
311: comp1.resetToDefault(FirstCD.PROPERTY_TEST);
312: result = comp1.isDefaultValue(FirstCD.PROPERTY_TEST);
313: expResult = true;
314: assertEquals(expResult, result);
315: }
316: });
317: }
318: }
|