[hibernate-commits] Hibernate SVN: r11621 - in branches/Branch_3_2/HibernateExt/tools/src: test/org/hibernate/tool/test/jdbc2cfg and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Jun 4 09:00:20 EDT 2007


Author: max.andersen at jboss.com
Date: 2007-06-04 09:00:19 -0400 (Mon, 04 Jun 2007)
New Revision: 11621

Added:
   branches/Branch_3_2/HibernateExt/tools/src/test5.0/org/hibernate/tool/test/
   branches/Branch_3_2/HibernateExt/tools/src/test5.0/org/hibernate/tool/test/jdbc2cfg/
   branches/Branch_3_2/HibernateExt/tools/src/test5.0/org/hibernate/tool/test/jdbc2cfg/VersioningForJDK50Test.java
Modified:
   branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/VersioningTest.java
Log:
HBX-942 @Version generation

Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java	2007-06-04 12:57:40 UTC (rev 11620)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java	2007-06-04 13:00:19 UTC (rev 11621)
@@ -307,6 +307,9 @@
 	public String generateBasicAnnotation(Property property) {
 		StringBuffer annotations = new StringBuffer( "    " );
 		if(property.getValue() instanceof SimpleValue) {
+			if (hasVersionProperty())
+				if (property.equals(getVersionProperty()))
+						buildVersionAnnotation(annotations);
 			String typeName = ((SimpleValue)property.getValue()).getTypeName();
 			if("date".equals(typeName) || "java.sql.Date".equals(typeName)) {
 				buildTemporalAnnotation( annotations, "DATE" );
@@ -315,6 +318,8 @@
 			} else if ("time".equals(typeName) || "java.sql.Time".equals(typeName)) {
 				buildTemporalAnnotation(annotations, "TIME");
 			} //TODO: calendar etc. ?
+
+						
 		}
 			
 		return annotations.toString();
@@ -327,6 +332,12 @@
 		return annotations.append( "@" + temporal +"(" + temporalType + "." + temporalTypeValue + ")");
 	}
 	
+	private StringBuffer buildVersionAnnotation(StringBuffer annotations) {
+		String version = importType("javax.persistence.Version");
+		
+		return annotations.append( "@" + version );
+	}
+	
 	public String generateAnnColumnAnnotation(Property property) {
 		StringBuffer annotations = new StringBuffer( "    " );
 		boolean insertable = property.isInsertable();

Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/VersioningTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/VersioningTest.java	2007-06-04 12:57:40 UTC (rev 11620)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/VersioningTest.java	2007-06-04 13:00:19 UTC (rev 11621)
@@ -17,13 +17,15 @@
 import org.hibernate.tool.hbm2x.HibernateMappingExporter;
 import org.hibernate.type.IntegerType;
 import org.hibernate.type.TimestampType;
+;
 
 /**
+ * To be extended by VersioningForJDK50Test for the JPA generation part
  * @author max
  *
  */
 public class VersioningTest extends JDBCMetaDataBinderTestCase {
-
+	
 	protected String[] getCreateSQL() {
 		
 		return new String[] {
@@ -59,10 +61,8 @@
 	}
 	
 	public void testGenerateMappings() {
-		
         cfg.buildMappings();
-        
-		Exporter exporter = new HibernateMappingExporter(cfg, getOutputDir());
+        Exporter exporter = new HibernateMappingExporter(cfg, getOutputDir());
 		
 		exporter.start();
 		
@@ -70,36 +70,40 @@
 		
 		derived.addFile(new File(getOutputDir(), "Withversion.hbm.xml") );
 		derived.addFile(new File(getOutputDir(), "Noversion.hbm.xml") );
-		derived.addFile(new File(getOutputDir(), "WithRealTimestamp.hbm.xml") );
-		derived.addFile(new File(getOutputDir(), "WithFakeTimestamp.hbm.xml") );
+		derived.addFile(new File(getOutputDir(), "Withrealtimestamp.hbm.xml") );
+		derived.addFile(new File(getOutputDir(), "Withfaketimestamp.hbm.xml") );
 		
+		testVersioningInDerivedCfg(derived);
+	}
+    
+	protected void testVersioningInDerivedCfg(Configuration derived){
 		derived.buildMappings();
 		
-		PersistentClass cl = derived.getClassMapping("Withversion");		
+		PersistentClass cl = derived.getClassMapping( "Withversion" );		
 		
 		Property version = cl.getVersion();
 		assertNotNull(version);
 		assertEquals("version", version.getName());
 		
-		cl = derived.getClassMapping("Noversion");
+		cl = derived.getClassMapping( "Noversion" );
 		assertNotNull(cl);
 		version = cl.getVersion();
 		assertNull(version);
 
-		cl = derived.getClassMapping("Withrealtimestamp");
+		cl = derived.getClassMapping( "Withrealtimestamp" );
 		assertNotNull(cl);
 		version = cl.getVersion();
 		assertNotNull(version);
 		assertTrue(version.getType() instanceof TimestampType);
 		
-		cl = derived.getClassMapping("Withfaketimestamp");
+		cl = derived.getClassMapping( "Withfaketimestamp" );
 		assertNotNull(cl);
 		version = cl.getVersion();
 		assertNotNull(version);
 		assertTrue(version.getType() instanceof IntegerType);
-		
 	}
 	
+	
 	public static Test suite() {
 		return new TestSuite(VersioningTest.class);
 	}

Added: branches/Branch_3_2/HibernateExt/tools/src/test5.0/org/hibernate/tool/test/jdbc2cfg/VersioningForJDK50Test.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test5.0/org/hibernate/tool/test/jdbc2cfg/VersioningForJDK50Test.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/test5.0/org/hibernate/tool/test/jdbc2cfg/VersioningForJDK50Test.java	2007-06-04 13:00:19 UTC (rev 11621)
@@ -0,0 +1,85 @@
+/*
+ * Created on 2004-11-24
+ *
+ */
+package org.hibernate.tool.test.jdbc2cfg;
+
+import java.io.File;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.hibernate.cfg.AnnotationConfiguration;
+import org.hibernate.cfg.JDBCMetaDataConfiguration;
+import org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy;
+import org.hibernate.cfg.reveng.ReverseEngineeringSettings;
+import org.hibernate.tool.hbm2x.POJOExporter;
+import org.hibernate.tool.test.TestHelper;
+;
+
+/**
+ * based on VersioningTest
+ * requires a default package to be easily added to classloader
+ * steps:
+ * 1- build mappings from jdbc (see table used in VersioningTest, some has version, some timestamp,...
+ * 2- use *annotated* pojo exporter
+ * 3- check if generated classes compile, add them to classloader
+ * 4- make a derived configuration from annotated classes
+ * 5- test the derived configuration
+ * 
+ * @author anthony
+ */
+public class VersioningForJDK50Test extends VersioningTest {
+    protected void configure(JDBCMetaDataConfiguration cfgToConfigure) {        
+        DefaultReverseEngineeringStrategy c = new DefaultReverseEngineeringStrategy();
+        c.setSettings(new ReverseEngineeringSettings(c));
+        cfgToConfigure.setReverseEngineeringStrategy(c);
+    }
+	
+	public void testGenerateJPA() throws Exception{
+        
+        cfg.buildMappings();  
+        
+		POJOExporter exporter = new POJOExporter(cfg, getOutputDir() );
+		exporter.setTemplatePath(new String[0]);
+		exporter.getProperties().setProperty("ejb3", "true");
+		exporter.getProperties().setProperty("jdk5", "true");
+		exporter.start();
+		
+		File file = new File( "ejb3compilable" );
+		file.mkdir();
+		
+		ArrayList list = new ArrayList();
+		List jars = new ArrayList();
+		jars.add("ejb3-persistence.jar");
+		jars.add("hibernate-annotations.jar");
+		TestHelper.compile(getOutputDir(), file, TestHelper.visitAllFiles(getOutputDir(), list), "1.5", TestHelper.buildClasspath(jars));
+		
+		URL[] urls = new URL[]{file.toURL()};
+		Thread currentThread = Thread.currentThread();
+		URLClassLoader ucl = new URLClassLoader( urls, currentThread.getContextClassLoader() );
+		currentThread.setContextClassLoader( ucl );
+
+		
+		Class withversionClazz = ucl.loadClass("Withversion");
+		Class withrealtimestampClazz = ucl.loadClass("Withrealtimestamp");
+		Class noversionClazz = ucl.loadClass("Noversion");
+		Class withfaketimestampClazz = ucl.loadClass("Withfaketimestamp");
+		AnnotationConfiguration derived = new AnnotationConfiguration();
+		derived.addAnnotatedClass( withversionClazz );
+		derived.addAnnotatedClass( withrealtimestampClazz );
+		derived.addAnnotatedClass( noversionClazz );
+		derived.addAnnotatedClass( withfaketimestampClazz );
+		
+		testVersioningInDerivedCfg(  derived );
+		
+	}
+	
+	public static Test suite() {
+		return new TestSuite(VersioningForJDK50Test.class);
+	}
+}




More information about the hibernate-commits mailing list