1 7 8 package org.netbeans.modules.xml.retriever.catalog.impl; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.io.OutputStream ; 13 import java.net.URI ; 14 import java.net.URISyntaxException ; 15 import java.net.URL ; 16 import java.util.logging.Formatter ; 17 import java.util.logging.Level ; 18 import javax.swing.text.Document ; 19 import junit.framework.*; 20 import java.util.logging.Logger ; 21 import java.util.logging.SimpleFormatter ; 22 import java.util.logging.StreamHandler ; 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 42 public class CatalogModelTest extends TestCase { 43 44 static { 45 TestUtil.registerXMLKit(); 46 } 47 48 public CatalogModelTest(String testName) { 49 super(testName); 50 } 51 52 protected void setUp() throws Exception { 53 } 54 55 protected void tearDown() throws Exception { 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 , CatalogModelException, IOException { 66 67 Logger logger = Utilities.getLogger(); 68 logger.setLevel(Level.ALL); 69 StreamHandler sh = new MyHandler(System.out, new SimpleFormatter ()); 70 sh.setLevel(logger.getLevel()); 71 CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true; 73 File catFile = new File (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 url = getClass().getResource("dummyFile.txt"); 78 FileObject peerfo = FileUtil.toFileObject(new File (url.toURI()).getAbsoluteFile()); 79 System.out.println(catFile); 80 CatalogWriteModel drz = new MyCatalogWriteModel(catFO); 81 drz.addURI(new URI ("dummy/dummy"), peerfo); 83 int length = drz.getCatalogEntries().size(); 84 85 assertEquals(1, length); 86 87 89 91 93 FileObject fob = (FileObject) drz.getModelSource(new URI ("dummy/dummy")).getLookup().lookup(FileObject.class); 94 95 assertNotNull(fob); 96 97 drz.removeURI(new URI ("dummy/dummy")); 98 99 length = drz.getCatalogEntries().size(); 100 101 assertEquals(0, length); 102 } 103 104 class MyCatalogWriteModel extends CatalogWriteModelImpl { 105 MyCatalogWriteModel(File file) throws IOException { 106 super(file); 107 } 108 MyCatalogWriteModel(FileObject fo) throws IOException { 109 super(fo); 110 } 111 112 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 document = null; 130 Logger l = Logger.getLogger(getClass().getName()); 131 document = getDocument(thisFileObj); 132 return Lookups.fixed(new Object [] { 133 thisFileObj, 134 document, 135 dobj, 136 catalogModel 137 }); 138 } 139 } 140 ); 141 return new ModelSource(proxyLookup, editable); 142 } 143 144 private Document getDocument(FileObject fo){ 145 Document result = null; 146 try { 147 DataObject dObject = DataObject.find(fo); 148 EditorCookie ec = (EditorCookie)dObject.getCookie(EditorCookie.class); 149 Document 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 str = doc.getText(0, doc.getLength()); 155 result.insertString(0,str,null); 156 157 } catch (Exception dObjEx) { 158 return null; 159 } 160 return result; 161 } 162 } 163 164 class MyHandler extends StreamHandler { 165 public MyHandler(OutputStream out, Formatter fmt){ 166 super(out, fmt); 167 } 168 public void publish(java.util.logging.LogRecord record) { 169 super.publish(record); 170 flush(); 171 } 172 173 } 174 175 } 176 | Popular Tags |