Formatierung

pull/11/head
Markus Kreth 1 year ago
parent 5ad43ca39f
commit 216c3922fa
  1. 276
      src/test/java/de/kreth/property2java/GeneratorTests.java
  2. 49
      src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java

@ -33,151 +33,137 @@ import de.kreth.property2java.processor.Format;
class GeneratorTests { class GeneratorTests {
private String path = "application.properties"; private String path = "application.properties";
private Configuration config; private Configuration config;
private Generator generator; private Generator generator;
@BeforeEach @BeforeEach
void setUp() throws Exception { void setUp() throws Exception {
Map<String, Reader> input = new HashMap<>(); Map<String, Reader> input = new HashMap<>();
input.put(path, testProperties()); input.put(path, testProperties());
config = Mockito.spy(TestImplConfig.class); config = Mockito.spy(TestImplConfig.class);
when(config.getRootPath()).thenReturn(new File(".").toPath()); when(config.getRootPath()).thenReturn(new File(".").toPath());
when(config.getFormat()).thenReturn(Format.WithInitializer); when(config.getFormat()).thenReturn(Format.WithInitializer);
when(config.getInput()).thenReturn(input); when(config.getInput()).thenReturn(input);
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
when(config.outputCharset()).thenCallRealMethod(); when(config.outputCharset()).thenCallRealMethod();
generator = new Generator(config); generator = new Generator(config);
} }
@Test @Test
void testClassDefinition() throws IOException, GeneratorException { void testClassDefinition() throws IOException, GeneratorException {
when(config.getPackage()).thenReturn("de.kreth.property2java"); when(config.getPackage()).thenReturn("de.kreth.property2java");
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
StringWriter out = new StringWriter(); StringWriter out = new StringWriter();
when(config.outWriter(anyString())).thenReturn(out); when(config.outWriter(anyString())).thenReturn(out);
generator.start(); generator.start();
String sourceCode = out.toString().trim(); String sourceCode = out.toString().trim();
StringTokenizer sourceTokenizer = new StringTokenizer(sourceCode, "\n"); StringTokenizer sourceTokenizer = new StringTokenizer(sourceCode, "\n");
String linePackage = null; String linePackage = null;
String lineClass = null; String lineClass = null;
int countOpenBaces = 0; int countOpenBaces = 0;
int countCloseBaces = 0; int countCloseBaces = 0;
while (sourceTokenizer.hasMoreTokens()) { while (sourceTokenizer.hasMoreTokens()) {
String line = sourceTokenizer.nextToken(); String line = sourceTokenizer.nextToken();
if (line.trim().startsWith("package")) { if (line.trim().startsWith("package")) {
linePackage = line; linePackage = line;
} else if (line.trim().startsWith("public enum")) { } else if (line.trim().startsWith("public enum")) {
lineClass = line; lineClass = line;
} }
if (line.contains("{")) { if (line.contains("{")) {
countOpenBaces++; countOpenBaces++;
} }
if (line.contains("}")) { if (line.contains("}")) {
countCloseBaces++; countCloseBaces++;
} }
}
assertEquals(countCloseBaces, countOpenBaces,
"Count of Braces doesn't match. Open = " + countOpenBaces + ", Close = " + countCloseBaces);
assertNotNull(linePackage);
assertNotNull(lineClass);
assertThat(linePackage,
Matchers.stringContainsInOrder(Arrays.asList("package", "de.kreth.property2java", ";")));
assertThat(lineClass,
Matchers.stringContainsInOrder(Arrays.asList("public", "enum", "Application_Properties")));
}
@Test
void testOneInputGeneratesOneOutput() throws IOException, GeneratorException {
Writer out = mock(Writer.class);
Writer nonOut = mock(Writer.class);
when(config.outWriter(anyString())).thenReturn(out, nonOut);
generator.start();
verify(out).close();
verify(nonOut, never()).close();
verify(nonOut, never()).flush();
} }
assertEquals(countCloseBaces, countOpenBaces, @Test
"Count of Braces doesn't match. Open = " + countOpenBaces + ", Close = " + countCloseBaces); void testKeys() throws IOException, GeneratorException {
assertNotNull(linePackage); StringWriter out = new StringWriter();
assertNotNull(lineClass); when(config.outWriter(anyString())).thenReturn(out);
generator.start();
assertThat(linePackage,
Matchers.stringContainsInOrder(Arrays.asList("package", "de.kreth.property2java", ";"))); List<String> lines = out.toString().lines().filter(line -> line.contains(" (\"")).collect(Collectors.toList());
assertThat(lineClass, assertEquals(21, lines.size());
Matchers.stringContainsInOrder(Arrays.asList("public", "enum", "Application_Properties"))); assertLineMatch(lines, "label", "label");
assertLineMatch(lines, "label_addarticle", "label.addarticle");
} assertLineMatch(lines, "label_user_register", "label.user.register");
assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror");
@Test assertLineMatch(lines, "message_invoiceitem_startbeforeend", "message.invoiceitem.startbeforeend");
void testOneInputGeneratesOneOutput() throws IOException, GeneratorException { assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", "message.invoiceitem.allfieldsmustbeset");
}
Writer out = mock(Writer.class);
Writer nonOut = mock(Writer.class); private void assertLineMatch(List<String> lines, String key, String expected) {
when(config.outWriter(anyString())).thenReturn(out, nonOut); Optional<String> found = lines.stream().filter(line -> keyMatches(line, key)).findFirst();
generator.start();
verify(out).close(); assertTrue(found.isPresent(), "No line found with key = " + key);
verify(nonOut, never()).close(); final String line = found.get().trim();
verify(nonOut, never()).flush(); int indexEquals = line.indexOf('(');
} String value = line.substring(indexEquals + 1).trim().substring(1);
value = value.substring(0, value.length() - 3);
@Test assertEquals(expected, value, "Line \"" + line + "\" don't match expected Value \"" + expected + "\"");
void testKeys() throws IOException, GeneratorException {
}
StringWriter out = new StringWriter();
when(config.outWriter(anyString())).thenReturn(out); private boolean keyMatches(String line, String key) {
generator.start(); line = line.toLowerCase();
key = key.toLowerCase();
List<String> lines = out.toString().lines().filter(line -> line.contains(" (\"")) return line.contains("\t" + key + " ");
.collect(Collectors.toList()); }
assertEquals(21, lines.size()); private StringReader testProperties() {
assertLineMatch(lines, "label", "label"); return new StringReader("\r\n" + "label = \r\n" + "\r\n" + "label.addarticle = Add Article\r\n"
assertLineMatch(lines, "label_addarticle", "label.addarticle"); + "label.cancel = Cancel\r\n" + "label.close = Close\r\n"
assertLineMatch(lines, "label_user_register", "label.user.register"); + "label.delete = Delete\r\n" + "label.discart = Discart\r\n"
assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror"); + "label.loggedin = Logged in:\r\n" + "label.logout = Logout\r\n"
assertLineMatch(lines, "message_invoiceitem_startbeforeend", + "label.ok = OK\r\n" + "label.store = Store\r\n"
"message.invoiceitem.startbeforeend"); + "label.preview = Preview\r\n" + "label.open = Open\r\n"
assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", + "label.user.register = Register\r\n" + "\r\n"
"message.invoiceitem.allfieldsmustbeset"); + "message.article.priceerror = Please set the price.\r\n"
} + "message.delete.text = Delete {0}?\r\n"
+ "message.delete.title = Really delete?\r\n"
private void assertLineMatch(List<String> lines, String key, String expected) { + "message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \\r\\n"
Optional<String> found = lines.stream().filter(line -> keyMatches(line, key)) + " empty!\r\n"
.findFirst(); + "message.invoiceitem.startbeforeend = End must be later than start.\r\n"
+ "message.user.create.success = Thanks {0} created!\r\n"
assertTrue(found.isPresent(), "No line found with key = " + key); + "message.user.loginfailure = Login Error! Wrong user or password?\r\n"
final String line = found.get().trim(); + "message.user.passwordmissmatch = Passwords don't match.\r\n" + "");
int indexEquals = line.indexOf('('); }
String value = line.substring(indexEquals + 1).trim().substring(1);
value = value.substring(0, value.length() - 3);
assertEquals(expected, value, "Line \"" + line + "\" don't match expected Value \"" + expected + "\"");
}
private boolean keyMatches(String line, String key) {
line = line.toLowerCase();
key = key.toLowerCase();
return line.contains("\t" + key + " ");
}
private StringReader testProperties() {
return new StringReader("\r\n" +
"label = \r\n" +
"\r\n" +
"label.addarticle = Add Article\r\n" +
"label.cancel = Cancel\r\n" +
"label.close = Close\r\n" +
"label.delete = Delete\r\n" +
"label.discart = Discart\r\n" +
"label.loggedin = Logged in:\r\n" +
"label.logout = Logout\r\n" +
"label.ok = OK\r\n" +
"label.store = Store\r\n" +
"label.preview = Preview\r\n" +
"label.open = Open\r\n" +
"label.user.register = Register\r\n" +
"\r\n" +
"message.article.priceerror = Please set the price.\r\n" +
"message.delete.text = Delete {0}?\r\n" +
"message.delete.title = Really delete?\r\n" +
"message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \\r\\n" +
" empty!\r\n" +
"message.invoiceitem.startbeforeend = End must be later than start.\r\n" +
"message.user.create.success = Thanks {0} created!\r\n" +
"message.user.loginfailure = Login Error! Wrong user or password?\r\n" +
"message.user.passwordmissmatch = Passwords don't match.\r\n" +
"");
}
} }

@ -18,29 +18,28 @@ import org.mockito.MockitoAnnotations;
public class Property2JavaGeneratorTest { public class Property2JavaGeneratorTest {
private Property2JavaGenerator processor; private Property2JavaGenerator processor;
@Mock @Mock
private ProcessingEnvironment processingEnv; private ProcessingEnvironment processingEnv;
@Mock @Mock
private RoundEnvironment roundEnv; private RoundEnvironment roundEnv;
private Set<TypeElement> annotations; private Set<TypeElement> annotations;
@Mock @Mock
private Messager messanger; private Messager messanger;
@BeforeEach @BeforeEach
void initProcesor() { void initProcesor() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
annotations = new HashSet<>(); annotations = new HashSet<>();
processor = new Property2JavaGenerator(); processor = new Property2JavaGenerator();
processor.init(processingEnv); processor.init(processingEnv);
when(processingEnv.getMessager()).thenReturn(messanger); when(processingEnv.getMessager()).thenReturn(messanger);
} }
@Test @Test
void testGeneratorInitializedCorrectly() { void testGeneratorInitializedCorrectly() {
when(roundEnv.getElementsAnnotatedWith(ArgumentMatchers.any(Class.class))) when(roundEnv.getElementsAnnotatedWith(ArgumentMatchers.any(Class.class))).thenReturn(annotations);
.thenReturn(annotations); processor.process(annotations, roundEnv);
processor.process(annotations, roundEnv); }
}
} }

Loading…
Cancel
Save