Skip to content

Commit 0dc5cf2

Browse files
committed
HBX-3249: Refactor class MappingExporter and improve tests
- Remove unneeded 'mappingBinder' argument from 'createMarshaller' - Create 'marshaller' instance variable and populate it in the constructor - Remove the 'marshaller' local variable in the 'start' method - Remove the 'marshaller' argument from the 'marshall' method Signed-off-by: Koen Aers <koen.aers@gmail.com>
1 parent ae24cd8 commit 0dc5cf2

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

orm/src/main/java/org/hibernate/tool/internal/export/mapping/MappingExporter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ public class MappingExporter implements Exporter {
3535
private boolean formatResult = true;
3636

3737
private final MappingBinder mappingBinder;
38+
private final Marshaller marshaller;
3839

3940
public MappingExporter() {
4041
mappingBinder = createMappingBinder();
42+
marshaller = createMarshaller();
4143
}
4244

4345
public void setHbmFiles(List<File> fileList) {
@@ -56,7 +58,6 @@ public Properties getProperties() {
5658
@Override
5759
public void start() {
5860
List<Binding<JaxbHbmHibernateMapping>> hbmBindings = getHbmBindings();
59-
Marshaller marshaller = createMarshaller(mappingBinder);
6061
MetadataSources metadataSources = new MetadataSources( createServiceRegistry() );
6162
hbmBindings.forEach( metadataSources::addHbmXmlBinding );
6263
List<Binding<JaxbEntityMappingsImpl>> transformedBindings = HbmXmlTransformer.transform(
@@ -68,7 +69,7 @@ public void start() {
6869
Binding<JaxbHbmHibernateMapping> hbmBinding = hbmBindings.get( i );
6970
Binding<JaxbEntityMappingsImpl> transformedBinding = transformedBindings.get( i );
7071
HbmXmlOrigin origin = (HbmXmlOrigin)hbmBinding.getOrigin();
71-
marshall(marshaller, transformedBinding.getRoot(), origin.getHbmXmlFile());
72+
marshall(transformedBinding.getRoot(), origin.getHbmXmlFile());
7273
}
7374
}
7475

@@ -109,7 +110,7 @@ private Binding<JaxbHbmHibernateMapping> bindHbmXml(HbmXmlOrigin origin) {
109110
}
110111
}
111112

112-
private Marshaller createMarshaller(MappingBinder mappingBinder) {
113+
private Marshaller createMarshaller() {
113114
try {
114115
return mappingBinder.mappingJaxbContext().createMarshaller();
115116
}
@@ -119,7 +120,6 @@ private Marshaller createMarshaller(MappingBinder mappingBinder) {
119120
}
120121

121122
private void marshall(
122-
Marshaller marshaller,
123123
JaxbEntityMappingsImpl mappings,
124124
File hbmXmlFile) {
125125
File mappingXmlFile = new File(

orm/src/test/java/org/hibernate/tool/internal/export/mapping/MappingExporterTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public void testConstructor() throws Exception {
4545
Field mappingBinderField = MappingExporter.class.getDeclaredField("mappingBinder");
4646
mappingBinderField.setAccessible(true);
4747
assertNotNull(mappingBinderField.get(mappingExporter));
48+
Field marshallerField = MappingExporter.class.getDeclaredField("marshaller");
49+
marshallerField.setAccessible(true);
50+
assertNotNull(marshallerField.get(mappingExporter));
4851
}
4952

5053
@Test
@@ -140,32 +143,35 @@ public void testGetHbmBindings() throws Exception {
140143

141144
@Test
142145
public void testCreateMarshaller() throws Exception {
143-
Method createMarshallerMethod = MappingExporter.class.getDeclaredMethod(
144-
"createMarshaller",
145-
MappingBinder.class);
146+
Method createMarshallerMethod = MappingExporter.class.getDeclaredMethod("createMarshaller");
146147
assertNotNull(createMarshallerMethod);
147148
createMarshallerMethod.setAccessible(true);
148149
final MappingBinder mappingBinder = new TestMappingBinder(new File("foo"), "foobar");
150+
Field mappingBinderField = MappingExporter.class.getDeclaredField("mappingBinder");
151+
mappingBinderField.setAccessible(true);
152+
mappingBinderField.set(mappingExporter, mappingBinder);
149153
assertSame(
150154
DUMMY_MARSHALLER,
151-
createMarshallerMethod.invoke(mappingExporter, mappingBinder));
155+
createMarshallerMethod.invoke(mappingExporter));
152156
}
153157

154158
@Test
155159
public void testMarshall() throws Exception {
156160
Method marshallMethod = MappingExporter.class.getDeclaredMethod(
157161
"marshall",
158-
Marshaller.class,
159162
JaxbEntityMappingsImpl.class,
160163
File.class);
161164
assertNotNull(marshallMethod);
162165
marshallMethod.setAccessible(true);
166+
Field marshallerField = MappingExporter.class.getDeclaredField("marshaller");
167+
marshallerField.setAccessible(true);
168+
marshallerField.set(mappingExporter, DUMMY_MARSHALLER);
163169
File hbmFile = new File(this.tempDir, "foo.hbm.xml");
164170
File mappingFile = new File(this.tempDir, "foo.mapping.xml");
165171
Files.writeString(mappingFile.toPath(), "<foo><bar>foobar</bar></foo>");
166172
List<String> lines = Files.readAllLines(mappingFile.toPath());
167173
assertEquals(1, lines.size());
168-
marshallMethod.invoke(mappingExporter, DUMMY_MARSHALLER, null, hbmFile);
174+
marshallMethod.invoke(mappingExporter, null, hbmFile);
169175
lines = Files.readAllLines(mappingFile.toPath());
170176
assertEquals(4, lines.size());
171177
}

0 commit comments

Comments
 (0)