KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
22 import java.net.URL JavaDoc;
23 import junit.framework.*;
24 import org.netbeans.modules.xml.axi.impl.AXIModelImpl;
25 import org.netbeans.modules.xml.schema.model.SchemaModel;
26 import org.netbeans.modules.xml.xam.Model;
27 import org.netbeans.modules.xml.xam.ModelSource;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.loaders.DataObject;
31 import org.openide.loaders.DataObjectNotFoundException;
32
33         
34 /**
35  *
36  * @author Samaresh (Samaresh.Panda@Sun.Com)
37  */

38 public abstract class AbstractTestCase extends TestCase {
39
40     //make it true if you want to see System.out.println messages.
41
public static final boolean printUnitTestResults = false;
42     
43     protected String JavaDoc schemaFileName;
44     protected String JavaDoc globalElementName;
45     protected AXIModel axiModel;
46     protected Element globalElement;
47     protected URL JavaDoc referenceXML;
48     protected boolean canCompareExpectedResultWithActual = true;
49     
50     
51     /**
52      * AbstractTestCase
53      */

54     public AbstractTestCase(String JavaDoc testName,
55             String JavaDoc schemaFileName, String JavaDoc globalElementName) {
56         super(testName);
57         this.schemaFileName = schemaFileName;
58         this.globalElementName = globalElementName;
59     }
60
61     protected void setUp() throws Exception JavaDoc {
62         loadModel(this.schemaFileName);
63     }
64     
65     protected void loadModel(String JavaDoc schemaFileName) throws Exception JavaDoc {
66         this.schemaFileName = schemaFileName;
67         this.axiModel = getModel(schemaFileName);
68         this.globalElement = findAXIGlobalElement(globalElementName);
69         String JavaDoc compareAgainst = schemaFileName.substring(0, schemaFileName.indexOf(".xsd")) + ".xml";
70         referenceXML = AbstractTestCase.class.getResource(compareAgainst);
71         if(referenceXML == null) {
72             canCompareExpectedResultWithActual = false;
73             return;
74         }
75     }
76
77     protected AXIModel getModel(String JavaDoc schemaFileName) throws Exception JavaDoc {
78         URL JavaDoc url = AbstractTestCase.class.getResource(schemaFileName);
79         File JavaDoc file = new File JavaDoc(url.toURI());
80         file = FileUtil.normalizeFile(file);
81         return TestCatalogModel.getDefault().
82                 getAXIModel(FileUtil.toFileObject(file));
83     }
84     
85     protected void tearDown() throws Exception JavaDoc {
86         TestCatalogModel.getDefault().clearDocumentPool();
87     }
88             
89     protected AXIModel getAXIModel() {
90         return axiModel;
91     }
92     
93     protected SchemaModel getSchemaModel() {
94         return getAXIModel().getSchemaModel();
95     }
96     
97     protected Element findAXIGlobalElement(String JavaDoc name) {
98         if(name == null)
99             return null;
100         
101         for(Element e : axiModel.getRoot().getElements()) {
102             if(e.getName().equals(name)) {
103                 return e;
104             }
105         }
106         
107         return null;
108     }
109     
110     protected ContentModel findContentModel(String JavaDoc name) {
111         for(ContentModel cm : axiModel.getRoot().getContentModels()) {
112             if(cm.getName().equals(name)) {
113                 return cm;
114             }
115         }
116         
117         return null;
118     }
119     
120     protected void validateSchema(SchemaModel sm) {
121         boolean status =
122             ((AXIModelImpl)getAXIModel()).getState()==Model.State.VALID;//((AXIModelImpl)getAXIModel()).validate();
123
assertTrue("Schema Validation failed", status);
124     }
125     
126     public final void print(String JavaDoc message) {
127         if(printUnitTestResults) {
128             System.out.println(message);
129         }
130     }
131 }
132
Popular Tags