diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /java/das/tools/src/test |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/das/tools/src/test')
3 files changed, 207 insertions, 0 deletions
diff --git a/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/DBToXSDTests.java b/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/DBToXSDTests.java new file mode 100644 index 0000000000..aba4eba68f --- /dev/null +++ b/java/das/tools/src/test/java/org/apache/tuscany/das/rdb/test/DBToXSDTests.java @@ -0,0 +1,142 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.das.rdb.test;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.das.rdb.dbconfig.DBInitializer;
+import org.apache.tuscany.das.rdb.util.DBToXSDGenerator;
+/**
+ * Tests the DB Schema to Model XSD converter tool
+ */
+public class DBToXSDTests extends TestCase {
+ protected void setUp() throws Exception {
+ super.setUp();
+ DBInitializer dbInitializer = new DBInitializer("dbConfig.xml");
+ dbInitializer.initializeDatabase(true);
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ static public String getContents(File aFile) {
+ //...checks on aFile are elided
+ StringBuffer contents = new StringBuffer();
+
+ //declared here only to make visible to finally clause
+ BufferedReader input = null;
+ try {
+ //use buffering, reading one line at a time
+ //FileReader always assumes default encoding is OK!
+ input = new BufferedReader( new FileReader(aFile) );
+ String line = null; //not declared within while loop
+ /*
+ * readLine is a bit quirky :
+ * it returns the content of a line MINUS the newline.
+ * it returns null only for the END of the stream.
+ * it returns an empty String if two newlines appear in a row.
+ */
+ while (( line = input.readLine()) != null){
+ contents.append(line);
+ contents.append(System.getProperty("line.separator"));
+ }
+ }
+ catch (FileNotFoundException ex) {
+ ex.printStackTrace();
+ }
+ catch (IOException ex){
+ ex.printStackTrace();
+ }
+ finally {
+ try {
+ if (input!= null) {
+ //flush and close both "input" and its underlying FileReader
+ input.close();
+ }
+ }
+ catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+ return contents.toString();
+ }
+
+ String compare1 = "<table javaName=\"STATES\" name=\"STATES\">";
+ String compare2 = "<table javaName=\"CITIES\" name=\"CITIES\">";
+ String compare3 = "<xsd:complexType name=\"CITIES\">";
+ String compare4 = "<xsd:complexType name=\"STATES\">";
+
+ public void testDBToSchemaFile() throws Exception {
+ DBToXSDGenerator.getSchemaFileFromDB("DBConnectionConfig.xml");
+ File file = new File("target/dbSchema.txt");
+ if(file.isFile()) {
+ String schemaContent = getContents(file);
+ if(schemaContent.indexOf(compare1) != -1 && schemaContent.indexOf(compare2) != -1) {
+ assertTrue(true);
+ }
+ } else {
+ fail("SchemaFile does not exists or has improper contents!");
+ }
+ }
+
+ public void testSchemaFileToXSD() throws Exception {
+ DBToXSDGenerator.getModelFileFromSchemaFile("target/dbSchema.txt", "target/schemaModel.xsd");
+
+ File modelfile = new File("target/schemaModel.xsd");
+ if(modelfile.isFile()) {
+ String modelContent = getContents(modelfile);
+ if(modelContent.indexOf(compare3) != -1 && modelContent.indexOf(compare4) != -1) {
+ assertTrue(true);
+ }
+ } else {
+ fail("ModelFile does not exists or has improper contents!");
+ }
+ }
+
+ public void testDBToXSD() throws Exception {
+ DBToXSDGenerator.getModelFileFromDB("DBConnectionConfig.xml");
+
+ File file = new File("target/dbSchema.txt");
+ if(file.isFile()) {
+ String schemaContent = getContents(file);
+ if(schemaContent.indexOf(compare1) != -1 && schemaContent.indexOf(compare2) != -1) {
+ assertTrue(true);
+ }
+ } else {
+ fail("SchemaFile does not exists or has improper contents!");
+ }
+
+ File modelfile = new File("target/schemaModel.xsd");
+ if(modelfile.isFile()) {
+ String modelContent = getContents(modelfile);
+ if(modelContent.indexOf(compare3) != -1 && modelContent.indexOf(compare4) != -1) {
+ assertTrue(true);
+ }
+ } else {
+ fail("ModelFile does not exists or has improper contents!");
+ }
+ }
+}
diff --git a/java/das/tools/src/test/resources/DBConnectionConfig.xml b/java/das/tools/src/test/resources/DBConnectionConfig.xml new file mode 100644 index 0000000000..c00bd462db --- /dev/null +++ b/java/das/tools/src/test/resources/DBConnectionConfig.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="ASCII"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<Config xmlns="http:///org.apache.tuscany.das.rdb/config.xsd">
+
+ <!--Uncomment below for derby test-->
+ <ConnectionInfo>
+ <ConnectionProperties
+ driverClass="org.apache.derby.jdbc.EmbeddedDriver"
+ databaseURL="jdbc:derby:target/dastest;create=true" schemaName="APP"
+ />
+ </ConnectionInfo>
+ <OutFiles schemaFile="target/dbSchema.txt"
+ modelFile="target/schemaModel.xsd" />
+</Config>
diff --git a/java/das/tools/src/test/resources/dbConfig.xml b/java/das/tools/src/test/resources/dbConfig.xml new file mode 100644 index 0000000000..b990d5cd0f --- /dev/null +++ b/java/das/tools/src/test/resources/dbConfig.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="ASCII"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<DBConfig xmlns="http:///org.apache.tuscany.das.rdb/dbconfig.xsd">
+ <ConnectionInfo>
+ <ConnectionProperties
+ driverClass="org.apache.derby.jdbc.EmbeddedDriver"
+ databaseURL="jdbc:derby:target/dastest; create = true"
+ />
+ </ConnectionInfo>
+ <Table name="STATES"
+ SQLCreate="CREATE TABLE STATES (ID INT PRIMARY KEY NOT NULL, NAME VARCHAR(2))">
+ </Table>
+ <Table name="CITIES"
+ SQLCreate="CREATE TABLE CITIES (ID INT PRIMARY KEY NOT NULL, NAME VARCHAR(50), STATES_ID INTEGER,
+ CONSTRAINT FK1 FOREIGN KEY (STATES_ID) REFERENCES STATES (ID) ON DELETE NO ACTION ON UPDATE NO ACTION)">
+ </Table>
+</DBConfig>
|