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: /*
043: * XAMCatalogModelTest.java
044: * JUnit based test
045: *
046: * Created on December 7, 2006, 3:03 PM
047: */
048:
049: package org.netbeans.modules.xml.retriever.catalog.model;
050:
051: import java.beans.PropertyChangeEvent;
052: import java.beans.PropertyChangeListener;
053: import java.io.File;
054: import java.net.URI;
055: import java.net.URISyntaxException;
056: import java.util.List;
057: import javax.swing.text.BadLocationException;
058: import junit.framework.*;
059: import org.netbeans.modules.xml.xam.ModelSource;
060: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063:
064: /**
065: *
066: * @author girix
067: */
068: public class XAMCatalogModelTest extends TestCase implements
069: PropertyChangeListener {
070:
071: public XAMCatalogModelTest(String testName) {
072: super (testName);
073: }
074:
075: protected void setUp() throws Exception {
076: }
077:
078: protected void tearDown() throws Exception {
079: }
080:
081: public static Test suite() {
082: TestSuite suite = new TestSuite(XAMCatalogModelTest.class);
083:
084: return suite;
085: }
086:
087: int eventFired = 0;
088:
089: public void testCatalogModel() throws URISyntaxException,
090: BadLocationException {
091: File inputFile = null;
092:
093: try {
094: inputFile = new File(XAMCatalogModelTest.class.getResource(
095: "catalog.xml").toURI());
096: } catch (URISyntaxException ex) {
097: assert false;
098: ex.printStackTrace();
099: return;
100: }
101:
102: FileObject inputFO = FileUtil.toFileObject(FileUtil
103: .normalizeFile(inputFile));
104:
105: ModelSource source = null;
106: try {
107: source = TestUtil.createModelSource(inputFO, true);
108: } catch (CatalogModelException ex) {
109: assert false;
110: ex.printStackTrace();
111: return;
112: }
113:
114: CatalogModelFactory instance = CatalogModelFactory
115: .getInstance();
116: CatalogModel cm = instance.getModel(source);
117: assertNotNull(cm);
118:
119: Catalog cat = cm.getRootComponent();
120: assertNotNull(cat);
121:
122: cat.addPropertyChangeListener(this );
123:
124: /* system related tests*/
125: List<System> systems = cat.getSystems();
126: assertNotNull(systems);
127:
128: assertEquals(1, systems.size());
129: System system = systems.get(0);
130:
131: system.addPropertyChangeListener(this );
132:
133: assertEquals("sysIDAttr", system.getSystemIDAttr());
134: assertEquals("uriAttr", system.getURIAttr());
135: assertEquals("xprojCatRefAttr", system
136: .getXprojectCatalogFileLocationAttr());
137: assertEquals("refFileAttr", system.getReferencingFileAttr());
138:
139: system.getModel().startTransaction();
140: system.setSystemIDAttr(new URI("dummy"));
141: system.setURIAttr(new URI("dummy"));
142: system.setXprojectCatalogFileLocationAttr(new URI("dummy"));
143: system.setReferencingFileAttr(new URI("dummy"));
144: system.getModel().endTransaction();
145:
146: assertEquals(4, eventFired);
147: eventFired = 0;
148:
149: assertEquals("dummy", system.getSystemIDAttr());
150: assertEquals("dummy", system.getURIAttr());
151: assertEquals("dummy", system
152: .getXprojectCatalogFileLocationAttr());
153: assertEquals("dummy", system.getReferencingFileAttr());
154:
155: System nsys = (System) system.copy(cat);
156:
157: cat.getModel().startTransaction();
158: cat.removeSystem(system);
159: cat.getModel().endTransaction();
160:
161: assertEquals(1, eventFired);
162: eventFired = 0;
163:
164: assertEquals(0, cat.getSystems().size());
165:
166: cat.getModel().startTransaction();
167: cat.addSystem(nsys);
168: cat.getModel().endTransaction();
169:
170: assertEquals(1, eventFired);
171: eventFired = 0;
172:
173: assertEquals(1, cat.getSystems().size());
174:
175: /* NextCatalog testcases*/
176: List<NextCatalog> nextCatalogs = cat.getNextCatalogs();
177: assertNotNull(nextCatalogs);
178:
179: assertEquals(1, nextCatalogs.size());
180: NextCatalog nextCatalog = nextCatalogs.get(0);
181:
182: nextCatalog.addPropertyChangeListener(this );
183:
184: assertEquals("catalogAttr", nextCatalog.getCatalogAttr());
185:
186: cat.getModel().startTransaction();
187: try {
188: nextCatalog.setCatalogAttr(new URI("dummy"));
189: } finally {
190: cat.getModel().endTransaction();
191: }
192:
193: assertEquals(1, eventFired);
194: eventFired = 0;
195:
196: assertEquals("dummy", nextCatalog.getCatalogAttr());
197:
198: NextCatalog nncat = (NextCatalog) nextCatalog.copy(cat);
199:
200: cat.getModel().startTransaction();
201: cat.removeNextCatalog(nextCatalog);
202: cat.getModel().endTransaction();
203:
204: assertEquals(1, eventFired);
205: eventFired = 0;
206:
207: assertEquals(0, cat.getNextCatalogs().size());
208:
209: cat.getModel().startTransaction();
210: cat.addNextCatalog(nncat);
211: cat.getModel().endTransaction();
212:
213: assertEquals(1, eventFired);
214: eventFired = 0;
215:
216: assertEquals(1, cat.getNextCatalogs().size());
217:
218: /*Document doc = (Document) cat.getModel().getModelSource().getLookup().lookup(Document.class);;
219: java.lang.System.out.println(doc.getText(0, doc.getLength()));*/
220: }
221:
222: public void propertyChange(PropertyChangeEvent evt) {
223: eventFired++;
224: }
225:
226: public void testEmptyCatalogModel() throws URISyntaxException,
227: BadLocationException {
228: File inputFile = null;
229:
230: try {
231: inputFile = new File(XAMCatalogModelTest.class.getResource(
232: "empty.xml").toURI());
233: } catch (URISyntaxException ex) {
234: assert false;
235: ex.printStackTrace();
236: return;
237: }
238:
239: FileObject inputFO = FileUtil.toFileObject(FileUtil
240: .normalizeFile(inputFile));
241:
242: ModelSource source = null;
243: try {
244: source = TestUtil.createModelSource(inputFO, true);
245: } catch (CatalogModelException ex) {
246: assert false;
247: ex.printStackTrace();
248: return;
249: }
250:
251: CatalogModelFactory instance = CatalogModelFactory
252: .getInstance();
253: CatalogModel cm = instance.getModel(source);
254: assertNotNull(cm);
255:
256: assertNotNull(cm.getRootComponent());
257:
258: NextCatalog nc = cm.getFactory().createNextCatalog();
259:
260: cm.startTransaction();
261: try {
262: cm.getRootComponent().addNextCatalog(nc);
263: nc.setCatalogAttr(new URI("dummy"));
264: } finally {
265: cm.endTransaction();
266: }
267:
268: assertEquals(1, cm.getRootComponent().getNextCatalogs().size());
269:
270: /*Document doc = (Document) cm.getModelSource().getLookup().lookup(Document.class);;
271: java.lang.System.out.println(doc.getText(0, doc.getLength()));*/
272: }
273:
274: }
|