|
| 1 | +package diffr.diff; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import com.google.common.io.Files; |
| 5 | +import com.google.common.io.LineProcessor; |
| 6 | +import com.google.common.io.Resources; |
| 7 | +import diffr.util.instruction.Instruction; |
| 8 | +import diffr.util.instruction.InstructionComposer; |
| 9 | +import diffr.util.instruction.InstructionParser; |
| 10 | +import org.testng.annotations.DataProvider; |
| 11 | +import org.testng.annotations.Test; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.URISyntaxException; |
| 16 | +import java.nio.charset.Charset; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import static org.hamcrest.Matchers.is; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tests {@link Diffr}. |
| 26 | + * |
| 27 | + * @author Sarina Gurung |
| 28 | + * @author Jakub D Kozlowski |
| 29 | + * @since 0.3 |
| 30 | + */ |
| 31 | +public class DiffrTest { |
| 32 | + |
| 33 | + private static final String DEFAULT_PROVIDER = "default-provider"; |
| 34 | + |
| 35 | + @DataProvider(name = "default-provider") |
| 36 | + public Object[][] getFiles() throws URISyntaxException, IOException { |
| 37 | + |
| 38 | + final List<Object[]> files = Lists.newArrayList(); |
| 39 | + |
| 40 | + final File originalDir = new File(Resources.getResource("original").toURI()); |
| 41 | + for (final File originalFile : originalDir.listFiles()) { |
| 42 | + final File newFile = new File(Resources.getResource("new/" + originalFile.getName()).toURI()); |
| 43 | + final File patchFile = new File(Resources.getResource("patch/" + originalFile.getName()).toURI()); |
| 44 | + files.add(new Object[]{ |
| 45 | + Files.readLines(originalFile, Charset.defaultCharset()), |
| 46 | + Files.readLines(newFile, Charset.defaultCharset()), |
| 47 | + Files.readLines(patchFile, Charset.defaultCharset(), new LineProcessor<List<Instruction>>() { |
| 48 | + |
| 49 | + private List<Instruction> instructions = Lists.newArrayList(); |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean processLine(String s) throws IOException { |
| 53 | + instructions.add(InstructionParser.parseInstruction(s).get()); |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public List<Instruction> getResult() { |
| 59 | + return instructions; |
| 60 | + } |
| 61 | + }) |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + return files.toArray(new Object[][]{}); |
| 66 | + } |
| 67 | + |
| 68 | + @Test(expectedExceptions = NullPointerException.class) |
| 69 | + public void testConstructorNullOriginalFile() { |
| 70 | + new Diffr(null, Collections.EMPTY_LIST); |
| 71 | + } |
| 72 | + |
| 73 | + @Test(expectedExceptions = NullPointerException.class) |
| 74 | + public void testConstructorNullNewFile() { |
| 75 | + new Diffr(Collections.EMPTY_LIST, null); |
| 76 | + } |
| 77 | + |
| 78 | + @Test(dataProvider = DEFAULT_PROVIDER) |
| 79 | + public void testDiff(final List<String> originalFile, |
| 80 | + final List<String> newFile, |
| 81 | + final List<Instruction> patchFile) |
| 82 | + throws IOException, URISyntaxException { |
| 83 | + |
| 84 | + |
| 85 | + final Diffr d = new Diffr(originalFile, newFile); |
| 86 | + |
| 87 | + final Iterator<Instruction> actualInstructions = d.diff().iterator(); |
| 88 | + |
| 89 | + for (final Instruction expected : patchFile) { |
| 90 | + final Instruction actual = actualInstructions.next(); |
| 91 | + assertThat(InstructionComposer.composeString(actual), |
| 92 | + is(InstructionComposer.composeString(expected))); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments