|
5 | 5 | import jakarta.xml.bind.Unmarshaller; |
6 | 6 | import org.apache.commons.collections4.list.UnmodifiableList; |
7 | 7 | import org.hibernate.boot.jaxb.Origin; |
| 8 | +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping; |
| 9 | +import org.hibernate.boot.jaxb.hbm.transform.UnsupportedFeatureHandling; |
8 | 10 | import org.hibernate.boot.jaxb.internal.MappingBinder; |
9 | 11 | import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl; |
10 | 12 | import org.hibernate.boot.jaxb.spi.Binding; |
|
14 | 16 | import org.hibernate.service.ServiceRegistry; |
15 | 17 | import org.hibernate.tool.internal.util.DummyDialect; |
16 | 18 | import org.junit.jupiter.api.BeforeEach; |
17 | | -import org.junit.jupiter.api.Disabled; |
18 | 19 | import org.junit.jupiter.api.Test; |
19 | 20 | import org.junit.jupiter.api.io.TempDir; |
20 | 21 |
|
21 | 22 | import java.io.File; |
| 23 | +import java.io.FileInputStream; |
22 | 24 | import java.io.InputStream; |
23 | 25 | import java.lang.reflect.*; |
24 | 26 | import java.nio.file.Files; |
@@ -174,14 +176,67 @@ public void testMarshall() throws Exception { |
174 | 176 | marshallMethod.invoke(mappingExporter, null, hbmFile); |
175 | 177 | lines = Files.readAllLines(mappingFile.toPath()); |
176 | 178 | assertEquals(4, lines.size()); |
| 179 | + Field formatResultField = MappingExporter.class.getDeclaredField("formatResult"); |
| 180 | + formatResultField.setAccessible(true); |
| 181 | + formatResultField.set(mappingExporter, false); |
| 182 | + Files.writeString(mappingFile.toPath(), "<foo><bar>foobar</bar></foo>"); |
| 183 | + lines = Files.readAllLines(mappingFile.toPath()); |
| 184 | + assertEquals(1, lines.size()); |
| 185 | + marshallMethod.invoke(mappingExporter, null, hbmFile); |
| 186 | + lines = Files.readAllLines(mappingFile.toPath()); |
| 187 | + assertEquals(1, lines.size()); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testTransformBindings() throws Exception { |
| 192 | + File simpleHbmXmlFile = new File(this.tempDir, "simple.hbm.xml"); |
| 193 | + Files.writeString(simpleHbmXmlFile.toPath(), SIMPLE_HBM_XML); |
| 194 | + MappingBinder mappingBinder = new MappingBinder( |
| 195 | + MappingBinder.class.getClassLoader()::getResourceAsStream, |
| 196 | + UnsupportedFeatureHandling.ERROR); |
| 197 | + Binding<JaxbHbmHibernateMapping> hbmBinding = mappingBinder.bind( |
| 198 | + new FileInputStream(simpleHbmXmlFile), |
| 199 | + new HbmXmlOrigin(simpleHbmXmlFile)); |
| 200 | + List<Binding<JaxbHbmHibernateMapping>> bindings = new ArrayList<>(); |
| 201 | + bindings.add(hbmBinding); |
| 202 | + Method transformBindingsMethod = MappingExporter.class.getDeclaredMethod( |
| 203 | + "transformBindings", |
| 204 | + List.class); |
| 205 | + assertNotNull(transformBindingsMethod); |
| 206 | + transformBindingsMethod.setAccessible(true); |
| 207 | + List<?> transformedBindings = (List<?>)transformBindingsMethod.invoke(mappingExporter, bindings); |
| 208 | + assertNotNull(transformedBindings); |
| 209 | + assertEquals(1, transformedBindings.size()); |
| 210 | + Object object = transformedBindings.get(0); |
| 211 | + assertInstanceOf(Binding.class, object); |
| 212 | + Binding<?> entityBinding = (Binding<?>)object; |
| 213 | + Origin origin = entityBinding.getOrigin(); |
| 214 | + assertInstanceOf(HbmXmlOrigin.class, origin); |
| 215 | + assertSame(simpleHbmXmlFile, ((HbmXmlOrigin)origin).getHbmXmlFile()); |
| 216 | + Object root = entityBinding.getRoot(); |
| 217 | + assertInstanceOf(JaxbEntityMappingsImpl.class, root); |
| 218 | + JaxbEntityMappingsImpl entityMappings = (JaxbEntityMappingsImpl)root; |
| 219 | + assertEquals(1, entityMappings.getEntities().size()); |
| 220 | + assertEquals("Foo", entityMappings.getEntities().get(0).getClazz()); |
177 | 221 | } |
178 | 222 |
|
179 | | - @Disabled |
180 | 223 | @Test |
181 | | - public void testStart() { |
182 | | - MappingExporter exporter = new MappingExporter(); |
183 | | - assertNotNull(exporter); |
184 | | - exporter.start(); |
| 224 | + public void testStart() throws Exception { |
| 225 | + File simpleHbmXmlFile = new File(this.tempDir, "simple.hbm.xml"); |
| 226 | + File simpleMappingXmlFile = new File(this.tempDir, "simple.mapping.xml"); |
| 227 | + Files.writeString(simpleHbmXmlFile.toPath(), SIMPLE_HBM_XML); |
| 228 | + Field hbmFilesField = MappingExporter.class.getDeclaredField("hbmXmlFiles"); |
| 229 | + hbmFilesField.setAccessible(true); |
| 230 | + hbmFilesField.set( |
| 231 | + mappingExporter, |
| 232 | + new UnmodifiableList<>(List.of(simpleHbmXmlFile))); |
| 233 | + assertTrue(simpleHbmXmlFile.exists()); |
| 234 | + assertFalse(simpleMappingXmlFile.exists()); |
| 235 | + mappingExporter.start(); |
| 236 | + assertTrue(simpleMappingXmlFile.exists()); |
| 237 | + String mappingXml = Files.readString(simpleMappingXmlFile.toPath()); |
| 238 | + assertTrue(mappingXml.contains("entity-mappings")); |
| 239 | + System.out.println(mappingXml); |
185 | 240 | } |
186 | 241 |
|
187 | 242 | private static final Marshaller DUMMY_MARSHALLER = (Marshaller) Proxy.newProxyInstance( |
@@ -225,4 +280,14 @@ public Marshaller createMarshaller() { |
225 | 280 | } |
226 | 281 | } |
227 | 282 |
|
| 283 | + private static final String SIMPLE_HBM_XML = |
| 284 | + """ |
| 285 | + <hibernate-mapping> |
| 286 | + <class name="Foo"> |
| 287 | + <id name="id" type="long"/> |
| 288 | + <property name="name" type="string"/> |
| 289 | + </class> |
| 290 | + </hibernate-mapping> |
| 291 | + """; |
| 292 | + |
228 | 293 | } |
0 commit comments