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

チュートリアル Javaコーディング問題(JUnit版) #41

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
21 changes: 1 addition & 20 deletions contents/tutorial/coding-java-junit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,10 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>track.Main</mainClass>
</manifest>
</archive>
</configuration>
<version>3.10.1</version>
</plugin>
</plugins>
</build>
Expand Down
6 changes: 3 additions & 3 deletions contents/tutorial/coding-java-junit/solution/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public class App {
public static int add(int x, int y) {
return x + y;
}
public static int add(int x, int y) {
return x + y;
}
}
6 changes: 3 additions & 3 deletions contents/tutorial/coding-java-junit/src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public class App {
public static int add(int x, int y) {
return -1;
}
public static int add(int x, int y) {
return -1;
}
}
7 changes: 0 additions & 7 deletions contents/tutorial/coding-java-junit/src/main/java/Main.java

This file was deleted.

49 changes: 30 additions & 19 deletions contents/tutorial/coding-java-junit/src/test/java/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.PrintStream;
Expand All @@ -16,24 +15,36 @@

@Timeout(5)
public class AppTest {
@ParameterizedTest
@CsvFileSource(resources = "/public.testcases.csv")
void test(String input, String expected) throws IOException {
@Test
public void test_2_3() throws IOException {
__testOutput("src/test/resources/in/public/01.in", "src/test/resources/out/public/01.out");
}
@Test
public void test_15_8() throws IOException {
__testOutput("src/test/resources/in/public/02.in", "src/test/resources/out/public/02.out");
}
@Test
public void test_65_94() throws IOException {
__testOutput("src/test/resources/in/public/03.in", "src/test/resources/out/public/03.out");
}
snakazawa marked this conversation as resolved.
Show resolved Hide resolved

PrintStream origin = System.out;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
System.setOut(new PrintStream(bos, true, "utf-8"));
String[] args = Files.readString(Paths.get(input), StandardCharsets.UTF_8).trim().split(" ");
Util.runApp(args);
String result = new String(bos.toByteArray(), "utf-8").trim();
private void __testOutput(String input, String expected) throws IOException {
PrintStream origin = System.out;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
System.setOut(new PrintStream(bos, true, "utf-8"));
String[] args = Files.readString(Paths.get(input), StandardCharsets.UTF_8).trim().split(" ");
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
Util.runApp(x, y);
String result = new String(bos.toByteArray(), "utf-8").trim();

List<String> expectedContent = Files.readAllLines(Paths.get(expected));
String output = String.join("\n",expectedContent).trim();
List<String> expectedContent = Files.readAllLines(Paths.get(expected));
String output = String.join("\n",expectedContent).trim();

assertThat(result, is(output));
} finally {
System.setOut(origin);
}
}
assertThat(result, is(output));
} finally {
System.setOut(origin);
}
}
}
36 changes: 19 additions & 17 deletions contents/tutorial/coding-java-junit/src/test/java/Util.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import static org.junit.jupiter.api.Assertions.fail;

public class Util {
private static long TIMEOUT = 5_000;
private static long TIMEOUT = 5_000;

public static void runApp(String[] args) {
AppThread th = new AppThread(args);
try {
th.start();
th.join(TIMEOUT);
} catch (InterruptedException e) {
fail("Timeout " + TIMEOUT + "ms");
}
public static void runApp(int x, int y) {
AppThread th = new AppThread(x, y);
try {
th.start();
th.join(TIMEOUT);
} catch (InterruptedException e) {
fail("Timeout " + TIMEOUT + "ms");
}
}

private static class AppThread extends Thread {
private final String[] args;
private static class AppThread extends Thread {
private final int x;
private final int y;

AppThread(String[] args) {
this.args = args;
}
AppThread(int x, int y) {
this.x = x;
this.y = y;
}

public void run() {
Main.main(args);
}
public void run() {
System.out.print(App.add(x, y));
}
snakazawa marked this conversation as resolved.
Show resolved Hide resolved
}
}

This file was deleted.

2 changes: 0 additions & 2 deletions contents/tutorial/coding-java-junit/track.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ main: src/main/java/App.java
editable:
- src/main/java/App.java
readonly:
- src/main/java/Main.java
- src/test/java/AppTest.java
- src/test/java/Util.java
- src/test/resources/in/public/*
- src/test/resources/out/public/*
- src/test/resources/*public*
- pom.xml
test: >
mvn test;
Expand Down