KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.xml.axi;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23 import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
24 import org.netbeans.modules.xml.axi.visitor.DeepAXITreeVisitor;
25
26 /**
27  * This test traverses the AXI model for a given schema and
28  * checks the parent component at each level.
29  *
30  * @author Samaresh
31  */

32 public class ContentModelTest extends AbstractTestCase {
33     public static final String JavaDoc TEST_XSD = "resources/po.xsd";
34     
35     public ContentModelTest(String JavaDoc testName) {
36         super(testName, TEST_XSD, null);
37     }
38         
39     public static Test suite() {
40         TestSuite suite = new TestSuite(ContentModelTest.class);
41         return suite;
42     }
43         
44     public void testContentModels() {
45         axiModel = getAXIModel();
46         
47         //traverse entire tree, so that the model gets fully initialized.
48
DeepAXITreeVisitor visitor = new DeepAXITreeVisitor();
49         axiModel.getRoot().accept(visitor);
50         
51         //check all content models
52
ContentModelVisitor cmv = new ContentModelVisitor();
53         cmv.checkContentModels(getAXIModel());
54     }
55         
56     private class ContentModelVisitor extends DeepAXITreeVisitor {
57         private int refCount = 0;
58         private ContentModel contentModel;
59         private AXIModel axiModel;
60         
61         public void checkContentModels(AXIModel model) {
62             axiModel = getAXIModel();
63             for(ContentModel cm : model.getRoot().getContentModels()) {
64                 //must belong to the same model
65
assert(axiModel == cm.getModel());
66                 
67                 contentModel = cm;
68                 print("checking ContentModel: " +
69                         cm.getName() + " Type: " + cm.getType() + ".....");
70                 if(cm.getRefSet() == null)
71                     continue;
72                 
73                 refCount = cm.getRefSet().size();
74                 cm.accept(this);
75             }
76         }
77
78         protected void visitChildren(AXIComponent component) {
79             if(component instanceof ContentModel) {
80                 super.visitChildren(component);
81                 return;
82             }
83             print("Component: " + component + " type: " + component.getComponentType());
84             assert(component.getRefSet().size() == refCount);
85             for(AXIComponent ref : component.getRefSet()) {
86                 AXIComponent original = ref.getSharedComponent();
87                 assert(original == component);
88             }
89             super.visitChildren(component);
90         }
91     }
92 }
93
Popular Tags