1 53 package test.xmldb; 54 55 import java.io.*; 56 57 import junit.framework.*; 58 59 import org.xmldb.api.base.*; 60 61 import java.util.Properties ; 62 import java.net.URL ; 63 64 import org.w3c.dom.Node ; 65 import org.w3c.dom.Document ; 66 import org.w3c.dom.Element ; 67 import org.w3c.dom.DOMImplementation ; 68 import org.apache.xml.serialize.OutputFormat; 69 import org.apache.xml.serialize.XMLSerializer; 70 import org.apache.xerces.dom.DOMImplementationImpl; 71 import org.apache.log4j.Logger; 72 73 74 77 public class XMLDBTestCase extends TestCase { 78 79 protected Collection col; 80 81 82 protected String xmlFileName = "LevelZeroTest.xml"; 83 84 85 protected Document document; 86 87 88 protected String collectionName; 89 90 private CollectionStorageHelper collectionStorageHelper; 91 92 private Logger logger = Logger.getLogger(XMLDBTestCase.class); 93 94 public XMLDBTestCase(String name) { 95 super(name); 96 } 97 98 public void setUp() throws Exception { 99 logger.debug("******************** set up ********************"); 100 Properties props; 101 if (XMLDBTestSuite.propertiesFileName == null) { 102 String defaultPropsFileLocation = "test/xmldb/XMLDBTestSuite.properties"; 104 URL url = this.getClass().getClassLoader().getResource(defaultPropsFileLocation); 105 if (url == null) { 106 throw new Exception ("failed to find default props file at " + defaultPropsFileLocation); 107 } 108 props = loadProps(url.openConnection().getInputStream()); 109 110 } else { 111 props = loadProps(XMLDBTestSuite.propertiesFileName); 112 } 113 String driver = props.getProperty("driverName"); 114 collectionName = props.getProperty("collectionName"); 115 String collectionURI = props.getProperty("URI") + collectionName; 116 String dbURI = props.getProperty("dbURI"); 117 118 collectionStorageHelper = new CollectionStorageHelper(dbURI); 120 collectionStorageHelper.createCollection(collectionName); 121 122 Database database = (Database) Class.forName(driver).newInstance(); 123 126 col = database.getCollection(collectionURI); 128 assertNotNull("XMLDBTestCase.setUp() - Collection could not be created", col); 129 130 document = createXMLFile(xmlFileName); 131 assertNotNull("XMLDBTestCase.setUp() - failed to create XML file", document); 132 } 133 134 public void tearDown() { 135 logger.debug("******************** tear down ********************"); 136 collectionStorageHelper.deleteCollection(collectionName); 137 col.close(); 138 assertTrue("XMLDBTestCase.tearDown() - failed to delete XML file", deleteXMLFile(xmlFileName)); 139 } 140 141 142 protected String toString(Document document) throws Exception { 143 StringWriter writer = new StringWriter(); 144 XMLSerializer serializer = new XMLSerializer(writer, new OutputFormat("xml", "UTF-8", true)); 145 serializer.serialize(document); 146 writer.flush(); 147 return writer.toString(); 148 } 149 150 151 protected String toString(Node node) throws Exception { 152 try { 153 if (node instanceof Document ) { 155 return toString((Document ) node); 156 } else { 157 Document doc = node.getOwnerDocument(); 158 return toString(doc); 159 } 160 } catch (Exception e) { 161 e.printStackTrace(); 162 throw e; 163 } 164 } 165 166 protected void deleteResource(String id) throws Exception { 167 col.removeResource(col.getResource(id)); 168 } 169 170 private Properties loadProps(InputStream in) { 171 Properties props = createDefaultProps(); 172 try { 173 props.load(in); 174 } catch (Exception e) { 175 logger.warn("Didn't find props file, using defaults"); 176 props.list(System.out); 177 } 178 return props; 179 180 } 181 182 private Properties loadProps(String iniFileName) { 183 Properties props = createDefaultProps(); 184 try { 186 props.load(new FileInputStream(new File(iniFileName))); 187 } catch (Exception e) { 188 logger.warn("Didn't find props file, using defaults"); 189 props.list(System.out); 190 } 191 return props; 192 } 193 194 199 private Properties createDefaultProps() { 200 Properties defaultProps = new Properties (); 201 202 defaultProps.put("driverName", "org.xmldb.api.reference.DatabaseImpl"); 204 defaultProps.put("URI", "xmldb:ref:///child1"); 205 206 210 Properties props = new Properties (defaultProps); 211 return props; 212 } 213 214 protected Document createXMLFile(String fileName) throws Exception { 215 logger.debug("XMLDBTestCase.createXMLFile() - Writing file= " + fileName); 216 FileWriter out = new FileWriter(fileName); 217 218 DOMImplementation documentCreator = new DOMImplementationImpl(); 219 Document doc = documentCreator.createDocument(null, "XMLDBTests", null); 220 221 Element root = doc.getDocumentElement(); 222 223 Element levelZeroTests = doc.createElement("levelZeroTests"); 224 levelZeroTests.setAttribute("complianceLevel", "0"); 225 root.appendChild(levelZeroTests); 226 227 Element testName = doc.createElement("testName"); 228 levelZeroTests.appendChild(testName); 229 230 Node name = doc.createTextNode("testBinary"); 231 testName.appendChild(name); 232 233 testName = doc.createElement("testName"); 234 levelZeroTests.appendChild(testName); 235 236 name = doc.createTextNode("testDOM"); 237 testName.appendChild(name); 238 239 OutputFormat format = new OutputFormat(doc, "UTF-8", true); 240 XMLSerializer serializer = new XMLSerializer(out, format); 241 242 serializer.serialize(doc); 243 out.close(); 244 245 return doc; 246 } 247 248 protected boolean deleteXMLFile(String xmlFileName) { 249 File file = new File(xmlFileName); 250 return file.delete(); 251 } 252 } 253 | Popular Tags |