Skip to content

Commit

Permalink
Merge branch 'master' into fw/environmentreporter
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart authored Aug 12, 2024
2 parents e0fbef2 + 2fd1953 commit 4b77319
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ If you're using Maven, add this to your pom file:
<dependency>
<groupId>com.approvaltests</groupId>
<artifactId>approvaltests</artifactId>
<version>24.2.0</version>
<version>24.3.0</version>
<scope>test</scope>
</dependency>
```
Expand All @@ -92,7 +92,7 @@ If you're using Maven, add this to your pom file:

```gradle
dependencies {
testImplementation("com.approvaltests:approvaltests:24.2.0")
testImplementation("com.approvaltests:approvaltests:24.3.0")
}
```

Expand Down
14 changes: 7 additions & 7 deletions approvaltests-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -81,7 +81,7 @@
<dependency>
<groupId>net.jqwik</groupId>
<artifactId>jqwik</artifactId>
<version>1.8.5</version>
<version>1.9.0</version>
</dependency>

<dependency>
Expand All @@ -97,12 +97,12 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.17.1</version>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.1</version>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
Expand All @@ -118,7 +118,7 @@
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
Expand All @@ -128,7 +128,7 @@
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.26.0</version>
<version>3.26.1</version>
</dependency>
</dependencies>

Expand All @@ -140,7 +140,7 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
<configuration>
<environmentVariables>
<APPROVAL_TESTS_USE_REPORTER>DiffReporter,QuietReporter</APPROVAL_TESTS_USE_REPORTER>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L291-L299) (Function7, Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L309-L317) (Function8, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L326-L333) (Function9, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L44-L47) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L39-L42) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L18-L21) (String, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L26-L30) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L53-L56) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L48-L51) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L17-L20) (String, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L35-L39) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L29-L34) (String, boolean, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L14-L17) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonXstreamApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonXstreamApprovals.java#L18-L21) (Object, $\color{#AAA}{\textsf{Options}}$)
* VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L16-L24) (ContextAware, $\color{#AAA}{\textsf{Options}}$)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.approvaltests;

import com.spun.util.Wrapper;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

public class JsonApprovalsTest
{
@Test
void nullDateTest()
{
LocalDateWrapper localDateWrapper = new LocalDateWrapper();
JsonApprovals.verifyAsJson(localDateWrapper, g -> g.serializeNulls());
}
private class LocalDateWrapper
{
public LocalDateTime getLocalDate()
{
return localDate;
}
public void setLocalDate(LocalDateTime localDate)
{
this.localDate = localDate;
}
private LocalDateTime localDate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"localDate": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ public void testJsonFieldOrdering()
JsonApprovals.verifyJson(JsonUtils.reorderFields(json1));
JsonApprovals.verifyJson(JsonUtils.reorderFields(json2));
}
@Test
public void testJsonFieldOrderingWithNulls()
{
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
String json1 = "{\"infos\":{\"address\":null,\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
JsonApprovals.verifyJson(json1, true, g -> g.serializeNulls());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"infos": {
"address": null,
"phone": "my phone"
},
"insurance": {
"forks": [
14,
53,
123
],
"prices": [
5,
8,
"3%"
]
}
}
2 changes: 1 addition & 1 deletion approvaltests-util-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,63 @@ public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
}
// end-snippet
}
class Step3
class Step2b
{
// begin-snippet: step3
// begin-snippet: step2_b
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
{
sendOutSeniorDiscounts(mailServer, database::getSeniorCustomers); // +
Loader<List<Customer>> seniorCustomerLoader = database::getSeniorCustomers;
List<Customer> seniorCustomers = seniorCustomerLoader.load();
// ...
// end-snippet
for (Customer customer : seniorCustomers)
{
Discount seniorDiscount = getSeniorDiscount();
String message = generateDiscountMessage(customer, seniorDiscount);
mailServer.sendMessage(customer, message);
}
}
}
class Step3a
{
// begin-snippet: step3_a
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
{
Loader<List<Customer>> seniorCustomerLoader = database::getSeniorCustomers;
sendOutSeniorDiscounts(mailServer, seniorCustomerLoader); // +
} // +
// +
public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader)
public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader) // +
{ // +
List<Customer> seniorCustomers = seniorCustomerLoader.load();
// ...
// end-snippet
for (Customer customer : seniorCustomers)
{
Discount seniorDiscount = getSeniorDiscount();
String message = generateDiscountMessage(customer, seniorDiscount);
mailServer.sendMessage(customer, message);
}
}
// begin-snippet: step0_b
@Test
public void senior_customer_list_includes_only_those_over_age_65()
{
Loader<List<Customer>> mailingList = () -> List.of(new Customer("Bob"), new Customer("Mary"),
new Customer("Tom"));
MailServer mailServer = initializeMailServer();
sendOutSeniorDiscounts(mailServer, mailingList);
Approvals.verifyAll("", mailServer.getRecipients());
}
// end-snippet
private MailServer initializeMailServer()
{
return new MailServer();
}
private DataBase initializeDatabase()
{
return null;
}
}
private String generateDiscountMessage(Customer customer, Discount seniorDiscount)
{
Expand Down
77 changes: 47 additions & 30 deletions approvaltests-util/docs/how_to/LoadersAndSavers.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ List.of(new Customer("Bob, Jones, 123 Elm St., Tempe, AZ, 14-MAR-1958"),
<!-- endSnippet -->

### Step 4: In the original method, replace the function call with a Loader
<!-- Snippet Compare: step1, step2 -->
<pre style="color: gray">
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
List&lt;Customer> seniorCustomers = <b style="color: red">database.getSeniorCustomers(); </b>
Expand All @@ -145,41 +146,57 @@ List.of(new Customer("Bob, Jones, 123 Elm St., Tempe, AZ, 14-MAR-1958"),

Step 5: Now we introduce the new loader function as a parameter to the original function. (If you use the IDE's refactoring tools to do this it will save a lot of effort).

```
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
&nbsp; List&lt;Customer&gt; seniorCustomers = ((Loader&lt;List<Customer&gt;>) () -> database.getSeniorCustomers()).load();
&nbsp; // ...
}
```

becomes

```
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer, Loader&lt;List<Customer&gt;> seniorCustomerLoader) {
&nbsp; List&lt;Customer&gt; seniorCustomers = seniorCustomerLoader.load();
&nbsp; // ...
}
```

Step 6: Update the calls to this function (including tests) to use the new Loader parameter.
<!-- Snippet Compare: step2_b, step3_a, "extract method" -->

public void senior_customer_list_includes_only_those_over_age_65() {

&nbsp; MailServer mailServer = // initialize server
<pre style="color: gray">
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
Loader&lt;List&lt;Customer>> seniorCustomerLoader = database::getSeniorCustomers;
List&lt;Customer> seniorCustomers = seniorCustomerLoader.load();
// ...
}
</pre>
# ⇓ extract method
<pre style="color: gray">
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
Loader&lt;List&lt;Customer>> seniorCustomerLoader = database::getSeniorCustomers;
<b style="color: green"> sendOutSeniorDiscounts(mailServer, seniorCustomerLoader); </b>
<b style="color: green"> } </b>
<b style="color: green"> </b>
<b style="color: green"> public void sendOutSeniorDiscounts(MailServer mailServer, Loader&lt;List&lt;Customer>> seniorCustomerLoader) { </b>
List&lt;Customer> seniorCustomers = seniorCustomerLoader.load();
// ...
}
</pre>

&nbsp; List&lt;Customer&gt; seniorCustomers = List.of(new Customer("Bob", "Jones", /\* ... /), / ... \*/);
Step 6: Update the unit tests to use the new Loader parameter.
We have now removed the reliance on the database to retrieve the data.
We still rely on a mail server to send the results.

&nbsp; sendOutSeniorDiscounts(null, mailServer, () -> seniorCustomers));
<!-- Snippet Compare: step0, step0_b -->

&nbsp; Approvals.verifyAll(mailServer.getRecipients());
<pre style="color: gray">
@Test
public void senior_customer_list_includes_only_those_over_age_65()
{
<b style="color: red"> DataBase database = initializeDatabase(); </b>
MailServer mailServer = initializeMailServer();
sendOutSeniorDiscounts( mailServer, <b style="color: red">database </b>);
Approvals.verifyAll("", mailServer.getRecipients());
}
</pre>
#
<pre style="color: gray">
@Test
public void senior_customer_list_includes_only_those_over_age_65()
{
<s style="color: red"> DataBase database = initializeDatabase(); </s>
<b style="color: green"> Loader&lt;List&lt;Customer>> mailingList = () -> List.of(new Customer("Bob"), new Customer("Mary"), new Customer("Tom")); </b>
MailServer mailServer = initializeMailServer();
sendOutSeniorDiscounts( mailServer, <s style="color: red">database </s> <b style="color: green">mailingList </b>);
Approvals.verifyAll("", mailServer.getRecipients());
}
</pre>

}

Step 7: Now we can remove the DataBase as a parameter altogether.

Expand Down
2 changes: 1 addition & 1 deletion approvaltests-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<version>3.16.0</version>
</dependency>

</dependencies>
Expand Down
15 changes: 13 additions & 2 deletions approvaltests-util/src/main/java/com/spun/util/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ private static GsonBuilder addHandlingForDateObjects(GsonBuilder builder)
return builder;
}
public static String reorderFields(String json)
{
return reorderFields(json, g -> g);
}
public static String reorderFields(String json, Function1<GsonBuilder, GsonBuilder> gsonBuilder)
{
JsonObject sortedJsonObject = sortJsonObject(json);
return asJson(sortedJsonObject);
return asJson(sortedJsonObject, gsonBuilder);
}
public static JsonObject sortJsonObject(String json)
{
Expand Down Expand Up @@ -112,7 +116,14 @@ public static class LocalDateTimeAdapter extends TypeAdapter<LocalDateTime>
@Override
public void write(JsonWriter jsonWriter, LocalDateTime instant) throws IOException
{
jsonWriter.value(instant.toString());
if (instant == null)
{
jsonWriter.nullValue();
}
else
{
jsonWriter.value("" + instant);
}
}
@Override
public LocalDateTime read(JsonReader jsonReader) throws IOException
Expand Down
2 changes: 1 addition & 1 deletion approvaltests/docs/how_to/CreateCustomizedVerifyMethods.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void verifyAsJson(Object o, Options options)
Approvals.verify(JsonUtils.asJson(o), options.forFile().withExtension(".json"));
}
```
<sup><a href='/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L43-L48' title='Snippet source file'>snippet source</a> | <a href='#snippet-verify_as_json' title='Start of snippet'>anchor</a></sup>
<sup><a href='/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L52-L57' title='Snippet source file'>snippet source</a> | <a href='#snippet-verify_as_json' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Create a `Verifiable` Object
Expand Down
9 changes: 5 additions & 4 deletions approvaltests/docs/reference/Verify.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ Here is a list:
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L291-L299) (Function7, Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L309-L317) (Function8, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L326-L333) (Function9, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L44-L47) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L39-L42) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L18-L21) (String, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L26-L30) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L53-L56) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L48-L51) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L17-L20) (String, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L35-L39) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L29-L34) (String, boolean, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L14-L17) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonXstreamApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonXstreamApprovals.java#L18-L21) (Object, $\color{#AAA}{\textsf{Options}}$)
* VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L16-L24) (ContextAware, $\color{#AAA}{\textsf{Options}}$) <!-- endInclude -->
Expand Down
Loading

0 comments on commit 4b77319

Please sign in to comment.