| 
4 | 4 |  */  | 
5 | 5 | package org.hibernate.orm.test.annotations.fetchprofile;  | 
6 | 6 | 
 
  | 
7 |  | -import java.io.InputStream;  | 
8 |  | - | 
9 | 7 | import org.hibernate.MappingException;  | 
10 | 8 | import org.hibernate.boot.MetadataSources;  | 
11 |  | -import org.hibernate.boot.registry.BootstrapServiceRegistry;  | 
12 |  | -import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;  | 
13 |  | -import org.hibernate.cfg.Configuration;  | 
14 |  | -import org.hibernate.cfg.Environment;  | 
15 | 9 | import org.hibernate.engine.spi.SessionFactoryImplementor;  | 
16 |  | -import org.hibernate.service.ServiceRegistry;  | 
17 |  | - | 
18 |  | -import org.hibernate.testing.ServiceRegistryBuilder;  | 
 | 10 | +import org.hibernate.testing.orm.junit.DomainModel;  | 
 | 11 | +import org.hibernate.testing.orm.junit.Jira;  | 
19 | 12 | import org.hibernate.testing.orm.junit.JiraKey;  | 
20 |  | -import org.hibernate.testing.junit4.BaseUnitTestCase;  | 
21 |  | -import org.hibernate.testing.util.ServiceRegistryUtil;  | 
22 |  | -import org.junit.After;  | 
23 |  | -import org.junit.Before;  | 
24 |  | -import org.junit.Test;  | 
 | 13 | +import org.hibernate.testing.orm.junit.NotImplementedYet;  | 
 | 14 | +import org.hibernate.testing.orm.junit.ServiceRegistry;  | 
 | 15 | +import org.hibernate.testing.orm.junit.ServiceRegistryScope;  | 
 | 16 | +import org.hibernate.testing.orm.junit.SessionFactory;  | 
 | 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope;  | 
 | 18 | +import org.junit.jupiter.api.Test;  | 
25 | 19 | 
 
  | 
26 |  | -import static org.junit.Assert.assertFalse;  | 
27 |  | -import static org.junit.Assert.assertTrue;  | 
28 |  | -import static org.junit.Assert.fail;  | 
 | 20 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 21 | +import static org.junit.jupiter.api.Assertions.fail;  | 
29 | 22 | 
 
  | 
30 | 23 | /**  | 
31 | 24 |  * Test case for HHH-4812  | 
32 | 25 |  *  | 
33 | 26 |  * @author Hardy Ferentschik  | 
34 | 27 |  */  | 
 | 28 | +@SuppressWarnings("JUnitMalformedDeclaration")  | 
35 | 29 | @JiraKey( value = "HHH-4812" )  | 
36 |  | -public class FetchProfileTest extends BaseUnitTestCase {  | 
37 |  | - | 
38 |  | -	private ServiceRegistry serviceRegistry;  | 
39 |  | - | 
40 |  | -	@Before  | 
41 |  | -	public void setUp() {  | 
42 |  | -		serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );  | 
43 |  | -	}  | 
44 |  | - | 
45 |  | -	@After  | 
46 |  | -	public void tearDown() {  | 
47 |  | -		if (serviceRegistry != null) ServiceRegistryBuilder.destroy(serviceRegistry);  | 
48 |  | -	}  | 
49 |  | - | 
 | 30 | +public class FetchProfileTest {  | 
50 | 31 | 	@Test  | 
51 |  | -	public void testFetchProfileConfigured() {  | 
52 |  | -		Configuration config = new Configuration();  | 
53 |  | -		config.addAnnotatedClass( Customer.class );  | 
54 |  | -		config.addAnnotatedClass( Order.class );  | 
55 |  | -		config.addAnnotatedClass( SupportTickets.class );  | 
56 |  | -		config.addAnnotatedClass( Country.class );  | 
57 |  | -		try (SessionFactoryImplementor sessionImpl = ( SessionFactoryImplementor ) config.buildSessionFactory(  | 
58 |  | -				serviceRegistry  | 
59 |  | -		)) {  | 
60 |  | - | 
61 |  | -			assertTrue(  | 
62 |  | -					"fetch profile not parsed properly",  | 
63 |  | -					sessionImpl.containsFetchProfileDefinition( "customer-with-orders" )  | 
64 |  | -			);  | 
65 |  | -			assertFalse(  | 
66 |  | -					"package info should not be parsed",  | 
67 |  | -					sessionImpl.containsFetchProfileDefinition( "package-profile-1" )  | 
68 |  | -			);  | 
69 |  | -		}  | 
 | 32 | +	@DomainModel(annotatedClasses = {Customer.class, Order.class, SupportTickets.class, Country.class})  | 
 | 33 | +	@SessionFactory  | 
 | 34 | +	public void testFetchProfileConfigured(SessionFactoryScope factoryScope) {  | 
 | 35 | +		final SessionFactoryImplementor sessionFactory = factoryScope.getSessionFactory();  | 
 | 36 | +		assertThat( sessionFactory.containsFetchProfileDefinition( "customer-with-orders" ) ).isTrue();  | 
 | 37 | +		assertThat( sessionFactory.containsFetchProfileDefinition( "package-profile-1" ) ).isFalse();  | 
70 | 38 | 	}  | 
71 | 39 | 
 
  | 
72 | 40 | 	@Test  | 
73 |  | -	public void testWrongAssociationName() {  | 
74 |  | -		final MetadataSources metadataSources = new MetadataSources( ServiceRegistryUtil.serviceRegistry() )  | 
75 |  | -				.addAnnotatedClass( Customer2.class )  | 
76 |  | -				.addAnnotatedClass( Order.class )  | 
77 |  | -				.addAnnotatedClass( Country.class );  | 
 | 41 | +	@ServiceRegistry  | 
 | 42 | +	public void testWrongAssociationName(ServiceRegistryScope registryScope) {  | 
 | 43 | +		final MetadataSources metadataSources = new MetadataSources( registryScope.getRegistry() )  | 
 | 44 | +				.addAnnotatedClasses( Customer2.class, Order.class, Country.class );  | 
78 | 45 | 
 
  | 
79 | 46 | 		try {  | 
80 | 47 | 			metadataSources.buildMetadata();  | 
81 |  | -			fail();  | 
 | 48 | +			fail( "Expecting an exception, but none thrown" );  | 
82 | 49 | 		}  | 
83 |  | -		catch ( MappingException e ) {  | 
84 |  | -			log.trace("success");  | 
85 |  | -		}  | 
86 |  | -		finally {  | 
87 |  | -			ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();  | 
88 |  | -			if(metaServiceRegistry instanceof BootstrapServiceRegistry) {  | 
89 |  | -				BootstrapServiceRegistryBuilder.destroy( metaServiceRegistry );  | 
90 |  | -			}  | 
 | 50 | +		catch (MappingException expected) {  | 
91 | 51 | 		}  | 
92 | 52 | 	}  | 
93 | 53 | 
 
  | 
94 | 54 | 	@Test  | 
95 |  | -	public void testWrongClass() {  | 
96 |  | -		final MetadataSources metadataSources = new MetadataSources( ServiceRegistryUtil.serviceRegistry() )  | 
97 |  | -				.addAnnotatedClass( Customer3.class )  | 
98 |  | -				.addAnnotatedClass( Order.class )  | 
99 |  | -				.addAnnotatedClass( Country.class );  | 
 | 55 | +	@ServiceRegistry  | 
 | 56 | +	public void testWrongClass(ServiceRegistryScope registryScope) {  | 
 | 57 | +		final MetadataSources metadataSources = new MetadataSources( registryScope.getRegistry() )  | 
 | 58 | +				.addAnnotatedClasses( Customer2.class, Order.class, Country.class );  | 
100 | 59 | 
 
  | 
101 | 60 | 		try {  | 
102 | 61 | 			metadataSources.buildMetadata();  | 
103 |  | -			fail();  | 
104 |  | -		}  | 
105 |  | -		catch ( MappingException e ) {  | 
106 |  | -			log.trace("success");  | 
 | 62 | +			fail( "Expecting an exception, but none thrown" );  | 
107 | 63 | 		}  | 
108 |  | -		finally {  | 
109 |  | -			ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();  | 
110 |  | -			if(metaServiceRegistry instanceof BootstrapServiceRegistry) {  | 
111 |  | -				BootstrapServiceRegistryBuilder.destroy( metaServiceRegistry );  | 
112 |  | -			}  | 
 | 64 | +		catch (MappingException expected) {  | 
113 | 65 | 		}  | 
114 | 66 | 	}  | 
115 | 67 | 
 
  | 
116 | 68 | 	@Test  | 
117 |  | -	public void testNowSupportedFetchMode() {  | 
118 |  | -		final MetadataSources metadataSources = new MetadataSources( ServiceRegistryUtil.serviceRegistry() )  | 
119 |  | -				.addAnnotatedClass( Customer4.class )  | 
120 |  | -				.addAnnotatedClass( Order.class )  | 
121 |  | -				.addAnnotatedClass( Country.class );  | 
122 |  | - | 
123 |  | -		try {  | 
124 |  | -			metadataSources.buildMetadata();  | 
125 |  | -		}  | 
126 |  | -		finally {  | 
127 |  | -			ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();  | 
128 |  | -			if(metaServiceRegistry instanceof BootstrapServiceRegistry) {  | 
129 |  | -				BootstrapServiceRegistryBuilder.destroy( metaServiceRegistry );  | 
130 |  | -			}  | 
131 |  | -		}  | 
 | 69 | +	@DomainModel(annotatedClasses = {Customer4.class, Order.class, Country.class})  | 
 | 70 | +	@SessionFactory  | 
 | 71 | +	public void testNowSupportedFetchMode(SessionFactoryScope factoryScope) {  | 
 | 72 | +		final SessionFactoryImplementor sessionFactory = factoryScope.getSessionFactory();  | 
 | 73 | +		assertThat( sessionFactory.containsFetchProfileDefinition( "unsupported-fetch-mode" ) ).isTrue();  | 
132 | 74 | 	}  | 
133 | 75 | 
 
  | 
134 | 76 | 	@Test  | 
135 |  | -	public void testXmlOverride() {  | 
136 |  | -		Configuration config = new Configuration();  | 
137 |  | -		config.addAnnotatedClass( Customer5.class );  | 
138 |  | -		config.addAnnotatedClass( Order.class );  | 
139 |  | -		config.addAnnotatedClass( Country.class );  | 
140 |  | -		InputStream is = Thread.currentThread()  | 
141 |  | -				.getContextClassLoader()  | 
142 |  | -				.getResourceAsStream( "org/hibernate/orm/test/annotations/fetchprofile/mappings.hbm.xml" );  | 
143 |  | -		config.addInputStream( is );  | 
144 |  | -		try (SessionFactoryImplementor sessionImpl = ( SessionFactoryImplementor ) config.buildSessionFactory(  | 
145 |  | -				serviceRegistry  | 
146 |  | -		)) {  | 
147 |  | - | 
148 |  | -			assertTrue(  | 
149 |  | -					"fetch profile not parsed properly",  | 
150 |  | -					sessionImpl.containsFetchProfileDefinition( "orders-profile" )  | 
151 |  | -			);  | 
152 |  | -		}  | 
153 |  | - | 
154 |  | -		// now the same with no xml  | 
155 |  | -		final MetadataSources metadataSources = new MetadataSources( ServiceRegistryUtil.serviceRegistry() )  | 
156 |  | -				.addAnnotatedClass( Customer5.class )  | 
157 |  | -				.addAnnotatedClass( Order.class )  | 
158 |  | -				.addAnnotatedClass( Country.class );  | 
 | 77 | +	@ServiceRegistry  | 
 | 78 | +	@NotImplementedYet(reason = "See HHH-19417")  | 
 | 79 | +	@Jira( "https://hibernate.atlassian.net/browse/HHH-19417" )  | 
 | 80 | +	public void testXmlOverride(ServiceRegistryScope registryScope) {  | 
 | 81 | +		final MetadataSources metadataSources = new MetadataSources( registryScope.getRegistry() )  | 
 | 82 | +				.addAnnotatedClasses( Customer5.class, Order.class, Country.class )  | 
 | 83 | +				.addResource( "org/hibernate/orm/test/annotations/fetchprofile/mappings.xml" );  | 
 | 84 | +		// NOTE : until HHH-19417 is addressed, this will fail  | 
 | 85 | +		metadataSources.buildMetadata();  | 
 | 86 | + | 
 | 87 | +//		final SessionFactoryImplementor sessionFactory = factoryScope.getSessionFactory();  | 
 | 88 | +//		assertThat( sessionFactory.containsFetchProfileDefinition( "orders-profile" ) ).isTrue();  | 
 | 89 | +	}  | 
159 | 90 | 
 
  | 
 | 91 | +	@Test  | 
 | 92 | +	@ServiceRegistry  | 
 | 93 | +	public void testMissingXmlOverride(ServiceRegistryScope registryScope) {  | 
 | 94 | +		final MetadataSources metadataSources = new MetadataSources( registryScope.getRegistry() )  | 
 | 95 | +				.addAnnotatedClasses( Customer5.class, Order.class, Country.class );  | 
160 | 96 | 		try {  | 
161 | 97 | 			metadataSources.buildMetadata();  | 
162 |  | -			fail();  | 
 | 98 | +			fail( "Expecting an exception, but none thrown" );  | 
163 | 99 | 		}  | 
164 |  | -		catch ( MappingException e ) {  | 
165 |  | -			log.trace("success");  | 
166 |  | -		}  | 
167 |  | -		finally {  | 
168 |  | -			ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();  | 
169 |  | -			if(metaServiceRegistry instanceof BootstrapServiceRegistry) {  | 
170 |  | -				BootstrapServiceRegistryBuilder.destroy( metaServiceRegistry );  | 
171 |  | -			}  | 
 | 100 | +		catch (MappingException expected) {  | 
172 | 101 | 		}  | 
173 | 102 | 	}  | 
174 | 103 | 
 
  | 
175 | 104 | 	@Test  | 
176 |  | -	public void testPackageConfiguredFetchProfile() {  | 
177 |  | -		Configuration config = new Configuration();  | 
178 |  | -		config.addAnnotatedClass( Customer.class );  | 
179 |  | -		config.addAnnotatedClass( Order.class );  | 
180 |  | -		config.addAnnotatedClass( SupportTickets.class );  | 
181 |  | -		config.addAnnotatedClass( Country.class );  | 
182 |  | -		config.addPackage( Customer.class.getPackage().getName() );  | 
183 |  | -		try (SessionFactoryImplementor sessionImpl = ( SessionFactoryImplementor ) config.buildSessionFactory(  | 
184 |  | -				serviceRegistry  | 
185 |  | -		)) {  | 
186 |  | - | 
187 |  | -			assertTrue(  | 
188 |  | -					"fetch profile not parsed properly",  | 
189 |  | -					sessionImpl.containsFetchProfileDefinition( "package-profile-1" )  | 
190 |  | -			);  | 
191 |  | -			assertTrue(  | 
192 |  | -					"fetch profile not parsed properly",  | 
193 |  | -					sessionImpl.containsFetchProfileDefinition( "package-profile-2" )  | 
194 |  | -			);  | 
195 |  | -		}  | 
 | 105 | +	@DomainModel(  | 
 | 106 | +			annotatedClasses = {Customer.class, Order.class, SupportTickets.class, Country.class},  | 
 | 107 | +			annotatedPackageNames = "org.hibernate.orm.test.annotations.fetchprofile"  | 
 | 108 | +	)  | 
 | 109 | +	@SessionFactory  | 
 | 110 | +	public void testPackageConfiguredFetchProfile(SessionFactoryScope factoryScope) {  | 
 | 111 | +		final SessionFactoryImplementor sessionFactory = factoryScope.getSessionFactory();  | 
 | 112 | +		assertThat( sessionFactory.containsFetchProfileDefinition( "package-profile-1" ) ).isTrue();  | 
 | 113 | +		assertThat( sessionFactory.containsFetchProfileDefinition( "package-profile-2" ) ).isTrue();  | 
196 | 114 | 	}  | 
197 | 115 | }  | 
0 commit comments