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: * DepResolverFactoryImplTest.java
044: * JUnit based test
045: *
046: * Created on January 18, 2006, 7:28 PM
047: */
048:
049: package org.netbeans.modules.xml.retriever.catalog.impl;
050:
051: import java.io.File;
052: import java.io.IOException;
053: import java.io.OutputStream;
054: import java.net.URI;
055: import java.net.URISyntaxException;
056: import java.net.URL;
057: import java.util.logging.Formatter;
058: import java.util.logging.Level;
059: import javax.swing.text.Document;
060: import junit.framework.*;
061: import java.util.logging.Logger;
062: import java.util.logging.SimpleFormatter;
063: import java.util.logging.StreamHandler;
064: import org.netbeans.editor.BaseDocument;
065: import org.netbeans.modules.xml.retriever.catalog.Utilities;
066: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
067: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
068: import org.netbeans.modules.xml.retriever.catalog.model.TestUtil;
069: import org.netbeans.modules.xml.xam.ModelSource;
070: import org.netbeans.modules.xml.xam.locator.CatalogModel;
071: import org.openide.cookies.EditorCookie;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074: import org.openide.loaders.DataObject;
075: import org.openide.loaders.DataObjectNotFoundException;
076: import org.openide.util.Lookup;
077: import org.openide.util.lookup.Lookups;
078:
079: /**
080: *
081: * @author girix
082: */
083: public class CatalogModelTest extends TestCase {
084:
085: static {
086: TestUtil.registerXMLKit();
087: }
088:
089: public CatalogModelTest(String testName) {
090: super (testName);
091: }
092:
093: protected void setUp() throws Exception {
094: }
095:
096: protected void tearDown() throws Exception {
097: }
098:
099: public static Test suite() {
100: TestSuite suite = new TestSuite(CatalogModelTest.class);
101:
102: return suite;
103: }
104:
105: public void testDepResolver() throws URISyntaxException,
106: CatalogModelException, IOException {
107:
108: Logger logger = Logger.getLogger(CatalogModelTest.class
109: .getName());
110: logger.setLevel(Level.ALL);
111: StreamHandler sh = new MyHandler(System.out,
112: new SimpleFormatter());
113: sh.setLevel(logger.getLevel());
114: //logger.addHandler(sh);
115: CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
116: File catFile = new File(System.getProperty("java.io.tmpdir")
117: + File.separator
118: + CatalogWriteModel.PUBLIC_CATALOG_FILE_NAME
119: + CatalogWriteModel.CATALOG_FILE_EXTENSION + ".girish");
120: catFile.delete();
121: catFile.createNewFile();
122: FileObject catFO = FileUtil.toFileObject(FileUtil
123: .normalizeFile(catFile));
124: URL url = getClass().getResource("dummyFile.txt");
125: FileObject peerfo = FileUtil.toFileObject(new File(url.toURI())
126: .getAbsoluteFile());
127: System.out.println(catFile);
128: CatalogWriteModel drz = new MyCatalogWriteModel(catFO);
129: //CatalogWriteModel drz = new MyCatalogWriteModel(new File(System.getProperty("java.io.tmpdir")));
130: drz.addURI(new URI("dummy/dummy"), peerfo);
131: int length = drz.getCatalogEntries().size();
132:
133: assertEquals(1, length);
134:
135: //System.out.println("%%%%"+drz.getModelSource(new URI("dummy/dummy")).getFileObject());
136:
137: //System.out.println("$$$$"+LSResourceResolverFactory.getDefault().resolveResource(null, null, null, "dummy/dummy", url.toURI().toString()).getSystemId());
138:
139: //assertTrue(LSResourceResolverFactory.getDefault().resolveResource(null, null, null, "dummy/dummy", url.toURI().toString()).getSystemId().endsWith("dummyFile.txt"));
140:
141: FileObject fob = (FileObject) drz.getModelSource(
142: new URI("dummy/dummy")).getLookup().lookup(
143: FileObject.class);
144:
145: assertNotNull(fob);
146:
147: drz.removeURI(new URI("dummy/dummy"));
148:
149: length = drz.getCatalogEntries().size();
150:
151: assertEquals(0, length);
152: }
153:
154: class MyCatalogWriteModel extends CatalogWriteModelImpl {
155: MyCatalogWriteModel(File file) throws IOException {
156: super (file);
157: }
158:
159: MyCatalogWriteModel(FileObject fo) throws IOException {
160: super (fo);
161: }
162:
163: /**
164: * This method could be overridden by the Unit testcase to return a special
165: * ModelSource object for a FileObject with custom impl of classes added to the lookup.
166: * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
167: */
168: protected ModelSource createModelSource(
169: final FileObject this FileObj, boolean editable)
170: throws CatalogModelException {
171: assert this FileObj != null : "Null file object.";
172: final CatalogModel catalogModel = createCatalogModel(this FileObj);
173: final DataObject dobj;
174: try {
175: dobj = DataObject.find(this FileObj);
176: } catch (DataObjectNotFoundException ex) {
177: throw new CatalogModelException(ex);
178: }
179: Lookup proxyLookup = Lookups.proxy(new Lookup.Provider() {
180: public Lookup getLookup() {
181: Document document = null;
182: Logger l = Logger.getLogger(getClass().getName());
183: document = getDocument(this FileObj);
184: return Lookups.fixed(new Object[] { this FileObj,
185: document, dobj, catalogModel });
186: }
187: });
188: return new ModelSource(proxyLookup, editable);
189: }
190:
191: private Document getDocument(FileObject fo) {
192: Document result = null;
193: try {
194: DataObject dObject = DataObject.find(fo);
195: EditorCookie ec = (EditorCookie) dObject
196: .getCookie(EditorCookie.class);
197: Document doc = ec.openDocument();
198: if (doc instanceof BaseDocument)
199: return doc;
200: result = new org.netbeans.editor.BaseDocument(
201: org.netbeans.modules.xml.text.syntax.XMLKit.class,
202: false);
203: String str = doc.getText(0, doc.getLength());
204: result.insertString(0, str, null);
205:
206: } catch (Exception dObjEx) {
207: return null;
208: }
209: return result;
210: }
211: }
212:
213: class MyHandler extends StreamHandler {
214: public MyHandler(OutputStream out, Formatter fmt) {
215: super (out, fmt);
216: }
217:
218: public void publish(java.util.logging.LogRecord record) {
219: super.publish(record);
220: flush();
221: }
222:
223: }
224:
225: }
|