Skip to content

Commit

Permalink
F!! added support for using jsonapprovals with jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Jul 18, 2022
1 parent 08f336e commit 462a641
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 7 deletions.
16 changes: 16 additions & 0 deletions approvaltests-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ else if (simpleName.equals("JsonApprovals"))
{
modifier = 2;
}
else if (simpleName.equals("JsonJacksonApprovals"))
{
modifier = -2;
}
else if (simpleName.equals("VelocityApprovals"))
{
modifier = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L25-L28) (Object)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L29-L32) (Object,Function1)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L17-L20) (String)
* JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L10-L13) (Object)
* VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L16-L19) (ContextAware)
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package org.approvaltests;

import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.approvaltests.reporters.UseReporter;
import org.approvaltests.reporters.macosx.DiffMergeReporter;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.Instant;

public class JsonFormattingTest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.approvaltests;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.junit.jupiter.api.Test;

public class JsonJacksonApprovalsTest
{
@Test
void testDuplicateFields()
{
JsonJacksonApprovals.verifyAsJson(new MyOtherClass());
}
private static class MyClass
{
@JsonIgnore
private String name = "MyClass";
public String lastName = "MyClass";
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
private static class MyOtherClass extends MyClass
{
@JsonIgnore
private String name = "MyOtherClass";
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"lastName" : "MyClass"
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void testEachMethodHasOneWithOptions()
public static List<Class<?>> getApprovalClasses()
{
return Arrays.asList(Approvals.class, CombinationApprovals.class, AwtApprovals.class, JsonApprovals.class,
VelocityApprovals.class);
VelocityApprovals.class, JsonJacksonApprovals.class);
}
private void verifyEachVerifyMethodHasOneWithOptions(Class<?> approvalsClass)
{
Expand Down
6 changes: 6 additions & 0 deletions approvaltests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.approvaltests;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spun.util.ObjectUtils;
import org.approvaltests.core.Options;

public class JsonJacksonApprovals
{
public static void verifyAsJson(Object o)
{
verifyAsJson(o, new Options());
}
public static void verifyAsJson(Object o, Options options)
{
Approvals.verify(asJson(o), options.forFile().withExtension(".json"));
}
public static String asJson(Object o)
{
try
{
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
}
catch (JsonProcessingException e)
{
throw ObjectUtils.throwAsError(e);
}
}
}

0 comments on commit 462a641

Please sign in to comment.