Skip to content

Commit

Permalink
Increase unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Nov 9, 2018
1 parent d00f7cc commit d9093cb
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept


/**
* Resource based java source
* Url based java source
*/
public static class JavaSourceFromUrl extends SimpleJavaFileObject {

Expand Down Expand Up @@ -138,6 +138,11 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
* @return
*/
public static SimpleJavaFileObject readFromResource(String location, Class<?> relativeLocationRoot) {

if (location == null) {
throw new IllegalArgumentException("Passed location must not be null");
}

return new JavaSourceFromResource(location, relativeLocationRoot);
}

Expand All @@ -149,6 +154,11 @@ public static SimpleJavaFileObject readFromResource(String location, Class<?> re
* @return
*/
public static SimpleJavaFileObject readFromResource(String location) {

if (location == null) {
throw new IllegalArgumentException("Passed location must not be null");
}

return new JavaSourceFromResource((!location.startsWith("/") ? "/" : "") + location, null);
}

Expand All @@ -161,6 +171,15 @@ public static SimpleJavaFileObject readFromResource(String location) {
* @return
*/
public static SimpleJavaFileObject readFromString(String location, String content) {

if (location == null) {
throw new IllegalArgumentException("Passed location must not be null");
}

if (content == null) {
throw new IllegalArgumentException("Passed content must not be null");
}

return new JavaSourceFromString(location, content);
}

Expand All @@ -171,6 +190,11 @@ public static SimpleJavaFileObject readFromString(String location, String conten
* @return
*/
public static SimpleJavaFileObject readFromUrl(URL url) throws URISyntaxException {

if (url == null) {
throw new IllegalArgumentException("Passed url must not be null");
}

return new JavaSourceFromUrl(url);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package io.toolisticon.annotationprocessortoolkit.testhelper.compiletest;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

import javax.tools.JavaFileObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;

/**
* Unit test for {@link JavaFileObjectUtils}.
*/
public class JavaFileObjectUtilsTest {

// ------------------------------------------------
// -- test JavaSourceFromString
// ------------------------------------------------

@Test
public void test_JavaSourceFromString_getCharContent() throws IOException {

final String content = "test";

JavaFileObject fileObject = JavaFileObjectUtils.readFromString("abc", content);

MatcherAssert.assertThat((String) fileObject.getCharContent(false), Matchers.is(content));

}

@Test
public void test_JavaSourceFromString_openReader() throws IOException {

final String content = "test";

JavaFileObject fileObject = JavaFileObjectUtils.readFromString("abc", content);

BufferedReader bufferedReader = new BufferedReader(fileObject.openReader(false));

MatcherAssert.assertThat(bufferedReader.readLine(), Matchers.is(content));
}

@Test
public void test_JavaSourceFromString_openInputStream() throws IOException {

final String content = "test";

JavaFileObject fileObject = JavaFileObjectUtils.readFromString("abc", content);

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileObject.openInputStream()));

MatcherAssert.assertThat(bufferedReader.readLine(), Matchers.is(content));
}

// ------------------------------------------------
// -- test JavaSourceFromResource
// ------------------------------------------------

@Test
public void test_JavaSourceFromResource_getCharContent() throws IOException {

final String content = "package io.toolisticon.annotationprocessortoolkit.testhelper.compiletest;";

JavaFileObject fileObject = JavaFileObjectUtils.readFromResource("/compiletests/javafileobjectutilstest/JavaSourceFromResourceTestClass.java");

// check first line
MatcherAssert.assertThat((String) fileObject.getCharContent(false), Matchers.containsString(content));

}

@Test
public void test_JavaSourceFromResource_openReader() throws IOException {

final String content = "package io.toolisticon.annotationprocessortoolkit.testhelper.compiletest;";

JavaFileObject fileObject = JavaFileObjectUtils.readFromResource("/compiletests/javafileobjectutilstest/JavaSourceFromResourceTestClass.java");

BufferedReader bufferedReader = new BufferedReader(fileObject.openReader(false));

// check first line
MatcherAssert.assertThat(bufferedReader.readLine(), Matchers.is(content));
}

@Test
public void test_JavaSourceFromResource_openInputStream() throws IOException {

final String content = "package io.toolisticon.annotationprocessortoolkit.testhelper.compiletest;";

JavaFileObject fileObject = JavaFileObjectUtils.readFromResource("/compiletests/javafileobjectutilstest/JavaSourceFromResourceTestClass.java");

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileObject.openInputStream()));

// check first line
MatcherAssert.assertThat(bufferedReader.readLine(), Matchers.is(content));
}

// ------------------------------------------------
// -- test JavaSourceFromResource
// ------------------------------------------------

@Test
public void test_JavaSourceFromUrl_getCharContent() throws IOException, URISyntaxException {

final String content = "package io.toolisticon.annotationprocessortoolkit.testhelper.compiletest;";


URL url = getClass().getResource("/compiletests/javafileobjectutilstest/JavaSourceFromResourceTestClass.java");

JavaFileObject fileObject = JavaFileObjectUtils.readFromUrl(url);

// check first line
MatcherAssert.assertThat((String) fileObject.getCharContent(false), Matchers.containsString(content));

}

@Test
public void test_JavaSourceFromUrl_openReader() throws IOException, URISyntaxException {

final String content = "package io.toolisticon.annotationprocessortoolkit.testhelper.compiletest;";

URL url = getClass().getResource("/compiletests/javafileobjectutilstest/JavaSourceFromResourceTestClass.java");


JavaFileObject fileObject = JavaFileObjectUtils.readFromUrl(url);

BufferedReader bufferedReader = new BufferedReader(fileObject.openReader(false));

// check first line
MatcherAssert.assertThat(bufferedReader.readLine(), Matchers.is(content));
}

@Test
public void test_JavaSourceFromUrl_openInputStream() throws IOException, URISyntaxException {

final String content = "package io.toolisticon.annotationprocessortoolkit.testhelper.compiletest;";

URL url = getClass().getResource("/compiletests/javafileobjectutilstest/JavaSourceFromResourceTestClass.java");

JavaFileObject fileObject = JavaFileObjectUtils.readFromUrl(url);

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileObject.openInputStream()));

// check first line
MatcherAssert.assertThat(bufferedReader.readLine(), Matchers.is(content));
}

// ------------------------------------------------
// -- test null safety of static accessors
// ------------------------------------------------

@Test(expected = IllegalArgumentException.class)
public void test_nullSafety_readFromResource() {
JavaFileObject fileObject = JavaFileObjectUtils.readFromResource(null);
}

@Test(expected = IllegalArgumentException.class)
public void test_nullSafety_readFromResource_withLocationAnd() {
JavaFileObject fileObject = JavaFileObjectUtils.readFromResource(null, this.getClass());
}

@Test(expected = IllegalArgumentException.class)
public void test_nullSafety_readFromUrl() throws URISyntaxException {
JavaFileObject fileObject = JavaFileObjectUtils.readFromUrl(null);
}

@Test(expected = IllegalArgumentException.class)
public void test_nullSafety_readFromString_locationIsNull() {
JavaFileObject fileObject = JavaFileObjectUtils.readFromString(null, "TEST");
}

@Test(expected = IllegalArgumentException.class)
public void test_nullSafety_readFromString_contentIsNull() {
JavaFileObject fileObject = JavaFileObjectUtils.readFromString("TEST", null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.toolisticon.annotationprocessortoolkit.testhelper.compiletest;

public class JavaSourceFromResourceTestClass {



}

0 comments on commit d9093cb

Please sign in to comment.