|
| 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