1 7 8 package org.netbeans.modules.xml.retriever.catalog.impl; 9 10 import javax.swing.text.BadLocationException ; 11 import javax.swing.text.Document ; 12 import junit.framework.*; 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URI ; 16 import java.net.URISyntaxException ; 17 import org.netbeans.modules.xml.retriever.catalog.model.CatalogModelFactory; 18 import org.netbeans.modules.xml.retriever.catalog.model.TestUtil; 19 import org.netbeans.modules.xml.xam.ModelSource; 20 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 21 import org.openide.filesystems.FileObject; 22 import org.openide.filesystems.FileUtil; 23 24 28 public class XAMCatalogWriteModelTest extends TestCase { 29 30 public XAMCatalogWriteModelTest(String testName) { 31 super(testName); 32 } 33 34 protected void setUp() throws Exception { 35 } 36 37 protected void tearDown() throws Exception { 38 } 39 40 public static Test suite() { 41 TestSuite suite = new TestSuite(XAMCatalogWriteModelTest.class); 42 43 return suite; 44 } 45 46 public void testSearchURI() { 47 URI locationURI = null; 48 try { 49 locationURI = new URI ("sysIDAttr"); 50 } catch (URISyntaxException ex) { 51 } 52 53 XAMCatalogWriteModelImpl instance = getTestCatModelInstance(); 54 55 URI result = instance.searchURI(locationURI); 56 assertNotNull(result); 57 } 58 59 public void testAddAndRemoveURI() throws Exception { 60 URI leftURI = new URI ("girish"); 61 URI rightURI = new URI ("kumar"); 62 FileObject fileObj = null; 63 MyXAMCatalogWriteModel instance = getTestCatModelInstance(); 64 65 int start = instance.getCatalogEntries().size(); 66 67 instance.addURI(leftURI, rightURI); 68 69 assertEquals(start+1, instance.getCatalogEntries().size()); 70 71 instance.removeURI(leftURI); 72 73 assertEquals(start, instance.getCatalogEntries().size()); 74 } 75 76 public MyXAMCatalogWriteModel getTestCatModelInstance(){ 77 FileObject inputFile = null; 78 79 try { 80 inputFile = FileUtil.toFileObject(FileUtil.normalizeFile(new File (XAMCatalogWriteModelTest.class. 81 getResource("catalog.xml").toURI()))); 82 } catch (URISyntaxException ex) { 83 assert false; 84 ex.printStackTrace(); 85 return null; 86 } 87 88 MyXAMCatalogWriteModel instance = null; 89 try { 90 instance = new MyXAMCatalogWriteModel(inputFile); 91 } catch (IOException ex) { 92 assert false; 93 } catch (CatalogModelException ex) { 94 ex.printStackTrace(); 95 assert false; 96 } 97 98 return instance; 99 } 100 101 public void testAddNextCatalog() throws Exception { 102 URI leftURI = new URI ("girish"); 103 FileObject fileObj = null; 104 MyXAMCatalogWriteModel instance = getTestCatModelInstance(); 105 106 int start = instance.getCatalogEntries().size(); 107 108 instance.addNextCatalog(leftURI, true); 109 110 assertEquals(start+1, instance.getCatalogEntries().size()); 111 112 instance.removeNextCatalog(leftURI); 113 114 assertEquals(start, instance.getCatalogEntries().size()); 115 } 116 117 118 119 class MyXAMCatalogWriteModel extends XAMCatalogWriteModelImpl{ 120 public MyXAMCatalogWriteModel(FileObject catFile) throws IOException , CatalogModelException{ 121 super(catFile); 122 } 123 124 protected ModelSource createModelSource(FileObject catFileObject) throws CatalogModelException { 125 ModelSource source = null; 126 try { 127 source = TestUtil.createModelSource(super.catalogFileObject, true); 128 } catch (CatalogModelException ex) { 129 assert false; 130 ex.printStackTrace(); 131 return null; 132 } 133 return source; 134 } 135 136 137 public String getContent(){ 138 Document doc = (Document ) getCatalogModel().getModelSource().getLookup().lookup(Document .class);; 139 try { 140 return doc.getText(0, doc.getLength()); 141 } catch (BadLocationException ex) { 142 ex.printStackTrace(); 143 } 144 return null; 145 } 146 } 147 } 148 | Popular Tags |