1 19 package org.netbeans.modules.xml.axi; 20 21 import java.io.File ; 22 import java.net.URL ; 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 38 public abstract class AbstractTestCase extends TestCase { 39 40 public static final boolean printUnitTestResults = false; 42 43 protected String schemaFileName; 44 protected String globalElementName; 45 protected AXIModel axiModel; 46 protected Element globalElement; 47 protected URL referenceXML; 48 protected boolean canCompareExpectedResultWithActual = true; 49 50 51 54 public AbstractTestCase(String testName, 55 String schemaFileName, String globalElementName) { 56 super(testName); 57 this.schemaFileName = schemaFileName; 58 this.globalElementName = globalElementName; 59 } 60 61 protected void setUp() throws Exception { 62 loadModel(this.schemaFileName); 63 } 64 65 protected void loadModel(String schemaFileName) throws Exception { 66 this.schemaFileName = schemaFileName; 67 this.axiModel = getModel(schemaFileName); 68 this.globalElement = findAXIGlobalElement(globalElementName); 69 String 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 schemaFileName) throws Exception { 78 URL url = AbstractTestCase.class.getResource(schemaFileName); 79 File file = new File (url.toURI()); 80 file = FileUtil.normalizeFile(file); 81 return TestCatalogModel.getDefault(). 82 getAXIModel(FileUtil.toFileObject(file)); 83 } 84 85 protected void tearDown() throws Exception { 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 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 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; assertTrue("Schema Validation failed", status); 124 } 125 126 public final void print(String message) { 127 if(printUnitTestResults) { 128 System.out.println(message); 129 } 130 } 131 } 132 | Popular Tags |