Search in sources :

Example 1 with InvalidMappingException

use of org.hibernate.InvalidMappingException in project hibernate-orm by hibernate.

the class OrmVersionTest method testInvalidOrm1.

@Test
public void testInvalidOrm1() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("invalid-orm1-test", "1.0").addMappingFileName("org/hibernate/jpa/test/jee/invalid-orm-1.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = null;
    try {
        emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
        Assert.fail("expecting 'invalid content' error");
    } catch (InvalidMappingException | AnnotationException expected) {
    // expected condition
    } catch (PersistenceException expected) {
    // expected condition
    } finally {
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : InvalidMappingException(org.hibernate.InvalidMappingException) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) AnnotationException(org.hibernate.AnnotationException) Test(org.junit.Test)

Example 2 with InvalidMappingException

use of org.hibernate.InvalidMappingException in project hibernate-orm by hibernate.

the class MappingExceptionTest method testNotFound.

@Test
public void testNotFound() throws MappingException, MalformedURLException {
    Configuration cfg = new Configuration();
    try {
        cfg.addCacheableFile("completelybogus.hbm.xml");
        fail();
    } catch (MappingNotFoundException e) {
        assertEquals(e.getType(), "file");
        assertEquals(e.getPath(), "completelybogus.hbm.xml");
    } catch (org.hibernate.boot.MappingNotFoundException e) {
        assertEquals(e.getOrigin().getType(), SourceType.FILE);
        assertEquals(e.getOrigin().getName(), "completelybogus.hbm.xml");
    }
    try {
        cfg.addCacheableFile(new File("completelybogus.hbm.xml"));
        fail();
    } catch (MappingNotFoundException e) {
        assertEquals(e.getType(), "file");
        assertEquals(e.getPath(), "completelybogus.hbm.xml");
    } catch (org.hibernate.boot.MappingNotFoundException e) {
        assertEquals(e.getOrigin().getType(), SourceType.FILE);
        assertEquals(e.getOrigin().getName(), "completelybogus.hbm.xml");
    }
    try {
        // TODO: String.class result in npe, because no classloader exists for it
        cfg.addClass(Hibernate.class);
        fail();
    } catch (MappingNotFoundException inv) {
        assertEquals(inv.getType(), "resource");
        assertEquals(inv.getPath(), "org/hibernate/Hibernate.hbm.xml");
    } catch (org.hibernate.boot.MappingNotFoundException e) {
        assertEquals(e.getOrigin().getType(), SourceType.RESOURCE);
        assertEquals(e.getOrigin().getName(), "org/hibernate/Hibernate.hbm.xml");
    }
    try {
        cfg.addFile("completelybogus.hbm.xml");
        fail();
    } catch (MappingNotFoundException e) {
        assertEquals(e.getType(), "file");
        assertEquals(e.getPath(), "completelybogus.hbm.xml");
    } catch (org.hibernate.boot.MappingNotFoundException e) {
        assertEquals(e.getOrigin().getType(), SourceType.FILE);
        assertEquals(e.getOrigin().getName(), "completelybogus.hbm.xml");
    }
    try {
        cfg.addFile(new File("completelybogus.hbm.xml"));
        fail();
    } catch (MappingNotFoundException inv) {
        assertEquals(inv.getType(), "file");
        assertEquals(inv.getPath(), "completelybogus.hbm.xml");
    } catch (org.hibernate.boot.MappingNotFoundException e) {
        assertEquals(e.getOrigin().getType(), SourceType.FILE);
        assertEquals(e.getOrigin().getName(), "completelybogus.hbm.xml");
    }
    try {
        cfg.addInputStream(new ByteArrayInputStream(new byte[0]));
        fail();
    } catch (org.hibernate.boot.InvalidMappingException e) {
        assertEquals(SourceType.INPUT_STREAM, e.getOrigin().getType());
        assertEquals(null, e.getOrigin().getName());
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "input stream");
        assertEquals(inv.getPath(), null);
    }
    try {
        cfg.addResource("nothere");
        fail();
    } catch (MappingNotFoundException inv) {
        assertEquals(inv.getType(), "resource");
        assertEquals(inv.getPath(), "nothere");
    } catch (org.hibernate.boot.MappingNotFoundException e) {
        assertEquals(e.getOrigin().getType(), SourceType.RESOURCE);
        assertEquals(e.getOrigin().getName(), "nothere");
    }
    try {
        cfg.addResource("nothere", getClass().getClassLoader());
        fail();
    } catch (MappingNotFoundException inv) {
        assertEquals(inv.getType(), "resource");
        assertEquals(inv.getPath(), "nothere");
    } catch (org.hibernate.boot.MappingNotFoundException e) {
        assertEquals(e.getOrigin().getType(), SourceType.RESOURCE);
        assertEquals(e.getOrigin().getName(), "nothere");
    }
    try {
        cfg.addURL(new URL("file://nothere"));
        fail();
    } catch (org.hibernate.boot.MappingNotFoundException e) {
        assertEquals(e.getOrigin().getType(), SourceType.URL);
        assertEquals(e.getOrigin().getName(), "file://nothere");
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "URL");
        assertEquals(inv.getPath(), "file://nothere");
    } catch (org.hibernate.boot.MappingException me) {
        assertEquals(me.getOrigin().getType(), SourceType.URL);
        assertEquals(me.getOrigin().getName(), "file://nothere");
    }
}
Also used : MappingNotFoundException(org.hibernate.MappingNotFoundException) Configuration(org.hibernate.cfg.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidMappingException(org.hibernate.InvalidMappingException) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 3 with InvalidMappingException

use of org.hibernate.InvalidMappingException in project hibernate-orm by hibernate.

the class MappingExceptionTest method testInvalidMapping.

@Test
public void testInvalidMapping() throws MappingException, IOException {
    String resourceName = "org/hibernate/test/mappingexception/InvalidMapping.hbm.xml";
    File file = File.createTempFile("TempInvalidMapping", ".hbm.xml");
    file.deleteOnExit();
    copy(ConfigHelper.getConfigStream(resourceName), file);
    Configuration cfg = new Configuration();
    try {
        cfg.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 {
        cfg.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 {
        cfg.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 {
        cfg.addFile(file.getAbsolutePath());
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "file");
        assertEquals(inv.getPath(), file.getPath());
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addFile(file);
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "file");
        assertEquals(inv.getPath(), file.getPath());
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addInputStream(ConfigHelper.getResourceAsStream(resourceName));
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "input stream");
        assertEquals(inv.getPath(), null);
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addResource(resourceName);
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "resource");
        assertEquals(inv.getPath(), resourceName);
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addResource(resourceName, getClass().getClassLoader());
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "resource");
        assertEquals(inv.getPath(), resourceName);
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addURL(ConfigHelper.findAsResource(resourceName));
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "URL");
        assertTrue(inv.getPath().endsWith("InvalidMapping.hbm.xml"));
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
}
Also used : MappingNotFoundException(org.hibernate.MappingNotFoundException) Configuration(org.hibernate.cfg.Configuration) InvalidMappingException(org.hibernate.InvalidMappingException) File(java.io.File) Test(org.junit.Test)

Example 4 with InvalidMappingException

use of org.hibernate.InvalidMappingException in project hibernate-orm by hibernate.

the class JpaXsdVersionsTest method testInvalidOrm1.

@Test
public void testInvalidOrm1() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("invalid-orm1-test", "1.0").addMappingFileName("org/hibernate/test/jpa/xml/versions/invalid-orm-1_0.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = null;
    try {
        emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
        Assert.fail("expecting 'invalid content' error");
    } catch (InvalidMappingException | AnnotationException expected) {
    // expected condition
    } catch (PersistenceException expected) {
    // expected condition
    } finally {
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : InvalidMappingException(org.hibernate.InvalidMappingException) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) AnnotationException(org.hibernate.AnnotationException) Test(org.junit.Test)

Aggregations

InvalidMappingException (org.hibernate.InvalidMappingException)4 Test (org.junit.Test)4 File (java.io.File)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 PersistenceException (javax.persistence.PersistenceException)2 AnnotationException (org.hibernate.AnnotationException)2 MappingNotFoundException (org.hibernate.MappingNotFoundException)2 Configuration (org.hibernate.cfg.Configuration)2 HibernatePersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URL (java.net.URL)1