-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #708 from lognaturel/base64
Add base64-decode
- Loading branch information
Showing
10 changed files
with
307 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/test/java/org/javarosa/xpath/expr/Base64DecodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package org.javarosa.xpath.expr; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static org.javarosa.core.test.AnswerDataMatchers.stringAnswer; | ||
import static org.javarosa.core.util.BindBuilderXFormsElement.bind; | ||
import static org.javarosa.core.util.XFormsElement.body; | ||
import static org.javarosa.core.util.XFormsElement.head; | ||
import static org.javarosa.core.util.XFormsElement.html; | ||
import static org.javarosa.core.util.XFormsElement.input; | ||
import static org.javarosa.core.util.XFormsElement.mainInstance; | ||
import static org.javarosa.core.util.XFormsElement.model; | ||
import static org.javarosa.core.util.XFormsElement.t; | ||
import static org.javarosa.core.util.XFormsElement.title; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.IOException; | ||
import org.javarosa.core.test.Scenario; | ||
import org.javarosa.xform.parse.XFormParser; | ||
import org.javarosa.xpath.XPathUnhandledException; | ||
import org.junit.Test; | ||
|
||
public class Base64DecodeTest { | ||
|
||
@Test | ||
public void asciiString_isSuccessfullyDecoded() throws IOException, XFormParser.ParseException { | ||
Scenario scenario = getBase64DecodeScenario("ASCII string", "SGVsbG8="); | ||
assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer("Hello"))); | ||
} | ||
|
||
@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 accentString_isSuccessfullyDecoded() throws IOException, XFormParser.ParseException { | ||
Scenario scenario = getBase64DecodeScenario("String with accented characters", "w6nDqMOx"); | ||
assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer("éèñ"))); | ||
} | ||
|
||
@Test | ||
public void emojiString_isSuccessfullyDecoded() throws IOException, XFormParser.ParseException { | ||
Scenario scenario = getBase64DecodeScenario("String with emoji", "8J+lsA=="); | ||
assertThat(scenario.answerOf("/data/decoded"), is(stringAnswer("🥰"))); | ||
} | ||
|
||
@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 | ||
} | ||
|
||
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_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") | ||
)) | ||
); | ||
|
||
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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.