Skip to content

End to End Testing (Java) #551

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

Merged
merged 35 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
981749b
added resource folder and files for testing strategy
SuyDesignz Aug 1, 2022
c50e070
implemented jsonReader, JPlagTestSuiteHelper and Constant class
SuyDesignz Aug 1, 2022
fb74117
created java test-result.json file
SuyDesignz Aug 1, 2022
2b698d1
implemented first test-cases and ResultComparison object
SuyDesignz Aug 1, 2022
a966761
improved testing-class
SuyDesignz Aug 1, 2022
dd025e6
Change from org.json to jackson-json, replacing current classes and f…
SuyDesignz Aug 1, 2022
26d253e
output changed to logger
SuyDesignz Aug 1, 2022
3ca6326
cleand code
SuyDesignz Aug 1, 2022
3c51904
Merge branch 'master' into feature/EndToEndTesting
SuyDesignz Aug 1, 2022
9cc3225
Adaptation of the function names to comply with the naming conventions
SuyDesignz Aug 2, 2022
93ac5af
Merge remote-tracking branch 'origin/master' into feature/EndToEndTes…
SuyDesignz Aug 4, 2022
e03be75
added jplag.endToendTesting module to JPlag pom
SuyDesignz Aug 4, 2022
9001c22
cleaned up code and added missing comments. test cases with more deta…
SuyDesignz Aug 4, 2022
d5f4759
changing the naming conventions in the json format
SuyDesignz Aug 4, 2022
9bcbfc6
remove unnecessary method calls and local variables for the use of po…
SuyDesignz Aug 4, 2022
a55197f
remove unnecessary variables in the method call and directly use the …
SuyDesignz Aug 4, 2022
6cd7868
Code factoring operated. Creating comments and documentation. Module …
SuyDesignz Aug 5, 2022
bed0d08
Changes made based on Sonar Cloud results
SuyDesignz Aug 5, 2022
58282b7
auto-formatted by mvn spotless:apply
SuyDesignz Aug 5, 2022
0ea2650
Modification of packet naming conventions and adaptation of logger in…
SuyDesignz Aug 5, 2022
d3cf755
Adjustments have been made to the Json models. Change to parameterize…
SuyDesignz Aug 6, 2022
bd4bddd
comments and object names adjusted
SuyDesignz Aug 6, 2022
926fbbc
formetted with mvn spotless:apply
SuyDesignz Aug 6, 2022
d20a265
Remove CodeSmells detected by sonar cloud
SuyDesignz Aug 6, 2022
8e71ce7
Changes made to the dependencies
SuyDesignz Aug 6, 2022
9bb4575
executed mvn spotless:apply
SuyDesignz Aug 6, 2022
f1ccc5d
removed unused files and name changes based on the conventions for js…
SuyDesignz Aug 6, 2022
9ca258b
pom patch implemented and test case name adjusted
SuyDesignz Aug 6, 2022
8e182d8
Removed unnecessary code and changed resource file verification.
SuyDesignz Aug 6, 2022
5a005f6
Update jplag.endToEndTesting/src/main/java/de/jplag/end_to_end_testin…
dfuchss Aug 6, 2022
479d73d
adjusted temporary folder.
SuyDesignz Aug 7, 2022
20dbb1c
removed duplicate code in the test cases.
SuyDesignz Aug 7, 2022
6971ceb
removed code Smells detected by SonarCloud
SuyDesignz Aug 7, 2022
e063e60
Changed logger call to Debug and renamed the Constant class to comply…
SuyDesignz Aug 8, 2022
b32575b
README.md file added
SuyDesignz Aug 8, 2022
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
28 changes: 28 additions & 0 deletions jplag.endToEndTesting/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.jplag</groupId>
<artifactId>aggregator</artifactId>
<version>${revision}</version>
</parent>
<artifactId>jplag.endToEndTesting</artifactId>

<dependencies>
<dependency>
<groupId>de.jplag</groupId>
<artifactId>jplag</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20070829</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.jplag.endToEndTesting.constants;

import java.nio.file.Path;

public final class Constant {

private Constant() {
// No need to instantiate the class, we can hide its constructor
}

// can be exchanged for a suitable standard path if necessary
public static final String EMPTY_STRING = "";
public static final String TEMP_DIRECTORY_NAME = "submission";
public static final String TEMP_SYSTEM_DIRECTORY = "java.io.tmpdir";

//constant final project strings
public static final Path BASE_PATH_TO_JAVA_RESOURCES_SORTALGO = Path.of("src", "test", "resources", "java",
"sortAlgo");
public static final Path BASE_PATH_TO_JAVA_RESULT_JSON = Path.of("src", "test", "resources", "results",
"javaResult.json");

//json-file constants
public static final String JSON_TEST_NAME_NODE = "functionName";
public static final String JSON_SIMILARITY_NODE = "resultSimilarity";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package de.jplag.endToEndTesting.helper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import java.util.Arrays;

import javax.annotation.processing.FilerException;

import de.jplag.endToEndTesting.constants.Constant;

public class JPlagTestSuiteHelper {

private String[] resourceNames;
private String tempFolderPath;
private String[] classNames;

/**
*
* @return the used temporary folder where the test data is copied to build the
* structure for the jplag api
*/
public String getFolderPath() {
return tempFolderPath;
}

/**
*
* @return the class names for the current test instance that were passed in the
* constructor
*/
public String[] resourceNames() {
return resourceNames;
}

/**
* Helper class for the endToEnd tests. In this class the necessary resources
* are loaded, prepared and copied for the tests based on the passed parameters.
*
* @param classNames of the resources that are required for the tests
* @throws Exception
*/
public JPlagTestSuiteHelper(String[] classNames) throws Exception {
this.classNames = classNames;
this.resourceNames = loadResource();
this.tempFolderPath = getTempFolderPath();

System.out.println(String.format("temp path at [%s]", this.tempFolderPath));
createNewTestCaseDirectory();
}

/**
* Copies the passed filenames to a temporary path to use them in the tests
*
* @throws Exception
*/
private void createNewTestCaseDirectory() throws Exception {
// before copying files to the test path, check if all files are in the resource
// directory
for (String className : classNames) {
if (!Arrays.asList(resourceNames).contains(className)) {
throw new FileNotFoundException(
String.format("The specified class could not be found! [%s]", className));
}
}
// Copy the resources data to the temporary path
for (int counter = 0; counter < classNames.length; counter++) {
Path originalPath = Path.of(Constant.BASE_PATH_TO_JAVA_RESOURCES_SORTALGO.toString(), classNames[counter]);
Path copiePath = Path.of(tempFolderPath, Constant.TEMP_DIRECTORY_NAME + (counter + 1), classNames[counter]);
try {
File directory = new File(copiePath.toString());
if (!directory.exists()) {
directory.mkdirs();
}

System.out.println(String.format("Copy file from [%s] to [%s]", originalPath, copiePath));
Files.copy(originalPath, copiePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new FilerException(
String.format("The specified file could not be copied! [%s] \n Exception [%s] ",
classNames[counter], e.getMessage()));
}
}
}

/**
* loads the filenames in the specified resources
*
* @return
*/
private String[] loadResource() {
String[] pathnames;
File f = new File(Constant.BASE_PATH_TO_JAVA_RESOURCES_SORTALGO.toString());
pathnames = f.list();
return pathnames;
}

/**
* Loads a suitable system path to temporarily store the test cases.
*
* @return The temporary system folder, if any, or the path of the current
* runtime environment
* @throws IOException
*/
private String getTempFolderPath() throws IOException {
return !System.getProperty(Constant.TEMP_SYSTEM_DIRECTORY).isBlank()
? Path.of(System.getProperty(Constant.TEMP_SYSTEM_DIRECTORY), Constant.TEMP_DIRECTORY_NAME).toString()
: Path.of(new File(".").getCanonicalPath(), Constant.TEMP_DIRECTORY_NAME).toString();
}

/**
* Delete directory with including files
*
* @param file
*/
private void deleteCopiedFiles(File folder) {
File[] files = folder.listFiles();
if (files != null) { // some JVMs return null for empty dirs
for (File f : files) {
if (f.isDirectory()) {
deleteCopiedFiles(f);
} else {
System.out.println(String.format("Delete file in folder: [%s]", f.toString()));
f.delete();
}
}
}
System.out.println(String.format("Delete folder: [%s]", folder.toString()));
folder.delete();
}

/**
* The copied data should be deleted after instance closure
*
* @throws Exception
*/
public void close() throws Exception {
System.out.println("Class instance is terminated!");
System.out.println(new File(tempFolderPath));
deleteCopiedFiles(new File(tempFolderPath));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.jplag.endToEndTesting.helper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;

import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

public class JsonReader {

private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

/**
* read in a json file located at the passing path
*
* @param path to the json file with extension
* @return a JSONObject of the persistence file in .json format
* @throws IOException
* @throws JSONException
*/
public static JSONObject readJsonFromPath(String path) throws IOException, JSONException {
if (!path.endsWith(".json")) {
throw new JSONException("the specified filename in the path must be a *.json file");
}

File initialFile = new File(path);
InputStream targetStream = new FileInputStream(initialFile);

try {
BufferedReader rd = new BufferedReader(new InputStreamReader(targetStream, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
targetStream.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package de.jplag.endToEndTesting.helper;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import de.jplag.endToEndTesting.constants.Constant;

public class ResultComparison {
private String functionName;
private float similarity;

/**
* Based on the passed function name the stored values in the json-file are
* loaded and parsed
*
* @param functionName of the current test which is searched for as key in the
* json-file
* @throws JSONException
* @throws IOException
*/
public ResultComparison(String functionName) throws JSONException, IOException {
if (functionName != null && !functionName.isEmpty()) {
this.functionName = functionName;
loadStoredComparativeValues();
} else {
throw new IllegalArgumentException("function name cannot be empty");
}
}

/**
* @return the function name passed in the constructor
*/
public String getFunctionName() {
return functionName;
}

/**
* @return the stored similarity value from the json file for the function
* specified in the constructor.
*/
public float getSimilarity() {
return similarity;
}

/**
* Loads the comparison values of the current function into the object
*
* @throws JSONException
* @throws IOException
*/
private void loadStoredComparativeValues() throws JSONException, IOException {

var jsonResultObj = JsonReader
.readJsonFromPath(Constant.BASE_PATH_TO_JAVA_RESULT_JSON.toAbsolutePath().toString());

var resultsArray = jsonResultObj.getJSONArray("results");

for (int i = 0; i < resultsArray.length(); i++) {
JSONObject articleObject = resultsArray.getJSONObject(i);
if (articleObject.getString(Constant.JSON_TEST_NAME_NODE).equals(functionName)) {
similarity = (float) articleObject.getDouble(Constant.JSON_SIMILARITY_NODE);
return;
}
}
}
}
Loading