KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24 import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
25 import org.netbeans.modules.xml.axi.visitor.DeepAXITreeVisitor;
26
27 /**
28  * This test traverses the AXI model for address2.xsd schema file
29  * and for each proxy component, verifies the number of indirection
30  * to the original component.
31  *
32  * @author Samaresh
33  */

34 public class ProxyComponentTest extends AbstractTestCase {
35     public static final String JavaDoc TEST_XSD = "resources/address2.xsd";
36     
37     public ProxyComponentTest(String JavaDoc testName) {
38         super(testName, TEST_XSD, null);
39     }
40         
41     public static Test suite() {
42         TestSuite suite = new TestSuite(ProxyComponentTest.class);
43         return suite;
44     }
45     
46     public void testProxyComponents() {
47         DeepModelVisitor visitor = new DeepModelVisitor();
48         getAXIModel().getRoot().accept(visitor);
49     }
50         
51     private class DeepModelVisitor extends DeepAXITreeVisitor {
52         private int counter = 0;
53
54         protected void visitChildren(AXIComponent component) {
55             if(component.getComponentType() == ComponentType.PROXY) {
56                 //reset the indirection level,
57
//the call getOriginal will set it.
58
proxyIndirection = 0;
59                 AXIComponent o = getOriginal(component);
60                 assert(proxyIndirectionTest == proxyIndirection);
61             }
62             
63             if(component instanceof ContentModel) {
64                 ContentModel cm = (ContentModel)component;
65                 if(cm.getName().equals("group")) {
66                     checkProxyIndirection = true;
67                     proxyIndirectionTest = 0;
68                 }
69                 if(cm.getName().equals("attr-group")) {
70                     checkProxyIndirection = true;
71                     proxyIndirectionTest = 0;
72                 }
73                 if(cm.getName().equals("USAddress")) {
74                     checkProxyIndirection = true;
75                     proxyIndirectionTest = 1;
76                 }
77             }
78                         
79             if(component instanceof Element) {
80                 Element e = (Element)component;
81                 if(e.getName().equals("address")) {
82                     checkProxyIndirection = true;
83                     proxyIndirectionTest = 2;
84                 } else
85                     checkProxyIndirection = false;
86             }
87             super.visitChildren(component);
88         }
89     }
90     
91     /**
92      * Returns the original from a proxy. A proxy may have multiple
93      * levels of indirection to an original.
94      */

95     private AXIComponent getOriginal(AXIComponent component) {
96         AXIComponent shared = component.getSharedComponent();
97         if(shared == null)
98             return component;
99         
100         proxyIndirection++;
101         if(shared.getComponentType() != ComponentType.PROXY) {
102             return shared;
103         }
104         
105         return getOriginal(shared);
106     }
107     
108     private int proxyIndirectionTest = 0;
109     private int proxyIndirection = 0;
110     private boolean checkProxyIndirection = false;
111 }
112
Popular Tags