KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > visitor > FindUsageVisitorTest


1 /*
2  * FindUsageVisitorTest.java
3  * JUnit based test
4  *
5  * Created on November 3, 2005, 2:34 PM
6  */

7
8 package org.netbeans.modules.xml.schema.model.visitor;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import junit.framework.*;
16 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
17 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
18 import org.netbeans.modules.xml.schema.model.GlobalElement;
19 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
20 import org.netbeans.modules.xml.schema.model.GlobalType;
21 import org.netbeans.modules.xml.schema.model.LocalElement;
22 import org.netbeans.modules.xml.schema.model.Schema;
23 import org.netbeans.modules.xml.schema.model.SchemaComponent;
24 import org.netbeans.modules.xml.schema.model.SchemaModel;
25 import org.netbeans.modules.xml.schema.model.TestCatalogModel;
26 import org.netbeans.modules.xml.schema.model.Util;
27 import org.netbeans.modules.xml.xam.NamedReferenceable;
28 /**
29  *
30  * @author Samaresh
31  */

32 public class FindUsageVisitorTest extends TestCase {
33     
34     public static final String JavaDoc TEST_XSD = "resources/J1_TravelItinerary.xsd";
35     public static final String JavaDoc FIND_USAGE_FOR_ATTR_GROUP = "OTA_PayloadStdAttributes";
36     public static final String JavaDoc FIND_USAGE_FOR_ELEMENT = "TPA_Extensions";
37     public static final String JavaDoc FIND_USAGE_FOR_TYPE = "TransactionActionType";
38     public static final String JavaDoc NO_TARGET_NAMESPACE = "resources/CTDerivations.xsd";
39     private Schema schema = null;
40     private GlobalElement global_element = null;
41     private GlobalType global_type = null;
42     private GlobalAttributeGroup global_attribute_group = null;
43     
44     public FindUsageVisitorTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49     }
50     protected void setUp1() throws Exception JavaDoc {
51     SchemaModel model = Util.loadSchemaModel(TEST_XSD);
52     schema = model.getSchema();
53         
54         Collection JavaDoc<GlobalType> types = new ArrayList JavaDoc(schema.getComplexTypes());
55         types.addAll(schema.getSimpleTypes());
56         for(GlobalType type : types) {
57             if(type.getName().equals(FIND_USAGE_FOR_TYPE)) {
58                 this.global_type = type;
59             }
60         }
61         
62         for(GlobalElement e : schema.getElements()) {
63             if(e.getName().equals(FIND_USAGE_FOR_ELEMENT)) {
64                 this.global_element = e;
65             }
66         }
67         
68         for(GlobalAttributeGroup gag : schema.getAttributeGroups()) {
69             if(gag.getName().equals(FIND_USAGE_FOR_ATTR_GROUP)) {
70                 this.global_attribute_group = gag;
71             }
72         }
73     }
74
75     protected void tearDown() throws Exception JavaDoc {
76         TestCatalogModel.getDefault().clearDocumentPool();
77     }
78
79     public static Test suite() {
80         TestSuite suite = new TestSuite(FindUsageVisitorTest.class);
81         return suite;
82     }
83     
84     public void testFindPath() throws Exception JavaDoc {
85         setUp1();
86         this.assertEquals(49, findUsageCountForItem(global_element));
87         this.assertEquals(3, findUsageCountForItem(global_type));
88         this.assertEquals(4, findUsageCountForItem(global_attribute_group));
89     }
90
91     public int findUsageCountForItem(NamedReferenceable<SchemaComponent> ref) {
92         long startTime = System.currentTimeMillis();
93         System.out.println("Finding Usage for " + ref.getName() == null? ref : ref.getName());
94         FindUsageVisitor usage = new FindUsageVisitor();
95         Preview preview = usage.findUsages(Collections.singletonList(schema), ref);
96         System.out.println(preview.getUsages().size() + " occurances found!!!");
97                 
98         Map JavaDoc<SchemaComponent, List JavaDoc<SchemaComponent>> usageMap = preview.getUsages();
99         for(SchemaComponent c : usageMap.keySet()) {
100             System.out.println("Path for component: " + c);
101             List JavaDoc<SchemaComponent> path = usageMap.get(c);
102             for(SchemaComponent e : path) {
103                 System.out.println(getComponentDetail(e));
104             }
105             System.out.println("\n");
106             //if u want to compare with the paths as it were obtained using PathFromRootVisitor
107
//getPathUsingPathFromRootVisitor(schema, c);
108
}
109         long endTime = System.currentTimeMillis();
110         System.out.println("Time taken: " + (endTime - startTime));
111         return usageMap.keySet().size();
112         //this.assertEquals(49, usageMap.keySet().size());
113
}
114     
115     private String JavaDoc getComponentDetail(SchemaComponent component) {
116         String JavaDoc details = component.toString();
117         if(component instanceof GlobalComplexType) {
118             return details + ":" + ((GlobalComplexType)component).getName();
119         }
120         if(component instanceof GlobalSimpleType) {
121             return details + ":" + ((GlobalSimpleType)component).getName();
122         }
123         if(component instanceof LocalElement) {
124             return details + ":" + ((LocalElement)component).getName();
125         }
126         if(component instanceof GlobalElement) {
127             return details + ":" + ((GlobalElement)component).getName();
128         }
129         return details;
130     }
131     
132     public void testNoTargetNamespace() throws Exception JavaDoc {
133         SchemaModel model = Util.loadSchemaModel(NO_TARGET_NAMESPACE);
134         Schema schema = model.getSchema();
135         GlobalComplexType gct = schema.getComplexTypes().iterator().next();
136         assertEquals("Base-For-Restriction", gct.getName());
137         FindUsageVisitor fuv = new FindUsageVisitor();
138         Preview pv = fuv.findUsages(Collections.singleton(schema), gct);
139         assertEquals("notargetnamespace.usages.count", 2, pv.getUsages().size());
140     }
141     
142     public void testUnion() throws Exception JavaDoc {
143     SchemaModel model = Util.loadSchemaModel("resources/PurchaseOrder_union.xsd");
144         FindUsageVisitor fuv = new FindUsageVisitor();
145         GlobalSimpleType type = Util.findGlobalSimpleType(model.getSchema(), "Money");
146         assertEquals("Money", type.getName());
147         model.getSchema().getSimpleTypes();
148         Preview pv = fuv.findUsages(Collections.singleton(model.getSchema()), type);
149         assertEquals("findusage on Money count", 1, pv.getUsages().size());
150     }
151 }
152
Popular Tags