|
21 | 21 |
|
22 | 22 | package com.igormaznitsa.jcp.usecases; |
23 | 23 |
|
| 24 | +import static java.util.Objects.requireNonNull; |
24 | 25 | import static org.junit.Assert.assertEquals; |
25 | 26 | import static org.junit.Assert.assertTrue; |
26 | 27 | import static org.junit.Assert.fail; |
@@ -50,83 +51,90 @@ public void before() throws Exception { |
50 | 51 |
|
51 | 52 | final File base = new File(testDir, this.getClass().getName().replace('.', File.separatorChar)); |
52 | 53 |
|
53 | | - final File simulfolder = new File(testDir.getParentFile(), "usecase_tests"); |
54 | | - if (!simulfolder.isDirectory()) { |
55 | | - assertTrue("Can't make folders for simulation", simulfolder.mkdirs()); |
| 54 | + final File simulationFolder = new File(testDir.getParentFile(), "usecase_tests"); |
| 55 | + if (!simulationFolder.isDirectory()) { |
| 56 | + assertTrue("Can't make folders for simulation", simulationFolder.mkdirs()); |
56 | 57 | } |
57 | 58 |
|
58 | | - tmpResultFolder = new TemporaryFolder(simulfolder); |
59 | | - tmpResultFolder.create(); |
| 59 | + this.tmpResultFolder = new TemporaryFolder(simulationFolder); |
| 60 | + this.tmpResultFolder.create(); |
60 | 61 |
|
61 | | - sourceFolder = new File(base, "src"); |
62 | | - etalonFolder = new File(base, "etl"); |
| 62 | + this.sourceFolder = new File(base, "src"); |
| 63 | + this.etalonFolder = new File(base, "etl"); |
63 | 64 | } |
64 | 65 |
|
65 | 66 | @After |
66 | 67 | public void after() throws Exception { |
67 | | - if (deleteResult()) { |
| 68 | + if (this.isDeleteTemporaryFolder()) { |
68 | 69 | try { |
69 | 70 | FileUtils.cleanDirectory(tmpResultFolder.getRoot()); |
70 | 71 | } finally { |
71 | | - tmpResultFolder.delete(); |
| 72 | + this.tmpResultFolder.delete(); |
72 | 73 | } |
73 | 74 | } |
74 | 75 | } |
75 | 76 |
|
76 | | - public boolean deleteResult() { |
| 77 | + public boolean isDeleteTemporaryFolder() { |
77 | 78 | return true; |
78 | 79 | } |
79 | 80 |
|
80 | 81 | public abstract void check(PreprocessorContext context, JcpPreprocessor.Statistics stat) |
81 | 82 | throws Exception; |
82 | 83 |
|
83 | | - private void assertFolder(final File folder1, final File folder2, final boolean ignoreEOL) |
| 84 | + private void assertFolder(final File etalonFolder, final File checkFolder, |
| 85 | + final boolean ignoreEOL) |
84 | 86 | throws Exception { |
85 | | - assertTrue("Folder 1 must be folder", folder1.isDirectory()); |
86 | | - assertTrue("Folder 2 must be folder", folder2.isDirectory()); |
87 | | - |
88 | | - final File[] folder1files = folder1.listFiles(); |
89 | | - File[] folde2files = folder2.listFiles(); |
90 | | - assertEquals("Must have the same number of files and folders", folder1files.length, |
91 | | - folde2files.length); |
92 | | - |
93 | | - for (final File f : folder1files) { |
94 | | - final File f2 = new File(folder2, f.getName()); |
95 | | - if (!f2.exists()) { |
96 | | - fail("Doesn't exist :" + f2.getAbsolutePath()); |
| 87 | + assertTrue("Etalon folder must be a folder", etalonFolder.isDirectory()); |
| 88 | + assertTrue("Checked folder must be folder", checkFolder.isDirectory()); |
| 89 | + |
| 90 | + final File[] etalonFolderFiles = requireNonNull(etalonFolder.listFiles()); |
| 91 | + final File[] checkFolderFiles = requireNonNull(checkFolder.listFiles()); |
| 92 | + assertEquals("Must have the same number of files and folders", etalonFolderFiles.length, |
| 93 | + checkFolderFiles.length); |
| 94 | + |
| 95 | + for (final File etalonFile : etalonFolderFiles) { |
| 96 | + final File checkFile = new File(checkFolder, etalonFile.getName()); |
| 97 | + if (!checkFile.exists()) { |
| 98 | + fail("Can't find generated file :" + checkFile.getAbsolutePath()); |
97 | 99 | } |
98 | | - if (f.isFile() && !f2.isFile()) { |
99 | | - fail("Must be file : " + f2.getAbsolutePath()); |
100 | | - } else if (f.isDirectory()) { |
101 | | - if (!f2.isDirectory()) { |
102 | | - fail("Must be file : " + f2.getAbsolutePath()); |
| 100 | + if (etalonFile.isFile() && !checkFile.isFile()) { |
| 101 | + fail("Expected file: " + checkFile.getAbsolutePath()); |
| 102 | + } else if (etalonFile.isDirectory()) { |
| 103 | + if (!checkFile.isDirectory()) { |
| 104 | + fail("Expected folder: " + checkFile.getAbsolutePath()); |
103 | 105 | } else { |
104 | | - assertFolder(f, f2, ignoreEOL); |
| 106 | + assertFolder(etalonFile, checkFile, ignoreEOL); |
105 | 107 | } |
106 | 108 | } else { |
107 | | - final boolean equalsLength = ignoreEOL ? true : f.length() == f2.length(); |
108 | | - if (!equalsLength) { |
109 | | - String fileOne = FileUtils.readFileToString(f, StandardCharsets.UTF_8); |
110 | | - String fileTwo = FileUtils.readFileToString(f2, StandardCharsets.UTF_8); |
111 | | - |
112 | | - System.err.println("FILE ONE====================="); |
113 | | - System.err.println(fileOne); |
114 | | - System.err.println("============================="); |
115 | | - |
116 | | - System.err.println("FILE TWO====================="); |
117 | | - System.err.println(fileTwo); |
118 | | - System.err.println("============================="); |
119 | | - |
120 | | - if (ignoreEOL) { |
121 | | - assertEquals("File content must be same", fileOne.replace('\r', ' ').replace('\n', ' '), |
122 | | - fileTwo.replace('\r', ' ').replace('\n', ' ')); |
123 | | - } else { |
124 | | - assertEquals("File content must be same", fileOne, fileTwo); |
| 109 | + if (ignoreEOL) { |
| 110 | + final String[] etalonLines = |
| 111 | + FileUtils.readFileToString(etalonFile, StandardCharsets.UTF_8).split("\\R", -1); |
| 112 | + final String[] checkLines = |
| 113 | + FileUtils.readFileToString(checkFile, StandardCharsets.UTF_8).split("\\R", -1); |
| 114 | + |
| 115 | + if (etalonLines.length != checkLines.length) { |
| 116 | + System.err.println( |
| 117 | + "----Etalon----\n" + String.join(System.lineSeparator(), etalonLines)); |
| 118 | + System.err.println( |
| 119 | + "----Checking----\n" + String.join(System.lineSeparator(), checkLines)); |
| 120 | + fail("Different number of lines, expected " + etalonLines.length + " but read " + |
| 121 | + checkLines.length + " : " + checkFile.getAbsolutePath()); |
| 122 | + } |
| 123 | + for (int j = 0; j < etalonLines.length; j++) { |
| 124 | + final String etalon = etalonLines[j]; |
| 125 | + final String check = checkLines[j]; |
| 126 | + if (!etalon.equals(check)) { |
| 127 | + fail("Difference at line " + (j + 1) + ": etalon='" + etalon + "', check='" + check + |
| 128 | + '\''); |
| 129 | + } |
| 130 | + } |
| 131 | + } else { |
| 132 | + final long checksumEtalon = FileUtils.checksumCRC32(etalonFile); |
| 133 | + final long checksumTested = FileUtils.checksumCRC32(checkFile); |
| 134 | + if (checksumEtalon != checksumTested) { |
| 135 | + fail("Wrong checksum, etalon file = " + etalonFile.getAbsolutePath() + |
| 136 | + " , check file " + checkFile.getAbsolutePath()); |
125 | 137 | } |
126 | | - } |
127 | | - if (!ignoreEOL) { |
128 | | - assertEquals("Checksum must be equal (" + f.getName() + ')', FileUtils.checksumCRC32(f), |
129 | | - FileUtils.checksumCRC32(f2)); |
130 | 138 | } |
131 | 139 | } |
132 | 140 | } |
@@ -158,19 +166,17 @@ protected PreprocessorContext createPreprocessorContext(final File baseFolder) { |
158 | 166 | } |
159 | 167 |
|
160 | 168 | @Test |
161 | | - public final void main() throws Exception { |
| 169 | + public final void executeTest() throws Exception { |
162 | 170 | final PreprocessorContext context = |
163 | 171 | createPreprocessorContext(new File("some_impossible_folder_121212")); |
164 | | - tuneDefaultContextOptions(context); |
165 | | - tuneContext(context); |
166 | | - |
| 172 | + this.tuneDefaultContextOptions(context); |
| 173 | + this.tuneContext(context); |
167 | 174 | System.setProperty("jcp.line.separator", "\n"); |
168 | 175 |
|
169 | | - JcpPreprocessor preprocessor = new JcpPreprocessor(context); |
170 | | - final JcpPreprocessor.Statistics stat = preprocessor.execute(); |
171 | | - |
172 | | - assertFolder(etalonFolder, tmpResultFolder.getRoot(), this.isIgnoreEolInCheck()); |
| 176 | + final JcpPreprocessor preprocessor = new JcpPreprocessor(context); |
| 177 | + final JcpPreprocessor.Statistics preprocessorStatistics = preprocessor.execute(); |
173 | 178 |
|
174 | | - check(context, stat); |
| 179 | + this.assertFolder(this.etalonFolder, this.tmpResultFolder.getRoot(), this.isIgnoreEolInCheck()); |
| 180 | + this.check(context, preprocessorStatistics); |
175 | 181 | } |
176 | 182 | } |
0 commit comments