KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > AXIModelExTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi;
21
22 import junit.framework.*;
23 import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
24 import org.netbeans.modules.xml.axi.visitor.DeepAXITreeVisitor;
25 import org.openide.filesystems.FileObject;
26
27         
28 /**
29  * This unit test extends AXIModelTest and tests components from multiple
30  * namespaces and their models. Ensures that the components from different
31  * namespaces belong to appropriate AXIModels.
32  *
33  * - Checks that components coming from diff namespaces are read-only.
34  * - Checks that children components for a reference should be proxies.
35  *
36  * @author Samaresh (Samaresh.Panda@Sun.Com)
37  */

38 public class AXIModelExTest extends AXIModelTest {
39             
40     public static final String JavaDoc TEST_XSD = "resources/multifilePO.xsd";
41     
42     private static String JavaDoc PO_TNS = "http://xml.netbeans.org/examples/targetNS/PO";
43     private static String JavaDoc ITEMS_TNS = "http://xml.netbeans.org/examples/targetNS/Items";
44     private static String JavaDoc ADDR_TNS = "http://xml.netbeans.org/examples/targetNS/Address";
45     private AXIModel aModelPO;
46     
47     /**
48      * AXIModelExTest
49      */

50     public AXIModelExTest(String JavaDoc testName) {
51         super(testName, TEST_XSD, GLOBAL_ELEMENT);
52     }
53     
54     public static Test suite() {
55         TestSuite suite = new TestSuite(AXIModelExTest.class);
56         return suite;
57     }
58     
59     public void testModel() {
60         aModelPO = getAXIModel();
61         assertEquals(getAXIModel().getRoot().getTargetNamespace(), PO_TNS);
62         CheckTargetNamespaceVisitor visitor = new CheckTargetNamespaceVisitor();
63         visitor.checkNamespace(getAXIModel().getRoot());
64     }
65     
66     private class CheckTargetNamespaceVisitor extends DeepAXITreeVisitor {
67         
68         public void checkNamespace(AXIDocument document) {
69             document.accept(this);
70         }
71
72         protected void visitChildren(AXIComponent component) {
73             String JavaDoc ns = component.getTargetNamespace();
74             
75             //components must belong to one of three namespaces
76
if( !ns.equals(PO_TNS) &&
77                 !ns.equals(ADDR_TNS) &&
78                 !ns.equals(ITEMS_TNS)) {
79                 assert(false);
80             }
81             
82             //components that come from PO namespaces, must all
83
//have the same AXIModel
84
if(component.getTargetNamespace().equals(PO_TNS)) {
85                 assert(aModelPO == component.getModel());
86             }
87             
88             //if a component is a reference, its children has to be proxies
89
if(component.getComponentType() == ComponentType.REFERENCE) {
90                 for(AXIComponent c: component.getChildren()) {
91                     assert(c.getComponentType() == ComponentType.PROXY);
92                 }
93             }
94             
95             //components that come from other namespaces, must all
96
//have the same AXIModel, but diff from the ones from PO namespace.
97
if(!component.getTargetNamespace().equals(PO_TNS)) {
98                 doValidate(component, component.getOriginal().getModel());
99             }
100             
101             super.visitChildren(component);
102         }
103         
104         private void doValidate(AXIComponent component, AXIModel otherModel) {
105             //must be a proxy and the proxy's model must be the same as PO model.
106
assert(component.getComponentType() == ComponentType.PROXY);
107             assert(component.getModel() == aModelPO);
108             assert(component.isReadOnly());
109             
110             //find the original for this proxy component
111
//and the original must belong to a diff model.
112
AXIComponent original = component.getOriginal();
113             assert(otherModel != aModelPO);
114             assert(otherModel == original.getModel());
115             
116             //also find the FileObject from the two models
117
//and they should be different
118
FileObject fPO = (FileObject)aModelPO.getModelSource().
119                     getLookup().lookup(FileObject.class);
120             FileObject otherFO = (FileObject)otherModel.getModelSource().
121                     getLookup().lookup(FileObject.class);
122             assert(fPO != null);
123             assert(otherFO != null);
124             assert(fPO != otherFO);
125         }
126     }
127     
128 }
129
Popular Tags