Skip to content

Commit

Permalink
Allow setting project directory
Browse files Browse the repository at this point in the history
  • Loading branch information
trickl committed Oct 5, 2019
1 parent 3dcf4d6 commit e9c6851
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.trickl</groupId>
<artifactId>assertj-json-serialize</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.1.6-SNAPSHOT</version>
<packaging>jar</packaging>
<developers>
<developer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;

import org.assertj.core.api.AbstractAssert;

/**
Expand All @@ -36,6 +38,8 @@ public abstract class AbstractJsonObjectAssert<S extends AbstractJsonObjectAsser

private boolean createExpectedIfAbsent = true;

private String projectDir = null;

public AbstractJsonObjectAssert(JsonObject actual, Class<?> selfType) {
super(actual, selfType);
}
Expand Down Expand Up @@ -157,6 +161,11 @@ public S usingObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return myself;
}

public S usingProjectDirectory(String projectDir) {
this.projectDir = projectDir;
return myself;
}

public S doNotCreateExpectedIfAbsent() {
createExpectedIfAbsent = false;
Expand Down Expand Up @@ -197,18 +206,35 @@ private JsonContainer safeJson(Path path) {
}
}

private URL classAsResourceUrlConvention(Class clazz, String extension) {
private URL classAsResourceUrlConvention(Class<?> clazz, String extension) {
String resourceName = clazz.getSimpleName() + extension;
return clazz.getResource(resourceName);
}

private Path classAsResourcePathConvention(Class clazz, String extension) {
String resourcePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
String projectDir = resourcePath.substring(0, resourcePath.indexOf("target"));
private Path classAsResourcePathConvention(Class<?> clazz, String extension) {
String projectDirectory = projectDir;
if (projectDirectory == null) {
projectDirectory = getProjectDirectoryFromLocalClazz(clazz);
}
if (projectDirectory == null) {
throw new RuntimeException(
"Project directory must be set explicity when using a non-local class.");
}
return Paths.get(
projectDir,
projectDirectory,
"src/test/resources/",
clazz.getPackage().getName().replaceAll("\\.", "/"),
clazz.getSimpleName() + extension);
}

private String getProjectDirectoryFromLocalClazz(Class<?> clazz) {
CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
if (codeSource != null) {
String resourcePath = codeSource.getLocation().getPath();
if (resourcePath.indexOf("target") >= 0) {
return resourcePath.substring(0, resourcePath.indexOf("target"));
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.trickl.assertj.core.api.json.JsonAssert;
import static com.trickl.assertj.core.api.JsonObjectAssertions.assertThat;

import java.util.AbstractMap;
import java.util.Map;

import com.trickl.assertj.examples.Example;
import org.junit.Test;

Expand All @@ -24,31 +28,44 @@ protected void verify_internal_effects() {
@Test
public void should_pass_on_deserialization_with_error() {
Example example = new Example();
assertThat(example)
assertThat(example)
.deserializesWithoutError();
}

@Test
public void should_pass_on_deserialization_equality() {
Example example = new Example();
example.setMyField("abc");
assertThat(example)
assertThat(example)
.deserializesAsExpected();
}

@Test
public void should_pass_on_serialization_match() {
Example example = new Example();
example.setMyField("abc");
assertThat(example)
assertThat(example)
.serializesAsExpected();
}

@Test
public void should_allow_explicit_project_directory() {
Example example = new Example();
String testPath = this.getClass().getProtectionDomain()
.getCodeSource().getLocation().getPath();
String projectDirectory = testPath.substring(0, testPath.indexOf("target"))
+ "/src/test/resources/project_dir";
example.setMyField("abc");
assertThat(example)
.usingProjectDirectory(projectDirectory)
.serializesAsExpected();
}

@Test
public void should_pass_on_schema_match() {
Example example = new Example();
example.setMyField("abc");
assertThat(example)
assertThat(example)
.schemaAsExpected();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"my-field": "abc"
}

0 comments on commit e9c6851

Please sign in to comment.