Skip to content

Commit

Permalink
support for z/OSMF variables (downgraded velocity to 1.7 (#11)
Browse files Browse the repository at this point in the history
* support for z/OSMF variables (downgraded velocity to 1.7)

* improved tests for encoding
  • Loading branch information
vvvlc authored and plavjanik committed Jan 25, 2019
1 parent 38fa14c commit 3f05670
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {
}

dependencies {
compile 'org.apache.velocity:velocity-engine-core:2.0'
compile 'org.apache.velocity:velocity:1.7'
compile 'info.picocli:picocli:3.6.1'
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'org.slf4j:slf4j-simple:1.7.25'
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/vtlcli/VelocityCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ public void run() {

try {
Writer writer = getWriter();
Template template;
if (inputEncoding != null) {
Charset.forName(inputEncoding);
template = engine.getTemplate(inputTemplate.getName(), inputEncoding);
} else {
template = engine.getTemplate(inputTemplate.getName());
}
Template template = engine.getTemplate(inputTemplate.getName(), inputEncoding);
template.merge(velocityContext, writer);
writer.flush();
} catch (ResourceNotFoundException e) {
Expand Down
60 changes: 53 additions & 7 deletions src/test/java/vtlcli/VelocityCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,30 +149,76 @@ public void testInvalidOutputEncoding() {
cli.run();
}

public void testEncoding() throws IOException {
@Test
public void testEncoding_all_in_ebcdic() throws IOException {
VelocityCli cli = new VelocityCli();
cli.inputTemplate = file("templates/hello.vtl");
cli.inputTemplate = file("templates/cp1047.vtl");
cli.yamlContextFile = file("templates/hello_cp1047.yml");
cli.inputEncoding = "Cp1047";
cli.yamlEncoding = "Cp1047";
cli.outputEncoding = "ISO-8859-1";
cli.outputFile = new File(folder.getRoot().getAbsolutePath(), "test.out");
cli.context = Collections.singletonMap("name", "world");
cli.run();
assertEquals(String.format("Hello, world!%n"), new String(Files.readAllBytes(cli.outputFile.toPath())));
assertEquals(String.format("Hello Petr!"), new String(Files.readAllBytes(cli.outputFile.toPath())));
}

@Test
public void testEnvironmentContext() throws Exception {
public void testEncoding_all_in_ebcdic_yml_encoding_not_specified() throws IOException {
VelocityCli cli = new VelocityCli();
cli.inputTemplate = file("templates/cp1047.vtl");
cli.yamlContextFile = file("templates/hello_cp1047.yml");
cli.inputEncoding = "Cp1047";
//cli.yamlEncoding = "Cp1047"; intentionally not specified it should be same like cli.inputEncoding
cli.outputEncoding = "ISO-8859-1";
cli.outputFile = new File(folder.getRoot().getAbsolutePath(), "test.out");
cli.run();
assertEquals(String.format("Hello Petr!"), new String(Files.readAllBytes(cli.outputFile.toPath())));
}

@Test
public void testEncoding_template_in_ebcdic_yaml_in_ascii() throws IOException {
VelocityCli cli = new VelocityCli();
cli.inputTemplate = file("templates/cp1047.vtl");
cli.yamlContextFile = file("templates/hello.yml");
cli.inputEncoding = "Cp1047";
cli.yamlEncoding = "ISO-8859-1";
cli.outputEncoding = "ISO-8859-1";
cli.outputFile = new File(folder.getRoot().getAbsolutePath(), "test.out");
cli.run();
assertEquals(String.format("Hello Petr!"), new String(Files.readAllBytes(cli.outputFile.toPath())));
}

@Test
public void testEnvironmentContext() throws Exception {
VelocityCli cli = new VelocityCli();
Map<String, String> env = System.getenv();
Map<String, String> newEnv = new HashMap<>(env);
newEnv.put("VTL_NAME", "environment");
EnvUtils.setenv(newEnv);
cli.inputTemplate = file("templates/env.vtl");
cli.envContext = true;
cli.run();
assertEquals(String.format("Hello, environment!%n"), getCapturedOut());
}

@Test
public void testZosmfVariablesWithDash() throws Exception {
VelocityCli cli = new VelocityCli();
cli.inputTemplate = file("templates/zOSMFvariables.vtl");
Map<String, String> m = new HashMap<String, String>();

m.put("DBname", "noscope");
m.put("instance-DBname", "instance-scope");
m.put("global-DBname", "global-scope");
cli.context = m;

cli.run();
assertEquals(String.format("Hello, environment!%n"), getCapturedOut());
}
assertEquals(String.format("Variable without scope 'noscope'%n"
+ "Variable in instance scope 'instance-scope'%n"
+ "Variable in global scope 'global-scope'"),
getCapturedOut());
}


@After
public void tearDown() {
Expand Down
1 change: 1 addition & 0 deletions templates/hello_cp1047.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
•”…z@¦–™“„•…˘Ł…„z@@•”…z@×…Ł™
Expand Down
3 changes: 3 additions & 0 deletions templates/zOSMFvariables.vtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Variable without scope '${DBname}'
Variable in instance scope '${instance-DBname}'
Variable in global scope '${global-DBname}'

0 comments on commit 3f05670

Please sign in to comment.