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 java.util.Collection;
044: import java.util.HashSet;
045:
046: import junit.framework.Test;
047: import junit.framework.TestCase;
048: import junit.framework.TestSuite;
049:
050: import org.netbeans.modules.vmd.api.model.descriptors.FirstCD;
051: import org.netbeans.modules.vmd.api.model.descriptors.SecondCD;
052: import org.netbeans.modules.vmd.api.model.utils.ModelTestUtil;
053:
054: /**
055: *
056: * @author Karol Harezlak
057: */
058: public class DesignDocumentTest extends TestCase {
059:
060: private DesignDocument document;
061:
062: public DesignDocumentTest(String testName) {
063: super (testName);
064: }
065:
066: protected void setUp() throws Exception {
067: document = ModelTestUtil.createTestDesignDocument();
068: }
069:
070: protected void tearDown() throws Exception {
071: }
072:
073: public static Test suite() {
074: TestSuite suite = new TestSuite(DesignDocumentTest.class);
075:
076: return suite;
077: }
078:
079: /**
080: * Test of getDescriptorRegistry method, of class org.netbeans.modules.vmd.api.model.DesignDocument.
081: */
082: public void testGetDescriptorRegistry() {
083: System.out.println("getDescriptorRegistry"); // NOI18N
084:
085: final DescriptorRegistry result = document
086: .getDescriptorRegistry();
087:
088: assertNotNull(result);
089: }
090:
091: /**
092: * Test of getListenerManager method, of class org.netbeans.modules.vmd.api.model.DesignDocument.
093: */
094: public void testGetListenerManager() {
095: System.out.println("getListenerManager"); // NOI18N
096:
097: ListenerManager listenerManager = document.getListenerManager();
098: assertNotNull(listenerManager);
099: DesignListener listener = new DesignListener() {
100: public void designChanged(DesignEvent event) {
101: //TODO Fill it with code
102: }
103: };
104:
105: assertNotNull(listenerManager);
106:
107: //Additional test
108: //TODO Expand this part of some real situatuon with listeneres, documents and components
109: listenerManager.addDesignListener(listener,
110: new DesignEventFilter());
111: listenerManager.removeDesignListener(listener);
112:
113: }
114:
115: /**
116: * Test of getTransactionManager method, of class org.netbeans.modules.vmd.api.model.DesignDocument.
117: */
118: public void testGetTransactionManager() {
119: System.out.println("getTransactionManager"); // NOI18N1
120:
121: final long compID = 0;
122: TransactionManager result = document.getTransactionManager();
123: assertNotNull(result);
124: result.writeAccess(new Runnable() {
125: public void run() {
126: document.createComponent(FirstCD.TYPEID_CLASS);
127: }
128: });
129: result.readAccess(new Runnable() {
130: public void run() {
131: document.getComponentByUID(compID);
132: }
133: });
134: }
135:
136: /**
137: * Test of getDeleteComponent method, of class org.netbeans.modules.vmd.api.model.DesignDocument.
138: */
139:
140: @SuppressWarnings("deprecation")
141: // NOI18N
142: public void testDeleteComponent() {
143: //TODO Right now there is no way to tested if document is deleted or not. This test only check if there is any exception rise when component is deleted
144: System.out.println("deleteComponent"); // NOI18N
145:
146: document.getTransactionManager().writeAccess(new Runnable() {
147: public void run() {
148: //createComponent
149: DesignComponent comp = document
150: .createComponent(FirstCD.TYPEID_CLASS);
151: DesignComponent comp2 = document
152: .createComponent(SecondCD.TYPEID_CLASS);
153: document.setRootComponent(comp);
154: comp.addComponent(comp2);
155:
156: document.deleteComponent(comp2);
157: }
158: });
159: }
160:
161: /**
162: * Test of getSelectedComponents method, of class org.netbeans.modules.vmd.api.model.DesignDocument.
163: */
164: public void testGetSetSelectedComponents() {
165: System.out
166: .println("getSetSelectedComponents, setSelectedComponents, getSelectionSourceID"); // NOI18N
167:
168: document.getTransactionManager().writeAccess(new Runnable() {
169: public void run() {
170: String sourceID = "TestSorceId"; // NOI18N
171: DesignComponent comp1 = document
172: .createComponent(FirstCD.TYPEID_CLASS);
173: DesignComponent comp2 = document
174: .createComponent(SecondCD.TYPEID_CLASS);
175:
176: Collection<DesignComponent> selectedComponents = new HashSet<DesignComponent>();
177: selectedComponents.add(comp1);
178: selectedComponents.add(comp2);
179: document.setSelectedComponents(sourceID,
180: selectedComponents); // NOI18N
181: for (DesignComponent comp : selectedComponents) {
182: boolean result = document.getSelectedComponents()
183: .contains(comp);
184: assertTrue(result);
185: }
186: String result = document.getSelectionSourceID();
187: String expResult = sourceID;
188: assertEquals(expResult, result);
189: }
190: });
191: }
192:
193: /**
194: * Complex test (addDocument, addComponent, getRootComponent, setRootComponent, createComponent, of class org.netbeans.modules.vmd.api.model.DesignDocument.
195: */
196: public void testComplex() {
197: System.out
198: .println("addDocument, addComponent, getRootComponent, setRootComponent,"
199: + //NOI18N
200: " createComponent,getComponentByID"
201: + "getComponentByID"); // NOI18N
202:
203: final DesignDocument instance = ModelTestUtil
204: .createTestDesignDocument(ModelTestUtil.PROJECT_ID);
205:
206: instance.getTransactionManager().writeAccess(new Runnable() {
207: public void run() {
208: DesignComponent comp1 = instance
209: .createComponent(FirstCD.TYPEID_CLASS);
210: DesignComponent comp2 = instance
211: .createComponent(SecondCD.TYPEID_CLASS);
212:
213: //setRooComponent, addComponent
214: instance.setRootComponent(comp1);
215: comp1.addComponent(comp2);
216: //getComponentByID
217: DesignComponent resultComp2ByID = instance
218: .getComponentByUID(comp2.getComponentID());
219: DesignComponent expComp2 = comp2;
220: assertEquals(expComp2, resultComp2ByID);
221: //setRootComponent getRootComponent
222: DesignComponent expGetComp = comp1;
223: DesignComponent resultGetComp = instance
224: .getRootComponent();
225: assertEquals(expGetComp, resultGetComp);
226: // writeProperty to Component
227: comp1.writeProperty(FirstCD.PROPERTY_REFERENCE,
228: PropertyValue.createComponentReference(comp2));
229: }
230: });
231: }
232: }
|