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: * XAMCatalogWriteModelTest.java
044: * JUnit based test
045: *
046: * Created on December 14, 2006, 4:19 PM
047: */
048:
049: package org.netbeans.modules.xml.retriever.catalog.impl;
050:
051: import javax.swing.text.BadLocationException;
052: import javax.swing.text.Document;
053: import junit.framework.*;
054: import java.io.File;
055: import java.io.IOException;
056: import java.net.URI;
057: import java.net.URISyntaxException;
058: import org.netbeans.modules.xml.retriever.catalog.model.CatalogModelFactory;
059: import org.netbeans.modules.xml.retriever.catalog.model.TestUtil;
060: import org.netbeans.modules.xml.xam.ModelSource;
061: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064:
065: /**
066: *
067: * @author girix
068: */
069: public class XAMCatalogWriteModelTest extends TestCase {
070:
071: public XAMCatalogWriteModelTest(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(XAMCatalogWriteModelTest.class);
083:
084: return suite;
085: }
086:
087: public void testSearchURI() {
088: URI locationURI = null;
089: try {
090: locationURI = new URI("sysIDAttr");
091: } catch (URISyntaxException ex) {
092: }
093:
094: XAMCatalogWriteModelImpl instance = getTestCatModelInstance();
095:
096: URI result = instance.searchURI(locationURI);
097: assertNotNull(result);
098: }
099:
100: public void testAddAndRemoveURI() throws Exception {
101: URI leftURI = new URI("girish");
102: URI rightURI = new URI("kumar");
103: FileObject fileObj = null;
104: MyXAMCatalogWriteModel instance = getTestCatModelInstance();
105:
106: int start = instance.getCatalogEntries().size();
107:
108: instance.addURI(leftURI, rightURI);
109:
110: assertEquals(start + 1, instance.getCatalogEntries().size());
111:
112: instance.removeURI(leftURI);
113:
114: assertEquals(start, instance.getCatalogEntries().size());
115: }
116:
117: public MyXAMCatalogWriteModel getTestCatModelInstance() {
118: FileObject inputFile = null;
119:
120: try {
121: inputFile = FileUtil.toFileObject(FileUtil
122: .normalizeFile(new File(
123: XAMCatalogWriteModelTest.class.getResource(
124: "catalog.xml").toURI())));
125: } catch (URISyntaxException ex) {
126: assert false;
127: ex.printStackTrace();
128: return null;
129: }
130:
131: MyXAMCatalogWriteModel instance = null;
132: try {
133: instance = new MyXAMCatalogWriteModel(inputFile);
134: } catch (IOException ex) {
135: assert false;
136: } catch (CatalogModelException ex) {
137: ex.printStackTrace();
138: assert false;
139: }
140:
141: return instance;
142: }
143:
144: public void testAddNextCatalog() throws Exception {
145: URI leftURI = new URI("girish");
146: FileObject fileObj = null;
147: MyXAMCatalogWriteModel instance = getTestCatModelInstance();
148:
149: int start = instance.getCatalogEntries().size();
150:
151: instance.addNextCatalog(leftURI, true);
152:
153: assertEquals(start + 1, instance.getCatalogEntries().size());
154:
155: instance.removeNextCatalog(leftURI);
156:
157: assertEquals(start, instance.getCatalogEntries().size());
158: }
159:
160: class MyXAMCatalogWriteModel extends XAMCatalogWriteModelImpl {
161: public MyXAMCatalogWriteModel(FileObject catFile)
162: throws IOException, CatalogModelException {
163: super (catFile);
164: }
165:
166: protected ModelSource createModelSource(FileObject catFileObject)
167: throws CatalogModelException {
168: ModelSource source = null;
169: try {
170: source = TestUtil.createModelSource(
171: super .catalogFileObject, true);
172: } catch (CatalogModelException ex) {
173: assert false;
174: ex.printStackTrace();
175: return null;
176: }
177: return source;
178: }
179:
180: public String getContent() {
181: Document doc = (Document) getCatalogModel()
182: .getModelSource().getLookup()
183: .lookup(Document.class);
184: ;
185: try {
186: return doc.getText(0, doc.getLength());
187: } catch (BadLocationException ex) {
188: ex.printStackTrace();
189: }
190: return null;
191: }
192: }
193: }
|