|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.boot.binding.cacheable; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileNotFoundException; |
| 11 | +import java.net.URL; |
| 12 | + |
| 13 | +import org.hibernate.boot.MappingException; |
| 14 | +import org.hibernate.boot.Metadata; |
| 15 | +import org.hibernate.boot.MetadataSources; |
| 16 | +import org.hibernate.boot.jaxb.internal.CacheableFileXmlSource; |
| 17 | +import org.hibernate.boot.jaxb.internal.MappingBinder; |
| 18 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 19 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 20 | +import org.hibernate.boot.spi.XmlMappingBinderAccess; |
| 21 | + |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import org.jboss.logging.Logger; |
| 27 | + |
| 28 | +import static org.junit.Assert.fail; |
| 29 | + |
| 30 | +/** |
| 31 | + * Originally developed to help diagnose HHH-10131 - the original tests |
| 32 | + * check 4 conditions:<ol> |
| 33 | + * <li>strict usage where the cached file does exist</li> |
| 34 | + * <li>strict usage where the cached file does not exist</li> |
| 35 | + * <li>non-strict usage where the cached file does exist</li> |
| 36 | + * <li>non-strict usage where the cached file does not exist</li> |
| 37 | + * </ol> |
| 38 | + * |
| 39 | + * @author Steve Ebersole |
| 40 | + */ |
| 41 | +public class CacheableHbmXmlTest { |
| 42 | + private static final Logger log = Logger.getLogger( CacheableHbmXmlTest.class ); |
| 43 | + |
| 44 | + private static final String HBM_RESOURCE_NAME = "org/hibernate/test/boot/binding/cacheable/SimpleEntity.hbm.xml"; |
| 45 | + |
| 46 | + private StandardServiceRegistry ssr; |
| 47 | + private MappingBinder binder; |
| 48 | + |
| 49 | + private File hbmXmlFile; |
| 50 | + private File hbmXmlBinFile; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void before() throws Exception { |
| 54 | + ssr = new StandardServiceRegistryBuilder() |
| 55 | + .build(); |
| 56 | + binder = new XmlMappingBinderAccess( ssr ).getMappingBinder(); |
| 57 | + |
| 58 | + final URL hbmXmlUrl = getClass().getClassLoader().getResource( HBM_RESOURCE_NAME ); |
| 59 | + if ( hbmXmlUrl == null ) { |
| 60 | + throw couldNotFindHbmXmlFile(); |
| 61 | + } |
| 62 | + hbmXmlFile = new File( hbmXmlUrl.getFile() ); |
| 63 | + if ( ! hbmXmlFile.exists() ) { |
| 64 | + throw couldNotFindHbmXmlFile(); |
| 65 | + } |
| 66 | + hbmXmlBinFile = CacheableFileXmlSource.determineCachedFile( hbmXmlFile ); |
| 67 | + } |
| 68 | + |
| 69 | + private Exception couldNotFindHbmXmlFile() { |
| 70 | + throw new IllegalStateException( "Could not locate hbm.xml file by resource lookup" ); |
| 71 | + } |
| 72 | + |
| 73 | + @After |
| 74 | + public void after() { |
| 75 | + if ( ssr != null ) { |
| 76 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testStrictCaseWhereFileDoesPreviouslyExist() throws FileNotFoundException { |
| 82 | + deleteBinFile(); |
| 83 | + createBinFile(); |
| 84 | + try { |
| 85 | + new MetadataSources( ssr ).addCacheableFileStrictly( hbmXmlFile ).buildMetadata(); |
| 86 | + } |
| 87 | + catch (MappingException e) { |
| 88 | + fail( "addCacheableFileStrictly led to MappingException when bin file existed" ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testStrictCaseWhereFileDoesNotPreviouslyExist() throws FileNotFoundException { |
| 94 | + deleteBinFile(); |
| 95 | + try { |
| 96 | + new MetadataSources( ssr ).addCacheableFileStrictly( hbmXmlFile ).buildMetadata(); |
| 97 | + fail( "addCacheableFileStrictly should be led to MappingException when bin file does not exist" ); |
| 98 | + } |
| 99 | + catch (MappingException ignore) { |
| 100 | + // this is the expected result |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testNonStrictCaseWhereFileDoesPreviouslyExist() { |
| 106 | + deleteBinFile(); |
| 107 | + createBinFile(); |
| 108 | + new MetadataSources( ssr ).addCacheableFile( hbmXmlFile ).buildMetadata(); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testNonStrictCaseWhereFileDoesNotPreviouslyExist() { |
| 113 | + deleteBinFile(); |
| 114 | + new MetadataSources( ssr ).addCacheableFile( hbmXmlFile ).buildMetadata(); |
| 115 | + } |
| 116 | + |
| 117 | + private void deleteBinFile() { |
| 118 | + // if it exists |
| 119 | + if ( hbmXmlBinFile.exists() ) { |
| 120 | + final boolean success = hbmXmlBinFile.delete(); |
| 121 | + if ( !success ) { |
| 122 | + log.warn( "Unable to delete existing cached hbm.xml.bin file", new Exception() ); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private void createBinFile() { |
| 128 | + if ( hbmXmlBinFile.exists() ) { |
| 129 | + log.warn( "Cached hbm.xml.bin file already existed on request to create", new Exception() ); |
| 130 | + } |
| 131 | + else { |
| 132 | + CacheableFileXmlSource.createSerFile( hbmXmlFile, binder ); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments