Skip to content

Commit

Permalink
#135: Fix issue with plugin path file generation, fix failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabrina Wullschleger committed Jun 26, 2024
1 parent 7000bf8 commit 7df7d80
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 53 deletions.
2 changes: 2 additions & 0 deletions gretl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ configurations {
}

dependencies {
testRuntime files(writePluginClassPath)

api gradleApi()

api libs.ili.validator
Expand Down
19 changes: 12 additions & 7 deletions gretl/integration-test.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@ sourceSets {
}

tasks.register("writePluginClassPath"){
def buildDirectory = project.layout.buildDirectory.get()
def outputDir = file("$buildDirectory/integrationTest/resources")

inputs.files configurations.compileClasspath + configurations.runtimeClasspath
outputs.dir outputDir

doLast {
def buildDirectory = project.layout.buildDirectory.get()
new File(buildDirectory.getAsFile(), "pluginClassPath.txt").text =
sourceSets.integrationTest.runtimeClasspath.files.join("\n")
outputDir.mkdirs()
file("$outputDir/pluginClassPath.txt").text = configurations.compileClasspath.files.join("\n") + "\n" + configurations.runtimeClasspath.files.join("\n")
}
}

/**
* Task for executing the integration tests. It references the
* classes and runtime classpath of the integration test source set.
*/
task jarTest(type: Test, dependsOn: writePluginClassPath) {
task jarTest(type: Test) {
description = 'Runs integration tests against the installed gretl jar.'
group = LifecycleBasePlugin.VERIFICATION_GROUP

Expand All @@ -49,7 +54,7 @@ task jarTest(type: Test, dependsOn: writePluginClassPath) {
jvmArgs '-Xmx1g'
}

task jarS3Test(type: Test) {
task jarS3Test(type: Test, dependsOn: writePluginClassPath) {
description = 'Runs the S3 integration tests against the installed gretl jar.'
group = LifecycleBasePlugin.VERIFICATION_GROUP

Expand All @@ -66,7 +71,7 @@ task jarS3Test(type: Test) {
jvmArgs '-Xmx1g'
}

task imageTest(type: Test){
task imageTest(type: Test, dependsOn: writePluginClassPath){
description = 'Runs integration tests against the installed gretl docker image.'
group = LifecycleBasePlugin.VERIFICATION_GROUP

Expand All @@ -82,7 +87,7 @@ task imageTest(type: Test){
classpath += sourceSets.integrationTest.runtimeClasspath
}

task imageS3Test(type: Test){
task imageS3Test(type: Test, dependsOn: writePluginClassPath){
description = 'Runs S3 integration tests against the installed gretl docker image.'
group = LifecycleBasePlugin.VERIFICATION_GROUP

Expand Down
89 changes: 43 additions & 46 deletions gretl/src/integrationTest/java/ch/so/agi/gretl/jobs/Av2chTest.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
package ch.so.agi.gretl.jobs;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import org.gradle.testkit.runner.UnexpectedBuildFailure;
import org.junit.Test;

import ch.so.agi.gretl.util.GradleVariable;
import ch.so.agi.gretl.util.IntegrationTestUtil;

import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.BuildResult;

public class Av2chTest {
import static org.junit.Assert.*;

public class Av2chTest {
@Test
public void transformation_Ok_with_test_kit() throws Exception{
public void transformation_Ok() throws Exception {
File projectDir = new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/Av2ch");
File initScript = new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/init.gradle");
List<File> classpath = new ArrayList<>();
File classpathFile = new File(System.getProperty("user.dir"),"build/pluginClassPath.txt");
List<String> lines = Files.readAllLines(classpathFile.toPath(), StandardCharsets.UTF_8);
for (String line : lines) {
classpath.add(new File(line));
}
List<File> classpath = IntegrationTestUtil.getPluginClassPaths();

IntegrationTestUtil.cleanFolder(new File(projectDir + "/output"));

GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath(classpath)
.withArguments("--init-script", initScript.getAbsolutePath(), "transform")
.build();
.withProjectDir(projectDir)
.withPluginClasspath(classpath)
.withArguments("--init-script", IntegrationTestUtil.getPathToInitScript(), "transform")
.build();

File resultFile = new File(projectDir + "/output/254900.itf");

Expand All @@ -50,40 +40,47 @@ public void transformation_Ok_with_test_kit() throws Exception{
assertTrue(resultString.contains("TABL LFP3Nachfuehrung"));
assertTrue(resultString.contains("OBJE 2540 2514 2611693.294 1233674.211 111.9 1 3 1"));
}

@Test
public void transformation_Ok() throws Exception {
GradleVariable[] gvs = null;
IntegrationTestUtil.runJob("src/integrationTest/jobs/Av2ch", gvs);

File resultFile = new File("src/integrationTest/jobs/Av2ch/output/254900.itf");

long resultSize = resultFile.length();
assertTrue("Size of result file is wrong.", resultSize > 580000);

String resultString = new String(Files.readAllBytes(resultFile.toPath()), StandardCharsets.ISO_8859_1);

assertThat(resultString, containsString("DM01 Interlis Converter"));
assertThat(resultString, containsString("MODL DM01AVCH24LV95D"));
assertThat(resultString, containsString("TABL LFP3Nachfuehrung"));
assertThat(resultString, containsString("OBJE 2540 2514 2611693.294 1233674.211 111.9 1 3 1"));
}

@Test
public void transformationFileSet_Ok() throws Exception {
GradleVariable[] gvs = null;
IntegrationTestUtil.runJob("src/integrationTest/jobs/Av2chFileSet", gvs);

File resultFile1 = new File("src/integrationTest/jobs/Av2ch/output/254900.itf");
File resultFile2 = new File("src/integrationTest/jobs/Av2ch/output/254900.itf");

File projectDir = new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/Av2chFileSet");
List<File> classpath = IntegrationTestUtil.getPluginClassPaths();

try{
IntegrationTestUtil.cleanFolder(new File(projectDir + "/output"));

GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath(classpath)
.withArguments("--init-script", IntegrationTestUtil.getPathToInitScript(), "transform")
.build();
} catch (UnexpectedBuildFailure e) {
System.out.println("Build failed with message: " + e.getMessage());
}

File resultFile1 = new File("src/integrationTest/jobs/Av2chFileSet/output/252400.itf");
File resultFile2 = new File("src/integrationTest/jobs/Av2chFileSet/output/254900.itf");

assertTrue(resultFile1.exists());
assertTrue(resultFile2.exists());
}

@Test
public void transformation_Fail() throws Exception {
GradleVariable[] gvs = null;
assertEquals(1, IntegrationTestUtil.runJob("src/integrationTest/jobs/Av2chFail", gvs, new StringBuffer(), new StringBuffer()));
File projectDir = new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/Av2chFail");
List<File> classpath = IntegrationTestUtil.getPluginClassPaths();

try {
GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath(classpath)
.withArguments("--init-script", IntegrationTestUtil.getPathToInitScript(), "transform")
.build();

fail("Expected an UnexpectedBuildFailure exception to be thrown");
} catch (UnexpectedBuildFailure e) {
System.out.println("Build failed with message: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package ch.so.agi.gretl.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
Expand Down Expand Up @@ -159,4 +165,26 @@ else if (TESTTYPE_IMAGE.equalsIgnoreCase(TESTTYPE)){

return buildJobDirOption;
}

public static ArrayList<File> getPluginClassPaths() throws IOException {
ArrayList<File> classpath = new ArrayList<>();
File classpathFile = new File(System.getProperty("user.dir"),"build/integrationTest/resources/pluginClassPath.txt");
List<String> lines = Files.readAllLines(classpathFile.toPath(), StandardCharsets.UTF_8);
for (String line : lines) {
classpath.add(new File(line));
}
return classpath;
}

public static void cleanFolder(File projectDir) throws IOException {
for(File file : Objects.requireNonNull(projectDir.listFiles())){
if (!file.isDirectory()) {
Files.delete(file.toPath());
}
}
}

public static String getPathToInitScript(){
return new File(System.getProperty("user.dir") + "/src/integrationTest/jobs/init.gradle").getAbsolutePath();
}
}

0 comments on commit 7df7d80

Please sign in to comment.