1 7 8 package org.netbeans.modules.xml.retriever.catalog.model; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 import java.io.File ; 13 import java.net.URI ; 14 import java.net.URISyntaxException ; 15 import java.util.List ; 16 import javax.swing.text.BadLocationException ; 17 import junit.framework.*; 18 import org.netbeans.modules.xml.xam.ModelSource; 19 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 20 import org.openide.filesystems.FileObject; 21 import org.openide.filesystems.FileUtil; 22 23 27 public class XAMCatalogModelTest extends TestCase implements PropertyChangeListener { 28 29 public XAMCatalogModelTest(String testName) { 30 super(testName); 31 } 32 33 protected void setUp() throws Exception { 34 } 35 36 protected void tearDown() throws Exception { 37 } 38 39 public static Test suite() { 40 TestSuite suite = new TestSuite(XAMCatalogModelTest.class); 41 42 return suite; 43 } 44 45 46 int eventFired = 0; 47 48 public void testCatalogModel() throws URISyntaxException , BadLocationException { 49 File inputFile = null; 50 51 try { 52 inputFile = new File (XAMCatalogModelTest.class.getResource("catalog.xml").toURI()); 53 } catch (URISyntaxException ex) { 54 assert false; 55 ex.printStackTrace(); 56 return; 57 } 58 59 FileObject inputFO = FileUtil.toFileObject(FileUtil. 60 normalizeFile(inputFile)); 61 62 ModelSource source = null; 63 try { 64 source = TestUtil.createModelSource(inputFO, true); 65 } catch (CatalogModelException ex) { 66 assert false; 67 ex.printStackTrace(); 68 return; 69 } 70 71 CatalogModelFactory instance = CatalogModelFactory.getInstance(); 72 CatalogModel cm = instance.getModel(source); 73 assertNotNull(cm); 74 75 Catalog cat = cm.getRootComponent(); 76 assertNotNull(cat); 77 78 cat.addPropertyChangeListener(this); 79 80 81 List <System > systems = cat.getSystems(); 82 assertNotNull(systems); 83 84 assertEquals(1, systems.size()); 85 System system = systems.get(0); 86 87 system.addPropertyChangeListener(this); 88 89 assertEquals("sysIDAttr", system.getSystemIDAttr()); 90 assertEquals("uriAttr", system.getURIAttr()); 91 assertEquals("xprojCatRefAttr", system.getXprojectCatalogFileLocationAttr()); 92 assertEquals("refFileAttr", system.getReferencingFileAttr()); 93 94 system.getModel().startTransaction(); 95 system.setSystemIDAttr(new URI ("dummy")); 96 system.setURIAttr(new URI ("dummy")); 97 system.setXprojectCatalogFileLocationAttr(new URI ("dummy")); 98 system.setReferencingFileAttr(new URI ("dummy")); 99 system.getModel().endTransaction(); 100 101 assertEquals(4, eventFired); 102 eventFired = 0; 103 104 assertEquals("dummy", system.getSystemIDAttr()); 105 assertEquals("dummy", system.getURIAttr()); 106 assertEquals("dummy", system.getXprojectCatalogFileLocationAttr()); 107 assertEquals("dummy", system.getReferencingFileAttr()); 108 109 System nsys = (System ) system.copy(cat); 110 111 cat.getModel().startTransaction(); 112 cat.removeSystem(system); 113 cat.getModel().endTransaction(); 114 115 assertEquals(1, eventFired); 116 eventFired = 0; 117 118 assertEquals(0, cat.getSystems().size()); 119 120 cat.getModel().startTransaction(); 121 cat.addSystem(nsys); 122 cat.getModel().endTransaction(); 123 124 assertEquals(1, eventFired); 125 eventFired = 0; 126 127 assertEquals(1, cat.getSystems().size()); 128 129 130 List <NextCatalog> nextCatalogs = cat.getNextCatalogs(); 131 assertNotNull(nextCatalogs); 132 133 assertEquals(1, nextCatalogs.size()); 134 NextCatalog nextCatalog = nextCatalogs.get(0); 135 136 nextCatalog.addPropertyChangeListener(this); 137 138 assertEquals("catalogAttr", nextCatalog.getCatalogAttr()); 139 140 cat.getModel().startTransaction(); 141 try { 142 nextCatalog.setCatalogAttr(new URI ("dummy")); 143 } finally{ 144 cat.getModel().endTransaction(); 145 } 146 147 assertEquals(1, eventFired); 148 eventFired = 0; 149 150 assertEquals("dummy", nextCatalog.getCatalogAttr()); 151 152 NextCatalog nncat = (NextCatalog) nextCatalog.copy(cat); 153 154 cat.getModel().startTransaction(); 155 cat.removeNextCatalog(nextCatalog); 156 cat.getModel().endTransaction(); 157 158 assertEquals(1, eventFired); 159 eventFired = 0; 160 161 assertEquals(0, cat.getNextCatalogs().size()); 162 163 cat.getModel().startTransaction(); 164 cat.addNextCatalog(nncat); 165 cat.getModel().endTransaction(); 166 167 assertEquals(1, eventFired); 168 eventFired = 0; 169 170 assertEquals(1, cat.getNextCatalogs().size()); 171 172 174 } 175 176 public void propertyChange(PropertyChangeEvent evt) { 177 eventFired++; 178 } 179 180 181 public void testEmptyCatalogModel() throws URISyntaxException , BadLocationException { 182 File inputFile = null; 183 184 try { 185 inputFile = new File (XAMCatalogModelTest.class.getResource("empty.xml").toURI()); 186 } catch (URISyntaxException ex) { 187 assert false; 188 ex.printStackTrace(); 189 return; 190 } 191 192 FileObject inputFO = FileUtil.toFileObject(FileUtil. 193 normalizeFile(inputFile)); 194 195 ModelSource source = null; 196 try { 197 source = TestUtil.createModelSource(inputFO, true); 198 } catch (CatalogModelException ex) { 199 assert false; 200 ex.printStackTrace(); 201 return; 202 } 203 204 CatalogModelFactory instance = CatalogModelFactory.getInstance(); 205 CatalogModel cm = instance.getModel(source); 206 assertNotNull(cm); 207 208 assertNotNull(cm.getRootComponent()); 209 210 NextCatalog nc = cm.getFactory().createNextCatalog(); 211 212 cm.startTransaction(); 213 try { 214 cm.getRootComponent().addNextCatalog(nc); 215 nc.setCatalogAttr(new URI ("dummy")); 216 } finally{ 217 cm.endTransaction(); 218 } 219 220 assertEquals(1, cm.getRootComponent().getNextCatalogs().size()); 221 222 224 } 225 226 } 227 | Popular Tags |