Skip to content

Commit

Permalink
Merge pull request #408 from jplag/frontend-tests
Browse files Browse the repository at this point in the history
Lightweight frontend tests and frontend-testutils (+ JUnit 5 for every test)
  • Loading branch information
tsaglam committed May 5, 2022
2 parents 020fab8 + 3c9e1f6 commit b6c2660
Show file tree
Hide file tree
Showing 37 changed files with 317 additions and 214 deletions.
18 changes: 18 additions & 0 deletions jplag.frontend-testutils/pom.xml
@@ -0,0 +1,18 @@
<?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 http://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>frontend-testutils</artifactId>

<dependencies>
<dependency>
<groupId>de.jplag</groupId>
<artifactId>frontend-utils</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,24 @@
package de.jplag.testutils;

import static org.junit.jupiter.api.Assertions.fail;

import de.jplag.ErrorConsumer;

/**
* Mock error consumer that fails the test case on error occurrence.
* @author Timur Saglam
*/
public class TestErrorConsumer implements ErrorConsumer {

@Override
public void addError(String errorMessage) {
System.err.println(errorMessage);
fail(errorMessage);
}

@Override
public void print(String message, String longMessage) {
System.out.println(message);
}

}
1 change: 0 additions & 1 deletion jplag.frontend-utils/pom.xml
Expand Up @@ -8,5 +8,4 @@
<version>${revision}</version>
</parent>
<artifactId>frontend-utils</artifactId>

</project>
7 changes: 7 additions & 0 deletions jplag.frontend-utils/src/main/java/de/jplag/Token.java
Expand Up @@ -79,6 +79,13 @@ public int getLine() {
return line;
}

/**
* @return the type identifier of the token.
*/
public int getType() {
return type;
}

/**
* Sets the character index which denotes where the code sections represented by this token starts in the line.
* @param column is the index in characters to set.
Expand Down
7 changes: 7 additions & 0 deletions jplag.frontend.csharp-6/pom.xml
Expand Up @@ -13,6 +13,13 @@
<groupId>de.jplag</groupId>
<artifactId>frontend-utils</artifactId>
</dependency>
<dependency>
<groupId>de.jplag</groupId>
<artifactId>frontend-testutils</artifactId>
<version>${revision}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
Expand Down
@@ -0,0 +1,55 @@
package de.jplag.csharp;

import static de.jplag.csharp.CSharpTokenConstants.*;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.StreamSupport;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import de.jplag.Token;
import de.jplag.TokenConstants;
import de.jplag.TokenList;
import de.jplag.TokenPrinter;
import de.jplag.testutils.TestErrorConsumer;

class MinimalCSharpFrontendTest {
private static final int EXPEXTED_NUMBER_OF_TOKENS = 15;
private static final Path BASE_PATH = Path.of("src", "test", "resources", "de", "jplag", "csharp");
private static final String TEST_SUBJECT = "TestClass.cs";

private de.jplag.Language frontend;
private File baseDirectory;

@BeforeEach
public void setUp() {
TestErrorConsumer consumer = new TestErrorConsumer();
frontend = new Language(consumer);
baseDirectory = BASE_PATH.toFile();
assertTrue(baseDirectory.exists(), "Could not find base directory!");
}

@Test
void testParsingTestClass() {
List<Integer> expectedToken = List.of(CLASS, CLASS_BEGIN, DECLARE_VAR, CONSTRUCTOR, METHOD, METHOD_BEGIN, INVOCATION, METHOD_END, PROPERTY,
DECLARE_VAR, PROPERTY, RETURN, ASSIGNMENT, CLASS_END, TokenConstants.FILE_END);

// Parse test input
String[] input = new String[] {TEST_SUBJECT};
TokenList result = frontend.parse(baseDirectory, input);
System.out.println(TokenPrinter.printTokens(result, baseDirectory, Arrays.asList(input)));

// Compare parsed tokens:
assertEquals(EXPEXTED_NUMBER_OF_TOKENS, result.size());
List<Integer> actualToken = StreamSupport.stream(result.allTokens().spliterator(), false).map(Token::getType).collect(toList());
assertEquals(expectedToken, actualToken);
}

}
@@ -0,0 +1,27 @@
/**
* Class for testing the C# language frontend.
*/
public class MyClass
{
public string myField = string.Empty;

public MyClass()
{
}

public void MyMethod(int parameter1, string parameter2)
{
Console.WriteLine("First Parameter {0}, second parameter {1}",
parameter1, parameter2);
}

public int MyAutoImplementedProperty { get; set; }

private int myPropertyVar;

public int MyProperty
{
get { return myPropertyVar; }
set { myPropertyVar = value; }
}
}
12 changes: 0 additions & 12 deletions jplag/pom.xml
Expand Up @@ -15,11 +15,6 @@
<groupId>net.sourceforge.argparse4j</groupId>
<artifactId>argparse4j</artifactId>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -30,13 +25,6 @@
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>de.jplag</groupId>
<artifactId>frontend-utils</artifactId>
Expand Down
37 changes: 19 additions & 18 deletions jplag/src/test/java/de/jplag/BaseCodeTest.java
@@ -1,11 +1,12 @@
package de.jplag;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.util.List;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import de.jplag.exceptions.BasecodeException;
import de.jplag.exceptions.ExitException;
Expand All @@ -14,24 +15,24 @@
public class BaseCodeTest extends TestBase {

@Test
public void testBasecodeUserSubmissionComparison() throws ExitException {
void testBasecodeUserSubmissionComparison() throws ExitException {
JPlagResult result = runJPlag("basecode", it -> it.setBaseCodeSubmissionName("base"));
verifyResults(result);
}

@Test(expected = BasecodeException.class)
public void testTinyBasecode() throws ExitException {
runJPlag("TinyBasecode", it -> it.setBaseCodeSubmissionName("base"));
@Test
void testTinyBasecode() {
assertThrows(BasecodeException.class, () -> runJPlag("TinyBasecode", it -> it.setBaseCodeSubmissionName("base")));
}

@Test
public void testEmptySubmission() throws ExitException {
void testEmptySubmission() throws ExitException {
JPlagResult result = runJPlag("emptysubmission", it -> it.setBaseCodeSubmissionName("base"));
verifyResults(result);
}

@Test
public void testAutoTrimFileSeparators() throws ExitException {
void testAutoTrimFileSeparators() throws ExitException {
JPlagResult result = runJPlag("basecode", it -> it.setBaseCodeSubmissionName(File.separator + "base" + File.separator));
verifyResults(result);
}
Expand All @@ -45,23 +46,23 @@ private void verifyResults(JPlagResult result) {
}

@Test
public void testBasecodePathComparison() throws ExitException {
void testBasecodePathComparison() throws ExitException {
JPlagResult result = runJPlag("basecode", it -> it.setBaseCodeSubmissionName(getBasePath("basecode-base")));
assertEquals(3, result.getNumberOfSubmissions()); // "basecode/base" is now a user submission.
}

@Test(expected = RootDirectoryException.class)
public void testInvalidRoot() throws ExitException {
runJPlag("basecode", it -> it.setSubmissionDirectories(List.of("WrongRoot")));
@Test
void testInvalidRoot() throws ExitException {
assertThrows(RootDirectoryException.class, () -> runJPlag("basecode", it -> it.setSubmissionDirectories(List.of("WrongRoot"))));
}

@Test(expected = BasecodeException.class)
public void testInvalidBasecode() throws ExitException {
runJPlag("basecode", it -> it.setBaseCodeSubmissionName("WrongBasecode"));
@Test
void testInvalidBasecode() {
assertThrows(BasecodeException.class, () -> runJPlag("basecode", it -> it.setBaseCodeSubmissionName("WrongBasecode")));
}

@Test(expected = BasecodeException.class)
public void testBasecodeUserSubmissionWithDots() throws ExitException {
runJPlag("basecode", it -> it.setBaseCodeSubmissionName("base.ext"));
@Test
void testBasecodeUserSubmissionWithDots() {
assertThrows(BasecodeException.class, () -> runJPlag("basecode", it -> it.setBaseCodeSubmissionName("base.ext")));
}
}
10 changes: 4 additions & 6 deletions jplag/src/test/java/de/jplag/InvalidSubmissionTest.java
@@ -1,19 +1,17 @@
package de.jplag;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import de.jplag.exceptions.ExitException;
import de.jplag.exceptions.SubmissionException;

public class InvalidSubmissionTest extends TestBase {
class InvalidSubmissionTest extends TestBase {

private static final String SAMPLE_NAME = "InvalidSubmissions";

Expand All @@ -22,7 +20,7 @@ public class InvalidSubmissionTest extends TestBase {
* invalid submissions being stored.
*/
@Test
public void testInvalidSubmissionsWithDebug() throws ExitException {
void testInvalidSubmissionsWithDebug() throws ExitException {
try {
runJPlag(SAMPLE_NAME, it -> it.setDebugParser(true));
fail("No submission exception was thrown!");
Expand Down
10 changes: 5 additions & 5 deletions jplag/src/test/java/de/jplag/NewJavaFeaturesTest.java
@@ -1,8 +1,8 @@
package de.jplag;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import de.jplag.exceptions.ExitException;

Expand All @@ -19,11 +19,11 @@ public void testJavaFeatureDuplicates() throws ExitException {
JPlagResult result = runJPlagWithExclusionFile(ROOT_DIRECTORY, EXCLUSION_FILE_NAME);

// Ensure test input did not change:
assertEquals(String.format(CHANGE_MESSAGE, "Submissions"), 2, result.getNumberOfSubmissions());
assertEquals(2, result.getNumberOfSubmissions(), String.format(CHANGE_MESSAGE, "Submissions"));
for (Submission submission : result.getSubmissions().getSubmissions()) {
assertEquals(String.format(CHANGE_MESSAGE, "Files"), 6, submission.getFiles().size());
assertEquals(6, submission.getFiles().size(), String.format(CHANGE_MESSAGE, "Files"));
}
assertEquals(String.format(CHANGE_MESSAGE, "Comparisons"), 1, result.getComparisons().size());
assertEquals(1, result.getComparisons().size(), String.format(CHANGE_MESSAGE, "Comparisons"));

// Check similarity and number of matches:
var comparison = result.getComparisons().get(0);
Expand Down

0 comments on commit b6c2660

Please sign in to comment.