-
Notifications
You must be signed in to change notification settings - Fork 33
/
RoundtripTest.java
148 lines (120 loc) · 4.56 KB
/
RoundtripTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.jvnet.hyperjaxb3.ejb.test;
import java.io.File;
import javax.persistence.EntityManager;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import org.jvnet.hyperjaxb3.ejb.util.EntityUtils;
import org.jvnet.jaxb2_commons.lang.ContextUtils;
import org.jvnet.jaxb2_commons.lang.EqualsStrategy;
import org.jvnet.jaxb2_commons.locator.DefaultRootObjectLocator;
import org.jvnet.jaxb2_commons.locator.ObjectLocator;
public abstract class RoundtripTest extends AbstractEntityManagerSamplesTest {
@Override
protected void checkSample(File sample) throws Exception {
final JAXBContext context = createContext();
logger.debug("Unmarshalling.");
final Unmarshaller unmarshaller = context.createUnmarshaller();
final JAXBElement unmarshalledElement;
final Object unmarshalledObject;
final Object unmarshalledDraft = unmarshaller.unmarshal(sample);
if (unmarshalledDraft instanceof JAXBElement) {
unmarshalledElement = (JAXBElement) unmarshalledDraft;
unmarshalledObject = unmarshalledElement.getValue();
} else {
unmarshalledElement = null;
unmarshalledObject = unmarshalledDraft;
}
final JAXBElement etalonElement;
final Object etalonObject;
final Object etalonDraft = unmarshaller.unmarshal(sample);
if (etalonDraft instanceof JAXBElement) {
etalonElement = (JAXBElement) etalonDraft;
etalonObject = etalonElement.getValue();
} else {
etalonElement = null;
etalonObject = etalonDraft;
}
logger.debug("Opening session.");
// Open the session, save object into the database
logger.debug("Saving the object.");
final EntityManager saveManager = createEntityManager();
saveManager.getTransaction().begin();
// final Object merged = saveSession.merge(object);
// saveSession.replicate(object, ReplicationMode.OVERWRITE);
// saveSession.get
// final Serializable id =
final Object mergedObject = saveManager.merge(unmarshalledObject);
saveManager.getTransaction().commit();
final Object id = EntityUtils.getId(saveManager, mergedObject);
// saveSession.getIdentifier(object);
saveManager.clear();
// Close the session
saveManager.close();
logger.debug("Opening session.");
// Open the session, load the object
final EntityManager loadManager = createEntityManager();
logger.debug("Loading the object.");
final Object loadedObject = loadManager.find(mergedObject.getClass(),
id);
logger.debug("Closing the session.");
if (unmarshalledElement != null) {
final JAXBElement<Object> mergedElement = new JAXBElement(
unmarshalledElement.getName(),
unmarshalledElement.getDeclaredType(), mergedObject);
final JAXBElement loadedElement = new JAXBElement(
unmarshalledElement.getName(),
unmarshalledElement.getDeclaredType(), loadedObject);
logger.debug("Initial object:\n"
+ ContextUtils.toString(context, etalonElement));
logger.debug("Source object:\n"
+ ContextUtils.toString(context, mergedElement));
logger.debug("Result object:\n"
+ ContextUtils.toString(context, loadedElement));
} else {
logger.debug("Initial object:\n"
+ ContextUtils.toString(context, etalonObject));
logger.debug("Source object:\n"
+ ContextUtils.toString(context, mergedObject));
logger.debug("Result object:\n"
+ ContextUtils.toString(context, loadedObject));
}
logger.debug("Checking the document identity.");
checkObjects(mergedObject, loadedObject);
checkObjects(etalonObject, loadedObject);
loadManager.close();
}
protected void checkObjects(final Object object, final Object loadedObject) {
final EqualsStrategy strategy = new org.jvnet.hyperjaxb3.lang.builder.ExtendedJAXBEqualsStrategy() {
@Override
public boolean equals(ObjectLocator leftLocator,
ObjectLocator rightLocator, Object lhs, Object rhs) {
if (!super.equals(leftLocator, rightLocator, lhs, rhs)) {
logger.debug("Objects are not equal.");
super.equals(leftLocator, rightLocator, lhs, rhs);
logger.debug("Left: "
+ (lhs == null ? "null" : lhs.toString()));
if (leftLocator != null) {
logger.debug("At [" + leftLocator.getPathAsString()
+ "].");
}
logger.debug("Right: "
+ (rhs == null ? "null" : rhs.toString()));
if (rightLocator != null) {
logger.debug("At [" + rightLocator.getPathAsString()
+ "].");
}
return false;
} else
{
return true;
}
}
};
;
assertTrue("Objects must be equal.", strategy.equals(
new DefaultRootObjectLocator(object),
new DefaultRootObjectLocator(loadedObject), object,
loadedObject));
}
}