Compare commits

...

3 Commits

  1. 5
      src/main/java/de/kreth/property2java/Configuration.java
  2. 12
      src/main/java/de/kreth/property2java/Generator.java
  3. 6
      src/main/java/de/kreth/property2java/cli/CliConfig.java
  4. 2
      src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Javas.java
  5. 20
      src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java
  6. 7
      src/test/java/de/kreth/property2java/ConfigurationTest.java
  7. 62
      src/test/java/de/kreth/property2java/GeneratorTests.java
  8. 27
      src/test/java/de/kreth/property2java/TestImplConfig.java
  9. 3
      src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java

@ -53,10 +53,7 @@ public interface Configuration {
default String mapFilenameToClassName(String fileName) {
String path = Regex.PATTERN.matcher(fileName)
.replaceAll(".")
.replaceAll("\\.", "_")
.replaceAll(" ", "_")
String path = Regex.PATTERN.matcher(fileName).replaceAll(".").replaceAll("\\.", "_").replaceAll(" ", "_")
.replaceAll("/", "_");
path = WordUtils.capitalize(path, '_');
return path;

@ -100,15 +100,9 @@ public class Generator {
ArgumentConfiguration.Builder config = new ArgumentConfiguration.Builder();
rescources
.stream()
.map(URL::getFile)
.map(File::new)
.map(File::getAbsolutePath)
.forEach(config::addPropFile);
config.setPackageName(locationClass.getPackageName())
.setTarget(relativeTargetDir);
rescources.stream().map(URL::getFile).map(File::new).map(File::getAbsolutePath).forEach(config::addPropFile);
config.setPackageName(locationClass.getPackageName()).setTarget(relativeTargetDir);
Generator g = new Generator(config.build());
g.start();

@ -35,12 +35,10 @@ public class CliConfig {
for (String value : cmd.getOptionValues("f")) {
builder.addPropFile(value);
}
}
catch (MissingOptionException e) {
} catch (MissingOptionException e) {
printHelp();
throw new IllegalStateException(e);
}
catch (ParseException e) {
} catch (ParseException e) {
throw new IOException("Unable to parse Arguments", e);
}
}

@ -15,7 +15,7 @@ import java.lang.annotation.Target;
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface GenerateResourceBundleProperty2Javas {
@interface GenerateResourceBundleProperty2Javas {
GenerateResourceBundleProperty2Java[] value();
}

@ -31,18 +31,15 @@ public class Property2JavaGenerator extends AbstractProcessor {
processGenerateProperty2Java(roundEnv);
processGenerateResourceBundleProperty2Javas(roundEnv);
} else {
processingEnv.getMessager().printMessage(Kind.NOTE,
"finished working on annotation " + annotations);
processingEnv.getMessager().printMessage(Kind.NOTE, "finished working on annotation " + annotations);
}
return true;
}
private void processGenerateProperty2Java(RoundEnvironment roundEnv) {
processingEnv.getMessager().printMessage(Kind.NOTE,
"Processing annotation " + GenerateProperty2Java.class);
processingEnv.getMessager().printMessage(Kind.NOTE, "Processing annotation " + GenerateProperty2Java.class);
Set<? extends Element> elementsAnnotatedWith = roundEnv
.getElementsAnnotatedWith(GenerateProperty2Java.class);
Set<? extends Element> elementsAnnotatedWith = roundEnv.getElementsAnnotatedWith(GenerateProperty2Java.class);
for (Element element : elementsAnnotatedWith) {
GenerateProperty2Java generateAnnotation = element.getAnnotation(GenerateProperty2Java.class);
@ -71,20 +68,15 @@ public class Property2JavaGenerator extends AbstractProcessor {
}
private void generateElementProperties(Element element, List<String> resources, Format format) {
processingEnv.getMessager().printMessage(Kind.NOTE,
"Generating Java for " + Arrays.asList(resources));
processingEnv.getMessager().printMessage(Kind.NOTE, "Generating Java for " + Arrays.asList(resources));
try {
ProcessorConfiguration
.builder(processingEnv.getFiler(), element)
.addAll(resources)
.withFormat(format)
ProcessorConfiguration.builder(processingEnv.getFiler(), element).addAll(resources).withFormat(format)
.startGeneration();
} catch (IOException | GeneratorException e) {
StringWriter out = new StringWriter();
e.printStackTrace(new PrintWriter(out));
out.flush();
processingEnv.getMessager().printMessage(Kind.ERROR, "Exception " + e + "\n" + out.toString(),
element);
processingEnv.getMessager().printMessage(Kind.ERROR, "Exception " + e + "\n" + out.toString(), element);
}
}

@ -16,18 +16,18 @@ import org.mockito.Mockito;
class ConfigurationTest {
private Configuration config;
private TestImplConfig config;
@BeforeEach
void initConfig() {
config = Mockito.mock(Configuration.class);
config = Mockito.spy(TestImplConfig.class);
}
@Test
void defaultWriterIsFileWriter() throws IOException {
when(config.outWriter(anyString())).thenCallRealMethod();
when(config.getRootPath()).thenReturn(new File(".").toPath());
when(config.outWriter(anyString())).thenCallRealMethod();
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
when(config.outputCharset()).thenCallRealMethod();
@ -50,5 +50,4 @@ class ConfigurationTest {
className = config.mapFilenameToClassName("application_en_US.properties");
assertEquals("Application_Properties", className);
}
}

@ -10,6 +10,7 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
@ -26,6 +27,9 @@ import java.util.stream.Collectors;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import de.kreth.property2java.processor.Format;
class GeneratorTests {
@ -40,7 +44,9 @@ class GeneratorTests {
Map<String, Reader> input = new HashMap<>();
input.put(path, testProperties());
config = mock(Configuration.class);
config = Mockito.spy(TestImplConfig.class);
when(config.getRootPath()).thenReturn(new File(".").toPath());
when(config.getFormat()).thenReturn(Format.WithInitializer);
when(config.getInput()).thenReturn(input);
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
when(config.outputCharset()).thenCallRealMethod();
@ -113,23 +119,19 @@ class GeneratorTests {
when(config.outWriter(anyString())).thenReturn(out);
generator.start();
List<String> lines = out.toString().lines().filter(line -> line.contains(" (\""))
.collect(Collectors.toList());
List<String> lines = out.toString().lines().filter(line -> line.contains(" (\"")).collect(Collectors.toList());
assertEquals(21, lines.size());
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");
assertLineMatch(lines, "message_invoiceitem_startbeforeend",
"message.invoiceitem.startbeforeend");
assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset",
"message.invoiceitem.allfieldsmustbeset");
assertLineMatch(lines, "message_invoiceitem_startbeforeend", "message.invoiceitem.startbeforeend");
assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", "message.invoiceitem.allfieldsmustbeset");
}
private void assertLineMatch(List<String> lines, String key, String expected) {
Optional<String> found = lines.stream().filter(line -> keyMatches(line, key))
.findFirst();
Optional<String> found = lines.stream().filter(line -> keyMatches(line, key)).findFirst();
assertTrue(found.isPresent(), "No line found with key = " + key);
final String line = found.get().trim();
@ -147,31 +149,21 @@ class GeneratorTests {
}
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" +
"");
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" + "");
}
}

@ -0,0 +1,27 @@
package de.kreth.property2java;
import java.io.Reader;
import java.nio.file.Path;
import java.util.Map;
class TestImplConfig implements Configuration {
@Override
public String getPackage() {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, Reader> getInput() {
// TODO Auto-generated method stub
return null;
}
@Override
public Path getRootPath() {
// TODO Auto-generated method stub
return null;
}
}

@ -39,8 +39,7 @@ public class Property2JavaGeneratorTest {
@Test
void testGeneratorInitializedCorrectly() {
when(roundEnv.getElementsAnnotatedWith(ArgumentMatchers.any(Class.class)))
.thenReturn(annotations);
when(roundEnv.getElementsAnnotatedWith(ArgumentMatchers.any(Class.class))).thenReturn(annotations);
processor.process(annotations, roundEnv);
}
}

Loading…
Cancel
Save