Просмотр исходного кода

implemented jsonReader, JPlagTestSuiteHelper and Constant class

Patrick Thiel 4 лет назад
Родитель
Сommit
c50e07067a
23 измененных файлов с 252 добавлено и 8 удалено
  1. 26 8
      jplag.endToEndTesting/pom.xml
  2. 25 0
      jplag.endToEndTesting/src/main/java/de/jplag/endToEndTesting/constants/Constant.java
  3. 148 0
      jplag.endToEndTesting/src/main/java/de/jplag/endToEndTesting/helper/JPlagTestSuiteHelper.java
  4. 53 0
      jplag.endToEndTesting/src/main/java/de/jplag/endToEndTesting/helper/JsonReader.java
  5. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo.java
  6. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1.java
  7. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_2.java
  8. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_3.java
  9. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_4.java
  10. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_5.java
  11. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_6.java
  12. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo2.java
  13. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo2_5.java
  14. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo3.java
  15. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo3_5.java
  16. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo3_6.java
  17. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo4.java
  18. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo4d1.java
  19. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo4d2.java
  20. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo4d3.java
  21. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo5.java
  22. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo6.java
  23. 0 0
      jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo7.java

+ 26 - 8
jplag.endToEndTesting/pom.xml

@@ -1,10 +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>
-  <name>jplag.endToEndTesting</name>
+	<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>

+ 25 - 0
jplag.endToEndTesting/src/main/java/de/jplag/endToEndTesting/constants/Constant.java

@@ -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";
+}

+ 148 - 0
jplag.endToEndTesting/src/main/java/de/jplag/endToEndTesting/helper/JPlagTestSuiteHelper.java

@@ -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));
+	}
+
+}

+ 53 - 0
jplag.endToEndTesting/src/main/java/de/jplag/endToEndTesting/helper/JsonReader.java

@@ -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();
+		}
+	}
+}

+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo1.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo1_2.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_2.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo1_3.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_3.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo1_4.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_4.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo1_5.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_5.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo1_6.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo1_6.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo2.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo2.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo2_5.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo2_5.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo3.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo3.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo3_5.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo3_5.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo3_6.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo3_6.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo4.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo4.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo4d1.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo4d1.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo4d2.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo4d2.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo4d3.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo4d3.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo5.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo5.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo6.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo6.java


+ 0 - 0
jplag.endToEndTesting/src/test/resources/Java/SortAlgo/SortAlgo7.java → jplag.endToEndTesting/src/test/resources/java/sortAlgo/SortAlgo7.java