Skip to content

Commit e51c54e

Browse files
committed
closes #7: Create test files for integration testing.
* Tests write to /tmp. * Refactored both Main classes for diffr.patch and diffr.diff.
1 parent 6c66f79 commit e51c54e

File tree

8 files changed

+286274
-20
lines changed

8 files changed

+286274
-20
lines changed

diff/src/main/java/diffr/diff/Main.java

+23-10
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,37 @@ public final class Main {
3636
*/
3737
private static void printUsage() {
3838
System.out.println("Usage: \n" +
39-
" diffr <original-file> <new-file>\n" +
40-
" diffr <original-file> <new-file> -o <output-file>");
39+
" diffr <original-file> <new-file>\n" +
40+
" diffr <original-file> <new-file> -o <output-file>");
4141
}
4242

4343
/**
4444
* Runs the diff tool on two files.
4545
*
4646
* @param args arguments to this tool.
47+
*
48+
* @return exit code.
4749
*/
48-
public static void main(String... args) throws IOException {
49-
50+
public static int run(String... args) {
5051
try {
5152
if (ArgumentsProcessor.containsHelpArgument(args)
5253
|| (2 != args.length
5354
&& 4 != args.length)) {
5455
printUsage();
55-
System.exit(-1);
56+
return -1;
5657
}
5758

5859
final File firstFile = new File(args[0]);
5960
final File secondFile = new File(args[1]);
6061

6162
if (!firstFile.exists()) {
6263
System.err.println("File " + firstFile + " not found.");
63-
System.exit(-1);
64+
return -1;
6465
}
6566

6667
if (!secondFile.exists()) {
6768
System.err.println("File " + secondFile + " not found.");
68-
System.exit(-1);
69+
return -1;
6970
}
7071

7172
final List<String> originalFile = Files.readLines(firstFile, Charset.defaultCharset());
@@ -84,16 +85,28 @@ public static void main(String... args) throws IOException {
8485
Instructions.writeInstruction(instruction, bufferedWriter);
8586
}
8687
bufferedWriter.close();
87-
} else {
88+
}
89+
else {
8890
for (final Instruction instruction : instructions) {
8991
System.out.println(InstructionComposer.composeString(instruction));
9092
}
9193
System.out.flush();
9294
}
9395

94-
System.exit(0);
95-
} catch (final IOException io) {
96+
return 0;
97+
}
98+
catch (final IOException io) {
9699
System.err.println("There was a problem reading the files: " + io);
100+
return -1;
97101
}
98102
}
103+
104+
/**
105+
* Invokes {@link #run(String...)} and calls {@link System#exit(int)}.
106+
*
107+
* @param args arguments to this tool.
108+
*/
109+
public static void main(String... args) {
110+
System.exit(run(args));
111+
}
99112
}

integration-tests/pom.xml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>diffr</groupId>
9+
<artifactId>parent</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<groupId>diffr</groupId>
14+
<artifactId>integration-tests</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
<packaging>jar</packaging>
17+
<name>${project.groupId}.${project.artifactId}</name>
18+
<description>Integration tests for patchr and diffr.</description>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>diffr</groupId>
23+
<artifactId>util</artifactId>
24+
<version>${current.version}</version>
25+
<scope>test</scope>
26+
<classifier>tests</classifier>
27+
</dependency>
28+
<dependency>
29+
<groupId>diffr</groupId>
30+
<artifactId>diff</artifactId>
31+
<version>${current.version}</version>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>diffr</groupId>
36+
<artifactId>patch</artifactId>
37+
<version>${current.version}</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.testng</groupId>
42+
<artifactId>testng</artifactId>
43+
<version>${testng.version}</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.hamcrest</groupId>
48+
<artifactId>hamcrest-all</artifactId>
49+
<version>${hamcrest.version}</version>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package diffr.integration;
2+
3+
import com.google.common.io.Files;
4+
import com.google.common.io.Resources;
5+
import diffr.patch.IllegalPatchFileException;
6+
import org.testng.annotations.Test;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.net.URISyntaxException;
11+
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.is;
14+
15+
/**
16+
* Tests diff/patch integration.
17+
*
18+
* @author Jakub D Kozlowski
19+
* @since 1.0
20+
*/
21+
public class DiffPatchIntegrationTest {
22+
23+
@Test
24+
public void testKernel01ToKernel26() throws IllegalPatchFileException, URISyntaxException, IOException {
25+
testDiffrPatchr("kernel01.txt", "kernel26.txt");
26+
}
27+
28+
@Test
29+
public void testKernel26ToKernel01() throws IllegalPatchFileException, URISyntaxException, IOException {
30+
testDiffrPatchr("kernel26.txt", "kernel01.txt");
31+
}
32+
33+
@Test
34+
public void testKernel01ToKernel33() throws IllegalPatchFileException, URISyntaxException, IOException {
35+
testDiffrPatchr("kernel01.txt", "kernel33.txt");
36+
}
37+
38+
@Test
39+
public void testKernel33ToKernel01() throws IllegalPatchFileException, URISyntaxException, IOException {
40+
testDiffrPatchr("kernel33.txt", "kernel01.txt");
41+
}
42+
43+
@Test
44+
public void testKernel26ToKernel33() throws IllegalPatchFileException, URISyntaxException, IOException {
45+
testDiffrPatchr("kernel26.txt", "kernel33.txt");
46+
}
47+
48+
@Test
49+
public void testKernel33ToKernel26() throws IllegalPatchFileException, URISyntaxException, IOException {
50+
testDiffrPatchr("kernel33.txt", "kernel26.txt");
51+
}
52+
53+
/**
54+
* Runs diffr on {@code originalFileName} and {@code newFileName}, runs patchr on the resulting patch file and
55+
* {@code originalFileName} and compares the result to {@code newFileName}.
56+
*
57+
* @param originalFileName file name of the original file.
58+
* @param newFileName file name of the new file.
59+
*
60+
* @throws IOException if there is a problem reading or writing the files.
61+
* @throws URISyntaxException if the file names cannot be found.
62+
*/
63+
public static void testDiffrPatchr(final String originalFileName, final String newFileName)
64+
throws IOException, URISyntaxException {
65+
66+
final File originalFile = getFile(originalFileName);
67+
final File newFile = getFile(newFileName);
68+
69+
final File tmpPatchFile = File.createTempFile("diffr", "patch", Files.createTempDir());
70+
final File tmpNewFile = File.createTempFile("diffr", "new", Files.createTempDir());
71+
72+
diffr.diff.Main.run(originalFile.getAbsolutePath(), newFile.getAbsolutePath(), "-o",
73+
tmpPatchFile.getAbsolutePath());
74+
75+
diffr.patch.Main.run(originalFile.getAbsolutePath(), tmpPatchFile.getAbsolutePath(), "-o",
76+
tmpNewFile.getAbsolutePath());
77+
78+
assertThat(Files.equal(newFile, tmpNewFile), is(true));
79+
}
80+
81+
/**
82+
* Gets the {@code fileName} from the classloader.
83+
*
84+
* @param fileName name of file to get.
85+
*
86+
* @return {@code fileName} from the classloader.
87+
*/
88+
public static File getFile(final String fileName) throws URISyntaxException, IOException {
89+
return new File(Resources.getResource(fileName).toURI());
90+
}
91+
}

0 commit comments

Comments
 (0)