KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > IncludeTest


1 /*
2  * IncludeTest.java
3  * JUnit based test
4  *
5  * Created on October 4, 2005, 4:26 PM
6  */

7
8 package org.netbeans.modules.xml.schema.model;
9 import java.beans.PropertyChangeListener JavaDoc;
10 import junit.framework.*;
11 import java.net.URI JavaDoc;
12 import java.util.Collection JavaDoc;
13
14 /**
15  *
16  * @author rico
17  */

18 public class IncludeTest extends TestCase {
19     private static final String JavaDoc TEST_XSD = "resources/testInclude.xsd";
20     
21     public IncludeTest(String JavaDoc testName) {
22         super(testName);
23     }
24     
25     protected void setUp() throws Exception JavaDoc {
26     }
27     
28     protected void tearDown() throws Exception JavaDoc {
29         TestCatalogModel.getDefault().clearDocumentPool();
30     }
31     
32     public static Test suite() {
33         TestSuite suite = new TestSuite(IncludeTest.class);
34         
35         return suite;
36     }
37     
38     /**
39      * Test of getSchemaLocation method, of class org.netbeans.modules.xmlschema.api.model.Include.
40      */

41     public void testGetSchemaLocation() throws Exception JavaDoc {
42         Include instance = null;
43         SchemaModel model = Util.loadSchemaModel(TEST_XSD);
44          Schema schema = model.getSchema();
45         assertNotNull("Null schema " , schema);
46         
47     Collection JavaDoc<Include> refs = schema.getIncludes();
48     this.assertNotNull("Null refs ", refs);
49     instance = refs.iterator().next();
50         this.assertNotNull("Null include ", instance);
51         String JavaDoc expResult = "somefile.xsd";
52         System.out.println("expResult: " + expResult.toString());
53         String JavaDoc result = instance.getSchemaLocation();
54         System.out.println("result: " + result.toString());
55         assertEquals(expResult.toString(), result.toString());
56     }
57     
58     /**
59      * Test of setSchemaLocation method, of class org.netbeans.modules.xmlschema.api.model.Include.
60      */

61     public void testSetSchemaLocation() throws Exception JavaDoc {
62         URI JavaDoc uri = null;
63         Include instance = null;
64         SchemaModel model = Util.loadSchemaModel(TEST_XSD);
65         Schema schema = model.getSchema();
66         assertNotNull("Null schema " , schema);
67         
68         java.util.List JavaDoc<SchemaComponent> children = schema.getChildren();
69         for(SchemaComponent child: children){
70             if(child instanceof Include){
71                 instance = (Include)child;
72                 break;
73             }
74         }
75         this.assertNotNull("Null include ", instance);
76     TestListener tl = new TestListener();
77     instance.getModel().addPropertyChangeListener(tl);
78         String JavaDoc result = instance.getSchemaLocation();
79         assertEquals("somefile.xsd", instance.getSchemaLocation().toString());
80     model.startTransaction();
81     instance.setSchemaLocation("newfile.xsd");
82         model.endTransaction();
83     assertEquals("newfile.xsd", instance.getSchemaLocation().toString());
84     assertTrue("only one event should be fired " + tl.getEventsFired(), tl.getEventsFired()==1);
85     assertEquals("event should be modified", Include.SCHEMA_LOCATION_PROPERTY, tl.getLastEventName());
86     }
87     
88     static class TestListener implements PropertyChangeListener JavaDoc {
89         private String JavaDoc eventName;
90         private int count = 0;
91         
92         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
93             eventName = evt.getPropertyName();
94             count++;
95         }
96         
97         public int getEventsFired() {
98             return count;
99         }
100         
101         public String JavaDoc getLastEventName() {
102             return eventName;
103         }
104         
105         public void resetFiredEvents() {
106             count = 0;
107         }
108     }
109 }
110
Popular Tags