|
19 | 19 |
|
20 | 20 | import static org.junit.jupiter.api.Assertions.*; |
21 | 21 |
|
22 | | -public class ReplacePersistentCollectionTest { |
23 | | - |
24 | | - private EntityManagerFactory emf; |
25 | | - |
26 | | - private EntityManager em; |
27 | | - |
28 | | - @BeforeEach |
29 | | - public void setUp() throws Exception { |
30 | | - emf = Persistence.createEntityManagerFactory("persistenceUnit"); |
31 | | - em = emf.createEntityManager(); |
32 | | - } |
33 | | - |
34 | | - @AfterEach |
35 | | - public void tearDown() throws Exception { |
36 | | - em.close(); |
37 | | - emf.close(); |
38 | | - } |
39 | | - |
40 | | - // [Issue#93], backwards compatible case |
41 | | - @Test |
42 | | - public void testNoReplacePersistentCollection() throws Exception { |
43 | | - final ObjectMapper mapper = hibernateMapper(new Hibernate4Module() |
44 | | - .configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, true) |
45 | | - ); |
46 | | - |
47 | | - Customer customer = em.find(Customer.class, 103); |
48 | | - assertFalse(Hibernate.isInitialized(customer.getPayments())); |
49 | | - String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer); |
50 | | - assertTrue(json.contains("org.hibernate.collection")); |
51 | | - // should force loading... |
52 | | - Set<Payment> payments = customer.getPayments(); |
53 | | - /* |
| 22 | +public class ReplacePersistentCollectionTest |
| 23 | +{ |
| 24 | + private EntityManagerFactory emf; |
| 25 | + |
| 26 | + private EntityManager em; |
| 27 | + |
| 28 | + @BeforeEach |
| 29 | + public void setUp() throws Exception { |
| 30 | + emf = Persistence.createEntityManagerFactory("persistenceUnit"); |
| 31 | + em = emf.createEntityManager(); |
| 32 | + } |
| 33 | + |
| 34 | + @AfterEach |
| 35 | + public void tearDown() throws Exception { |
| 36 | + em.close(); |
| 37 | + emf.close(); |
| 38 | + } |
| 39 | + |
| 40 | + // [Issue#93], backwards compatible case |
| 41 | + @Test |
| 42 | + public void testNoReplacePersistentCollection() throws Exception { |
| 43 | + final ObjectMapper mapper = hibernateMapper(new Hibernate4Module() |
| 44 | + .configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, true) |
| 45 | + ); |
| 46 | + |
| 47 | + Customer customer = em.find(Customer.class, 103); |
| 48 | + assertFalse(Hibernate.isInitialized(customer.getPayments())); |
| 49 | + String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer); |
| 50 | + assertTrue(json.contains("org.hibernate.collection")); |
| 51 | + // should force loading... |
| 52 | + Set<Payment> payments = customer.getPayments(); |
| 53 | + /* |
54 | 54 | System.out.println("--- JSON ---"); |
55 | 55 | System.out.println(json); |
56 | 56 | System.out.println("--- /JSON ---"); |
57 | | - */ |
58 | | - |
59 | | - assertTrue(Hibernate.isInitialized(payments)); |
60 | | - // TODO: verify |
61 | | - assertNotNull(json); |
62 | | - |
63 | | - boolean exceptionThrown = false; |
64 | | - try { |
65 | | - /*Map<?, ?> stuff =*/ mapper.readValue(json, Map.class); |
66 | | - } catch (DatabindException e) { |
67 | | - exceptionThrown = true; |
68 | | - } |
69 | | - assertTrue(exceptionThrown); |
70 | | - } |
71 | | - |
72 | | - // [Issue#93], backwards compatible case |
73 | | - @Test |
74 | | - public void testReplacePersistentCollection() throws Exception { |
75 | | - final ObjectMapper mapper = hibernateMapper(new Hibernate4Module() |
76 | | - .configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, true) |
77 | | - .configure(Hibernate4Module.Feature.REPLACE_PERSISTENT_COLLECTIONS, true)); |
78 | | - |
79 | | - Customer customer = em.find(Customer.class, 103); |
80 | | - assertFalse(Hibernate.isInitialized(customer.getPayments())); |
81 | | - String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer); |
82 | | - assertFalse(json.contains("org.hibernate.collection")); |
83 | | - // should force loading... |
84 | | - Set<Payment> payments = customer.getPayments(); |
| 57 | + */ |
| 58 | + |
| 59 | + assertTrue(Hibernate.isInitialized(payments)); |
| 60 | + // TODO: verify |
| 61 | + assertNotNull(json); |
| 62 | + |
| 63 | + boolean exceptionThrown = false; |
| 64 | + try { |
| 65 | + /*Map<?, ?> stuff =*/ mapper.readValue(json, Map.class); |
| 66 | + } catch (DatabindException e) { |
| 67 | + exceptionThrown = true; |
| 68 | + } |
| 69 | + assertTrue(exceptionThrown); |
| 70 | + } |
| 71 | + |
| 72 | + // [Issue#93], backwards compatible case |
| 73 | + @Test |
| 74 | + public void testReplacePersistentCollection() throws Exception { |
| 75 | + final ObjectMapper mapper = hibernateMapper(new Hibernate4Module() |
| 76 | + .configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, true) |
| 77 | + .configure(Hibernate4Module.Feature.REPLACE_PERSISTENT_COLLECTIONS, true)); |
| 78 | + |
| 79 | + Customer customer = em.find(Customer.class, 103); |
| 80 | + assertFalse(Hibernate.isInitialized(customer.getPayments())); |
| 81 | + String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer); |
| 82 | + assertFalse(json.contains("org.hibernate.collection")); |
| 83 | + // should force loading... |
| 84 | + Set<Payment> payments = customer.getPayments(); |
85 | 85 | /* |
86 | 86 | System.out.println("--- JSON ---"); |
87 | 87 | System.out.println(json); |
88 | 88 | System.out.println("--- /JSON ---"); |
89 | 89 | */ |
90 | 90 |
|
91 | | - assertTrue(Hibernate.isInitialized(payments)); |
92 | | - // TODO: verify |
93 | | - assertNotNull(json); |
| 91 | + assertTrue(Hibernate.isInitialized(payments)); |
| 92 | + // TODO: verify |
| 93 | + assertNotNull(json); |
94 | 94 |
|
95 | 95 | /* |
96 | 96 | * Currently this cannot be verified due to Issue#94 default typing fails on 2.7.0 - 2.8.2-SNAPSHOT, |
97 | 97 | * commented out until that is fixed. |
98 | 98 | */ |
99 | 99 |
|
100 | | - boolean issue94failed = false; |
101 | | - try { |
102 | | - /* Map<?, ?> stuff =*/ mapper.readValue(json, Map.class); |
103 | | - } catch (DatabindException e) { |
104 | | - issue94failed = true; |
105 | | - } |
| 100 | + boolean issue94failed = false; |
| 101 | + try { |
| 102 | + /* Map<?, ?> stuff =*/ mapper.readValue(json, Map.class); |
| 103 | + } catch (DatabindException e) { |
| 104 | + issue94failed = true; |
| 105 | + } |
106 | 106 |
|
107 | | - assertTrue(issue94failed, "If this fails, means #94 is fixed. Replace to the below commented lines"); |
| 107 | + assertTrue(issue94failed, "If this fails, means #94 is fixed. Replace to the below commented lines"); |
108 | 108 |
|
109 | 109 | // Map<?, ?> stuff = mapper.readValue(json, Map.class); |
110 | 110 | // |
111 | 111 | // assertTrue(stuff.containsKey("payments")); |
112 | 112 | // assertTrue(stuff.containsKey("orders")); |
113 | 113 | // assertNull(stuff.get("orderes")); |
114 | | - } |
115 | | - |
116 | | - private ObjectMapper hibernateMapper(Hibernate4Module module) { |
117 | | - return JsonMapper.builder() |
118 | | - .addModule(module) |
119 | | - .activateDefaultTyping(new DefaultBaseTypeLimitingValidator(), DefaultTyping.NON_FINAL) |
120 | | - .build(); |
121 | | - } |
| 114 | + } |
| 115 | + |
| 116 | + private ObjectMapper hibernateMapper(Hibernate4Module module) { |
| 117 | + return JsonMapper.builder() |
| 118 | + .addModule(module) |
| 119 | + .activateDefaultTyping(new DefaultBaseTypeLimitingValidator(), DefaultTyping.NON_FINAL) |
| 120 | + .build(); |
| 121 | + } |
122 | 122 | } |
0 commit comments