Skip to content

Commit

Permalink
Don't use JUnit Parameterized
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Mar 15, 2023
1 parent f49591d commit 8f73505
Showing 1 changed file with 71 additions and 74 deletions.
145 changes: 71 additions & 74 deletions src/test/java/org/javarosa/xpath/expr/Base64DecodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,92 +17,65 @@
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.Arrays;
import org.javarosa.core.test.Scenario;
import org.javarosa.xform.parse.XFormParser;
import org.javarosa.xpath.XPathUnhandledException;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Enclosed.class)
public class Base64DecodeTest {

@RunWith(Parameterized.class)
public static class ValidValuesTest {
@Parameterized.Parameter(value = 0)
public String testName;

@Parameterized.Parameter(value = 1)
public String source;

@Parameterized.Parameter(value = 2)
public String expectedOutput;
@Test
public void asciiString_isSuccessfullyDecoded() throws IOException, XFormParser.ParseException {
Scenario scenario = getBase64DecodeScenario("ASCII string", "SGVsbG8=");
assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer("Hello")));
}

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{
{"ASCII string", "SGVsbG8=", "Hello"},
{"Example from Saxonica", "RGFzc2Vs", "Dassel"},
{"String with accented characters", "w6nDqMOx", "éèñ"},
{"String with emoji", "8J+lsA==", "🥰"},
{"UTF-16 encoded string", "AGEAYgBj", "\u0000a\u0000b\u0000c"}, // source string: "abc" in UTF-16
});
}
@Test
public void exampleFromSaxonica_isSuccessfullyDecoded() throws IOException, XFormParser.ParseException {
Scenario scenario = getBase64DecodeScenario("Example from Saxonica", "RGFzc2Vs");
assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer("Dassel")));
}

@Test
public void base64DecodeFunction_acceptsDynamicParameters() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init(testName, html(
head(
title(testName),
model(
mainInstance(t("data id=\"base64\"",
t("text", source),
t("decoded")
)),
bind("/data/text").type("string"),
bind("/data/decoded").type("string").calculate("base64-decode(/data/text)")
)
),
body(
input("/data/text")
))
);
@Test
public void accentString_isSuccessfullyDecoded() throws IOException, XFormParser.ParseException {
Scenario scenario = getBase64DecodeScenario("String with accented characters", "w6nDqMOx");
assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer("éèñ")));
}

assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer(expectedOutput)));
}
@Test
public void emojiString_isSuccessfullyDecoded() throws IOException, XFormParser.ParseException {
Scenario scenario = getBase64DecodeScenario("String with emoji", "8J+lsA==");
assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer("🥰")));
}

public static class InvalidValuesTest {
@Test
public void base64DecodeFunction_throwsWhenNotExactlyOneArg() throws IOException, XFormParser.ParseException {
try {
Scenario scenario = Scenario.init("Invalid base64 string", html(
head(
title("Invalid base64 string"),
model(
mainInstance(t("data id=\"base64\"",
t("text", "a"),
t("decoded")
)),
bind("/data/text").type("string"),
bind("/data/decoded").type("string").calculate("base64-decode()")
)
),
body(
input("/data/text")
))
);
@Test
public void utf16String_isDecodedToGarbage() throws IOException, XFormParser.ParseException {
Scenario scenario = getBase64DecodeScenario("UTF-16 encoded string", "AGEAYgBj");
assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer("\u0000a\u0000b\u0000c"))); // source string: "abc" in UTF-16
}

fail("RuntimeException caused by XPathUnhandledException expected");
} catch (RuntimeException e) {
assertThat(e.getCause(), instanceOf(XPathUnhandledException.class));
}
}
private static Scenario getBase64DecodeScenario(String testName, String source) throws IOException, XFormParser.ParseException {
return Scenario.init(testName, html(
head(
title(testName),
model(
mainInstance(t("data id=\"base64\"",
t("text", source),
t("decoded")
)),
bind("/data/text").type("string"),
bind("/data/decoded").type("string").calculate("base64-decode(/data/text)")
)
),
body(
input("/data/text")
))
);
}

@Test
public void base64DecodeFunction_returnsEmptyStringWhenInputInvalid() throws IOException, XFormParser.ParseException {
@Test
public void base64DecodeFunction_throwsWhenNotExactlyOneArg() throws IOException, XFormParser.ParseException {
try {
Scenario scenario = Scenario.init("Invalid base64 string", html(
head(
title("Invalid base64 string"),
Expand All @@ -112,15 +85,39 @@ public void base64DecodeFunction_returnsEmptyStringWhenInputInvalid() throws IOE
t("decoded")
)),
bind("/data/text").type("string"),
bind("/data/decoded").type("string").calculate("base64-decode(/data/text)")
bind("/data/decoded").type("string").calculate("base64-decode()")
)
),
body(
input("/data/text")
))
);

assertThat(scenario.answerOf("/data/decoded"), is(nullValue()));
fail("RuntimeException caused by XPathUnhandledException expected");
} catch (RuntimeException e) {
assertThat(e.getCause(), instanceOf(XPathUnhandledException.class));
}
}

@Test
public void base64DecodeFunction_returnsEmptyStringWhenInputInvalid() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init("Invalid base64 string", html(
head(
title("Invalid base64 string"),
model(
mainInstance(t("data id=\"base64\"",
t("text", "a"),
t("decoded")
)),
bind("/data/text").type("string"),
bind("/data/decoded").type("string").calculate("base64-decode(/data/text)")
)
),
body(
input("/data/text")
))
);

assertThat(scenario.answerOf("/data/decoded"), is(nullValue()));
}
}

0 comments on commit 8f73505

Please sign in to comment.