KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > catalog > impl > CatalogModelTest


1 /*
2  * DepResolverFactoryImplTest.java
3  * JUnit based test
4  *
5  * Created on January 18, 2006, 7:28 PM
6  */

7
8 package org.netbeans.modules.xml.retriever.catalog.impl;
9
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.net.URI JavaDoc;
14 import java.net.URISyntaxException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.logging.Formatter JavaDoc;
17 import java.util.logging.Level JavaDoc;
18 import javax.swing.text.Document JavaDoc;
19 import junit.framework.*;
20 import java.util.logging.Logger JavaDoc;
21 import java.util.logging.SimpleFormatter JavaDoc;
22 import java.util.logging.StreamHandler JavaDoc;
23 import org.netbeans.editor.BaseDocument;
24 import org.netbeans.modules.xml.retriever.catalog.Utilities;
25 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
26 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
27 import org.netbeans.modules.xml.retriever.catalog.model.TestUtil;
28 import org.netbeans.modules.xml.xam.ModelSource;
29 import org.netbeans.modules.xml.xam.locator.CatalogModel;
30 import org.openide.cookies.EditorCookie;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.loaders.DataObject;
34 import org.openide.loaders.DataObjectNotFoundException;
35 import org.openide.util.Lookup;
36 import org.openide.util.lookup.Lookups;
37
38 /**
39  *
40  * @author girix
41  */

42 public class CatalogModelTest extends TestCase {
43     
44     static {
45         TestUtil.registerXMLKit();
46     }
47     
48     public CatalogModelTest(String JavaDoc testName) {
49         super(testName);
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53     }
54     
55     protected void tearDown() throws Exception JavaDoc {
56     }
57     
58     public static Test suite() {
59         TestSuite suite = new TestSuite(CatalogModelTest.class);
60         
61         return suite;
62     }
63     
64     
65     public void testDepResolver() throws URISyntaxException JavaDoc, CatalogModelException, IOException JavaDoc {
66         
67         Logger JavaDoc logger = Utilities.getLogger();
68         logger.setLevel(Level.ALL);
69         StreamHandler JavaDoc sh = new MyHandler(System.out, new SimpleFormatter JavaDoc());
70         sh.setLevel(logger.getLevel());
71         //logger.addHandler(sh);
72
CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
73         File JavaDoc catFile = new File JavaDoc(System.getProperty("java.io.tmpdir")+File.separator+CatalogWriteModel.PUBLIC_CATALOG_FILE_NAME+CatalogWriteModel.CATALOG_FILE_EXTENSION+".girish");
74         catFile.delete();
75         catFile.createNewFile();
76         FileObject catFO = FileUtil.toFileObject(FileUtil.normalizeFile(catFile));
77         URL JavaDoc url = getClass().getResource("dummyFile.txt");
78         FileObject peerfo = FileUtil.toFileObject(new File JavaDoc(url.toURI()).getAbsoluteFile());
79         System.out.println(catFile);
80         CatalogWriteModel drz = new MyCatalogWriteModel(catFO);
81         //CatalogWriteModel drz = new MyCatalogWriteModel(new File(System.getProperty("java.io.tmpdir")));
82
drz.addURI(new URI JavaDoc("dummy/dummy"), peerfo);
83         int length = drz.getCatalogEntries().size();
84         
85         assertEquals(1, length);
86         
87         //System.out.println("%%%%"+drz.getModelSource(new URI("dummy/dummy")).getFileObject());
88

89         //System.out.println("$$$$"+LSResourceResolverFactory.getDefault().resolveResource(null, null, null, "dummy/dummy", url.toURI().toString()).getSystemId());
90

91         //assertTrue(LSResourceResolverFactory.getDefault().resolveResource(null, null, null, "dummy/dummy", url.toURI().toString()).getSystemId().endsWith("dummyFile.txt"));
92

93         FileObject fob = (FileObject) drz.getModelSource(new URI JavaDoc("dummy/dummy")).getLookup().lookup(FileObject.class);
94         
95         assertNotNull(fob);
96         
97         drz.removeURI(new URI JavaDoc("dummy/dummy"));
98         
99         length = drz.getCatalogEntries().size();
100         
101         assertEquals(0, length);
102     }
103     
104     class MyCatalogWriteModel extends CatalogWriteModelImpl {
105         MyCatalogWriteModel(File JavaDoc file) throws IOException JavaDoc{
106             super(file);
107         }
108         MyCatalogWriteModel(FileObject fo) throws IOException JavaDoc{
109             super(fo);
110         }
111         
112         /**
113          * This method could be overridden by the Unit testcase to return a special
114          * ModelSource object for a FileObject with custom impl of classes added to the lookup.
115          * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
116          */

117         protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{
118             assert thisFileObj != null : "Null file object.";
119             final CatalogModel catalogModel = createCatalogModel(thisFileObj);
120             final DataObject dobj;
121             try {
122                 dobj = DataObject.find(thisFileObj);
123             } catch (DataObjectNotFoundException ex) {
124                 throw new CatalogModelException(ex);
125             }
126             Lookup proxyLookup = Lookups.proxy(
127                     new Lookup.Provider() {
128                 public Lookup getLookup() {
129                     Document JavaDoc document = null;
130                     Logger JavaDoc l = Logger.getLogger(getClass().getName());
131                     document = getDocument(thisFileObj);
132                     return Lookups.fixed(new Object JavaDoc[] {
133                         thisFileObj,
134                         document,
135                         dobj,
136                         catalogModel
137                     });
138                 }
139             }
140             );
141             return new ModelSource(proxyLookup, editable);
142         }
143         
144         private Document JavaDoc getDocument(FileObject fo){
145             Document JavaDoc result = null;
146             try {
147                 DataObject dObject = DataObject.find(fo);
148                 EditorCookie ec = (EditorCookie)dObject.getCookie(EditorCookie.class);
149                 Document JavaDoc doc = ec.openDocument();
150                 if(doc instanceof BaseDocument)
151                     return doc;
152                 result = new org.netbeans.editor.BaseDocument(
153                         org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
154                 String JavaDoc str = doc.getText(0, doc.getLength());
155                 result.insertString(0,str,null);
156                 
157             } catch (Exception JavaDoc dObjEx) {
158                 return null;
159             }
160             return result;
161         }
162     }
163     
164     class MyHandler extends StreamHandler JavaDoc{
165         public MyHandler(OutputStream JavaDoc out, Formatter JavaDoc fmt){
166             super(out, fmt);
167         }
168         public void publish(java.util.logging.LogRecord JavaDoc record) {
169             super.publish(record);
170             flush();
171         }
172         
173     }
174     
175 }
176
Popular Tags