Skip to content

Commit 0db61c6

Browse files
committed
HHH-19324 - Switch tests using hbm.xml to use mapping.xml
1 parent 1987e92 commit 0db61c6

File tree

3 files changed

+69
-188
lines changed

3 files changed

+69
-188
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/fetchprofile/FetchProfileTest.java

Lines changed: 64 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -4,194 +4,112 @@
44
*/
55
package org.hibernate.orm.test.annotations.fetchprofile;
66

7-
import java.io.InputStream;
8-
97
import org.hibernate.MappingException;
108
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;
159
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;
1912
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;
2519

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;
2922

3023
/**
3124
* Test case for HHH-4812
3225
*
3326
* @author Hardy Ferentschik
3427
*/
28+
@SuppressWarnings("JUnitMalformedDeclaration")
3529
@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 {
5031
@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();
7038
}
7139

7240
@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 );
7845

7946
try {
8047
metadataSources.buildMetadata();
81-
fail();
48+
fail( "Expecting an exception, but none thrown" );
8249
}
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) {
9151
}
9252
}
9353

9454
@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 );
10059

10160
try {
10261
metadataSources.buildMetadata();
103-
fail();
104-
}
105-
catch ( MappingException e ) {
106-
log.trace("success");
62+
fail( "Expecting an exception, but none thrown" );
10763
}
108-
finally {
109-
ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();
110-
if(metaServiceRegistry instanceof BootstrapServiceRegistry) {
111-
BootstrapServiceRegistryBuilder.destroy( metaServiceRegistry );
112-
}
64+
catch (MappingException expected) {
11365
}
11466
}
11567

11668
@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();
13274
}
13375

13476
@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+
}
15990

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 );
16096
try {
16197
metadataSources.buildMetadata();
162-
fail();
98+
fail( "Expecting an exception, but none thrown" );
16399
}
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) {
172101
}
173102
}
174103

175104
@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();
196114
}
197115
}

hibernate-core/src/test/resources/org/hibernate/orm/test/annotations/derivedidentities/e1/b/specjmapid/lazy/hbm_order.xml

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
~ SPDX-License-Identifier: Apache-2.0
44
~ Copyright Red Hat Inc. and Hibernate Authors
55
-->
6-
<!DOCTYPE hibernate-mapping
7-
SYSTEM
8-
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
9-
10-
<hibernate-mapping package="org.hibernate.test.annotations.fetchprofile">
6+
<entity-mappings xmlns="http://www.hibernate.org/xsd/orm/mapping"
7+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8+
version="7.0">
9+
<package>org.hibernate.test.annotations.fetchprofile</package>
1110
<fetch-profile name="orders-profile">
1211
<fetch entity="Customer5" association="orders" style="join"/>
1312
</fetch-profile>
14-
</hibernate-mapping>
13+
</entity-mappings>

0 commit comments

Comments
 (0)