|
| 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.cfgXml; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import org.hibernate.boot.cfgxml.internal.ConfigLoader; |
| 15 | +import org.hibernate.boot.registry.BootstrapServiceRegistry; |
| 16 | +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; |
| 17 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 18 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 19 | +import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl; |
| 20 | +import org.hibernate.engine.config.spi.ConfigurationService; |
| 21 | + |
| 22 | +import org.hibernate.testing.TestForIssue; |
| 23 | +import org.hibernate.testing.junit4.BaseUnitTestCase; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import static org.junit.Assert.assertNotNull; |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | + |
| 30 | +/** |
| 31 | + * Test that makes sure the input stream inside {@link ConfigLoader#loadConfigXmlResource(java.lang.String)} |
| 32 | + * gets closed. |
| 33 | + * |
| 34 | + * @author Steve Ebersole |
| 35 | + */ |
| 36 | +@TestForIssue( jiraKey = "HHH-10120" ) |
| 37 | +public class CfgXmlResourceNameClosingTest extends BaseUnitTestCase { |
| 38 | + private static class InputStreamWrapper extends InputStream { |
| 39 | + private final InputStream wrapped; |
| 40 | + private boolean wasClosed = false; |
| 41 | + |
| 42 | + public InputStreamWrapper(InputStream wrapped) { |
| 43 | + this.wrapped = wrapped; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public int read() throws IOException { |
| 48 | + return wrapped.read(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void close() throws IOException { |
| 53 | + wrapped.close(); |
| 54 | + wasClosed = true; |
| 55 | + super.close(); |
| 56 | + } |
| 57 | + |
| 58 | + public boolean wasClosed() { |
| 59 | + return wasClosed; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static class LocalClassLoaderServiceImpl extends ClassLoaderServiceImpl { |
| 64 | + final List<InputStreamWrapper> openedStreams = new ArrayList<InputStreamWrapper>(); |
| 65 | + boolean stopped = false; |
| 66 | + |
| 67 | + @Override |
| 68 | + public InputStream locateResourceStream(String name) { |
| 69 | + InputStreamWrapper stream = new InputStreamWrapper( super.locateResourceStream( name ) ); |
| 70 | + openedStreams.add( stream ); |
| 71 | + return stream; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void stop() { |
| 76 | + for ( InputStreamWrapper openedStream : openedStreams ) { |
| 77 | + if ( !openedStream.wasClosed ) { |
| 78 | + try { |
| 79 | + openedStream.close(); |
| 80 | + } |
| 81 | + catch (IOException ignore) { |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + openedStreams.clear(); |
| 86 | + stopped = true; |
| 87 | + super.stop(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + LocalClassLoaderServiceImpl classLoaderService = new LocalClassLoaderServiceImpl(); |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testStreamClosing() { |
| 95 | + BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder() |
| 96 | + .applyClassLoaderService( classLoaderService ) |
| 97 | + .build(); |
| 98 | + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder( bsr ) |
| 99 | + .configure( "org/hibernate/test/boot/cfgXml/hibernate.cfg.xml" ) |
| 100 | + .build(); |
| 101 | + try { |
| 102 | + for ( InputStreamWrapper openedStream : classLoaderService.openedStreams ) { |
| 103 | + assertTrue( openedStream.wasClosed ); |
| 104 | + } |
| 105 | + } |
| 106 | + finally { |
| 107 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 108 | + } |
| 109 | + |
| 110 | + assertTrue( classLoaderService.stopped ); |
| 111 | + } |
| 112 | +} |
0 commit comments