Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change action invoke to allow to set the charset #134

Merged
merged 1 commit into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions src/main/java/org/mozilla/zest/core/v1/ZestActionInvoke.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -28,6 +30,15 @@ public class ZestActionInvoke extends ZestAction {

private List<String[]> parameters = new ArrayList<String[]>();

/**
* The name of the charset used for read operations (for example, read the script or the output of the invoked program).
* <p>
* Might be {@code null}, in which case it is used the default charset of the JVM.
*
* @see #getCharsetImpl()
*/
private String charset;

/**
* Instantiates a new zest action invoke.
*/
Expand All @@ -52,10 +63,25 @@ public ZestActionInvoke(int index) {
* @param parameters the parameters for the script or the process.
*/
public ZestActionInvoke(String script, String variableName, List<String[]> parameters) {
this(script, variableName, parameters, null);
}

/**
* Constructs a {@code ZestActionInvoke} with the given data.
*
* @param script the name of the script or process to invoke.
* @param variableName the name of the variable to assign the result of the invocation.
* @param parameters the parameters for the script or the process.
* @param charset the name of the charset used for read operations (for example, read the script or the output of the
* invoked program).
* @since 0.14
*/
public ZestActionInvoke(String script, String variableName, List<String[]> parameters, String charset) {
super();
this.script = script;
this.variableName = variableName;
this.parameters = parameters;
this.charset = charset;
}

@Override
Expand Down Expand Up @@ -119,7 +145,7 @@ public String invoke(ZestResponse response, ZestRuntime runtime) throws ZestActi
p.waitFor();

// Capture anything written to stdout/err
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), getCharsetImpl()));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
Expand All @@ -136,15 +162,15 @@ public String invoke(ZestResponse response, ZestRuntime runtime) throws ZestActi
// Set the same writer so that output not lost
engine.getContext().setWriter(runtime.getScriptEngineFactory().getScriptEngine().getContext().getWriter());

try (FileReader fr = new FileReader(f)) {
try (Reader reader = Files.newBufferedReader(f.toPath(), getCharsetImpl())) {
Bindings bindings = engine.createBindings();
if (this.parameters != null) {
for (String[] kvPair : this.parameters) {
bindings.put(kvPair[0], runtime.replaceVariablesInString(kvPair[1], false));
}
}

Object result = engine.eval(fr, bindings);
Object result = engine.eval(reader, bindings);
if (result == null) {
return null;
}
Expand All @@ -156,6 +182,13 @@ public String invoke(ZestResponse response, ZestRuntime runtime) throws ZestActi
}
}

private Charset getCharsetImpl() {
if (charset == null || charset.isEmpty()) {
return Charset.defaultCharset();
}
return Charset.forName(charset);
}

public String getVariableName() {
return variableName;
}
Expand All @@ -180,6 +213,28 @@ public void setParameters(List<String[]> parameters) {
this.parameters = parameters;
}

/**
* Gets the name of the charset used for read operations (for example, read the script from the file or the output of the
* invoked program).
*
* @return the name of the charset.
* @since 0.14
*/
public String getCharset() {
return charset;
}

/**
* Sets the name of the charset used for read operations (for example, read the script from the file or the output of the
* invoked program).
*
* @param charset the name of the charset.
* @since 0.14
*/
public void setCharset(String charset) {
this.charset = charset;
}

@Override
public ZestActionInvoke deepCopy() {
ZestActionInvoke copy = new ZestActionInvoke(this.getIndex());
Expand All @@ -190,6 +245,7 @@ public ZestActionInvoke deepCopy() {
copy.parameters.add(new String[] {kvPair[0], kvPair[1]});
}
copy.setEnabled(this.isEnabled());
copy.setCharset(getCharset());
return copy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import static org.junit.Assert.assertEquals;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -26,12 +27,14 @@ public void shouldUseArgsPassedInConstructor() throws Exception {
String script = "script.js";
String variable = "var";
List<String[]> parameters = new ArrayList<>();
String charset = StandardCharsets.UTF_8.name();
// When
ZestActionInvoke invokeAction = new ZestActionInvoke(script, variable, parameters);
ZestActionInvoke invokeAction = new ZestActionInvoke(script, variable, parameters, charset);
// Then
assertEquals(invokeAction.getScript(), script);
assertEquals(invokeAction.getVariableName(), variable);
assertEquals(invokeAction.getParameters(), parameters);
assertEquals(invokeAction.getCharset(), charset);
}

/**
Expand Down Expand Up @@ -143,6 +146,7 @@ public void shouldReplaceVariablesPassedAsParameters() throws Exception {
public void testSerialization() {
ZestActionInvoke inv = new ZestActionInvoke();
inv.setVariableName("test");
inv.setCharset(StandardCharsets.UTF_8.name());
inv.setParameters(params(param("first", "AAA"), param("second", "BBB")));

inv.setScript(ZestActionInvokeUnitTest.class.getResource("/data/simple-script.js").getPath());
Expand All @@ -154,6 +158,7 @@ public void testSerialization() {
assertEquals(inv.getElementType(), inv2.getElementType());
assertEquals(inv.getVariableName(), inv2.getVariableName());
assertEquals(inv.getScript(), inv2.getScript());
assertEquals(inv.getCharset(), inv2.getCharset());
assertEquals(inv.getParameters().size(), inv2.getParameters().size());
for (int i=0; i < inv.getParameters().size(); i++) {
assertEquals(inv.getParameters().get(i).length, inv2.getParameters().get(i).length);
Expand Down