empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From franci...@apache.org
Subject svn commit: r932968 - in /incubator/empire-db/trunk/empire-db-codegen: ./ src/main/java/org/apache/empire/db/codegen/ src/test/java/org/apache/empire/db/codegen/ src/test/resources/ src/test/resources/hsqldb/
Date Sun, 11 Apr 2010 18:15:22 GMT
Author: francisdb
Date: Sun Apr 11 18:15:22 2010
New Revision: 932968

URL: http://svn.apache.org/viewvc?rev=932968&view=rev
Log:
fail fast in the codegen if the template folder does not exist or is not readable

Added:
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/CodeGenAppTest.java
    incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/
    incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.properties
    incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.script
    incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_invalid_template_folder.xml
    incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_using_template_folder.xml
Modified:
    incubator/empire-db/trunk/empire-db-codegen/pom.xml
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
    incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig.xml

Modified: incubator/empire-db/trunk/empire-db-codegen/pom.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/pom.xml?rev=932968&r1=932967&r2=932968&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/pom.xml (original)
+++ incubator/empire-db/trunk/empire-db-codegen/pom.xml Sun Apr 11 18:15:22 2010
@@ -37,7 +37,7 @@
 		<dependency>
 			<groupId>org.apache.velocity</groupId>
 			<artifactId>velocity</artifactId>
-			<version>1.6.2</version>
+			<version>1.6.3</version>
 		</dependency>
 
 		<!--  Databases -->

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java?rev=932968&r1=932967&r2=932968&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
Sun Apr 11 18:15:22 2010
@@ -33,7 +33,6 @@ import org.apache.empire.db.DBView;
 import org.apache.empire.db.codegen.util.FileUtils;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
 import org.apache.velocity.app.VelocityEngine;
 import org.apache.velocity.exception.ParseErrorException;
 import org.apache.velocity.exception.ResourceNotFoundException;
@@ -78,7 +77,8 @@ public class CodeGenWriter {
 
 	// Services
 	private final WriterService writerService;
-
+	private final VelocityEngine engine;
+	
 	// Properties
 	private final CodeGenConfig config;
 	private File baseDir;
@@ -92,17 +92,23 @@ public class CodeGenWriter {
 	public CodeGenWriter(CodeGenConfig config) {
 		this.writerService = new WriterService(config);
 		this.config = config;
+		this.engine = new VelocityEngine();
 		// we have to keep this in sync with our logging system
 		// http://velocity.apache.org/engine/releases/velocity-1.5/developer-guide.html#simpleexampleofacustomlogger
-		Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,
+		engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,
 				new CommonsLogLogChute());
 		if(useClasspathTemplates()){
-			Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
-			Velocity.setProperty("classpath." + VelocityEngine.RESOURCE_LOADER + ".class", ClasspathResourceLoader.class.getName());
-		} 
+			engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
+			engine.setProperty("classpath." + VelocityEngine.RESOURCE_LOADER + ".class", ClasspathResourceLoader.class.getName());
+		}else{
+			File templateFolder = new File(config.getTemplateFolder());
+			if(!templateFolder.canRead()){
+				throw new RuntimeException("Provided template folder missing or not readable: " + config.getTemplateFolder());
+			}
+		}
 		
 		try {
-			Velocity.init();
+			engine.init();
 		} catch (Exception e) {
 			log.fatal(e);
 			throw new RuntimeException(e);
@@ -298,7 +304,7 @@ public class CodeGenWriter {
 		Writer writer = null;
 		try {
 			log.info("Writing " + file);
-			Template velocityTemplate = Velocity.getTemplate(templatePath);
+			Template velocityTemplate = engine.getTemplate(templatePath);
 			writer = new FileWriter(file);
 			velocityTemplate.merge(context, writer);
 		} catch (IOException e) {

Added: incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/CodeGenAppTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/CodeGenAppTest.java?rev=932968&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/CodeGenAppTest.java
(added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/CodeGenAppTest.java
Sun Apr 11 18:15:22 2010
@@ -0,0 +1,61 @@
+package org.apache.empire.db.codegen;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.empire.db.codegen.util.FileUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CodeGenAppTest {
+	
+	@Before
+	public void cleanup() throws IOException{
+		File generated = new File("target/generated");
+		if(generated.exists()){
+			boolean deleted = FileUtils.deleteDirectory(generated);
+			if(!deleted){
+				throw new IOException("Could not delete previously generated sources");
+			}
+		}
+	}
+
+	@Test
+	public void testMain() {
+		String[] args = new String[]{"src/test/resources/testconfig.xml"};
+		
+		// execute app
+		CodeGenApp.main(args);
+		
+		// expected files
+		File expected = new File("target/generated/dbsample/org/apache/empire/db/samples/dbsample/SampleDB.java");
+		assertTrue("missing generated code", expected.exists());
+	}
+	
+	@Test
+	public void testMainUsingTemplateFolder() {
+		String[] args = new String[]{"src/test/resources/testconfig_using_template_folder.xml"};
+		
+		// execute app
+		CodeGenApp.main(args);
+		
+		// expected files
+		File expected = new File("target/generated/dbsample/org/apache/empire/db/samples/dbsample/SampleDB.java");
+		assertTrue("missing generated code", expected.exists());
+	}
+	
+	@Test
+	public void testMainFailInvalidTemplateFolder() {
+		String[] args = new String[]{"src/test/resources/testconfig_invalid_template_folder.xml"};
+		try{
+			CodeGenApp.main(args);
+			fail("This should fail as the template path is missing");
+		}catch(RuntimeException ex){
+			assertTrue("Wrong message", ex.getMessage().startsWith("Provided template folder missing
or not readable:"));
+		}
+	}
+
+}

Added: incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.properties
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.properties?rev=932968&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.properties
(added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.properties
Sun Apr 11 18:15:22 2010
@@ -0,0 +1,35 @@
+# 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.
+#
+#HSQL Database Engine 1.8.0.10
+#Tue Feb 02 00:26:54 CET 2010
+hsqldb.script_format=0
+runtime.gc_interval=0
+sql.enforce_strict_size=false
+hsqldb.cache_size_scale=8
+readonly=false
+hsqldb.nio_data_file=true
+hsqldb.cache_scale=14
+version=1.8.0
+hsqldb.default_table_type=memory
+hsqldb.cache_file_scale=1
+hsqldb.log_size=200
+modified=no
+hsqldb.cache_version=1.7.0
+hsqldb.original_version=1.8.0
+hsqldb.compatible_version=1.8.0
+hsqldb.files_readonly=true

Added: incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.script
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.script?rev=932968&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.script (added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/resources/hsqldb/sample.script Sun
Apr 11 18:15:22 2010
@@ -0,0 +1,33 @@
+-- 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.
+
+CREATE SCHEMA PUBLIC AUTHORIZATION DBA
+CREATE SEQUENCE DEP_ID_SEQUENCE AS INTEGER START WITH 3 
+CREATE SEQUENCE EMPLOYEE_ID_SEQUENCE AS INTEGER START WITH 4 
+CREATE MEMORY TABLE DEPARTMENTS(DEPARTMENT_ID BIGINT NOT NULL,NAME VARCHAR(80) NOT NULL,HEAD
VARCHAR(80),BUSINESS_UNIT VARCHAR(4) NOT NULL,UPDATE_TIMESTAMP TIMESTAMP NOT NULL,CONSTRAINT
DEPARTMENTS_PK PRIMARY KEY(DEPARTMENT_ID))
+CREATE UNIQUE INDEX DEARTMENT_NAME_IDX ON DEPARTMENTS(NAME)
+CREATE MEMORY TABLE EMPLOYEES(EMPLOYEE_ID BIGINT NOT NULL,SALUTATION VARCHAR(20),FIRSTNAME
VARCHAR(40) NOT NULL,LASTNAME VARCHAR(40) NOT NULL,DATE_OF_BIRTH DATE,DEPARTMENT_ID BIGINT
NOT NULL,GENDER VARCHAR(1),PHONE_NUMBER VARCHAR(40),EMAIL VARCHAR(80),SALARY DECIMAL(10,2),RETIRED
BOOLEAN NOT NULL,UPDATE_TIMESTAMP TIMESTAMP NOT NULL,CONSTRAINT EMPLOYEES_PK PRIMARY KEY(EMPLOYEE_ID),CONSTRAINT
EMPLOYEES_DEPARTMENT_I_FK FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENTS(DEPARTMENT_ID))
+CREATE UNIQUE INDEX EMPLOYEE_NAME_IDX ON EMPLOYEES(FIRSTNAME,LASTNAME,DATE_OF_BIRTH)
+CREATE USER SA PASSWORD ""
+GRANT DBA TO SA
+SET WRITE_DELAY 10
+SET SCHEMA PUBLIC
+INSERT INTO DEPARTMENTS VALUES(1,'Development',NULL,'ITTK','2010-02-01 22:27:44.200000000')
+INSERT INTO DEPARTMENTS VALUES(2,'Sales',NULL,'ITTK','2010-02-01 22:27:44.212000000')
+INSERT INTO EMPLOYEES VALUES(1,NULL,'Peter','Sharp',NULL,1,'M','+49-7531-457160',NULL,NULL,FALSE,'2010-02-01
22:27:44.226000000')
+INSERT INTO EMPLOYEES VALUES(2,NULL,'Fred','Bloggs',NULL,1,'M','+49-5555-505050',NULL,NULL,FALSE,'2010-02-01
22:27:44.240000000')
+INSERT INTO EMPLOYEES VALUES(3,NULL,'Emma','White',NULL,2,'F','+49-040-125486',NULL,NULL,FALSE,'2010-02-01
22:27:44.243000000')

Modified: incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig.xml?rev=932968&r1=932967&r2=932968&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig.xml (original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig.xml Sun Apr
11 18:15:22 2010
@@ -21,11 +21,12 @@
 
 	<properties>
 		<!-- provider name must match the property-section containing the connection data -->
-		<jdbcClass>oracle.jdbc.driver.OracleDriver</jdbcClass>
-		<jdbcURL>jdbc:oracle:thin:@192.168.0.2:1521:ora10</jdbcURL>
-		<jdbcUser>DBSAMPLE</jdbcUser>
-		<jdbcPwd>DBSAMPLE</jdbcPwd>
-
+		<jdbcClass>org.hsqldb.jdbcDriver</jdbcClass>
+		<jdbcURL>jdbc:hsqldb:file:src/test/resources/hsqldb/sample;shutdown=true</jdbcURL>
+		<jdbcUser>sa</jdbcUser>
+		<jdbcPwd></jdbcPwd>
+		<packageName>org.apache.empire.db.example</packageName>
+		
 		<!-- Schema options -->
 		<dbCatalog></dbCatalog>
 		<dbSchema>DBSAMPLE</dbSchema>
@@ -42,8 +43,8 @@
 		<tableClassPrefix>T</tableClassPrefix>
 		<tableClassSuffix>Table</tableClassSuffix>
 		<viewClassPrefix>V</viewClassPrefix>
-		<nestTables>True</nestTables>
-		<nestViews>False</nestViews>
+		<nestTables>true</nestTables>
+		<nestViews>false</nestViews>
 		<createRecordProperties>true</createRecordProperties>
 	</properties>
 	

Added: incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_invalid_template_folder.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_invalid_template_folder.xml?rev=932968&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_invalid_template_folder.xml
(added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_invalid_template_folder.xml
Sun Apr 11 18:15:22 2010
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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>
+
+	<properties>
+		<!-- provider name must match the property-section containing the connection data -->
+		<jdbcClass>org.hsqldb.jdbcDriver</jdbcClass>
+		<jdbcURL>jdbc:hsqldb:file:src/test/resources/hsqldb/sample;shutdown=true</jdbcURL>
+		<jdbcUser>sa</jdbcUser>
+		<jdbcPwd></jdbcPwd>
+		<packageName>org.apache.empire.db.example</packageName>
+		
+		<!-- Schema options -->
+		<dbCatalog></dbCatalog>
+		<dbSchema>DBSAMPLE</dbSchema>
+		<dbTablePattern></dbTablePattern>
+		<timestampColumn>CREATIONDATE</timestampColumn>
+		
+		<!-- generation options -->
+		<templateFolder>i/do/not/exist</templateFolder>
+		<targetFolder>target/generated/dbsample</targetFolder>
+		<packageName>org.apache.empire.db.samples.dbsample</packageName>
+		<dbClassName>SampleDB</dbClassName>
+		<tableBaseName>SampleTable</tableBaseName>
+		<viewBaseName>SampleView</viewBaseName>
+		<recordBaseName>SampleRecord</recordBaseName>
+		<tableClassPrefix>T</tableClassPrefix>
+		<tableClassSuffix>Table</tableClassSuffix>
+		<viewClassPrefix>V</viewClassPrefix>
+		<nestTables>true</nestTables>
+		<nestViews>false</nestViews>
+		<createRecordProperties>true</createRecordProperties>
+	</properties>
+	
+	<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+
+		<appender name="default" class="org.apache.log4j.ConsoleAppender">
+			<!-- layout class="org.apache.log4j.TTCCLayout"/ -->
+			<layout class="org.apache.log4j.PatternLayout">
+				<!-- param name="ConversionPattern" value="NSB(%c) %-5p %m	at %l%n"/ -->
+				<param name="ConversionPattern" value="%-5p [%d{yyyy/MM/dd HH:mm}]: %m		at %l %n"/>
+			</layout>
+		</appender>
+	
+		<!-- log detail configuration -->
+		<logger name="org.apache.empire.commons" additivity="false">
+			<level value="warn"/>
+			<appender-ref ref="default"/>
+		</logger>
+	
+		<root>
+			<priority value="info"/>
+			<appender-ref ref="default"/>
+		</root>
+
+	</log4j:configuration>
+	
+</config>

Added: incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_using_template_folder.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_using_template_folder.xml?rev=932968&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_using_template_folder.xml
(added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/resources/testconfig_using_template_folder.xml
Sun Apr 11 18:15:22 2010
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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>
+
+	<properties>
+		<!-- provider name must match the property-section containing the connection data -->
+		<jdbcClass>org.hsqldb.jdbcDriver</jdbcClass>
+		<jdbcURL>jdbc:hsqldb:file:src/test/resources/hsqldb/sample;shutdown=true</jdbcURL>
+		<jdbcUser>sa</jdbcUser>
+		<jdbcPwd></jdbcPwd>
+		<packageName>org.apache.empire.db.example</packageName>
+		
+		<!-- Schema options -->
+		<dbCatalog></dbCatalog>
+		<dbSchema>DBSAMPLE</dbSchema>
+		<dbTablePattern></dbTablePattern>
+		<timestampColumn>CREATIONDATE</timestampColumn>
+		
+		<!-- generation options -->
+		<templateFolder>src/main/resources/templates</templateFolder>
+		<targetFolder>target/generated/dbsample</targetFolder>
+		<packageName>org.apache.empire.db.samples.dbsample</packageName>
+		<dbClassName>SampleDB</dbClassName>
+		<tableBaseName>SampleTable</tableBaseName>
+		<viewBaseName>SampleView</viewBaseName>
+		<recordBaseName>SampleRecord</recordBaseName>
+		<tableClassPrefix>T</tableClassPrefix>
+		<tableClassSuffix>Table</tableClassSuffix>
+		<viewClassPrefix>V</viewClassPrefix>
+		<nestTables>true</nestTables>
+		<nestViews>false</nestViews>
+		<createRecordProperties>true</createRecordProperties>
+	</properties>
+	
+	<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+
+		<appender name="default" class="org.apache.log4j.ConsoleAppender">
+			<!-- layout class="org.apache.log4j.TTCCLayout"/ -->
+			<layout class="org.apache.log4j.PatternLayout">
+				<!-- param name="ConversionPattern" value="NSB(%c) %-5p %m	at %l%n"/ -->
+				<param name="ConversionPattern" value="%-5p [%d{yyyy/MM/dd HH:mm}]: %m		at %l %n"/>
+			</layout>
+		</appender>
+	
+		<!-- log detail configuration -->
+		<logger name="org.apache.empire.commons" additivity="false">
+			<level value="warn"/>
+			<appender-ref ref="default"/>
+		</logger>
+	
+		<root>
+			<priority value="info"/>
+			<appender-ref ref="default"/>
+		</root>
+
+	</log4j:configuration>
+	
+</config>



Mime
View raw message