Skip to content

Commit da9f3c6

Browse files
committed
changes to the test methods and bean classes
1 parent 212596c commit da9f3c6

6 files changed

Lines changed: 163 additions & 75 deletions

File tree

‎jackson/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<dependency>
6161
<groupId>com.fasterxml.jackson.module</groupId>
6262
<artifactId>jackson-module-jsonSchema</artifactId>
63-
<version>2.6.0</version>
63+
<version>2.7.2</version>
6464
</dependency>
6565

6666
<dependency>

‎jackson/src/test/java/com/baeldung/jackson/annotation/extra/AppendBean.java‎

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.jackson.annotation.extra;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonAppend;
4+
5+
public class AppendBeans {
6+
public static class BeanWithoutAppend {
7+
private int id;
8+
private String name;
9+
10+
public BeanWithoutAppend(int id, String name) {
11+
this.id = id;
12+
this.name = name;
13+
}
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
}
31+
32+
@JsonAppend(attrs = { @JsonAppend.Attr(value = "version") })
33+
public static class BeanWithAppend {
34+
private int id;
35+
private String name;
36+
37+
public BeanWithAppend(int id, String name) {
38+
this.id = id;
39+
this.name = name;
40+
}
41+
42+
public int getId() {
43+
return id;
44+
}
45+
46+
public void setId(int id) {
47+
this.id = id;
48+
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public void setName(String name) {
55+
this.name = name;
56+
}
57+
}
58+
}

‎jackson/src/test/java/com/baeldung/jackson/annotation/extra/ExtraAnnotationTest.java‎

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,77 @@
11
package com.baeldung.jackson.annotation.extra;
22

3-
import static org.junit.Assert.*;
43
import static org.hamcrest.CoreMatchers.containsString;
54
import static org.hamcrest.CoreMatchers.instanceOf;
5+
import static org.hamcrest.CoreMatchers.not;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertThat;
8+
9+
import org.junit.Test;
610

711
import java.io.IOException;
812
import java.util.ArrayList;
913
import java.util.List;
1014

11-
import org.junit.Test;
12-
15+
import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithAppend;
16+
import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithoutAppend;
17+
import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithIdentityReference;
18+
import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithoutIdentityReference;
1319
import com.fasterxml.jackson.core.JsonProcessingException;
1420
import com.fasterxml.jackson.databind.ObjectMapper;
1521
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
22+
import com.fasterxml.jackson.databind.ObjectWriter;
1623
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
1724
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
1825

1926
public class ExtraAnnotationTest {
27+
@Test
28+
public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
29+
ObjectMapper mapper = new ObjectMapper();
30+
BeanWithoutIdentityReference bean = new BeanWithoutIdentityReference(1, "Bean Without Identity Reference Annotation");
31+
String jsonString = mapper.writeValueAsString(bean);
32+
33+
assertThat(jsonString, containsString("Bean Without Identity Reference Annotation"));
34+
}
35+
2036
@Test
2137
public void whenUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
2238
ObjectMapper mapper = new ObjectMapper();
23-
IdentityReferenceBean bean = new IdentityReferenceBean(1, "Identity Reference Bean");
39+
BeanWithIdentityReference bean = new BeanWithIdentityReference(1, "Bean With Identity Reference Annotation");
2440
String jsonString = mapper.writeValueAsString(bean);
2541

2642
assertEquals("1", jsonString);
2743
}
2844

45+
@Test
46+
public void whenNotUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException {
47+
ObjectMapper mapper = new ObjectMapper();
48+
49+
BeanWithoutAppend bean = new BeanWithoutAppend(2, "Bean Without Append Annotation");
50+
ObjectWriter writer = mapper.writerFor(BeanWithoutAppend.class).withAttribute("version", "1.0");
51+
String jsonString = writer.writeValueAsString(bean);
52+
53+
assertThat(jsonString, not(containsString("version")));
54+
assertThat(jsonString, not(containsString("1.0")));
55+
}
56+
2957
@Test
3058
public void whenUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException {
3159
ObjectMapper mapper = new ObjectMapper();
32-
AppendBean bean = new AppendBean(2, "Append Bean");
33-
String jsonString = mapper.writeValueAsString(bean);
3460

35-
assertThat(jsonString, containsString("appendedProperty"));
61+
BeanWithAppend bean = new BeanWithAppend(2, "Bean With Append Annotation");
62+
ObjectWriter writer = mapper.writerFor(BeanWithAppend.class).withAttribute("version", "1.0");
63+
String jsonString = writer.writeValueAsString(bean);
64+
65+
assertThat(jsonString, containsString("version"));
66+
assertThat(jsonString, containsString("1.0"));
3667
}
3768

3869
@Test
3970
public void whenUsingJsonNamingAnnotation_thenCorrect() throws JsonProcessingException {
4071
ObjectMapper mapper = new ObjectMapper();
4172
NamingBean bean = new NamingBean(3, "Naming Bean");
4273
String jsonString = mapper.writeValueAsString(bean);
43-
74+
4475
assertThat(jsonString, containsString("bean_name"));
4576
}
4677

@@ -51,7 +82,7 @@ public void whenUsingJsonPropertyDescriptionAnnotation_thenCorrect() throws Json
5182
mapper.acceptJsonFormatVisitor(PropertyDescriptionBean.class, wrapper);
5283
JsonSchema jsonSchema = wrapper.finalSchema();
5384
String jsonString = mapper.writeValueAsString(jsonSchema);
54-
85+
System.out.println(jsonString);
5586
assertThat(jsonString, containsString("This is a description of the name property"));
5687
}
5788

@@ -86,7 +117,7 @@ public void whenUsingJsonTypeIdResolverAnnotation_thenCorrect() throws IOExcepti
86117

87118
TypeIdResolverStructure.BeanContainer serializedContainer = new TypeIdResolverStructure.BeanContainer();
88119
serializedContainer.setBeans(beans);
89-
120+
90121
ObjectMapper mapper = new ObjectMapper();
91122
String jsonString = mapper.writeValueAsString(serializedContainer);
92123
assertThat(jsonString, containsString("bean1"));

‎jackson/src/test/java/com/baeldung/jackson/annotation/extra/IdentityReferenceBean.java‎

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.jackson.annotation.extra;
2+
3+
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
4+
import com.fasterxml.jackson.annotation.JsonIdentityReference;
5+
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
6+
7+
8+
public class IdentityReferenceBeans {
9+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
10+
public static class BeanWithoutIdentityReference {
11+
private int id;
12+
private String name;
13+
14+
public BeanWithoutIdentityReference(int id, String name) {
15+
this.id = id;
16+
this.name = name;
17+
}
18+
19+
public int getId() {
20+
return id;
21+
}
22+
23+
public void setId(int id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
}
35+
36+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
37+
@JsonIdentityReference(alwaysAsId = true)
38+
public static class BeanWithIdentityReference {
39+
private int id;
40+
private String name;
41+
42+
public BeanWithIdentityReference(int id, String name) {
43+
this.id = id;
44+
this.name = name;
45+
}
46+
47+
public int getId() {
48+
return id;
49+
}
50+
51+
public void setId(int id) {
52+
this.id = id;
53+
}
54+
55+
public String getName() {
56+
return name;
57+
}
58+
59+
public void setName(String name) {
60+
this.name = name;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)