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 16 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
21 changes: 21 additions & 0 deletions jplag.endToEndTesting/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.jplag.endToEndTesting.constants;

import java.nio.file.Path;

public final class Constant {

private Constant() {
// private constructor to prevent instantiation
}

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

public static final String TEMPORARY_SUBMISSION_DIRECTORY_NAME = Path.of(System.getProperty(TEMPORARY_SYSTEM_DIRECTORY), TEMPORARY_DIRECTORY_NAME).toString();

// 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");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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 java.util.List;

import javax.annotation.processing.FilerException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.jplag.endToEndTesting.constants.Constant;
import de.jplag.options.LanguageOption;
import model.ResultJsonModel;
import model.TestCaseModel;

public class JPlagTestSuiteHelper {

private static final Logger logger = LoggerFactory.getLogger("EndToEndTesting");

private String[] resourceNames;
private List<ResultJsonModel> resultModel;
private LanguageOption languageOption;

/**
* 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.
* An instance of this class loads all necessary paths and properties for a test
* run with the specified language
*
* @param languageOption for loading language-specific resources
* @throws Exception
*/
public JPlagTestSuiteHelper(LanguageOption languageOption) throws Exception {
this.languageOption = languageOption;
this.resourceNames = new File(Constant.BASE_PATH_TO_JAVA_RESOURCES_SORTALGO.toString()).list();

this.resultModel = JsonHelper.getResultModelFromPath();
logger.info(String.format("temp path at [%s]", Constant.TEMPORARY_SUBMISSION_DIRECTORY_NAME));
}

/**
* creates all necessary folder paths and objects for a test run. Also searches
* for the stored previous results of the test in order to compare them with the
* current results.
*
* @param classNames
* @return comparison results saved for the test
* @throws Exception
*/
public TestCaseModel createNewTestCase(String[] classNames) throws Exception {
createNewTestCaseDirectory(classNames);
var functionName = StackWalker.getInstance().walk(stream -> stream.skip(1).findFirst().get()).getMethodName();
ResultJsonModel resultJsonModel = resultModel.stream()
.filter(jsonModel -> functionName.equals(jsonModel.getFunctionName())).findAny().orElse(null);
return new TestCaseModel(Constant.TEMPORARY_SUBMISSION_DIRECTORY_NAME, resultJsonModel, languageOption);
}

/**
* The copied data should be deleted after instance closure
*
* @throws Exception
*/
public void clear() throws Exception {
logger.info("Class instance was cleaned!");
deleteCopiedFiles(new File(Constant.TEMPORARY_SUBMISSION_DIRECTORY_NAME));
}

/**
* Copies the passed filenames to a temporary path to use them in the tests
*
* @throws Exception
*/
private void createNewTestCaseDirectory(String[] classNames) 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(Constant.TEMPORARY_SUBMISSION_DIRECTORY_NAME,
Constant.TEMPORARY_DIRECTORY_NAME + (counter + 1), classNames[counter]);

File directory = new File(copiePath.toString());
if (!directory.exists()) {
directory.mkdirs();
}

Files.copy(originalPath, copiePath, StandardCopyOption.REPLACE_EXISTING);
logger.info(String.format("Copy file from [%s] to [%s]", originalPath, copiePath));
}
}

/**
* 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 {
logger.info(String.format("Delete file in folder: [%s]", f.toString()));
f.delete();
}
}
}
logger.info(String.format("Delete folder: [%s]", folder.toString()));
folder.delete();
}

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

import java.nio.file.Paths;

import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import de.jplag.endToEndTesting.constants.Constant;
import model.ResultJsonModel;

public final class JsonHelper {

private JsonHelper() {
// private constructor to prevent instantiation
}

/**
* Parsing the old results in the json file as a list from ResultJsonModel.
*
* @param pathToJsonFile
* @return list of saved results for the test cases
* @throws JsonMappingException
* @throws JsonProcessingException
* @throws Exception
*/
public static List<ResultJsonModel> getResultModelFromPath()
throws JsonMappingException, JsonProcessingException, Exception {
return Arrays.asList(
new ObjectMapper().readValue(Constant.BASE_PATH_TO_JAVA_RESULT_JSON.toFile(), ResultJsonModel[].class));
}
}
31 changes: 31 additions & 0 deletions jplag.endToEndTesting/src/main/java/model/ResultJsonModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package model;

import com.fasterxml.jackson.annotation.JsonProperty;

public class ResultJsonModel {

@JsonProperty("function_name")
private String functionName;
@JsonProperty("result_similarity")
private String resultSimilarity;

public ResultJsonModel(String functionName, String resultSimilarity)
{
this.functionName = functionName;
this.resultSimilarity = resultSimilarity;
}

public ResultJsonModel()
{
}

public float similarity()
{
return Float.parseFloat(resultSimilarity);
}

public String getFunctionName()
{
return functionName;
}
}
44 changes: 44 additions & 0 deletions jplag.endToEndTesting/src/main/java/model/TestCaseModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package model;

import java.util.ArrayList;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.jplag.JPlagComparison;
import de.jplag.JPlagResult;
import de.jplag.options.JPlagOptions;
import de.jplag.options.LanguageOption;

public class TestCaseModel {
private static final Logger logger = LoggerFactory.getLogger("EndToEndTesting");

private ResultJsonModel resultJsonModel;
private LanguageOption languageOption;
private String submissionFolderPath;

public TestCaseModel(String submissionFolderPath, ResultJsonModel resultJsonModel, LanguageOption languageOption) {
this.submissionFolderPath = submissionFolderPath;
this.resultJsonModel = resultJsonModel;
this.languageOption = languageOption;
}

/**
* returns the, for the test, current result model that has been persisted in the saved list.
* @return ResultJsonModel loaded for the test
*/
public ResultJsonModel getCurrentResultJsonModel()
{
return resultJsonModel;
}

/**
* creates the JPlag options for the test run from the values passed in the constructor.
* @return current JPlag options for the created object
*/
public JPlagOptions getJPlagOptionsFromCurrentModel() {
return new JPlagOptions(new ArrayList<>(Arrays.asList(submissionFolderPath)), new ArrayList<String>(),
languageOption);
}
}
Loading