Skip to content

Commit b535553

Browse files
committed
improved tests
1 parent 14afd76 commit b535553

File tree

6 files changed

+149
-62
lines changed

6 files changed

+149
-62
lines changed

jcp/src/test/java/com/igormaznitsa/jcp/usecases/AbstractUseCaseTest.java

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package com.igormaznitsa.jcp.usecases;
2323

24+
import static java.util.Objects.requireNonNull;
2425
import static org.junit.Assert.assertEquals;
2526
import static org.junit.Assert.assertTrue;
2627
import static org.junit.Assert.fail;
@@ -50,83 +51,90 @@ public void before() throws Exception {
5051

5152
final File base = new File(testDir, this.getClass().getName().replace('.', File.separatorChar));
5253

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());
5657
}
5758

58-
tmpResultFolder = new TemporaryFolder(simulfolder);
59-
tmpResultFolder.create();
59+
this.tmpResultFolder = new TemporaryFolder(simulationFolder);
60+
this.tmpResultFolder.create();
6061

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");
6364
}
6465

6566
@After
6667
public void after() throws Exception {
67-
if (deleteResult()) {
68+
if (this.isDeleteTemporaryFolder()) {
6869
try {
6970
FileUtils.cleanDirectory(tmpResultFolder.getRoot());
7071
} finally {
71-
tmpResultFolder.delete();
72+
this.tmpResultFolder.delete();
7273
}
7374
}
7475
}
7576

76-
public boolean deleteResult() {
77+
public boolean isDeleteTemporaryFolder() {
7778
return true;
7879
}
7980

8081
public abstract void check(PreprocessorContext context, JcpPreprocessor.Statistics stat)
8182
throws Exception;
8283

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)
8486
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());
9799
}
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());
103105
} else {
104-
assertFolder(f, f2, ignoreEOL);
106+
assertFolder(etalonFile, checkFile, ignoreEOL);
105107
}
106108
} 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());
125137
}
126-
}
127-
if (!ignoreEOL) {
128-
assertEquals("Checksum must be equal (" + f.getName() + ')', FileUtils.checksumCRC32(f),
129-
FileUtils.checksumCRC32(f2));
130138
}
131139
}
132140
}
@@ -158,19 +166,17 @@ protected PreprocessorContext createPreprocessorContext(final File baseFolder) {
158166
}
159167

160168
@Test
161-
public final void main() throws Exception {
169+
public final void executeTest() throws Exception {
162170
final PreprocessorContext context =
163171
createPreprocessorContext(new File("some_impossible_folder_121212"));
164-
tuneDefaultContextOptions(context);
165-
tuneContext(context);
166-
172+
this.tuneDefaultContextOptions(context);
173+
this.tuneContext(context);
167174
System.setProperty("jcp.line.separator", "\n");
168175

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();
173178

174-
check(context, stat);
179+
this.assertFolder(this.etalonFolder, this.tmpResultFolder.getRoot(), this.isIgnoreEolInCheck());
180+
this.check(context, preprocessorStatistics);
175181
}
176182
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2019 Igor Maznitsa (http://www.igormaznitsa.com)
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.igormaznitsa.jcp.usecases;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
import com.igormaznitsa.jcp.JcpPreprocessor;
27+
import com.igormaznitsa.jcp.context.PreprocessorContext;
28+
import java.util.Collections;
29+
30+
public class TextBufferVariablesTest extends AbstractUseCaseTest {
31+
32+
@Override
33+
protected void tuneContext(final PreprocessorContext context) {
34+
context.setExcludeExtensions(Collections.singletonList("bin"));
35+
}
36+
37+
@Override
38+
public void check(final PreprocessorContext context, final JcpPreprocessor.Statistics stat)
39+
throws Exception {
40+
assertEquals(1, stat.getPreprocessed());
41+
assertEquals(0, stat.getCopied());
42+
assertEquals(1, context.findAllInputFiles().size());
43+
assertEquals(1, context.findAllProducedFiles().size());
44+
}
45+
46+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
test var has HUZZAAA! value
2-
test var has HUZZAAA! value
2+
test var has HUZZAAA! value
33
test var has /*$TEST_VAR$*/ value
4-
test var has /*$TEST_VAR$*/ value
4+
test var has /*$TEST_VAR$*/ value

jcp/src/test/resources/com/igormaznitsa/jcp/usecases/SpacesBeforeDirectivesNotAllowedTest/etl/body.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ text1
33
// #else
44
text2
55
// #endif
6+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Some block text which
2+
will be placed into a variable as multiline text
3+
and prefix buffer is used to accumulate it
4+
---
5+
Some block text which
6+
will be placed into a variable as multiline text
7+
and prefix buffer is used to accumulate it
8+
9+
===
10+
---
11+
Some block text which
12+
will be placed into a variable as multiline text
13+
and prefix buffer is used to accumulate it
14+
15+
===
16+
17+
...
18+
defined postfix
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//#postfix+
2+
defined postfix
3+
//#postfix-
4+
//#prefix+
5+
Some block text which
6+
will be placed into a variable as multiline text
7+
and prefix buffer is used to accumulate it
8+
//#prefix-
9+
//#local multiline_prefix = jcp.text.buffer.prefix
10+
//#global jcp.text.buffer.prefix="set prefix"
11+
//#global jcp.text.buffer.postfix="set postfix"
12+
---
13+
//$/*$multiline_prefix$*/
14+
===
15+
//$/*$jcp.text.buffer.middle$*/
16+
...

0 commit comments

Comments
 (0)