|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.bootstrap.binding.annotations.access.xml; |
6 | 6 |
|
7 | | -import java.io.IOException; |
8 | | -import java.io.InputStream; |
9 | | -import java.util.ArrayList; |
10 | | -import java.util.Collections; |
11 | | -import java.util.List; |
12 | | - |
13 | | -import org.hibernate.cfg.Configuration; |
14 | 7 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
15 | 8 | import org.hibernate.metamodel.mapping.AttributeMapping; |
16 | 9 | import org.hibernate.metamodel.mapping.AttributeMappingsList; |
|
19 | 12 | import org.hibernate.property.access.spi.GetterFieldImpl; |
20 | 13 | import org.hibernate.property.access.spi.GetterMethodImpl; |
21 | 14 |
|
22 | | -import org.hibernate.testing.util.ServiceRegistryUtil; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
23 | 19 | import org.junit.jupiter.api.Test; |
24 | 20 |
|
25 | 21 | import jakarta.persistence.AccessType; |
|
31 | 27 | * |
32 | 28 | * @author Hardy Ferentschik |
33 | 29 | */ |
| 30 | +@SuppressWarnings("JUnitMalformedDeclaration") |
34 | 31 | public class XmlAccessTest { |
35 | 32 |
|
36 | 33 | @Test |
37 | | - public void testAccessOnBasicXmlElement() { |
38 | | - Class<?> classUnderTest = Tourist.class; |
39 | | - List<Class<?>> classes = new ArrayList<>(); |
40 | | - classes.add( classUnderTest ); |
41 | | - List<String> configFiles = Collections.emptyList(); |
42 | | - SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); |
43 | | - |
| 34 | + @ServiceRegistry |
| 35 | + @DomainModel(annotatedClasses = Tourist.class) |
| 36 | + @SessionFactory |
| 37 | + void testBaseline(SessionFactoryScope factoryScope) { |
44 | 38 | // without any xml configuration we have field access |
45 | | - assertAccessType( factory, classUnderTest, AccessType.FIELD ); |
46 | | - factory.close(); |
47 | | - // now with an additional xml configuration file changing the default access type for Tourist using basic |
48 | | - configFiles = new ArrayList<>(); |
49 | | - configFiles.add( "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Tourist.xml" ); |
50 | | - factory = buildSessionFactory( classes, configFiles ); |
51 | | - assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); |
52 | | - factory.close(); |
| 39 | + assertAccessType( factoryScope.getSessionFactory(), Tourist.class, AccessType.FIELD ); |
53 | 40 | } |
54 | 41 |
|
55 | 42 | @Test |
56 | | - public void testAccessOnPersistenceUnitDefaultsXmlElement() { |
57 | | - Class<?> classUnderTest = Tourist.class; |
58 | | - List<Class<?>> classes = new ArrayList<>(); |
59 | | - classes.add( classUnderTest ); |
60 | | - List<String> configFiles = Collections.emptyList(); |
61 | | - SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); |
62 | | - |
63 | | - // without any xml configuration we have field access |
64 | | - assertAccessType( factory, classUnderTest, AccessType.FIELD ); |
65 | | - factory.close(); |
66 | | - // now with an additional xml configuration file changing the default access type for Tourist using persitence unit defaults |
67 | | - configFiles = new ArrayList<>(); |
68 | | - configFiles.add( "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Tourist2.xml" ); |
69 | | - factory = buildSessionFactory( classes, configFiles ); |
70 | | - assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); |
71 | | - factory.close(); |
| 43 | + @ServiceRegistry |
| 44 | + @DomainModel(annotatedClasses = Tourist.class, xmlMappings = "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Tourist.xml") |
| 45 | + @SessionFactory |
| 46 | + public void testAccessOnBasicXmlElement(SessionFactoryScope factoryScope) { |
| 47 | + assertAccessType( factoryScope.getSessionFactory(), Tourist.class, AccessType.PROPERTY ); |
72 | 48 | } |
73 | 49 |
|
74 | 50 | @Test |
75 | | - public void testAccessOnEntityMappingsXmlElement() { |
76 | | - Class<?> classUnderTest = Tourist.class; |
77 | | - List<Class<?>> classes = new ArrayList<>(); |
78 | | - classes.add( classUnderTest ); |
79 | | - List<String> configFiles = Collections.emptyList(); |
80 | | - SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); |
81 | | - |
82 | | - // without any xml configuration we have field access |
83 | | - assertAccessType( factory, classUnderTest, AccessType.FIELD ); |
84 | | - factory.close(); |
85 | | - // now with an additional xml configuration file changing the default access type for Tourist using default in entity-mappings |
86 | | - configFiles = new ArrayList<>(); |
87 | | - configFiles.add( "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Tourist3.xml" ); |
88 | | - factory = buildSessionFactory( classes, configFiles ); |
89 | | - assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); |
90 | | - factory.close(); |
| 51 | + @ServiceRegistry |
| 52 | + @DomainModel(annotatedClasses = Tourist.class, xmlMappings = "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Tourist2.xml") |
| 53 | + @SessionFactory |
| 54 | + public void testAccessOnPersistenceUnitDefaultsXmlElement(SessionFactoryScope factoryScope) { |
| 55 | + assertAccessType( factoryScope.getSessionFactory(), Tourist.class, AccessType.PROPERTY ); |
91 | 56 | } |
92 | 57 |
|
93 | 58 | @Test |
94 | | - public void testAccessOnEntityXmlElement() { |
95 | | - Class<?> classUnderTest = Tourist.class; |
96 | | - List<Class<?>> classes = new ArrayList<>(); |
97 | | - classes.add( classUnderTest ); |
98 | | - List<String> configFiles = Collections.emptyList(); |
99 | | - SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); |
100 | | - |
101 | | - // without any xml configuration we have field access |
102 | | - assertAccessType( factory, classUnderTest, AccessType.FIELD ); |
103 | | - factory.close(); |
104 | | - // now with an additional xml configuration file changing the default access type for Tourist using entity level config |
105 | | - configFiles = new ArrayList<>(); |
106 | | - configFiles.add( "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Tourist4.xml" ); |
107 | | - factory = buildSessionFactory( classes, configFiles ); |
108 | | - assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); |
109 | | - factory.close(); |
| 59 | + @ServiceRegistry |
| 60 | + @DomainModel(annotatedClasses = Tourist.class, xmlMappings = "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Tourist3.xml") |
| 61 | + @SessionFactory |
| 62 | + public void testAccessOnEntityMappingsXmlElement(SessionFactoryScope factoryScope) { |
| 63 | + assertAccessType( factoryScope.getSessionFactory(), Tourist.class, AccessType.PROPERTY ); |
110 | 64 | } |
111 | 65 |
|
112 | 66 | @Test |
113 | | - public void testAccessOnMappedSuperClassXmlElement() { |
114 | | - Class<?> classUnderTest = Waiter.class; |
115 | | - List<Class<?>> classes = new ArrayList<>(); |
116 | | - classes.add( classUnderTest ); |
117 | | - classes.add( Crew.class ); |
118 | | - List<String> configFiles = new ArrayList<>(); |
119 | | - configFiles.add( "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Crew.xml" ); |
120 | | - SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); |
121 | | - assertAccessType( factory, classUnderTest, AccessType.FIELD ); |
122 | | - factory.close(); |
| 67 | + @ServiceRegistry |
| 68 | + @DomainModel(annotatedClasses = Tourist.class, xmlMappings = "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Tourist4.xml") |
| 69 | + @SessionFactory |
| 70 | + public void testAccessOnEntityXmlElement(SessionFactoryScope factoryScope) { |
| 71 | + assertAccessType( factoryScope.getSessionFactory(), Tourist.class, AccessType.PROPERTY ); |
123 | 72 | } |
124 | 73 |
|
125 | 74 | @Test |
126 | | - public void testAccessOnAssociationXmlElement() { |
127 | | - Class<?> classUnderTest = RentalCar.class; |
128 | | - List<Class<?>> classes = new ArrayList<>(); |
129 | | - classes.add( classUnderTest ); |
130 | | - classes.add( Driver.class ); |
131 | | - List<String> configFiles = new ArrayList<>(); |
132 | | - configFiles.add( "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/RentalCar.xml" ); |
133 | | - SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); |
134 | | - assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); |
135 | | - factory.close(); |
| 75 | + @ServiceRegistry |
| 76 | + @DomainModel(annotatedClasses = Waiter.class, xmlMappings = "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Crew.xml") |
| 77 | + @SessionFactory |
| 78 | + public void testAccessOnMappedSuperClassXmlElement(SessionFactoryScope factoryScope) { |
| 79 | + assertAccessType( factoryScope.getSessionFactory(), Waiter.class, AccessType.FIELD ); |
136 | 80 | } |
137 | 81 |
|
138 | 82 | @Test |
139 | | - public void testAccessOnEmbeddedXmlElement() { |
140 | | - Class<?> classUnderTest = Cook.class; |
141 | | - List<Class<?>> classes = new ArrayList<>(); |
142 | | - classes.add( classUnderTest ); |
143 | | - classes.add( Knive.class ); |
144 | | - List<String> configFiles = new ArrayList<>(); |
145 | | - configFiles.add( "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Cook.xml" ); |
146 | | - SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); |
147 | | - assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); |
148 | | - factory.close(); |
| 83 | + @ServiceRegistry |
| 84 | + @DomainModel(annotatedClasses = {RentalCar.class, Driver.class}, xmlMappings = "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/RentalCar.xml") |
| 85 | + @SessionFactory |
| 86 | + public void testAccessOnAssociationXmlElement(SessionFactoryScope factoryScope) { |
| 87 | + assertAccessType( factoryScope.getSessionFactory(), RentalCar.class, AccessType.PROPERTY ); |
149 | 88 | } |
150 | 89 |
|
151 | 90 | @Test |
152 | | - public void testAccessOnElementCollectionXmlElement() { |
153 | | - Class<?> classUnderTest = Boy.class; |
154 | | - List<Class<?>> classes = new ArrayList<>(); |
155 | | - classes.add( classUnderTest ); |
156 | | - List<String> configFiles = new ArrayList<>(); |
157 | | - configFiles.add( "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Boy.xml" ); |
158 | | - SessionFactoryImplementor factory = buildSessionFactory( classes, configFiles ); |
159 | | - assertAccessType( factory, classUnderTest, AccessType.PROPERTY ); |
160 | | - factory.close(); |
| 91 | + @ServiceRegistry |
| 92 | + @DomainModel(annotatedClasses = {Cook.class, Knive.class}, xmlMappings = "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Cook.xml") |
| 93 | + @SessionFactory |
| 94 | + public void testAccessOnEmbeddedXmlElement(SessionFactoryScope factoryScope) { |
| 95 | + assertAccessType( factoryScope.getSessionFactory(), Cook.class, AccessType.PROPERTY ); |
161 | 96 | } |
162 | 97 |
|
163 | | - private SessionFactoryImplementor buildSessionFactory(List<Class<?>> classesUnderTest, List<String> configFiles) { |
164 | | - assert classesUnderTest != null; |
165 | | - assert configFiles != null; |
166 | | - Configuration cfg = new Configuration(); |
167 | | - for ( Class<?> clazz : classesUnderTest ) { |
168 | | - cfg.addAnnotatedClass( clazz ); |
169 | | - } |
170 | | - ServiceRegistryUtil.applySettings( cfg.getStandardServiceRegistryBuilder() ); |
171 | | - for ( String configFile : configFiles ) { |
172 | | - try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( configFile )) { |
173 | | - cfg.addInputStream( is ); |
174 | | - } |
175 | | - catch (IOException e) { |
176 | | - throw new IllegalArgumentException( e ); |
177 | | - } |
178 | | - } |
179 | | - return (SessionFactoryImplementor) cfg.buildSessionFactory(); |
| 98 | + @Test |
| 99 | + @ServiceRegistry |
| 100 | + @DomainModel(annotatedClasses = Boy.class, xmlMappings = "org/hibernate/orm/test/bootstrap/binding.annotations.access.xml/Boy.xml") |
| 101 | + @SessionFactory |
| 102 | + public void testAccessOnElementCollectionXmlElement(SessionFactoryScope factoryScope) { |
| 103 | + assertAccessType( factoryScope.getSessionFactory(), Boy.class, AccessType.PROPERTY ); |
180 | 104 | } |
181 | 105 |
|
182 | | - // uses the first getter of the tupelizer for the assertions |
183 | 106 |
|
184 | 107 | private void assertAccessType(SessionFactoryImplementor factory, Class<?> classUnderTest, AccessType accessType) { |
185 | 108 | final EntityPersister entityDescriptor = factory.getRuntimeMetamodels() |
|
0 commit comments