Skip to content
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

Overhaul unit tests #44

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions src/test/java/org/cadixdev/mercury/test/Java10Tests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.cadixdev.mercury.test;

import org.cadixdev.mercury.Mercury;
import org.eclipse.jdt.core.JavaCore;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class Java10Tests {

@Test
@DisplayName("doesn't remove var keyword (remapped type)")
void remapVarKeywordRemappedType() throws Exception {
new RemapperTest("java-10", Java10Tests::configureMercury)
.copy("a")
.register("b", "b")
.test();
}
@Test
@DisplayName("doesn't remove var keyword (not remapped type)")
void remapVarKeywordNotRemappedType() throws Exception {
new RemapperTest("java-10", Java10Tests::configureMercury)
.register("c", "c")
.test();
}

static void configureMercury(final Mercury mercury) {
mercury.setSourceCompatibility(JavaCore.VERSION_10);
}

}
40 changes: 40 additions & 0 deletions src/test/java/org/cadixdev/mercury/test/Java11Tests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.cadixdev.mercury.test;

import org.cadixdev.mercury.Mercury;
import org.eclipse.jdt.core.JavaCore;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class Java11Tests {

@Test
@DisplayName("doesn't remove var keyword in lambda parameter (remapped type)")
void remapVarKeywordRemappedTypeLambdaParameter() throws Exception {
new RemapperTest("java-11", Java11Tests::configureMercury)
.copy("a")
.register("b", "b")
.test();
}
@Test
@DisplayName("doesn't remove var keyword in lambda parameter (not remapped type)")
void remapVarKeywordNotRemappedTypeLambdaParameter() throws Exception {
new RemapperTest("java-11", Java11Tests::configureMercury)
.register("c", "c")
.test();
}

static void configureMercury(final Mercury mercury) {
mercury.setSourceCompatibility(JavaCore.VERSION_11);
}

}
121 changes: 121 additions & 0 deletions src/test/java/org/cadixdev/mercury/test/RemapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.cadixdev.mercury.test;

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

import org.cadixdev.bombe.util.ByteStreams;
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.io.MappingFormats;
import org.cadixdev.lorenz.io.MappingsReader;
import org.cadixdev.mercury.Mercury;
import org.cadixdev.mercury.remapper.MercuryRemapper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class RemapperTest {

private final String name;
private final Consumer<Mercury> mercuryConfigure;

private final Path dir;
private final MappingSet mappings;

private final Map<String, String> expected = new HashMap<>();

public RemapperTest(final String name) throws IOException {
this(name, mercury -> {});
}

public RemapperTest(final String name, final Consumer<Mercury> mercuryConfigure) throws IOException {
this.name = name;
this.mercuryConfigure = mercuryConfigure;

// Create temporary directory, as Mercury needs to operate on the actual file
// system.
this.dir = Files.createTempDirectory("mercury-test");
Files.createDirectories(this.dir.resolve("a"));
Files.createDirectories(this.dir.resolve("b"));

// Read test mappings
this.mappings = MappingSet.create();
try (final MappingsReader reader = MappingFormats.byId("jam")
.createReader(RemapperTest.class.getResourceAsStream("/" + this.name + "/test.jam"))) {
reader.read(this.mappings);
}
}

public RemapperTest copy(final String file) throws IOException {
final Path path = this.dir.resolve("a").resolve(file + ".java");

// Make sure the parent directory exists
Files.createDirectories(path.getParent());

// Copy the file to the file system
Files.copy(
RemapperTest.class.getResourceAsStream("/" + this.name + "/a/" + file + ".java"),
path,
StandardCopyOption.REPLACE_EXISTING
);

// Finally verify the file exists, to prevent issues later on
assertTrue(Files.exists(path), file + " failed to copy!");

return this;
}

public RemapperTest register(final String a, final String b) throws IOException {
// Copy to a
this.copy(a);

// Register test
this.expected.put(a + ".java", b + ".java");

return this;
}

public void test() throws Exception {
final Path in = this.dir.resolve("a");
final Path out = this.dir.resolve("b");

final Mercury mercury = new Mercury();
this.mercuryConfigure.accept(mercury);
mercury.getProcessors().add(MercuryRemapper.create(this.mappings));
mercury.rewrite(in, out);

for (final String file : this.expected.values()) {
final Path path = out.resolve(file);

// First check the path exists
assertTrue(Files.exists(path), file + " doesn't exists!");

// Check the file matches the expected output
final String expected;
try (final InputStream is = RemapperTest.class.getResourceAsStream("/" + this.name + "/b/" + file)) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteStreams.copy(is, baos);
expected = baos.toString();
}
final String actual = new String(Files.readAllBytes(path));
assertEquals(expected, actual, "Remapped code for " + file + " does not match expected");
}
}

}
129 changes: 129 additions & 0 deletions src/test/java/org/cadixdev/mercury/test/RemappingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.cadixdev.mercury.Mercury;
import org.cadixdev.mercury.remapper.MercuryRemapper;
import org.eclipse.jdt.core.JavaCore;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
Expand All @@ -33,6 +34,134 @@

class RemappingTests {

@Test
@DisplayName("remaps class declaration")
void remapClassDeclaration() throws Exception {
new RemapperTest("remap-class")
.register("a", "Declaration")
.test();
}

@Test
@DisplayName("remaps field type")
void remapFieldType() throws Exception {
new RemapperTest("remap-class")
.copy("a")
.register("b", "b")
.test();
}

@Test
@DisplayName("remaps method type")
void remapMethodType() throws Exception {
new RemapperTest("remap-class")
.copy("a")
.register("c", "c")
.test();
}

@Test
@DisplayName("remaps method declaration")
void remapMethodDeclaration() throws Exception {
new RemapperTest("remap-method")
.register("a", "a")
.test();
}

@Test
@DisplayName("remaps method call")
void remapMethodCall() throws Exception {
new RemapperTest("remap-method")
.copy("a")
.register("b", "b")
.test();
}

@Test
@DisplayName("remaps field declaration")
void remapFieldDeclaration() throws Exception {
new RemapperTest("remap-field")
.register("a", "a")
.test();
}

@Test
@DisplayName("remaps field usage")
void remapFieldUsage() throws Exception {
new RemapperTest("remap-field")
.copy("a")
.register("b", "b")
.test();
}

@Test
@DisplayName("remaps enum constant declaration")
void remapEnumConstantDeclaration() throws Exception {
new RemapperTest("remap-field")
.register("c", "c")
.test();
}

@Test
@DisplayName("remaps enum constant usage")
void remapEnumConstantUsage() throws Exception {
new RemapperTest("remap-field")
.copy("c")
.register("d", "d")
.test();
}

@Test
@DisplayName("remaps single method parameter")
void remapSingleParameter() throws Exception {
new RemapperTest("remap-param")
.register("a", "a")
.test();
}

@Test
@DisplayName("remaps multiple method parameters")
void remapMultipleParameters() throws Exception {
new RemapperTest("remap-param")
.register("b", "b")
.test();
}

@Test
@DisplayName("remaps class reference in javadocs")
void remapJavadocClassReference() throws Exception {
new RemapperTest("remap-javadocs")
.copy("a")
.register("b", "b")
.test();
}

@Test
@DisplayName("remaps field reference in javadocs")
void remapJavadocFieldReference() throws Exception {
new RemapperTest("remap-javadocs")
.copy("a")
.register("c", "c")
.test();
}

@Test
@DisplayName("remaps method reference in javadocs")
void remapJavadocMethodReference() throws Exception {
new RemapperTest("remap-javadocs")
.copy("a")
.register("d", "d")
.test();
}

@Test
@DisplayName("remaps parameter reference in javadocs")
void remapJavadocParamReference() throws Exception {
new RemapperTest("remap-javadocs")
.register("e", "e")
.test();
}

// Mercury contains the following tests:
// 1. Simple remaps
// This test is used to verify that Mercury can remap simple things:
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/java-10/a/a.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

public class a {
}
15 changes: 15 additions & 0 deletions src/test/resources/java-10/a/b.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

public class b {
public static void main(final String[] args) {
final var instance = new a();
}
}
15 changes: 15 additions & 0 deletions src/test/resources/java-10/a/c.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

public class c {
public static void main(final String[] args) {
final var name = "Demo Name";
}
}
Loading