Author: max...@jb... Date: 2006-07-25 10:17:16 -0400 (Tue, 25 Jul 2006) New Revision: 10148 Added: trunk/Hibernate3/src/org/hibernate/InvalidMappingException.java trunk/Hibernate3/src/org/hibernate/MappingNotFoundException.java trunk/Hibernate3/test/org/hibernate/test/mappingexception/ trunk/Hibernate3/test/org/hibernate/test/mappingexception/InvalidMapping.hbm.xml trunk/Hibernate3/test/org/hibernate/test/mappingexception/InvalidMapping.java trunk/Hibernate3/test/org/hibernate/test/mappingexception/MappingExceptionTest.java trunk/Hibernate3/test/org/hibernate/test/mappingexception/User.hbm.xml trunk/Hibernate3/test/org/hibernate/test/mappingexception/User.java Modified: trunk/Hibernate3/src/org/hibernate/DuplicateMappingException.java trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java Log: HHH-1941 Be more specific about x not found and invalid mapping exceptions to allow tools to tell about it Modified: trunk/Hibernate3/src/org/hibernate/DuplicateMappingException.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/DuplicateMappingException.java 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/src/org/hibernate/DuplicateMappingException.java 2006-07-25 14:17:16 UTC (rev 10148) @@ -1,5 +1,12 @@ package org.hibernate; +/** + * Raised whenever a duplicate for a certain type occurs. + * Duplicate class, table, property name etc. + * + * @author Max Rydahl Andersen + * + */ public class DuplicateMappingException extends MappingException { private final String name; Added: trunk/Hibernate3/src/org/hibernate/InvalidMappingException.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/InvalidMappingException.java 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/src/org/hibernate/InvalidMappingException.java 2006-07-25 14:17:16 UTC (rev 10148) @@ -0,0 +1,42 @@ +package org.hibernate; + +/** + * Thrown when a mapping is found to be invalid. + * Similar to MappingException, but this contains more info about the path and type of mapping (e.g. file, resource or url) + * + * @author Max Rydahl Andersen + * + */ +public class InvalidMappingException extends MappingException { + + private final String path; + private final String type; + + public InvalidMappingException(String customMessage, String type, String path, Throwable cause) { + super(customMessage, cause); + this.type=type; + this.path=path; + } + + public InvalidMappingException(String customMessage, String type, String path) { + super(customMessage); + this.type=type; + this.path=path; + } + + public InvalidMappingException(String type, String path) { + this("Could not parse mapping document from " + type + (path==null?"":" " + path), type, path); + } + + public InvalidMappingException(String type, String path, Throwable cause) { + this("Could not parse mapping document from " + type + (path==null?"":" " + path), type, path, cause); + } + + public String getType() { + return type; + } + + public String getPath() { + return path; + } +} Added: trunk/Hibernate3/src/org/hibernate/MappingNotFoundException.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/MappingNotFoundException.java 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/src/org/hibernate/MappingNotFoundException.java 2006-07-25 14:17:16 UTC (rev 10148) @@ -0,0 +1,41 @@ +package org.hibernate; + +/** + * Thrown when a resource for a mapping could not be found. + * + * @author Max Rydahl Andersen + * + */ +public class MappingNotFoundException extends MappingException { + + private final String path; + private final String type; + + public MappingNotFoundException(String customMessage, String type, String path, Throwable cause) { + super(customMessage, cause); + this.type=type; + this.path=path; + } + + public MappingNotFoundException(String customMessage, String type, String path) { + super(customMessage); + this.type=type; + this.path=path; + } + + public MappingNotFoundException(String type, String path) { + this(type + ": " + path + " not found", type, path); + } + + public MappingNotFoundException(String type, String path, Throwable cause) { + this(type + ": " + path + " not found", type, path, cause); + } + + public String getType() { + return type; + } + + public String getPath() { + return path; + } +} Modified: trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-07-25 14:17:16 UTC (rev 10148) @@ -34,7 +34,9 @@ import org.hibernate.EmptyInterceptor; import org.hibernate.HibernateException; import org.hibernate.Interceptor; +import org.hibernate.InvalidMappingException; import org.hibernate.MappingException; +import org.hibernate.MappingNotFoundException; import org.hibernate.SessionFactory; import org.hibernate.proxy.EntityNotFoundDelegate; import org.hibernate.dialect.Dialect; @@ -271,20 +273,22 @@ public Configuration addFile(String xmlFile) throws MappingException { log.info( "Reading mappings from file: " + xmlFile ); try { - List errors = new ArrayList(); - org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile, errors, entityResolver ) - .read( new File( xmlFile ) ); - if ( errors.size() != 0 ) { - throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) ); + File file = new File( xmlFile ); + if(file.exists()) { + List errors = new ArrayList(); + org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile, errors, entityResolver ) + .read( file ); + if ( errors.size() != 0 ) { + throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) ); + } + add( doc ); + return this; + } else { + throw new MappingNotFoundException("file", file.toString()); } - add( doc ); - return this; } catch (Exception e) { - throw new MappingException( - "Could not read mapping document from file: " + xmlFile, - e - ); + throw new InvalidMappingException("file", xmlFile, e); } } @@ -299,10 +303,7 @@ addInputStream( new FileInputStream( xmlFile ) ); } catch (Exception e) { - throw new MappingException( - "Could not read mapping document from file: " + xmlFile.getPath(), - e - ); + throw new InvalidMappingException( "file", xmlFile.getPath(),e ); } return this; } @@ -332,8 +333,8 @@ } } - // If deserialization failed - if ( doc == null ) { + // If deserialization failed or cached file does not exist + if ( doc == null && xmlFile.exists()) { log.info( "Reading mappings from file: " + xmlFile ); doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver ) .read( xmlFile ); @@ -344,6 +345,8 @@ catch (SerializationException e) { log.warn( "Could not write cached file: " + lazyfile, e ); } + } else { + throw new MappingNotFoundException("file", xmlFile.toString()); } if ( errors.size() != 0 ) { @@ -353,10 +356,7 @@ return this; } catch (Exception e) { - throw new MappingException( - "Could not read mapping document from file: " + xmlFile, - e - ); + throw new InvalidMappingException("file", xmlFile.toString(), e); } } @@ -402,7 +402,7 @@ addInputStream( url.openStream() ); } catch (Exception e) { - throw new MappingException( "Could not read mapping document from URL: " + url, e ); + throw new InvalidMappingException( "URL", ""+url, e ); } return this; } @@ -466,7 +466,7 @@ return this; } catch (DocumentException e) { - throw new MappingException( "Could not parse mapping document in input stream", e ); + throw new InvalidMappingException( "input stream", null, e ); } finally { try { @@ -488,13 +488,13 @@ log.info( "Reading mappings from resource: " + path ); InputStream rsrc = classLoader.getResourceAsStream( path ); if ( rsrc == null ) { - throw new MappingException( "Resource: " + path + " not found" ); + throw new MappingNotFoundException( "resource", path ); } try { return addInputStream( rsrc ); } catch (MappingException me) { - throw new MappingException( "Could not read mappings from resource: " + path, me ); + throw new InvalidMappingException( "resource", path, me ); } } @@ -514,13 +514,13 @@ rsrc = Environment.class.getClassLoader().getResourceAsStream( path ); } if ( rsrc == null ) { - throw new MappingException( "Resource: " + path + " not found" ); + throw new MappingNotFoundException( "resource", path ); } try { return addInputStream( rsrc ); } catch (MappingException me) { - throw new MappingException( "Could not read mappings from resource: " + path, me ); + throw new InvalidMappingException( "resource", path, me ); } } @@ -535,16 +535,14 @@ log.info( "Reading mappings from resource: " + fileName ); InputStream rsrc = persistentClass.getClassLoader().getResourceAsStream( fileName ); if ( rsrc == null ) { - throw new MappingException( "Resource: " + fileName + " not found" ); + throw new MappingNotFoundException( "resource", fileName ); } try { return addInputStream( rsrc ); } catch (MappingException me) { - throw new MappingException( - "Could not read mappings from resource: " + fileName, - me - ); + throw new InvalidMappingException( + "resource", fileName, me ); } } @@ -564,8 +562,8 @@ jarFile = new JarFile( jar ); } catch (IOException ioe) { - throw new MappingException( - "Could not read mapping documents from jar: " + jar.getName(), + throw new InvalidMappingException( + "Could not read mapping documents from jar: " + jar.getName(), "jar", jar.getName(), ioe ); } @@ -581,8 +579,10 @@ addInputStream( jarFile.getInputStream( ze ) ); } catch (Exception e) { - throw new MappingException( - "Could not read mapping documents from jar: " + jar.getName(), + throw new InvalidMappingException( + "Could not read mapping documents from jar: " + jar.getName(), + "jar", + jar.getName(), e ); } Added: trunk/Hibernate3/test/org/hibernate/test/mappingexception/InvalidMapping.hbm.xml =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/mappingexception/InvalidMapping.hbm.xml 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/test/org/hibernate/test/mappingexception/InvalidMapping.hbm.xml 2006-07-25 14:17:16 UTC (rev 10148) @@ -0,0 +1,4 @@ +<?xml version="1.0"?> +<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > + +This File Intentionally Left Blank \ No newline at end of file Added: trunk/Hibernate3/test/org/hibernate/test/mappingexception/InvalidMapping.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/mappingexception/InvalidMapping.java 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/test/org/hibernate/test/mappingexception/InvalidMapping.java 2006-07-25 14:17:16 UTC (rev 10148) @@ -0,0 +1,5 @@ +package org.hibernate.test.mappingexception; + +public class InvalidMapping { +// This Class Intentionally Left Blank +} Added: trunk/Hibernate3/test/org/hibernate/test/mappingexception/MappingExceptionTest.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/mappingexception/MappingExceptionTest.java 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/test/org/hibernate/test/mappingexception/MappingExceptionTest.java 2006-07-25 14:17:16 UTC (rev 10148) @@ -0,0 +1,236 @@ +// $Id: SQLExceptionConversionTest.java 6847 2005-05-21 15:46:41Z oneovthafew $ +package org.hibernate.test.mappingexception; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.MalformedURLException; +import java.net.URL; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.hibernate.DuplicateMappingException; +import org.hibernate.Hibernate; +import org.hibernate.InvalidMappingException; +import org.hibernate.MappingException; +import org.hibernate.MappingNotFoundException; +import org.hibernate.test.TestCase; +import org.hibernate.util.ConfigHelper; + +/** + * Test for various mapping exceptions thrown when mappings are not found or invalid. + * + * @author Max Rydahl Andersen + */ +public class MappingExceptionTest extends TestCase { + + public MappingExceptionTest(String name) { + super(name); + } + + protected String[] getMappings() { + return new String[] {"mappingexception/User.hbm.xml"}; + } + + public void testNotFound() throws MappingException, MalformedURLException { + + try { + getCfg().addCacheableFile( "completelybogus.hbm.xml" ); + fail(); + } catch(InvalidMappingException inv) { // TODO: should be MappingNotFound + assertEquals(inv.getType(), "file"); + assertEquals(inv.getPath(), "completelybogus.hbm.xml"); + assertClassAssignability( inv.getCause().getClass(), MappingNotFoundException.class); + } + + try { + getCfg().addCacheableFile( new File("completelybogus.hbm.xml") ); + fail(); + } catch(InvalidMappingException inv) { // TODO: should be MappingNotFound + assertEquals(inv.getType(), "file"); + assertEquals(inv.getPath(), "completelybogus.hbm.xml"); + assertClassAssignability( inv.getCause().getClass(), MappingNotFoundException.class); + } + + try { + getCfg().addClass( Hibernate.class ); // TODO: String.class result in npe, because no classloader exists for it + fail(); + } catch(MappingNotFoundException inv) { + assertEquals(inv.getType(), "resource"); + assertEquals(inv.getPath(), "org/hibernate/Hibernate.hbm.xml"); + } + + try { + getCfg().addFile( "completelybogus.hbm.xml" ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "file"); + assertEquals(inv.getPath(), "completelybogus.hbm.xml"); + assertClassAssignability( inv.getCause().getClass(), MappingNotFoundException.class); + } + + try { + getCfg().addFile( new File("completelybogus.hbm.xml")); + fail(); + } catch(InvalidMappingException inv) { // TODO: could be a MappingNotFoundException + assertEquals(inv.getType(), "file"); + assertEquals(inv.getPath(), "completelybogus.hbm.xml"); + } + + try { + getCfg().addInputStream( new ByteArrayInputStream(new byte[0])); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "input stream"); + assertEquals(inv.getPath(), null); + } + + try { + getCfg().addResource( "nothere" ); + fail(); + } catch(MappingNotFoundException inv) { + assertEquals(inv.getType(), "resource"); + assertEquals(inv.getPath(), "nothere"); + } + + try { + getCfg().addResource( "nothere", getClass().getClassLoader() ); + fail(); + } catch(MappingNotFoundException inv) { + assertEquals(inv.getType(), "resource"); + assertEquals(inv.getPath(), "nothere"); + } + + try { + getCfg().addURL( new URL("file://nothere") ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "URL"); + assertEquals(inv.getPath(), "file://nothere"); + } + } + + public void testDuplicateMapping() { + try { + getCfg().addResource( getBaseForMappings() + "mappingexception/User.hbm.xml" ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "resource"); + assertEquals(inv.getPath(), getBaseForMappings() + "mappingexception/User.hbm.xml"); + assertClassAssignability( inv.getCause().getClass(), DuplicateMappingException.class); + } + } + + void copy(InputStream in, File dst) throws IOException { + OutputStream out = new FileOutputStream(dst); + + // Transfer bytes from in to out + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + in.close(); + out.close(); + } + + public void testInvalidMapping() throws MappingException, IOException { + String resourceName = getBaseForMappings() + "mappingexception/InvalidMapping.hbm.xml"; + + File file = File.createTempFile( "TempInvalidMapping", ".hbm.xml" ); + file.deleteOnExit(); + copy( ConfigHelper.getConfigStream( resourceName ), file ); + + try { + getCfg().addCacheableFile( file.getAbsolutePath() ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "file"); + assertNotNull(inv.getPath()); + assertTrue(inv.getPath().endsWith(".hbm.xml")); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + + try { + getCfg().addCacheableFile( file ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "file"); + assertNotNull(inv.getPath()); + assertTrue(inv.getPath().endsWith(".hbm.xml")); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + + try { + getCfg().addClass( InvalidMapping.class ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "resource"); + assertEquals(inv.getPath(), "org/hibernate/test/mappingexception/InvalidMapping.hbm.xml"); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + + try { + getCfg().addFile( file.getAbsolutePath() ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "file"); + assertEquals(inv.getPath(), file.getPath()); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + + try { + getCfg().addFile( file ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "file"); + assertEquals(inv.getPath(), file.getPath()); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + + + try { + getCfg().addInputStream( ConfigHelper.getResourceAsStream( resourceName ) ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "input stream"); + assertEquals(inv.getPath(), null); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + + try { + getCfg().addResource( resourceName ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "resource"); + assertEquals(inv.getPath(), resourceName); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + + try { + getCfg().addResource( resourceName, getClass().getClassLoader() ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "resource"); + assertEquals(inv.getPath(), resourceName); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + + try { + getCfg().addURL( ConfigHelper.findAsResource( resourceName ) ); + fail(); + } catch(InvalidMappingException inv) { + assertEquals(inv.getType(), "URL"); + assertTrue(inv.getPath().endsWith("InvalidMapping.hbm.xml")); + assertTrue(!(inv.getCause() instanceof MappingNotFoundException )); + } + } + + public static Test suite() { + return new TestSuite(MappingExceptionTest.class); + } +} Added: trunk/Hibernate3/test/org/hibernate/test/mappingexception/User.hbm.xml =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/mappingexception/User.hbm.xml 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/test/org/hibernate/test/mappingexception/User.hbm.xml 2006-07-25 14:17:16 UTC (rev 10148) @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > + +<hibernate-mapping package="org.hibernate.test.exception" > + <class name="User" table="T_USER" > + <id name="id" unsaved-value="null" column="user_id" > + <generator class="native"/> + </id> + <property name="username" type="string" column="user_name" /> + </class> +</hibernate-mapping> \ No newline at end of file Added: trunk/Hibernate3/test/org/hibernate/test/mappingexception/User.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/mappingexception/User.java 2006-07-25 05:27:42 UTC (rev 10147) +++ trunk/Hibernate3/test/org/hibernate/test/mappingexception/User.java 2006-07-25 14:17:16 UTC (rev 10148) @@ -0,0 +1,31 @@ +// $Id: User.java 4746 2004-11-11 20:57:28Z steveebersole $ +package org.hibernate.test.mappingexception; + +import java.util.Set; +import java.util.HashSet; + +/** + * + * + * @author Max Rydahl Andersen + */ +public class User { + private Long id; + private String username; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } +} |