Skip to content

Commit ecb086d

Browse files
committed
Add failing test for #41
1 parent 4807f7f commit ecb086d

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

hibernate3/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Hibernate (http://hibernate.org) version 3.x data types.
6060
<dependency>
6161
<groupId>com.fasterxml.jackson.core</groupId>
6262
<artifactId>jackson-annotations</artifactId>
63-
<version>${version.jackson.annotations}</version>
6463
<scope>test</scope>
6564
</dependency>
6665
<dependency>

hibernate4/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ Hibernate (http://hibernate.org) version 4.x data types.
5959
<dependency>
6060
<groupId>com.fasterxml.jackson.core</groupId>
6161
<artifactId>jackson-annotations</artifactId>
62-
<version>${version.jackson.annotations}</version>
6362
<scope>test</scope>
6463
</dependency>
6564
<dependency>

hibernate5/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,16 @@ Hibernate (http://hibernate.org) version 5.x data types.
6464
<version>4.8.2</version>
6565
<scope>test</scope>
6666
</dependency>
67+
<!-- and for some contributed tests Mockito -->
68+
<dependency>
69+
<groupId>org.mockito</groupId>
70+
<artifactId>mockito-core</artifactId>
71+
<version>1.10.19</version>
72+
<scope>test</scope>
73+
</dependency>
6774
<dependency>
6875
<groupId>com.fasterxml.jackson.core</groupId>
6976
<artifactId>jackson-annotations</artifactId>
70-
<version>${version.jackson.annotations}</version>
7177
<scope>test</scope>
7278
</dependency>
7379
<dependency>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.fasterxml.jackson.datatype.hibernate5.failing;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
import org.mockito.stubbing.Answer;
7+
import org.mockito.invocation.InvocationOnMock;
8+
9+
import static org.mockito.Mockito.mock;
10+
import static org.mockito.Mockito.when;
11+
12+
import org.hibernate.proxy.HibernateProxy;
13+
import org.hibernate.proxy.LazyInitializer;
14+
15+
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
16+
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.fasterxml.jackson.datatype.hibernate5.BaseTest;
20+
21+
/**
22+
* Problem with handling of Object Id, [datatype-hibernate#41]
23+
*/
24+
public class Issue41Test extends BaseTest
25+
{
26+
@Entity
27+
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "id")
28+
public static class SomeJPAEntity {
29+
@Id
30+
public String id;
31+
32+
// @ManyToOne not needed because we mock this
33+
public SomeJPAEntity owner;
34+
}
35+
36+
public static class EntityHibernateProxy extends SomeJPAEntity implements HibernateProxy
37+
{
38+
private static final long serialVersionUID = 1L;
39+
40+
private LazyInitializer lazyInitializer;
41+
42+
public EntityHibernateProxy() {
43+
this(true);
44+
}
45+
46+
public EntityHibernateProxy(final boolean uninitialized) {
47+
lazyInitializer = mock(LazyInitializer.class);
48+
when(lazyInitializer.isUninitialized()).thenReturn(uninitialized);
49+
final String currId = "d0024148-3040-4fee-a78a-e94fd2449ac6";
50+
when(lazyInitializer.getIdentifier()).thenReturn(currId);
51+
when(lazyInitializer.getEntityName()).thenReturn("someJPAEntity");
52+
53+
when(lazyInitializer.getImplementation()).thenAnswer(new Answer<Object>() {
54+
55+
@Override
56+
public Object answer(InvocationOnMock invocation) throws Throwable {
57+
SomeJPAEntity entity = new SomeJPAEntity();
58+
entity.id = id;
59+
entity.owner = new EntityHibernateProxy(false);
60+
return entity;
61+
}});
62+
}
63+
64+
@Override
65+
public Object writeReplace() {
66+
return this;
67+
}
68+
69+
@Override
70+
public LazyInitializer getHibernateLazyInitializer() {
71+
return lazyInitializer;
72+
}
73+
}
74+
75+
public void testIssue41() throws Exception
76+
{
77+
EntityHibernateProxy entity = new EntityHibernateProxy(false);
78+
entity.id = "3cf7a573-f528-440c-83b9-873d7594b373";
79+
entity.owner = entity;
80+
81+
ObjectMapper mapper = mapperWithModule(true);
82+
String json = mapper.writeValueAsString(entity);
83+
84+
// will throw an exception long before here so this should suffice:
85+
assertNotNull(json);
86+
}
87+
}

0 commit comments

Comments
 (0)