Author: steve.ebersole(a)jboss.com
Date: 2009-11-12 14:46:04 -0500 (Thu, 12 Nov 2009)
New Revision: 17964
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationSerializationTest.java
core/trunk/testsuite/src/test/perf/org/hibernate/test/perf/ConfigurationPerformanceTest.java
Removed:
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationPerformanceTest.java
Modified:
core/trunk/core/src/main/java/org/hibernate/cfg/Configuration.java
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/CacheableFileTest.java
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ListenerTest.java
Log:
HHH-4569 - Split focus of ConfigurationPerformanceTest
Modified: core/trunk/core/src/main/java/org/hibernate/cfg/Configuration.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/cfg/Configuration.java 2009-11-12 16:17:12
UTC (rev 17963)
+++ core/trunk/core/src/main/java/org/hibernate/cfg/Configuration.java 2009-11-12 19:46:04
UTC (rev 17964)
@@ -424,70 +424,85 @@
* the non-cached file.
*/
public Configuration addCacheableFile(File xmlFile) throws MappingException {
+ File cachedFile = determineCachedDomFile( xmlFile );
+
try {
- File cachedFile = new File( xmlFile.getAbsolutePath() + ".bin" );
- org.dom4j.Document doc = null;
+ return addCacheableFileStrictly( xmlFile );
+ }
+ catch ( SerializationException e ) {
+ log.warn( "Could not deserialize cache file: " + cachedFile.getPath() +
" : " + e );
+ }
+ catch ( FileNotFoundException e ) {
+ log.warn( "I/O reported cached file could not be found : " +
cachedFile.getPath() + " : " + e );
+ }
- final boolean useCachedFile = xmlFile.exists() &&
- cachedFile.exists() &&
- xmlFile.lastModified() < cachedFile.lastModified();
+ if ( !xmlFile.exists() ) {
+ throw new MappingNotFoundException( "file", xmlFile.toString() );
+ }
- if ( useCachedFile ) {
- try {
- log.info( "Reading mappings from cache file: " + cachedFile );
- doc = ( org.dom4j.Document ) SerializationHelper.deserialize( new FileInputStream(
cachedFile ) );
- }
- catch ( SerializationException e ) {
- log.warn( "Could not deserialize cache file: " + cachedFile.getPath(), e
);
- }
- catch ( FileNotFoundException e ) {
- log.warn( "I/O reported cached file could not be found : " +
cachedFile.getPath(), e );
- }
+ log.info( "Reading mappings from file: " + xmlFile );
+ List errors = new ArrayList();
+ try {
+ org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors,
entityResolver ).read( xmlFile );
+ if ( errors.size() != 0 ) {
+ throw new MappingException( "invalid mapping", ( Throwable ) errors.get( 0
) );
}
- // if doc is null, then for whatever reason, the cached file cannot be used...
- if ( doc == null ) {
- if ( !xmlFile.exists() ) {
- throw new MappingNotFoundException( "file", xmlFile.toString() );
- }
-
- log.info( "Reading mappings from file: " + xmlFile );
- List errors = new ArrayList();
- try {
- doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver
).read( xmlFile );
- if ( errors.size() != 0 ) {
- throw new MappingException( "invalid mapping", ( Throwable ) errors.get(
0 ) );
- }
- }
- catch( DocumentException e){
- throw new MappingException( "invalid mapping", e );
- }
-
- try {
- log.debug( "Writing cache file for: " + xmlFile + " to: " +
cachedFile );
- SerializationHelper.serialize( ( Serializable ) doc, new FileOutputStream(
cachedFile ) );
- }
- catch ( SerializationException e ) {
- log.warn( "Could not write cached file: " + cachedFile, e );
- }
- catch ( FileNotFoundException e ) {
- log.warn( "I/O reported error writing cached file : " +
cachedFile.getPath(), e );
- }
+ try {
+ log.debug( "Writing cache file for: " + xmlFile + " to: " +
cachedFile );
+ SerializationHelper.serialize( ( Serializable ) doc, new FileOutputStream( cachedFile
) );
}
+ catch ( SerializationException e ) {
+ log.warn( "Could not write cached file: " + cachedFile, e );
+ }
+ catch ( FileNotFoundException e ) {
+ log.warn( "I/O reported error writing cached file : " +
cachedFile.getPath(), e );
+ }
add( doc );
- return this;
-
}
- catch ( InvalidMappingException e ) {
- throw e;
+ catch( DocumentException e){
+ throw new MappingException( "invalid mapping", e );
}
- catch ( MappingNotFoundException e ) {
- throw e;
+
+ return this;
+ }
+
+ private File determineCachedDomFile(File xmlFile) {
+ return new File( xmlFile.getAbsolutePath() + ".bin" );
+ }
+
+ /**
+ * <b>INTENDED FOR TESTSUITE USE ONLY!</b>
+ * <p/>
+ * Much like {@link addCacheableFile(File)} except that here we will fail immediately
if
+ * the cache version cannot be found or used for whatever reason
+ *
+ * @param xmlFile The xml file, not the bin!
+ *
+ * @return The dom "deserialized" from the cached file.
+ *
+ * @throws MappingException Indicates a problem in the underlyiong call to {@link
#add(org.dom4j.Document)}
+ * @throws SerializationException Indicates a problem deserializing the cached dom tree
+ * @throws FileNotFoundException Indicates that the cached file was not found or was not
usable.
+ */
+ public Configuration addCacheableFileStrictly(File xmlFile)
+ throws MappingException, SerializationException, FileNotFoundException {
+ final File cachedFile = determineCachedDomFile( xmlFile );
+
+ final boolean useCachedFile = xmlFile.exists()
+ && cachedFile.exists()
+ && xmlFile.lastModified() < cachedFile.lastModified();
+
+ if ( ! useCachedFile ) {
+ throw new FileNotFoundException( "Cached file could not be found or could not be
used" );
}
- catch ( Exception e ) {
- throw new InvalidMappingException( "file", xmlFile.toString(), e );
- }
+
+ log.info( "Reading mappings from cache file: " + cachedFile );
+ org.dom4j.Document document =
+ ( org.dom4j.Document ) SerializationHelper.deserialize( new FileInputStream(
cachedFile ) );
+ add( document );
+ return this;
}
/**
Modified:
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/CacheableFileTest.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/CacheableFileTest.java 2009-11-12
16:17:12 UTC (rev 17963)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/CacheableFileTest.java 2009-11-12
19:46:04 UTC (rev 17964)
@@ -1,3 +1,26 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
package org.hibernate.test.cfg;
import java.io.File;
@@ -4,17 +27,18 @@
import org.hibernate.cfg.Configuration;
import org.hibernate.junit.UnitTestCase;
+import org.hibernate.util.SerializationHelper;
/**
- * {@inheritDoc}
+ * Tests using of cacheable configuration files.
*
* @author Steve Ebersole
*/
public class CacheableFileTest extends UnitTestCase {
-
public static final String MAPPING =
"org/hibernate/test/cfg/Cacheable.hbm.xml";
private File mappingFile;
+ private File mappingBinFile;
public CacheableFileTest(String string) {
super( string );
@@ -24,21 +48,31 @@
super.setUp();
mappingFile = new File( getClass().getClassLoader().getResource( MAPPING ).toURI() );
assertTrue( mappingFile.exists() );
- File cached = new File( mappingFile.getParentFile(), mappingFile.getName() +
".bin" );
- if ( cached.exists() ) {
- cached.delete();
+ mappingBinFile = new File( mappingFile.getParentFile(), mappingFile.getName() +
".bin" );
+ if ( mappingBinFile.exists() ) {
+ //noinspection ResultOfMethodCallIgnored
+ mappingBinFile.delete();
}
}
protected void tearDown() throws Exception {
+ if ( mappingBinFile != null && mappingBinFile.exists() ) {
+ // be nice
+ //noinspection ResultOfMethodCallIgnored
+ mappingBinFile.delete();
+ }
+ mappingBinFile = null;
mappingFile = null;
super.tearDown();
}
- public void testCachedFiles() {
- Configuration cfg = new Configuration();
- cfg.addCacheableFile( mappingFile );
- Configuration cfg2 = new Configuration();
- cfg2.addCacheableFile( mappingFile );
+ public void testCachedFiles() throws Exception {
+ assertFalse( mappingBinFile.exists() );
+ // This call should create the cached file
+ new Configuration().addCacheableFile( mappingFile );
+ assertTrue( mappingBinFile.exists() );
+
+ Configuration cfg = new Configuration().addCacheableFileStrictly( mappingFile );
+ SerializationHelper.clone( cfg );
}
}
Deleted:
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationPerformanceTest.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationPerformanceTest.java 2009-11-12
16:17:12 UTC (rev 17963)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationPerformanceTest.java 2009-11-12
19:46:04 UTC (rev 17964)
@@ -1,352 +0,0 @@
-/*
- * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use, modify,
- * copy, or redistribute it subject to the terms and conditions of the GNU
- * Lesser General Public License, v. 2.1. This program is distributed in the
- * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
- * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details. You should have received a
- * copy of the GNU Lesser General Public License, v.2.1 along with this
- * distribution; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Red Hat Author(s): Max Andersen, Steve Ebersole
- */
-package org.hibernate.test.cfg;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.PrintWriter;
-
-import junit.framework.Test;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
-
-import org.hibernate.SessionFactory;
-import org.hibernate.cfg.Configuration;
-import org.hibernate.cfg.Environment;
-import org.hibernate.classic.Session;
-import org.hibernate.junit.UnitTestCase;
-
-/**
- * Test of configuration, specifically "cacheable files".
- *
- * @author Max Andersen
- * @author Steve Ebersole
- */
-public class ConfigurationPerformanceTest extends UnitTestCase {
-
- private final String workPackageName = "org.hibernate.test.cfg.work";
- private File compilationBaseDir;
- private File mappingBaseDir;
- private File workPackageDir;
-
- protected void setUp() throws Exception {
- compilationBaseDir = getTestComplileDirectory();
- mappingBaseDir = new File( compilationBaseDir, "org/hibernate/test" );
- workPackageDir = new File( compilationBaseDir, workPackageName.replace( '.',
'/' ) );
- if ( workPackageDir.exists() ) {
- //noinspection ResultOfMethodCallIgnored
- workPackageDir.delete();
- }
- boolean created = workPackageDir.mkdirs();
- if ( !created ) {
- System.err.println( "Unable to create workPackageDir during setup" );
- }
- }
-
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- private static final String[] FILES = new String[] {
- "legacy/ABC.hbm.xml",
- "legacy/ABCExtends.hbm.xml",
- "legacy/Baz.hbm.xml",
- "legacy/Blobber.hbm.xml",
- "legacy/Broken.hbm.xml",
- "legacy/Category.hbm.xml",
- "legacy/Circular.hbm.xml",
- "legacy/Commento.hbm.xml",
- "legacy/ComponentNotNullMaster.hbm.xml",
- "legacy/Componentizable.hbm.xml",
- "legacy/Container.hbm.xml",
- "legacy/Custom.hbm.xml",
- "legacy/CustomSQL.hbm.xml",
- "legacy/Eye.hbm.xml",
- "legacy/Fee.hbm.xml",
- "legacy/Fo.hbm.xml",
- "legacy/FooBar.hbm.xml",
- "legacy/Fum.hbm.xml",
- "legacy/Fumm.hbm.xml",
- "legacy/Glarch.hbm.xml",
- "legacy/Holder.hbm.xml",
- "legacy/IJ2.hbm.xml",
- "legacy/Immutable.hbm.xml",
- "legacy/Location.hbm.xml",
- "legacy/Many.hbm.xml",
- "legacy/Map.hbm.xml",
- "legacy/Marelo.hbm.xml",
- "legacy/MasterDetail.hbm.xml",
- "legacy/Middle.hbm.xml",
- "legacy/Multi.hbm.xml",
- "legacy/MultiExtends.hbm.xml",
- "legacy/Nameable.hbm.xml",
- "legacy/One.hbm.xml",
- "legacy/ParentChild.hbm.xml",
- "legacy/Qux.hbm.xml",
- "legacy/Simple.hbm.xml",
- "legacy/SingleSeveral.hbm.xml",
- "legacy/Stuff.hbm.xml",
- "legacy/UpDown.hbm.xml",
- "legacy/Vetoer.hbm.xml",
- "legacy/WZ.hbm.xml",
- };
-
- public ConfigurationPerformanceTest(String string) {
- super( string );
- }
-
- public static Test suite() {
- return new TestSuite( ConfigurationPerformanceTest.class );
- }
-
- public static void main(String[] args) throws Exception {
- TestRunner.run( suite() );
- }
-
- public void testLoadingAndSerializationOfConfiguration() throws Throwable {
- final File cachedCfgFile = new File( workPackageDir, "hibernate.cfg.bin" );
- try {
- System.err.println( "#### Preparing serialized configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
- prepareSerializedConfiguration( mappingBaseDir, FILES, cachedCfgFile );
- System.err.println( "#### Preparing serialized configuration complete
~~~~~~~~~~~~~~~~~~~~~~~~" );
-
- // now make sure we can reload the serialized configuration...
- System.err.println( "#### Reading serialized configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
- readSerializedConfiguration( cachedCfgFile );
- System.err.println( "#### Reading serialized configuration complete
~~~~~~~~~~~~~~~~~~~~~~~~~~" );
- }
- finally {
- System.err.println( "###CLEANING UP###" );
- if ( ! cachedCfgFile.delete() ) {
- System.err.println( "Unable to cleanup file " +
cachedCfgFile.getAbsolutePath() );
- }
- //noinspection ForLoopReplaceableByForEach
- for ( int i = 0; i < FILES.length; i++ ) {
- File file = new File( mappingBaseDir, FILES[i] + ".bin" );
- if ( ! file.delete() ) {
- System.err.println( "Unable to cleanup file " + file.getAbsolutePath() );
- }
- }
- }
- }
-
- public void testSessionFactoryCreationTime() throws Throwable {
- generateTestFiles();
- if ( !workPackageDir.exists() ) {
- System.err.println( workPackageDir.getAbsoluteFile() + " not found" );
- return;
- }
-
- long start = System.currentTimeMillis();
- Configuration configuration = buildConfigurationFromCacheableFiles(
- workPackageDir,
- workPackageDir.list(
- new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return name.endsWith( ".hbm.xml" );
- }
- }
- )
- );
- SessionFactory factory = configuration.buildSessionFactory();
- long initial = System.currentTimeMillis() - start;
- factory.close();
-
- start = System.currentTimeMillis();
- configuration = buildConfigurationFromCacheableFiles(
- workPackageDir,
- workPackageDir.list(
- new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return name.endsWith( ".hbm.xml" );
- }
- }
- )
- );
- factory = configuration.buildSessionFactory();
- long subsequent = System.currentTimeMillis() - start;
-
- // Let's make sure the mappings were read in correctly (in termas of they are
operational).
- Session session = factory.openSession();
- session.beginTransaction();
- session.createQuery( "from Test1" ).list();
- session.getTransaction().commit();
- session.close();
- factory.close();
-
- System.err.println( "Initial SessionFactory load time : " + initial );
- System.err.println( "Subsequent SessionFactory load time : " + subsequent );
- }
-
- private void prepareSerializedConfiguration(
- File mappingFileBase,
- String[] files,
- File cachedCfgFile) throws IOException {
- Configuration cfg = buildConfigurationFromCacheableFiles( mappingFileBase, files );
-
- ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream( cachedCfgFile )
);
- os.writeObject( cfg ); // need to serialize Configuration *before* building sf since it
would require non-mappings and cfg types to be serializable
- os.flush();
- os.close();
-
- timeBuildingSessionFactory( cfg );
- }
-
- private Configuration buildConfigurationFromCacheableFiles(File mappingFileBase,
String[] files) {
- long start = System.currentTimeMillis();
- Configuration cfg = new Configuration();
- cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
- System.err.println(
- "Created configuration: " + ( System.currentTimeMillis() - start ) / 1000.0
+ " sec."
- );
-
- start = System.currentTimeMillis();
- //noinspection ForLoopReplaceableByForEach
- for ( int i = 0; i < files.length; i++ ) {
- cfg.addCacheableFile( new File( mappingFileBase, files[i] ) );
- }
- System.err.println(
- "Added " + ( files.length ) + " resources: " +
- ( System.currentTimeMillis() - start ) / 1000.0 + " sec."
- );
- return cfg;
- }
-
- private void timeBuildingSessionFactory(Configuration configuration) {
- long start = System.currentTimeMillis();
- System.err.println( "Start build of session factory" );
- SessionFactory factory = configuration.buildSessionFactory();
- System.err.println( "Built session factory :" + ( System.currentTimeMillis()
- start ) / 1000.0 + " sec." );
- factory.close();
- }
-
- private void readSerializedConfiguration(File cachedCfgFile) throws
ClassNotFoundException, IOException {
- long start = System.currentTimeMillis();
- ObjectInputStream is = new ObjectInputStream( new FileInputStream( cachedCfgFile ) );
- Configuration cfg = ( Configuration ) is.readObject();
- is.close();
- System.err.println(
- "Loaded serializable configuration :" +
- ( System.currentTimeMillis() - start ) / 1000.0 + " sec."
- );
-
- timeBuildingSessionFactory( cfg );
- }
-
- public void generateTestFiles() throws Throwable {
- String filesToCompile = "";
- for ( int count = 0; count < 100; count++ ) {
- String name = "Test" + count;
- File javaFile = new File( workPackageDir, name + ".java" );
- File hbmFile = new File( workPackageDir, name + ".hbm.xml" );
- filesToCompile += ( javaFile.getAbsolutePath() + " " );
-
- System.out.println( "Generating " + javaFile.getAbsolutePath() );
- PrintWriter javaWriter = null;
- PrintWriter hbmWriter = null;
- try {
- javaWriter = new PrintWriter( new FileWriter( javaFile ) );
- hbmWriter = new PrintWriter( new FileWriter( hbmFile ) );
-
- javaWriter.println( "package " + workPackageName + ";" );
- hbmWriter.println(
- "<?xml version=\"1.0\"?>\r\n" +
- "<!DOCTYPE hibernate-mapping PUBLIC \r\n" +
- " \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\r\n" +
- " \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\r\n"
- );
-
- hbmWriter.println( "<hibernate-mapping package=\"" +
workPackageName + "\">" );
-
- javaWriter.println( "public class " + name + " {" );
- javaWriter.println( " static { System.out.println(\"" + name + "
initialized!\"); }" );
- hbmWriter.println( "<class name=\"" + name +
"\">" );
-
- hbmWriter.println( "<id type=\"long\"><generator
class=\"assigned\"/></id>" );
- for ( int propCount = 0; propCount < 100; propCount++ ) {
- String propName = "Prop" + propCount;
-
- writeJavaProperty( javaWriter, propName );
-
- hbmWriter.println( "<property name=\"" + propName + "\"
type=\"string\"/>" );
-
- }
- hbmWriter.println( "</class>" );
- javaWriter.println( "}" );
- hbmWriter.println( "</hibernate-mapping>" );
- }
- finally {
- if ( javaWriter != null ) {
- javaWriter.flush();
- javaWriter.close();
- }
- if ( hbmWriter != null ) {
- hbmWriter.flush();
- hbmWriter.close();
- }
- }
- }
-
- String javac = "javac -version -d " + compilationBaseDir + " " +
filesToCompile;
- System.err.println( "JAVAC : " + javac );
- Process process = Runtime.getRuntime().exec( javac );
- process.waitFor();
- System.err.println( "********************* JAVAC OUTPUT
**********************" );
- pullStream( process.getInputStream() );
- System.err.println(
"---------------------------------------------------------" );
- pullStream( process.getErrorStream() );
- System.err.println(
"*********************************************************" );
- }
-
- private void pullStream(InputStream stream) throws IOException {
- if ( stream == null || stream.available() <= 0 ) {
- return;
- }
- byte[] buffer = new byte[256];
- while ( true ) {
- int read = stream.read( buffer );
- if ( read == -1 ) {
- break;
- }
- System.err.write( buffer, 0, read );
- }
-// System.err.println( "" );
- }
-
- private void writeJavaProperty(PrintWriter javaWriter, String propName) {
- javaWriter.println( " String " + propName + ";" );
- javaWriter.println( " String get" + propName + "() { return " +
propName + "; }" );
- javaWriter.println( " void set" + propName + "(String newVal) { " +
propName + "=newVal; }" );
- }
-
- private File getTestComplileDirectory() {
- String resourceName = "org/hibernate/test/legacy/ABC.hbm.xml";
- String prefix = getClass().getClassLoader().getResource( resourceName ).getFile();
- prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // ABC.hbm.xml
- prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // legacy/
- prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // test/
- prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // hibernate/
- prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // org/
- return new File( prefix + '/' );
- }
-}
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationSerializationTest.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationSerializationTest.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationSerializationTest.java 2009-11-12
19:46:04 UTC (rev 17964)
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors. All
+ * third-party contributions are distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA\
+ */
+package org.hibernate.test.cfg;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.hibernate.junit.UnitTestCase;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.util.SerializationHelper;
+import org.hibernate.SessionFactory;
+
+/**
+ * Copied over mostly from ConfigurationPerformanceTest
+ *
+ * @author Steve Ebersole
+ * @author Max Andersen
+ */
+public class ConfigurationSerializationTest extends UnitTestCase {
+ public ConfigurationSerializationTest(String string) {
+ super( string );
+ }
+
+ public static Test suite() {
+ return new TestSuite( ConfigurationSerializationTest.class );
+ }
+
+ private static final String[] FILES = new String[] {
+ "legacy/ABC.hbm.xml",
+ "legacy/ABCExtends.hbm.xml",
+ "legacy/Baz.hbm.xml",
+ "legacy/Blobber.hbm.xml",
+ "legacy/Broken.hbm.xml",
+ "legacy/Category.hbm.xml",
+ "legacy/Circular.hbm.xml",
+ "legacy/Commento.hbm.xml",
+ "legacy/ComponentNotNullMaster.hbm.xml",
+ "legacy/Componentizable.hbm.xml",
+ "legacy/Container.hbm.xml",
+ "legacy/Custom.hbm.xml",
+ "legacy/CustomSQL.hbm.xml",
+ "legacy/Eye.hbm.xml",
+ "legacy/Fee.hbm.xml",
+ "legacy/Fo.hbm.xml",
+ "legacy/FooBar.hbm.xml",
+ "legacy/Fum.hbm.xml",
+ "legacy/Fumm.hbm.xml",
+ "legacy/Glarch.hbm.xml",
+ "legacy/Holder.hbm.xml",
+ "legacy/IJ2.hbm.xml",
+ "legacy/Immutable.hbm.xml",
+ "legacy/Location.hbm.xml",
+ "legacy/Many.hbm.xml",
+ "legacy/Map.hbm.xml",
+ "legacy/Marelo.hbm.xml",
+ "legacy/MasterDetail.hbm.xml",
+ "legacy/Middle.hbm.xml",
+ "legacy/Multi.hbm.xml",
+ "legacy/MultiExtends.hbm.xml",
+ "legacy/Nameable.hbm.xml",
+ "legacy/One.hbm.xml",
+ "legacy/ParentChild.hbm.xml",
+ "legacy/Qux.hbm.xml",
+ "legacy/Simple.hbm.xml",
+ "legacy/SingleSeveral.hbm.xml",
+ "legacy/Stuff.hbm.xml",
+ "legacy/UpDown.hbm.xml",
+ "legacy/Vetoer.hbm.xml",
+ "legacy/WZ.hbm.xml",
+ };
+
+ public void testConfiguraionSerializability() {
+ Configuration cfg = new Configuration();
+ for ( String file : FILES ) {
+ cfg.addResource( "org/hibernate/test/" + file );
+ }
+
+ byte[] bytes = SerializationHelper.serialize( cfg );
+ cfg = ( Configuration ) SerializationHelper.deserialize( bytes );
+
+ // try to build SF
+ SessionFactory factory = cfg.buildSessionFactory();
+ factory.close();
+ }
+}
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ListenerTest.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ListenerTest.java 2009-11-12
16:17:12 UTC (rev 17963)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ListenerTest.java 2009-11-12
19:46:04 UTC (rev 17964)
@@ -1,3 +1,26 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
package org.hibernate.test.cfg;
import java.util.Set;
Copied:
core/trunk/testsuite/src/test/perf/org/hibernate/test/perf/ConfigurationPerformanceTest.java
(from rev 17887,
core/trunk/testsuite/src/test/java/org/hibernate/test/cfg/ConfigurationPerformanceTest.java)
===================================================================
---
core/trunk/testsuite/src/test/perf/org/hibernate/test/perf/ConfigurationPerformanceTest.java
(rev 0)
+++
core/trunk/testsuite/src/test/perf/org/hibernate/test/perf/ConfigurationPerformanceTest.java 2009-11-12
19:46:04 UTC (rev 17964)
@@ -0,0 +1,248 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.test.perf;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.classic.Session;
+import org.hibernate.junit.UnitTestCase;
+
+/**
+ * Test of configuration, specifically "cacheable files".
+ *
+ * @author Max Andersen
+ * @author Steve Ebersole
+ */
+public class ConfigurationPerformanceTest extends UnitTestCase {
+
+ private final String workPackageName = "org.hibernate.test.cfg.work";
+ private File compilationBaseDir;
+ private File workPackageDir;
+
+ protected void setUp() throws Exception {
+ compilationBaseDir = getTestComplileDirectory();
+ workPackageDir = new File( compilationBaseDir, workPackageName.replace( '.',
'/' ) );
+ if ( workPackageDir.exists() ) {
+ //noinspection ResultOfMethodCallIgnored
+ workPackageDir.delete();
+ }
+ boolean created = workPackageDir.mkdirs();
+ if ( !created ) {
+ System.err.println( "Unable to create workPackageDir during setup" );
+ }
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public ConfigurationPerformanceTest(String string) {
+ super( string );
+ }
+
+ public static Test suite() {
+ return new TestSuite( ConfigurationPerformanceTest.class );
+ }
+
+ public static void main(String[] args) throws Exception {
+ TestRunner.run( suite() );
+ }
+
+ public void testSessionFactoryCreationTime() throws Throwable {
+ generateTestFiles();
+ if ( !workPackageDir.exists() ) {
+ System.err.println( workPackageDir.getAbsoluteFile() + " not found" );
+ return;
+ }
+
+ long start = System.currentTimeMillis();
+ Configuration configuration = buildConfigurationFromCacheableFiles(
+ workPackageDir,
+ workPackageDir.list(
+ new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ return name.endsWith( ".hbm.xml" );
+ }
+ }
+ )
+ );
+ SessionFactory factory = configuration.buildSessionFactory();
+ long initial = System.currentTimeMillis() - start;
+ factory.close();
+
+ start = System.currentTimeMillis();
+ configuration = buildConfigurationFromCacheableFiles(
+ workPackageDir,
+ workPackageDir.list(
+ new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ return name.endsWith( ".hbm.xml" );
+ }
+ }
+ )
+ );
+ factory = configuration.buildSessionFactory();
+ long subsequent = System.currentTimeMillis() - start;
+
+ // Let's make sure the mappings were read in correctly (in termas of they are
operational).
+ Session session = factory.openSession();
+ session.beginTransaction();
+ session.createQuery( "from Test1" ).list();
+ session.getTransaction().commit();
+ session.close();
+ factory.close();
+
+ System.err.println( "Initial SessionFactory load time : " + initial );
+ System.err.println( "Subsequent SessionFactory load time : " + subsequent );
+ }
+
+ private Configuration buildConfigurationFromCacheableFiles(File mappingFileBase,
String[] files) {
+ long start = System.currentTimeMillis();
+ Configuration cfg = new Configuration();
+ cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
+ System.err.println(
+ "Created configuration: " + ( System.currentTimeMillis() - start ) / 1000.0
+ " sec."
+ );
+
+ start = System.currentTimeMillis();
+ //noinspection ForLoopReplaceableByForEach
+ for ( int i = 0; i < files.length; i++ ) {
+ cfg.addCacheableFile( new File( mappingFileBase, files[i] ) );
+ }
+ System.err.println(
+ "Added " + ( files.length ) + " resources: " +
+ ( System.currentTimeMillis() - start ) / 1000.0 + " sec."
+ );
+ return cfg;
+ }
+
+ public void generateTestFiles() throws Throwable {
+ String filesToCompile = "";
+ for ( int count = 0; count < 100; count++ ) {
+ String name = "Test" + count;
+ File javaFile = new File( workPackageDir, name + ".java" );
+ File hbmFile = new File( workPackageDir, name + ".hbm.xml" );
+ filesToCompile += ( javaFile.getAbsolutePath() + " " );
+
+ System.out.println( "Generating " + javaFile.getAbsolutePath() );
+ PrintWriter javaWriter = null;
+ PrintWriter hbmWriter = null;
+ try {
+ javaWriter = new PrintWriter( new FileWriter( javaFile ) );
+ hbmWriter = new PrintWriter( new FileWriter( hbmFile ) );
+
+ javaWriter.println( "package " + workPackageName + ";" );
+ hbmWriter.println(
+ "<?xml version=\"1.0\"?>\r\n" +
+ "<!DOCTYPE hibernate-mapping PUBLIC \r\n" +
+ " \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\r\n" +
+ " \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\r\n"
+ );
+
+ hbmWriter.println( "<hibernate-mapping package=\"" +
workPackageName + "\">" );
+
+ javaWriter.println( "public class " + name + " {" );
+ javaWriter.println( " static { System.out.println(\"" + name + "
initialized!\"); }" );
+ hbmWriter.println( "<class name=\"" + name +
"\">" );
+
+ hbmWriter.println( "<id type=\"long\"><generator
class=\"assigned\"/></id>" );
+ for ( int propCount = 0; propCount < 100; propCount++ ) {
+ String propName = "Prop" + propCount;
+
+ writeJavaProperty( javaWriter, propName );
+
+ hbmWriter.println( "<property name=\"" + propName + "\"
type=\"string\"/>" );
+
+ }
+ hbmWriter.println( "</class>" );
+ javaWriter.println( "}" );
+ hbmWriter.println( "</hibernate-mapping>" );
+ }
+ finally {
+ if ( javaWriter != null ) {
+ javaWriter.flush();
+ javaWriter.close();
+ }
+ if ( hbmWriter != null ) {
+ hbmWriter.flush();
+ hbmWriter.close();
+ }
+ }
+ }
+
+ String javac = "javac -version -d " + compilationBaseDir + " " +
filesToCompile;
+ System.err.println( "JAVAC : " + javac );
+ Process process = Runtime.getRuntime().exec( javac );
+ process.waitFor();
+ System.err.println( "********************* JAVAC OUTPUT
**********************" );
+ pullStream( process.getInputStream() );
+ System.err.println(
"---------------------------------------------------------" );
+ pullStream( process.getErrorStream() );
+ System.err.println(
"*********************************************************" );
+ }
+
+ private void pullStream(InputStream stream) throws IOException {
+ if ( stream == null || stream.available() <= 0 ) {
+ return;
+ }
+ byte[] buffer = new byte[256];
+ while ( true ) {
+ int read = stream.read( buffer );
+ if ( read == -1 ) {
+ break;
+ }
+ System.err.write( buffer, 0, read );
+ }
+// System.err.println( "" );
+ }
+
+ private void writeJavaProperty(PrintWriter javaWriter, String propName) {
+ javaWriter.println( " String " + propName + ";" );
+ javaWriter.println( " String get" + propName + "() { return " +
propName + "; }" );
+ javaWriter.println( " void set" + propName + "(String newVal) { " +
propName + "=newVal; }" );
+ }
+
+ private File getTestComplileDirectory() {
+ String resourceName = "org/hibernate/test/legacy/ABC.hbm.xml";
+ String prefix = getClass().getClassLoader().getResource( resourceName ).getFile();
+ prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // ABC.hbm.xml
+ prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // legacy/
+ prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // test/
+ prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // hibernate/
+ prefix = prefix.substring( 0, prefix.lastIndexOf( '/' ) ); // org/
+ return new File( prefix + '/' );
+ }
+}