Skip to content

Commit

Permalink
Update tests for jUnit 4.x
Browse files Browse the repository at this point in the history
Introduce use of assertThrows() from jUnit 4.x

Clean up exception declarations in tests
  • Loading branch information
rhowe authored and spullara committed Feb 26, 2024
1 parent 01054a7 commit 9896a26
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import org.junit.Test;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class AbstractClassTest {
static abstract class AbstractFoo {
public abstract String getValue();
Expand Down Expand Up @@ -42,12 +44,13 @@ public void testAbstractClass() throws IOException {
containers.add(new Container(new Foo()));
containers.add(new Container(new Bar()));
HashMap<String, Object> scopes = new HashMap<>();
Writer writer = new OutputStreamWriter(System.out);
Writer writer = new StringWriter();
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(new StringReader("{{#containers}} {{foo.value}} {{/containers}}"), "example");
scopes.put("containers", containers);
mustache.execute(writer, scopes);
writer.flush();
assertEquals(" I am Foo I am Bar ", writer.toString());
}

@Test
Expand All @@ -56,11 +59,12 @@ public void testAbstractClassNoDots() throws IOException {
containers.add(new Container(new Foo()));
containers.add(new Container(new Bar()));
HashMap<String, Object> scopes = new HashMap<>();
Writer writer = new OutputStreamWriter(System.out);
Writer writer = new StringWriter();
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(new StringReader("{{#containers}} {{#foo}}{{value}}{{/foo}} {{/containers}}"), "example");
scopes.put("containers", containers);
mustache.execute(writer, scopes);
writer.flush();
assertEquals(" I am Foo I am Bar ", writer.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

/**
* Shows a simple way to add indexes for arrays.
Expand Down Expand Up @@ -66,8 +66,8 @@ public Object coerce(final Object object) {
}

@Test
public void testThrowMustacheExceptionInCaseOfNullHandler() throws Exception {
try {
public void testThrowMustacheExceptionInCaseOfNullHandler() {
MustacheException expected = assertThrows(MustacheException.class, () -> {
String template = "<ol>\n" +
" <li>{{test.1}}</li>\n" +
" <li>{{test.0}}</li>\n" +
Expand All @@ -79,16 +79,14 @@ public void testThrowMustacheExceptionInCaseOfNullHandler() throws Exception {
"{{/test}}\n" +
"</ol>";
Object scope = new Object() {
String[] test = new String[]{ "a", "b", "c", "d" };
String[] test = new String[]{"a", "b", "c", "d"};
};
DefaultMustacheFactory mf = new DefaultMustacheFactory();
mf.setObjectHandler(null);
Mustache m = mf.compile(new StringReader(template), "template");
m.execute(new StringWriter(), scope).flush();
fail("should have thrown MustacheException");
} catch (MustacheException expected) {
assertEquals("Failed to get value for test.1 @[template:2]", expected.getMessage());
}
});
assertEquals("Failed to get value for test.1 @[template:2]", expected.getMessage());
}

private static class ArrayMap extends AbstractMap<Object, Object> implements Iterable<Object> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import static com.github.mustachejavabenchmarks.BenchmarkTest.skip;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;

/**
* Inspired by an unconfirmed bug report.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.List;
import java.util.function.Function;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;

public class ConvertMethodsToFunctionsTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import com.github.mustachejava.util.Wrapper;
import org.junit.Test;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

public class FailOnMissingTest {
@Test
Expand All @@ -34,19 +33,15 @@ protected synchronized Wrapper getWrapper(String name, List<Object> scopes) {
};
DefaultMustacheFactory dmf = new DefaultMustacheFactory();
dmf.setObjectHandler(roh);
try {
MustacheException me = assertThrows(MustacheException.class, () -> {
Mustache test = dmf.compile(new StringReader("{{test}}"), "test");
StringWriter sw = new StringWriter();
test.execute(sw, new Object() {
String test = "ok";
}).close();
assertEquals("ok", sw.toString());
test.execute(new StringWriter(), new Object());
fail("Should have failed");
} catch (MustacheException me) {
assertEquals("test not found in [test:1] @[test:1]", me.getCause().getMessage());
} catch (IOException e) {
e.printStackTrace();
}
});
assertEquals("test not found in [test:1] @[test:1]", me.getCause().getMessage());
}
}
17 changes: 7 additions & 10 deletions compiler/src/test/java/com/github/mustachejava/FallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import static com.github.mustachejava.TestUtil.getContents;
import static junit.framework.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class FallbackTest {

private static File rootDefault;
private static File rootOverride;

@Test
public void testDefaultPage1() throws MustacheException, IOException, ExecutionException, InterruptedException {
public void testDefaultPage1() throws MustacheException, IOException {
MustacheFactory c = new FallbackMustacheFactory(rootDefault, rootDefault); // no override
Mustache m = c.compile("page1.html");
StringWriter sw = new StringWriter();
Expand All @@ -29,7 +28,7 @@ public void testDefaultPage1() throws MustacheException, IOException, ExecutionE
}

@Test
public void testOverridePage1() throws MustacheException, IOException, ExecutionException, InterruptedException {
public void testOverridePage1() throws MustacheException, IOException {
MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault);
Mustache m = c.compile("page1.html");
StringWriter sw = new StringWriter();
Expand All @@ -40,7 +39,7 @@ public void testOverridePage1() throws MustacheException, IOException, Execution
}

@Test
public void testOverridePage2() throws MustacheException, IOException, ExecutionException, InterruptedException {
public void testOverridePage2() throws MustacheException, IOException {
MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault);
Mustache m = c.compile("page2.html");
StringWriter sw = new StringWriter();
Expand All @@ -53,13 +52,11 @@ public void testOverridePage2() throws MustacheException, IOException, Execution
@Test
public void testMustacheNotFoundException() {
String nonExistingMustache = "404";
try {
MustacheNotFoundException e = assertThrows(MustacheNotFoundException.class, () -> {
MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault);
c.compile(nonExistingMustache);
fail("Didn't throw an exception");
} catch (MustacheNotFoundException e) {
assertEquals(nonExistingMustache, e.getName());
}
});
assertEquals(nonExistingMustache, e.getName());
}

@BeforeClass
Expand Down
Loading

0 comments on commit 9896a26

Please sign in to comment.