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:
042: package org.netbeans.modules.vmd.api.model;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046:
047: import junit.framework.Test;
048: import junit.framework.TestCase;
049: import junit.framework.TestSuite;
050: import org.netbeans.modules.vmd.api.model.common.PrimitiveDescriptorSupport;
051: import org.netbeans.modules.vmd.api.model.common.TypesSupport;
052:
053: import org.netbeans.modules.vmd.api.model.descriptors.FirstCD;
054: import org.netbeans.modules.vmd.api.model.utils.ModelTestUtil;
055:
056: import static org.netbeans.modules.vmd.api.model.utils.TestTypes.*;
057:
058: /**
059: *
060: * @author Karol Harezlak
061: */
062: public class PropertyValueTest extends TestCase {
063:
064: //This values have to equals values from PropertyValue class
065: private static final char USER_CODE_ID = 'U'; // NOI18N
066:
067: /*
068: private static final char NULL_ID = 'N'; // NOI18N
069: private static final char REFERENCE_ID = 'R'; // NOI18N
070: private static final char VALUE_ID = 'V'; // NOI18N
071: private static final char ENUM_ID = 'E'; // NOI18N
072: private static final char ARRAY_ID = 'A'; // NOI18N
073: private static final char ARRAY_SIZE_SEPARATOR = ':'; // NOI18N
074: private static final char ENCODED_LENGTH_SEPARATOR = '_'; // NOI18N
075: */
076: private DesignDocument document;
077: private PrimitiveDescriptor primitveDescritor = new DefaultPrimitiveDescriptor();
078:
079: public PropertyValueTest(String testName) {
080: super (testName);
081: }
082:
083: protected void setUp() throws Exception {
084: document = ModelTestUtil.createTestDesignDocument();
085: }
086:
087: protected void tearDown() throws Exception {
088: }
089:
090: public static Test suite() {
091: TestSuite suite = new TestSuite(PropertyValueTest.class);
092:
093: return suite;
094: }
095:
096: /**
097: * Test of createUserCode method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
098: */
099: public void testCreateUserCode() {
100: System.out.println("createUserCode"); // NOI18N
101:
102: String _userCode = " testing user code"; // NOI18N
103: PropertyValue.Kind _kind = PropertyValue.Kind.USERCODE;
104: String expResult = String.valueOf(this .USER_CODE_ID)
105: + _userCode;
106: PropertyValue result = PropertyValue.createUserCode(_userCode);
107:
108: assertEquals(_kind, result.getKind());
109: assertTrue(expResult.equals(result.toString()));
110: }
111:
112: /**
113: * Test of createComponentReference method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
114: */
115: public void testCreateComponentReference() {
116: System.out.println("createComponentReference"); // NOI18N
117:
118: final DesignDocument document = ModelTestUtil
119: .createTestDesignDocument("TEST_PROJECT"); // NOI18N
120:
121: document.getTransactionManager().writeAccess(new Runnable() {
122: public void run() {
123: DesignComponent expResult = document
124: .createComponent(FirstCD.TYPEID_CLASS);
125: DesignComponent result = PropertyValue
126: .createComponentReference(expResult)
127: .getComponent();
128: assertEquals(expResult, result);
129: }
130: });
131: }
132:
133: /**
134: * Test of createNull method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
135: */
136: public void testCreateNull() {
137: System.out.println("createNull");// NOI18N
138:
139: PropertyValue result = PropertyValue.createNull();
140:
141: assertNotNull(result);
142: assertEquals(result.getKind(), PropertyValue.Kind.NULL);
143: }
144:
145: /**
146: * Test of createValue method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
147: */
148: public void testCreateValue() {
149: System.out.println("createValue"); // NOI18N
150:
151: TypeID type = new TypeID(TypeID.Kind.PRIMITIVE, "javacode");// NOI18N
152: String value = "Test value";// NOI18N
153: PropertyValue result = PropertyValue
154: .createValue(
155: new PrimitiveDescriptorSupport()
156: .getDescriptorForTypeIDString(TypesSupport.TYPEID_JAVA_LANG_STRING
157: .getString()), type, value);
158: assertEquals(PropertyValue.Kind.VALUE, result.getKind());
159: assertEquals(type, result.getType());
160: assertEquals(value, result.getValue());
161: }
162:
163: /**
164: * Test of createArray method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
165: */
166: public void testCreateArray() {
167: System.out.println("createArray");// NOI18N
168:
169: TypeID type = new TypeID(TypeID.Kind.PRIMITIVE, "javacode");// NOI18N
170: PrimitiveDescriptor arrayDescriptor = new PrimitiveDescriptor() {
171: public Object deserialize(String serialized) {
172: return "array"; //NOI18N
173: }
174:
175: public boolean isValidInstance(Object object) {
176: return true;
177: }
178:
179: public String serialize(Object value) {
180: return "array"; // NOI18N
181: }
182: };
183:
184: PropertyValue arrayPropertyValue = PropertyValue.createValue(
185: arrayDescriptor, type,
186: DesignComponentTest.PROPERTY3_VALUE_STRING);// NOI18N
187: List<PropertyValue> array = new ArrayList();
188: array.add(arrayPropertyValue);
189:
190: PropertyValue result = PropertyValue.createArray(type, array);
191:
192: assertEquals(PropertyValue.Kind.ARRAY, result.getKind());
193: type = type.getArrayType();
194: assertEquals(type, result.getType());
195: assertNotNull(result.getArray());
196: }
197:
198: /**
199: * Test of createEmptyArray method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
200: */
201: public void testCreateEmptyArray() {
202: System.out.println("createEmptyArray"); // NOI18N
203:
204: TypeID componentType = new TypeID(TypeID.Kind.COMPONENT, "Root"); // NOI18N
205:
206: PropertyValue result = PropertyValue
207: .createEmptyArray(componentType);
208: List<PropertyValue> expResult = new ArrayList<PropertyValue>(0);
209:
210: assertEquals(expResult, result.getArray());
211: }
212:
213: /**
214: * Test of getKind method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
215: */
216: public void testGetKind() {
217: System.out.println("getKind"); // NOI18N
218:
219: PropertyValue.Kind expResult = PropertyValue.Kind.VALUE;
220: PropertyValue.Kind result;
221: String value = "Test value"; // NOI18N
222: TypeID type = new TypeID(TypeID.Kind.PRIMITIVE, "javacode");// NOI18N
223: PrimitiveDescriptor javacodeDescriptor = new PrimitiveDescriptor() {
224: public Object deserialize(String serialized) {
225: return "javacode"; //NOI18N
226: }
227:
228: public boolean isValidInstance(Object object) {
229: return true;
230: }
231:
232: public String serialize(Object value) {
233: return "javacode"; //NOI18N
234: }
235: };
236: result = PropertyValue.createValue(javacodeDescriptor, type,
237: value).getKind();
238:
239: assertEquals(expResult, result);
240: }
241:
242: /**
243: * Test of getType method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
244: */
245: public void testGetType() {
246: System.out.println("getType"); // NOI18N
247:
248: TypeID expResult;
249: TypeID result;
250: String value = "Test value"; // NOI18N
251: TypeID type = new TypeID(TypeID.Kind.PRIMITIVE, "javacode");// NOI18N
252:
253: result = PropertyValue.createValue(primitveDescritor, type,
254: value).getType();
255: expResult = type;
256: assertEquals(expResult, result);
257: }
258:
259: /**
260: * Test of getUserCode method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
261: */
262: public void testGetUserCode() {
263: System.out.println("getUserCode"); // NOI18N
264:
265: String userCode = "User Code;"; // NOI18N
266: String result = PropertyValue.createUserCode(userCode)
267: .getUserCode();
268: String expResult = userCode;
269:
270: assertEquals(expResult, result);
271: }
272:
273: /**
274: * Test of getComponent method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
275: */
276: public void testGetComponent() {
277: System.out.println("getComponent"); // NOI18N
278:
279: document.getTransactionManager().writeAccess(new Runnable() {
280: public void run() {
281: DesignComponent comp = document
282: .createComponent(FirstCD.TYPEID_CLASS);
283: DesignComponent result = PropertyValue
284: .createComponentReference(comp).getComponent();
285: DesignComponent expResult = comp;
286:
287: assertEquals(expResult, result);
288: }
289: });
290:
291: }
292:
293: /**
294: * Test of getValue method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
295: */
296: public void testGetValue() {
297: System.out.println("getValue"); // NOI18N
298:
299: PropertyValue result;
300: TypeID type = new TypeID(TypeID.Kind.PRIMITIVE, "javacode");// NOI18N
301: String value = "Test value"; // NOI18N
302:
303: result = PropertyValue.createValue(primitveDescritor, type,
304: value);
305:
306: assertEquals(PropertyValue.Kind.VALUE, result.getKind());
307: assertEquals(type, result.getType());
308: assertEquals(value, result.getValue());
309: }
310:
311: /**
312: * Test of getArray method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
313: */
314: public void testGetArray() {
315: System.out.println("getArray"); // NOI18N
316:
317: TypeID type = new TypeID(TypeID.Kind.PRIMITIVE, "javacode");// NOI18N
318: PropertyValue arrayPropertyValue = PropertyValue.createValue(
319: primitveDescritor, type,
320: DesignComponentTest.PROPERTY1_VALUE_STRING); // NOI18N
321: List<PropertyValue> array = new ArrayList();
322:
323: array.add(arrayPropertyValue);
324:
325: PropertyValue result = PropertyValue.createArray(type, array);
326:
327: type = type.getArrayType();
328: assertEquals(type, result.getType());
329: assertNotNull(result.getArray());
330: }
331:
332: /**
333: * Test of isCompatible method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
334: */
335: public void testIsCompatible() {
336: System.out.println("isCompatible"); // NOI18N
337:
338: TypeID requiredType = TYPEID_JAVA_LANG_STRING; // NOI18N
339: PropertyValue instance = TypesSupport
340: .createStringValue(FirstCD.PROPERTY_TEST); // NOI18N
341: boolean expResult = true;
342: boolean result = instance.isCompatible(requiredType);
343:
344: assertEquals(expResult, result);
345: }
346:
347: /**
348: * Test of serialize method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
349: */
350: //TODO Improve this test right know testing only if serialization works, doesn't test particular values
351: public void testSerialize() {
352: System.out.println("serialize and toString"); // NOI18N
353:
354: document.getTransactionManager().writeAccess(new Runnable() {
355: public void run() {
356: DesignComponent comp = document
357: .createComponent(FirstCD.TYPEID_CLASS);
358:
359: document.setRootComponent(comp);
360: String result = comp
361: .readProperty(FirstCD.PROPERTY_TEST).getValue()
362: .toString(); // NOI18N
363: assertNotNull(result);
364: }
365: });
366:
367: }
368:
369: /**
370: * Test of deserialize method, of class org.netbeans.modules.vmd.api.model.PropertyValue.
371: */
372: //TODO Improve this test right know testing only if desarialization works, doesn't test all Kinds
373: public void testDeserialize() {
374: System.out.println("deserialize"); // NOI18N
375:
376: document.getTransactionManager().writeAccess(new Runnable() {
377: public void run() {
378: DesignComponent comp = document
379: .createComponent(FirstCD.TYPEID_CLASS);
380:
381: document.setRootComponent(comp);
382:
383: String result = PropertyValue.deserialize(
384: comp.getReferenceValue().serialize(), document,
385: comp.getType()).serialize();
386: String expResult = comp.getReferenceValue().serialize();
387:
388: assertNotNull(result);
389: assertEquals(expResult, result);
390: }
391: });
392: }
393:
394: private class DefaultPrimitiveDescriptor implements
395: PrimitiveDescriptor {
396: public String serialize(Object value) {
397: return "default"; //NOI18N
398: }
399:
400: public Object deserialize(String serialized) {
401: return "default"; //NOI18N
402: }
403:
404: public boolean isValidInstance(Object object) {
405: return true;
406: }
407:
408: }
409: }
|