Compare commits

..

50 Commits

Author SHA1 Message Date
Markus Kreth 5f2f60b038 [maven-release-plugin] prepare for next development iteration 3 years ago
Markus Kreth 6b7bd09106 [maven-release-plugin] prepare release PropertyToJavaGenerator-2.0.2 3 years ago
Markus Kreth aab2f7b24f Annotation accepts format parameter 3 years ago
Markus Kreth 959b7e924c added some comments to generation templates. 3 years ago
Markus Kreth 4c2b2a5580 Merge branch 'master' of https://github.com/markuskreth/PropertyToJavaGenerator 3 years ago
Markus Kreth 204740ca8b different Format Templates 3 years ago
Markus Kreth 67f4b40989 [maven-release-plugin] prepare for next development iteration 3 years ago
Markus Kreth 776f8b9cf6 [maven-release-plugin] prepare release PropertyToJavaGenerator-2.0.1 3 years ago
Markus Kreth 871d4b6edd release rollback 3 years ago
Markus Kreth 8db7e5cb59 change scm to gitea full capable repository 3 years ago
Markus Kreth 9147db8ee7 [maven-release-plugin] prepare release PropertyToJavaGenerator-2.0.1 3 years ago
Markus Kreth 2f2791d8fe [maven-release-plugin] rollback the release of PropertyToJavaGenerator-2.0.1 3 years ago
Markus Kreth 73c43df3a8 [maven-release-plugin] prepare release PropertyToJavaGenerator-2.0.1 3 years ago
Markus Kreth d386e9d31b fixed distribution repository id. 3 years ago
Markus Kreth a5f8a5f681 using gitea https url 3 years ago
Markus Kreth 68890f1e76 switch scm from github to gitea 3 years ago
Markus Kreth 378972d41d Next development Version 2.0.1-SNAPSHOT 3 years ago
Markus Kreth 34ed824fa2 [maven-release-plugin] prepare release PropertyToJavaGenerator-2.0.0 3 years ago
Markus Kreth 2121643075 [maven-release-plugin] rollback the release of PropertyToJavaGenerator-2.0.0 3 years ago
Markus Kreth b13a64b108 Merge remote-tracking branch 'origin/master' 3 years ago
Markus Kreth 467b39c2e0 [maven-release-plugin] rollback the release of PropertyToJavaGenerator-2.0.0 3 years ago
Markus Kreth 4415368954 [maven-release-plugin] prepare release PropertyToJavaGenerator-1.0.0 3 years ago
Markus Kreth 35dcb32124 [maven-release-plugin] prepare release PropertyToJavaGenerator-2.0.0 3 years ago
Markus Kreth fdfe368bfc [maven-release-plugin] rollback the release of PropertyToJavaGenerator-2.0.0 3 years ago
Markus Kreth 4b864a9546 [maven-release-plugin] prepare release PropertyToJavaGenerator-2.0.0 3 years ago
Markus Kreth b1d16ebef0 setup github scm server id 3 years ago
Markus Kreth 4e796859c0 fixed pom.xml and added scm and distribution management. 3 years ago
Markus Kreth 1d447b2045 Annotation Processor for the generator 3 years ago
Markus Kreth 11f18a9abf
Merge pull request #9 from markuskreth/dependabot/maven/org.apache.logging.log4j-log4j-core-2.17.1 4 years ago
Markus Kreth 82d0ed20ae
Merge pull request #8 from markuskreth/dependabot/maven/org.apache.logging.log4j-log4j-api-2.17.1 4 years ago
dependabot[bot] 64aa57a06a
Bump log4j-core from 2.17.0 to 2.17.1 4 years ago
dependabot[bot] f85bc0cdc7
Bump log4j-api from 2.17.0 to 2.17.1 4 years ago
Markus Kreth fd412f977f
Merge pull request #7 from markuskreth/dependabot/maven/org.apache.logging.log4j-log4j-core-2.17.0 4 years ago
Markus Kreth 01489253f9
Merge pull request #6 from markuskreth/dependabot/maven/org.apache.logging.log4j-log4j-api-2.17.0 4 years ago
dependabot[bot] 90376f7bc5
Bump log4j-core from 2.15.0 to 2.17.0 4 years ago
dependabot[bot] 3bc752100f
Bump log4j-api from 2.15.0 to 2.17.0 4 years ago
Markus Kreth 363caca736
Merge pull request #2 from markuskreth/dependabot/maven/org.apache.logging.log4j-log4j-api-2.15.0 4 years ago
Markus Kreth 8a2a7edf91
Merge pull request #3 from markuskreth/dependabot/maven/org.apache.logging.log4j-log4j-core-2.15.0 4 years ago
dependabot[bot] aeef7c396e
Bump log4j-core from 2.13.3 to 2.15.0 4 years ago
dependabot[bot] 8a3c68be9b
Bump log4j-api from 2.13.3 to 2.15.0 4 years ago
Markus Kreth 25205b5301
Create maven.yml 5 years ago
Markus Kreth a6fe8ace9b
Merge pull request #1 from markuskreth/dependabot/maven/org.apache.logging.log4j-2.13.3 6 years ago
dependabot[bot] a6f0d2d78f
Bump org.apache.logging.log4j from 2.11.0 to 2.13.3 6 years ago
Markus Kreth 9f90973ee3 set development version 7 years ago
Markus Kreth 35bdaf7dc5 fixed test dependencies. 7 years ago
Markus Kreth e4cc48ec17 Sorting properties by name 7 years ago
Markus Kreth f41a50093a corrected merge error 7 years ago
Jenkins c7545499e2 Revert "Merge branch 'master' of github.com:markuskreth/PropertyToJavaGenerator" 7 years ago
Markus Kreth aab67f43fc [artifactory-release] Next development version 7 years ago
Markus Kreth 58cb637d9e [artifactory-release] Next development version 7 years ago
  1. 37
      .classpath
  2. 24
      .github/workflows/maven.yml
  3. 6
      .gitignore
  4. 23
      .project
  5. 1
      META-INF/services/javax.annotation.processing.Processor
  6. 64
      pom.xml
  7. 35
      src/main/java/de/kreth/property2java/Configuration.java
  8. 35
      src/main/java/de/kreth/property2java/Entry.java
  9. 93
      src/main/java/de/kreth/property2java/Generator.java
  10. 8
      src/main/java/de/kreth/property2java/GeneratorException.java
  11. 27
      src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java
  12. 2
      src/main/java/de/kreth/property2java/cli/CliConfig.java
  13. 34
      src/main/java/de/kreth/property2java/config/FreemarkerConfig.java
  14. 24
      src/main/java/de/kreth/property2java/config/FreemarkerConfigImpl.java
  15. 27
      src/main/java/de/kreth/property2java/processor/Format.java
  16. 42
      src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java
  17. 46
      src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java
  18. 21
      src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Javas.java
  19. 116
      src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java
  20. 91
      src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java
  21. 152
      src/main/java/de/kreth/property2java/replace/Replacement.java
  22. 14
      src/main/java/de/kreth/property2java/replace/Replacements.java
  23. 1
      src/main/resources/META-INF/services/javax.annotation.processing.Processor
  24. 2
      src/main/resources/template/enum_template.tpl
  25. 62
      src/main/resources/template/enum_template_with_initializer.tpl
  26. 48
      src/main/resources/template/enum_template_with_inner_properties.tpl
  27. 50
      src/main/resources/template/enum_template_with_inner_propertyresourcebundle.tpl
  28. 28
      src/test/java/de/kreth/property2java/GeneratorTests.java
  29. 55
      src/test/java/de/kreth/property2java/cli/ArgumentConfigurationTest.java
  30. 46
      src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java
  31. 163
      src/test/java/de/kreth/property2java/replace/ReplacementTest.java

@ -1,37 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

@ -0,0 +1,24 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: PropertyToJavaGenerator Library and console programm
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build with Maven
run: mvn -B package --file pom.xml

6
.gitignore vendored

@ -1,3 +1,5 @@
\.settings/
.classpath
.project
.factorypath
.settings
target

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PropertyToJavaGenerator</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

@ -0,0 +1 @@
de.kreth.property2java.processor.Property2JavaGenerator

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.kreth.property2java</groupId>
<artifactId>PropertyToJavaGenerator</artifactId>
<version>0.0.3</version>
<version>2.0.3-SNAPSHOT</version>
<properties>
@ -12,20 +12,12 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<org.junit.jupiter>5.3.0-M1</org.junit.jupiter>
<org.slf4j>1.7.21</org.slf4j>
<org.apache.logging.log4j>2.11.0</org.apache.logging.log4j>
<org.slf4j>1.7.36</org.slf4j>
<org.apache.logging.log4j>2.17.2</org.apache.logging.log4j>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
<sonar.login>3f3a1fa86ea83226b564895f3a8503f67e855440</sonar.login>
<sonar.jacoco.reportPaths>target/surefire-reports</sonar.jacoco.reportPaths>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.exclusions>**/src/main/webapp/VAADIN/**/*</sonar.exclusions>
<project.scm.id>github</project.scm.id>
</properties>
<dependencies>
@ -98,11 +90,13 @@
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.5.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.5.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
@ -120,9 +114,22 @@
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.1-rc4</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
<id>kreth.snapshots</id>
<url>https://nexus.kreth-development.de/repository/maven-snapshots/</url>
</snapshotRepository>
<repository>
<id>kreth.releases</id>
<url>https://nexus.kreth-development.de/repository/maven-releases/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
@ -131,35 +138,14 @@
<version>3.8.0</version>
<configuration>
<release>${java.version}</release>
<compilerArgs>-proc:none</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>default-jacoco-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-jacoco-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<scm>
<developerConnection>scm:git:https://gitea.kreth-development.de/markus/PropertyToJavaGenerator_migration.git</developerConnection>
<connection>scm:git:https://gitea.kreth-development.de/markus/PropertyToJavaGenerator_migration.git</connection>
<tag>HEAD</tag>
</scm>
</project>

@ -1,11 +1,9 @@
package de.kreth.property2java;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Path;
@ -13,40 +11,37 @@ import java.util.Map;
import org.apache.commons.text.WordUtils;
import de.kreth.property2java.config.FreemarkerConfigImpl;
import de.kreth.property2java.config.Regex;
import freemarker.template.Template;
import de.kreth.property2java.processor.Format;
public interface Configuration {
/**
* Package for generated Java Classes eg. "de.kreth.property2java". If null - no package line is generated.
* Package for generated Java Classes eg. "de.kreth.property2java". If null - no
* package line is generated.
*
* @return
*/
String getPackage();
default Format getFormat() {
return Format.WithUnaryOperatorParameter;
}
/**
* Filename to InputReader Entries
*
* @return
*/
Map<String, Reader> getInput();
/**
* Path of java source folder.
*
* @return
*/
Path getRootPath();
default Reader previousGenerated(String fileName) throws IOException {
File file = new File(getRootPath().toFile(), mapFilenameToClassName(fileName) + ".java");
if (file.exists()) {
return new FileReader(file);
}
else {
return new StringReader("");
}
}
default Writer outWriter(String fileName) throws IOException {
return new FileWriter(new File(getRootPath().toFile(), mapFilenameToClassName(fileName) + ".java"),
outputCharset());
@ -58,13 +53,13 @@ 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;
}
default Template getTemplate() throws IOException {
return FreemarkerConfigImpl.INSTANCE.getTemplate();
}
}

@ -1,35 +0,0 @@
package de.kreth.property2java;
public class Entry {
public final String constant;
public final String key;
public final String value;
public Entry(String constant, String key, String value) {
super();
this.constant = constant;
this.key = key;
this.value = value;
}
public String getConstant() {
return constant;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "Entry [constant=" + constant + ", key=" + key + ", value=" + value + "]";
}
}

@ -1,10 +1,13 @@
package de.kreth.property2java;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
@ -13,6 +16,7 @@ import java.util.Map;
import java.util.Properties;
import de.kreth.property2java.cli.ArgumentConfiguration;
import de.kreth.property2java.config.FreemarkerConfig;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@ -27,9 +31,8 @@ public class Generator {
public Generator(Configuration config) {
this.config = config;
try {
template = config.getTemplate();
}
catch (IOException e) {
template = FreemarkerConfig.INSTANCE.getTemplate(config.getFormat());
} catch (IOException e) {
throw new IllegalStateException("Unable to load freemarker template", e);
}
}
@ -44,8 +47,7 @@ public class Generator {
properties.load(entry.getValue());
try {
generate(properties, out, fileName, config);
}
catch (TemplateException e) {
} catch (TemplateException e) {
throw new GeneratorException("Error configuring Engine", e);
}
}
@ -60,6 +62,7 @@ public class Generator {
root.put("generation_date", dateTimeInstance.format(new Date()));
root.put("package", config.getPackage());
root.put("fileName", fileName);
root.put("bundle_base_name", fileName.substring(0, min(fileName.length(), fileName.lastIndexOf('.'))));
root.put("classname", config.mapFilenameToClassName(fileName));
List<Entry> entries = new ArrayList<>();
@ -67,10 +70,10 @@ public class Generator {
root.put("entries", entries);
@SuppressWarnings("unchecked")
Enumeration<String> propertyNames = (Enumeration<String>) properties.propertyNames();
List<String> propertyNames = Collections.list((Enumeration<String>) properties.propertyNames());
Collections.sort(propertyNames);
while (propertyNames.hasMoreElements()) {
final String propertyKeyString = propertyNames.nextElement();
for (String propertyKeyString : propertyNames) {
final String propertyValue = properties.getProperty(propertyKeyString);
entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString,
@ -79,9 +82,83 @@ public class Generator {
template.process(root, out);
}
int min(int a, int b) {
int result = Math.min(a, b);
if (result < 0) {
result = Math.max(a, b);
}
return result;
}
public static void main(String[] args) throws IOException, GeneratorException {
Generator generator = new Generator(ArgumentConfiguration.parse(args));
generator.start();
}
public static void generateFor(Class<?> locationClass, List<URL> rescources, String relativeTargetDir)
throws IOException, GeneratorException {
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);
Generator g = new Generator(config.build());
g.start();
}
/**
* Represents an Property Entry for the generated java class.
*
* @author markus
*
*/
public class Entry {
public final String constant;
public final String key;
public final String value;
/**
* Creates Property Entry data for the generated java class.
*
* @param constant name for the created constant.
* @param key property key
* @param value property value
*/
public Entry(String constant, String key, String value) {
super();
this.constant = constant;
this.key = key;
this.value = value;
}
public String getConstant() {
return constant;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "Entry [constant=" + constant + ", key=" + key + ", value=" + value + "]";
}
}
}

@ -8,4 +8,12 @@ public class GeneratorException extends Exception {
super(message, cause);
}
public GeneratorException(String message) {
super(message);
}
public GeneratorException(Throwable cause) {
super(cause);
}
}

@ -53,8 +53,7 @@ public class ArgumentConfiguration implements Configuration {
File dir;
if (packageName != null && packageName.isBlank() == false) {
dir = new File(rootPath.toFile(), packageName.replace('.', File.separatorChar));
}
else {
} else {
dir = rootPath.toFile();
}
return new FileWriter(new File(dir, mapFilenameToClassName(fileName) + ".java"), false);
@ -68,20 +67,13 @@ public class ArgumentConfiguration implements Configuration {
return builder.build();
}
static class Builder {
public static class Builder {
String target;
List<String> propFiles = new ArrayList<>();
String packageName;
boolean replaceUsages;
/**
* Target Directory Path.
* @param target
* @return
*/
public Builder setTarget(String target) {
this.target = target;
return this;
@ -92,26 +84,11 @@ public class ArgumentConfiguration implements Configuration {
return this;
}
/**
* Package to be used in generated Sources
* @param packageName
* @return
*/
public Builder setPackageName(String packageName) {
this.packageName = packageName;
return this;
}
/**
* Search and replace usages of generated sources.
* @param replaceUsages
* @return
*/
public Builder setReplaceUsages(boolean replaceUsages) {
this.replaceUsages = replaceUsages;
return this;
}
public Configuration build() throws IOException {
return new ArgumentConfiguration(this);
}

@ -22,7 +22,6 @@ public class CliConfig {
retVal.addOption(Option.builder("t").longOpt("targetSourcePath").hasArg().required().build());
retVal.addOption(Option.builder("f").longOpt("files").hasArgs().required().valueSeparator(',').build());
retVal.addOption(Option.builder("p").longOpt("package").hasArg().required(false).build());
retVal.addOption(Option.builder("u").longOpt("replaceUsages").hasArg(false).required(false).build());
return retVal;
}
@ -33,7 +32,6 @@ public class CliConfig {
CommandLine cmd = parser.parse(options, args);
builder.setTarget(cmd.getOptionValue("t", "."));
builder.setPackageName(cmd.getOptionValue("p"));
builder.setReplaceUsages(cmd.hasOption("u"));
for (String value : cmd.getOptionValues("f")) {
builder.addPropFile(value);
}

@ -1,5 +1,37 @@
package de.kreth.property2java.config;
public interface FreemarkerConfig {
import java.io.IOException;
import de.kreth.property2java.processor.Format;
import freemarker.template.Configuration;
import freemarker.template.Template;
public enum FreemarkerConfig {
INSTANCE;
private final Configuration cfg;
public Template getTemplate(Format format) throws IOException {
switch (format) {
case WithInitializer:
return cfg.getTemplate("enum_template_with_initializer.tpl");
case WithInnerPropertyLoader:
return cfg.getTemplate("enum_template_with_inner_properties.tpl");
case WithInnerPropertyResourceBundle:
return cfg.getTemplate("enum_template_with_inner_propertyresourcebundle.tpl");
case WithUnaryOperatorParameter:
return cfg.getTemplate("enum_template.tpl");
default:
throw new IllegalArgumentException("Format " + format + " is not supported.");
}
}
private FreemarkerConfig() {
cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setClassForTemplateLoading(this.getClass(), "/template/");
cfg.setDefaultEncoding("UTF-8");
}
}

@ -1,24 +0,0 @@
package de.kreth.property2java.config;
import java.io.IOException;
import freemarker.template.Configuration;
import freemarker.template.Template;
public enum FreemarkerConfigImpl implements FreemarkerConfig {
INSTANCE;
private final Configuration cfg;
public Template getTemplate() throws IOException {
return cfg.getTemplate("enum_template.tpl");
}
private FreemarkerConfigImpl() {
cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setClassForTemplateLoading(this.getClass(), "/template/");
cfg.setDefaultEncoding("UTF-8");
}
}

@ -0,0 +1,27 @@
package de.kreth.property2java.processor;
import java.util.PropertyResourceBundle;
public enum Format {
/**
* Offers a getString(UnaryOperator<String> resourceFunction) method to access
* the String value
*/
WithUnaryOperatorParameter,
/**
* Generates {@link PropertyResourceBundle} to offer a getText() method without
* parameters.
*/
WithInnerPropertyResourceBundle,
/**
* Offers a generated {@link PropertyResourceBundle} to offer a getText() method
* without parameters.
*/
WithInnerPropertyLoader,
/**
* Offers a static init(UnaryOperator<String> resourceFunction) method to offer
* a getText() method. The init method must be called before any getText() call.
*/
WithInitializer
}

@ -0,0 +1,42 @@
package de.kreth.property2java.processor;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(TYPE)
@Retention(RetentionPolicy.SOURCE)
/**
* Für die konfigurierten Resourcen wird jeweils eine Java Klasse erzeugt. Es
* muss nur die Abhängigkeit eingebunden werden und die Annotation in einer
* Klasse verwendet werden, in deren Package die neuen Klassen generiert werden.
*
* Für die Ausgabe der Prozessornachrichten muss folgendes im maven compiler
* konfiguriert werden:
*
* <pre>
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.8.0&lt;/version&gt;
&lt;configuration&gt;
&lt;release&gt;${java.version}&lt;/release&gt;
<b>&lt;showWarnings&gt;true&lt;/showWarnings&gt;</b>
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
* </pre>
*
* @author Markus
*
*/
public @interface GenerateProperty2Java {
String[] resources();
Format format() default Format.WithUnaryOperatorParameter;
}

@ -0,0 +1,46 @@
package de.kreth.property2java.processor;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(TYPE)
@Retention(RetentionPolicy.SOURCE)
@Repeatable(value = GenerateResourceBundleProperty2Javas.class)
/**
* Für die konfigurierten Resourcen wird jeweils eine Java Klasse erzeugt. Es
* muss nur die Abhängigkeit eingebunden werden und die Annotation in einer
* Klasse verwendet werden, in deren Package die neuen Klassen generiert werden.
* Wenn mehrere Resourcen verarbeitet werden sollen, kann diese Annotation
* mehrfach parallel angegeben werden.
*
* Für die Ausgabe der Prozessornachrichten muss folgendes im maven compiler
* konfiguriert werden:
*
* <pre>
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.8.0&lt;/version&gt;
&lt;configuration&gt;
&lt;release&gt;${java.version}&lt;/release&gt;
<b>&lt;showWarnings&gt;true&lt;/showWarnings&gt;</b>
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
* </pre>
*
* @author Markus
*
*/
public @interface GenerateResourceBundleProperty2Java {
String resource();
Format format();
}

@ -0,0 +1,21 @@
package de.kreth.property2java.processor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Diese Annotation sollte nicht verwendet werden. Sie sammelt nur
* {@link GenerateResourceBundleProperty2Java} wenn diese mehrfach verwendet
* wird.
*
* @author Markus
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface GenerateResourceBundleProperty2Javas {
GenerateResourceBundleProperty2Java[] value();
}

@ -0,0 +1,116 @@
package de.kreth.property2java.processor;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.processing.Filer;
import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import de.kreth.property2java.Configuration;
import de.kreth.property2java.Generator;
import de.kreth.property2java.GeneratorException;
public class ProcessorConfiguration implements Configuration {
private final Filer filer;
private final Element element;
private final Map<String, Reader> input;
private final Format format;
ProcessorConfiguration(Builder builder) throws IOException {
this.filer = builder.filer;
this.element = builder.element;
this.format = Objects.requireNonNullElse(builder.format, Format.WithUnaryOperatorParameter);
this.input = new HashMap<>();
for (String resource : builder.resourcenames) {
FileObject ressource = filer.getResource(StandardLocation.CLASS_PATH, "", resource);
input.put(resource, ressource.openReader(false));
}
}
@Override
public String getPackage() {
String packageName = "";
if (element instanceof TypeElement) {
TypeElement typeElement = (TypeElement) element;
PackageElement packageElement = (PackageElement) typeElement.getEnclosingElement();
packageName = packageElement.getQualifiedName().toString();
}
return packageName;
}
@Override
public Format getFormat() {
return format;
}
@Override
public Map<String, Reader> getInput() {
return input;
}
@Override
public Path getRootPath() {
throw new UnsupportedOperationException(
"For Annotation Processor this is not supported as outWriter is overwritten.");
}
@Override
public Writer outWriter(String fileName) throws IOException {
String packageName = getPackage();
if (packageName != null && !packageName.isBlank()) {
fileName = packageName + "." + mapFilenameToClassName(fileName);
}
return filer.createSourceFile(fileName, element).openWriter();
}
static Builder builder(Filer filer, Element element) {
return new Builder(filer, element);
}
static class Builder {
private final Filer filer;
private final Element element;
private final List<String> resourcenames;
private Format format = Format.WithUnaryOperatorParameter;
private Builder(Filer filer, Element element) {
this.filer = filer;
this.element = element;
this.resourcenames = new ArrayList<>();
}
public Builder withFormat(Format format) {
this.format = format;
return this;
}
public Builder addAll(String[] resourceNames) {
this.resourcenames.addAll(Arrays.asList(resourceNames));
return this;
}
public Builder addAll(List<String> resourceNames) {
this.resourcenames.addAll(resourceNames);
return this;
}
public void startGeneration() throws IOException, GeneratorException {
Generator g = new Generator(new ProcessorConfiguration(this));
g.start();
}
}
}

@ -0,0 +1,91 @@
package de.kreth.property2java.processor;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
import de.kreth.property2java.GeneratorException;
@SupportedAnnotationTypes({ "de.kreth.property2java.processor.GenerateProperty2Java",
"de.kreth.property2java.processor.GenerateResourceBundleProperty2Javas",
"de.kreth.property2java.processor.GenerateResourceBundleProperty2Java" })
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class Property2JavaGenerator extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
processGenerateProperty2Java(roundEnv);
processGenerateResourceBundleProperty2Javas(roundEnv);
} else {
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);
Set<? extends Element> elementsAnnotatedWith = roundEnv
.getElementsAnnotatedWith(GenerateProperty2Java.class);
for (Element element : elementsAnnotatedWith) {
GenerateProperty2Java generateAnnotation = element.getAnnotation(GenerateProperty2Java.class);
String[] resources = generateAnnotation.resources();
Format format = generateAnnotation.format();
generateElementProperties(element, Arrays.asList(resources), format);
}
}
private void processGenerateResourceBundleProperty2Javas(RoundEnvironment roundEnv) {
processingEnv.getMessager().printMessage(Kind.NOTE,
"Processing annotation " + GenerateResourceBundleProperty2Javas.class);
Set<? extends Element> elementsAnnotatedWith = roundEnv
.getElementsAnnotatedWith(GenerateResourceBundleProperty2Javas.class);
for (Element element : elementsAnnotatedWith) {
GenerateResourceBundleProperty2Java[] value = element
.getAnnotation(GenerateResourceBundleProperty2Javas.class).value();
for (GenerateResourceBundleProperty2Java generateResourceBundleProperty2Java : value) {
List<String> resources = Arrays.asList(generateResourceBundleProperty2Java.resource());
generateElementProperties(element, resources, generateResourceBundleProperty2Java.format());
}
}
}
private void generateElementProperties(Element element, List<String> resources, Format format) {
processingEnv.getMessager().printMessage(Kind.NOTE,
"Generating Java for " + Arrays.asList(resources));
try {
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);
}
}
}

@ -1,152 +0,0 @@
package de.kreth.property2java.replace;
import java.util.Arrays;
import java.util.List;
public class Replacement {
private static final List<Character> CHARS_NOT_ALLOWED_BEFORE_PROPERTY = Arrays.asList('.');
private final String packageName;
private final String simpleClassName;
private final String oldPropertyName;
private final String newPropertyName;
private String qualifiedClassName;
private String qualifiedOldProperty;
public Replacement(String packageName, String simpleClassName, String oldPropertyName, String newPropertyName) {
super();
this.packageName = packageName;
this.simpleClassName = simpleClassName;
this.oldPropertyName = oldPropertyName;
this.newPropertyName = newPropertyName;
this.qualifiedClassName = packageName + "." + simpleClassName;
this.qualifiedOldProperty = qualifiedClassName + "." + oldPropertyName;
}
public String getOldPropertyName() {
return oldPropertyName;
}
public String getNewPropertyName() {
return newPropertyName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((newPropertyName == null) ? 0 : newPropertyName.hashCode());
result = prime * result + ((oldPropertyName == null) ? 0 : oldPropertyName.hashCode());
result = prime * result + ((packageName == null) ? 0 : packageName.hashCode());
result = prime * result + ((simpleClassName == null) ? 0 : simpleClassName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Replacement other = (Replacement) obj;
if (newPropertyName == null) {
if (other.newPropertyName != null) {
return false;
}
}
else if (!newPropertyName.equals(other.newPropertyName)) {
return false;
}
if (oldPropertyName == null) {
if (other.oldPropertyName != null) {
return false;
}
}
else if (!oldPropertyName.equals(other.oldPropertyName)) {
return false;
}
if (packageName == null) {
if (other.packageName != null) {
return false;
}
}
else if (!packageName.equals(other.packageName)) {
return false;
}
if (simpleClassName == null) {
if (other.simpleClassName != null) {
return false;
}
}
else if (!simpleClassName.equals(other.simpleClassName)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Replacement [packageName=" + packageName + ", simpleClassName=" + simpleClassName + ", oldPropertyName="
+ oldPropertyName + ", newPropertyName=" + newPropertyName + "]";
}
public String replaceOccurrences(String sourceCode) {
if (sourceCode.contains("import static " + qualifiedOldProperty)) {
sourceCode = replaceStaticImportedUsages(sourceCode);
}
if (sourceCode.contains(qualifiedOldProperty)) {
sourceCode = replaceFullQualifiedUnimported(sourceCode);
}
if (sourceCode.contains("import " + qualifiedClassName)) {
sourceCode = replaceClassUsages(sourceCode);
}
return sourceCode;
}
private String replaceClassUsages(String sourceCode) {
String search = this.simpleClassName + "." + oldPropertyName;
String replacement = this.simpleClassName + "." + newPropertyName;
int index = sourceCode.indexOf(search);
while (index > 0) {
if (!CHARS_NOT_ALLOWED_BEFORE_PROPERTY.contains(sourceCode.charAt(index - 1))) {
StringBuilder source = new StringBuilder(sourceCode);
source.replace(index, index + search.length(), replacement);
sourceCode = source.toString();
}
index = sourceCode.indexOf(search, index + 1);
}
return sourceCode;
}
private String replaceStaticImportedUsages(String sourceCode) {
int index = sourceCode.indexOf(oldPropertyName);
while (index > 0) {
if (!CHARS_NOT_ALLOWED_BEFORE_PROPERTY.contains(sourceCode.charAt(index - 1))) {
StringBuilder source = new StringBuilder(sourceCode);
source.replace(index, index + oldPropertyName.length(), newPropertyName);
sourceCode = source.toString();
}
index = sourceCode.indexOf(oldPropertyName, index + 1);
}
return sourceCode;
}
private String replaceFullQualifiedUnimported(String sourceCode) {
return sourceCode.replace(qualifiedOldProperty,
qualifiedClassName + "." + newPropertyName);
}
}

@ -1,14 +0,0 @@
package de.kreth.property2java.replace;
import java.util.ArrayList;
import java.util.List;
public class Replacements {
private final List<Replacement> replacementList = new ArrayList<>();
public boolean add(Replacement e) {
return replacementList.add(e);
}
}

@ -0,0 +1 @@
de.kreth.property2java.processor.Property2JavaGenerator

@ -8,6 +8,8 @@ import javax.annotation.processing.Generated;
/**
* Property keys from ${fileName}
* {@link #getValue()} gives the key for the entry, with {@link #getString(UnaryOperator<String>)}
* the value is given directly.
*/
@Generated(date = "${generation_date}", value = "${generator_name}")
public enum ${classname} {

@ -0,0 +1,62 @@
<#if package??>package ${package};
</#if>import java.util.Properties;
import java.util.ResourceBundle;
import java.util.function.UnaryOperator;
import javax.annotation.processing.Generated;
/**
* Property keys from ${fileName}
* {@link #getValue()} gives the key for the entry, with {@link #getText()} the value for the key is given directly.
* This enum needs to be initialized before any use by {@link #init(UnaryOperator)}.
*/
@Generated(date = "${generation_date}", value = "${generator_name}")
public enum ${classname} {
<#list entries as e>
/**
* ${e.key} = "${e.value}"
*/
${e.constant} ("${e.key}")<#sep>,
</#sep>
</#list>;
private static UnaryOperator<String> function;
private final String value;
private ${classname} (String value) {
this.value = value;
}
public static void init(UnaryOperator<String> resourceFunction) {
function = resourceFunction;
}
/**
* Represented Key in property File.
* @return key
*/
public String getValue() {
return value;
}
/**
* Resolves the value for this key.
* {@link #init(UnaryOperator<String>)} must be called before.
*/
public String getText() {
return function.apply(value);
}
/**
* Resolves the value for this key from the parameter function.
* <p>
* e.g. <code>${classname}.getString(resBundle::getString)</code>
* @param resourceFunction {@link Properties#getProperty(String)} or {@link ResourceBundle#getString(String)}
* @return
*/
public String getString(UnaryOperator<String> resourceFunction) {
return resourceFunction.apply(value);
}
}

@ -0,0 +1,48 @@
<#if package??>package ${package};
</#if>import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.function.UnaryOperator;
import javax.annotation.processing.Generated;
/**
* Property keys from ${fileName}
*/
@Generated(date = "${generation_date}", value = "${generator_name}")
public enum ${classname} {
<#list entries as e>
/**
* ${e.key} = "${e.value}"
*/
${e.constant} ("${e.key}")<#sep>,
</#sep>
</#list>;
private final String value;
private ${classname} (String value) {
this.value = value;
}
/**
* Represented Key in property File.
* @return key
*/
public String getValue() {
return value;
}
private static ResourceBundle bundle = PropertyResourceBundle.getBundle("${bundle_base_name}");
/**
* The Text for this Key from PropertyResourceBundle
* @return human readable text
*/
public String getText() {
return bundle.getString(value);
}
}

@ -0,0 +1,50 @@
<#if package??>package ${package};
</#if>import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.function.UnaryOperator;
import javax.annotation.processing.Generated;
/**
* Property keys from ${fileName}
* {@link #getValue()} gives the key for the entry, with {@link #getText()} the value for the key is given directly.
* Initializationis generated also.
*/
@Generated(date = "${generation_date}", value = "${generator_name}")
public enum ${classname} {
<#list entries as e>
/**
* ${e.key} = "${e.value}"
*/
${e.constant} ("${e.key}")<#sep>,
</#sep>
</#list>;
private final String value;
private ${classname} (String value) {
this.value = value;
}
/**
* Represented Key in property File.
* @return key
*/
public String getValue() {
return value;
}
private static ResourceBundle bundle = PropertyResourceBundle.getBundle("${bundle_base_name}");
/**
* The Text for this Key from PropertyResourceBundle
* @return human readable text
*/
public String getText() {
return bundle.getString(value);
}
}

@ -3,11 +3,8 @@ package de.kreth.property2java;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@ -30,9 +27,6 @@ import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import freemarker.template.Template;
import freemarker.template.TemplateException;
class GeneratorTests {
private String path = "application.properties";
@ -49,7 +43,7 @@ class GeneratorTests {
config = mock(Configuration.class);
when(config.getInput()).thenReturn(input);
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
when(config.getTemplate()).thenCallRealMethod();
when(config.outputCharset()).thenCallRealMethod();
generator = new Generator(config);
}
@ -75,8 +69,7 @@ class GeneratorTests {
String line = sourceTokenizer.nextToken();
if (line.trim().startsWith("package")) {
linePackage = line;
}
else if (line.trim().startsWith("public enum")) {
} else if (line.trim().startsWith("public enum")) {
lineClass = line;
}
if (line.contains("{")) {
@ -101,22 +94,6 @@ class GeneratorTests {
}
@Test()
void testTemplateException() throws IOException, GeneratorException, TemplateException {
Writer out = mock(Writer.class);
when(config.outWriter(anyString())).thenReturn(out);
Template template = mock(Template.class);
when(config.getTemplate()).thenReturn(template);
doThrow(TemplateException.class).when(template).process(any(), any());
generator = new Generator(config);
assertThrows(GeneratorException.class, () -> generator.start());
}
@Test
void testOneInputGeneratesOneOutput() throws IOException, GeneratorException {
@ -153,6 +130,7 @@ class GeneratorTests {
private void assertLineMatch(List<String> lines, String key, String expected) {
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();
int indexEquals = line.indexOf('(');

@ -1,55 +0,0 @@
package de.kreth.property2java.cli;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import de.kreth.property2java.Configuration;
class ArgumentConfigurationTest {
private static File target;
private static File input;
private static File input2;
@BeforeAll
static void createTargetDir() throws IOException {
target = new File("testTargetDir");
target.mkdir();
input = new File(target, "application.properties");
input.createNewFile();
input2 = new File(target, "application2.properties");
input2.createNewFile();
}
@AfterAll
static void deleteTestfiles() {
target.delete();
input.delete();
input2.delete();
}
@Test
void testArgumentParser() throws IOException {
String[] args = { "-t", target.getAbsolutePath(),
"-f",
input.getAbsolutePath() + "," + input2.getAbsolutePath(),
"-p", "de.kreth.clubinvoice" };
Configuration config = ArgumentConfiguration.parse(args);
assertEquals("de.kreth.clubinvoice", config.getPackage());
assertEquals(target.getAbsolutePath(), config.getRootPath().toFile().getAbsolutePath());
Map<String, Reader> inputMap = config.getInput();
assertEquals(2, inputMap.size());
}
}

@ -0,0 +1,46 @@
package de.kreth.property2java.processor;
import static org.mockito.Mockito.when;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.TypeElement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class Property2JavaGeneratorTest {
private Property2JavaGenerator processor;
@Mock
private ProcessingEnvironment processingEnv;
@Mock
private RoundEnvironment roundEnv;
private Set<TypeElement> annotations;
@Mock
private Messager messanger;
@BeforeEach
void initProcesor() {
MockitoAnnotations.initMocks(this);
annotations = new HashSet<>();
processor = new Property2JavaGenerator();
processor.init(processingEnv);
when(processingEnv.getMessager()).thenReturn(messanger);
}
@Test
void testGeneratorInitializedCorrectly() {
when(roundEnv.getElementsAnnotatedWith(ArgumentMatchers.any(Class.class)))
.thenReturn(annotations);
processor.process(annotations, roundEnv);
}
}

@ -1,163 +0,0 @@
package de.kreth.property2java.replace;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import de.kreth.property2java.replace.Replacement;
class ReplacementTest {
private Replacement captionArticles;
private Replacement labelLogout;
private Replacement captionUserDetails;
@BeforeEach
void initTestItem() {
captionArticles = new Replacement("de.kreth.clubinvoice", "Application_Properties", "CAPTION_ARTICLES",
"CAPTION1_ARTICLES");
captionUserDetails = new Replacement("de.kreth.clubinvoice", "Application_Properties",
"CAPTION_USER_DETAILS",
"CAPTION1_USER_DETAILS");
labelLogout = new Replacement("de.kreth.clubinvoice", "Application_Properties", "LABEL_LOGOUT",
"LABEL1_LOGOUT");
}
@Test
void testReplaceStaticImported() {
String exampleSourceCode = getExampleSourceCode();
String result = captionArticles.replaceOccurrences(exampleSourceCode);
assertNotNull(result);
Optional<String> firstLine = result.lines()
.filter(line -> line.contains("de.kreth.clubinvoice.Application_Properties")).findFirst();
assertTrue(firstLine.isPresent());
String captionArticlesLine = firstLine.get();
assertEquals("import static de.kreth.clubinvoice.Application_Properties.CAPTION1_ARTICLES;",
captionArticlesLine.trim());
firstLine = result.lines().filter(l -> l.contains("new Button(CAPTION1_ARTICLES.getString(")).findFirst();
assertTrue(firstLine.isPresent(), "CAPTION1_ARTICLES usage not found");
}
@Test
void testReplaceClassImport() {
String exampleSourceCode = getExampleSourceCode();
String result = labelLogout.replaceOccurrences(exampleSourceCode);
Optional<String> line = result.lines()
.filter(l -> l.contains(".getString(Application_Properties.LABEL1_LOGOUT"))
.findFirst();
assertTrue(line.isPresent());
}
@Test
void testReplaceUnimportedOccurrence() {
String exampleSourceCode = getExampleSourceCode();
String result = captionUserDetails.replaceOccurrences(exampleSourceCode);
Optional<String> line = result.lines()
.filter(l -> l.contains("new Button(de.kreth.clubinvoice.Application_Properties")
&& l.contains("_USER_DETAILS"))
.findFirst();
assertTrue(line.isPresent());
assertTrue(line.get().contains("de.kreth.clubinvoice.Application_Properties.CAPTION1_USER_DETAILS"),
"absoulute class field CAPTION1_USER_DETAILS not found in : " + line.get());
}
@Test
void testReplaceUnimportedOccurrenceWithLinebreak() {
String exampleSourceCode = getExampleSourceCode();
String result = captionUserDetails.replaceOccurrences(exampleSourceCode);
List<String> lines = result.lines()
.filter(l -> l.contains(".Application_Properties")
&& l.contains("_USER_DETAILS"))
.collect(Collectors.toList());
assertEquals(2, lines.size());
//
// assertTrue(line.get().contains("de.kreth.clubinvoice.Application_Properties.CAPTION1_USER_DETAILS"),
// "absoulute class field CAPTION1_USER_DETAILS not found in : " + line.get());
}
String getExampleSourceCode() {
return "package de.kreth.clubinvoice.ui;\r\n" +
"\r\n" +
"import static de.kreth.clubinvoice.Application_Properties.CAPTION_ARTICLES;\r\n" +
"import static de.kreth.clubinvoice.Application_Properties.CAPTION_INVOICEITEM_ADD;\r\n" +
"" +
"import static de.kreth.clubinvoice.Application_Properties.CAPTION_INVOICE_PATTERN;\r\n" +
"\r\n" +
"import org.slf4j.Logger;\r\n" +
"import org.slf4j.LoggerFactory;\r\n" +
"\r\n" +
"\r\n" +
"import de.kreth.clubinvoice.Application_Properties;\r\n" +
"\r\n" +
"public class OverviewUi extends VerticalLayout implements InvoiceUi {\r\n" +
"\r\n" +
" public void setContent(UI ui, VaadinRequest vaadinRequest) {\r\n" +
"\r\n" +
" }\r\n" +
"\r\n" +
" public VerticalLayout createInvoicesView(final UI ui) {\r\n" +
"\r\n" +
" createInvoice = new Button(CAPTION_INVOICE_CREATE.getString(resBundle::getString));\r\n" +
" return right;\r\n" +
" }\r\n" +
"\r\n" +
" public VerticalLayout createItemsView(final UI ui) {\r\n" +
"\r\n" +
" addItem = new Button(de.kreth.clubinvoice.Application_Properties.CAPTION_INVOICE_CREATE.getString(resBundle::getString));\r\n"
+
"\r\n" +
" VerticalLayout left = new VerticalLayout();\r\n" +
" addItem = new Button(de.kreth.clubinvoice"
+ " \t.Application_Properties.CAPTION_INVOICE_CREATE.getString(resBundle::getString));\r\n" +
" left.addComponents(addItem, gridItems);\r\n" +
" left.setStyleName(STYLE_BORDERED);\r\n" +
" return left;\r\n" +
" }\r\n" +
"\r\n" +
" public HorizontalLayout createHeadView(final UI ui, VaadinRequest vaadinRequest) {\r\n" +
" Label l2 = new Label(String.format(\"%s %s\", user.getPrename(), user.getSurname()));\r\n" +
"\r\n" +
" Button addArticle = new Button(CAPTION_ARTICLES.getString(resBundle::getString));\r\n" +
" Button logoutButton = new Button(resBundle.getString(Application_Properties.LABEL_LOGOUT.getValue()));\r\n"
+
" logoutButton.addClickListener(ev -> {\r\n" +
" LOGGER.warn(\"Logging out.\");\r\n" +
" logout(ui, vaadinRequest);\r\n" +
" });\r\n" +
"\r\n" +
" Button userDetail = new Button(de.kreth.clubinvoice.Application_Properties.CAPTION_USER_DETAILS.getString(resBundle::getString), ev -> {\r\n"
+
" Button userDetail2 = new Button(de.kreth.clubinvoice"
+ " \t.Application_Properties.CAPTION_USER_DETAILS.getString(resBundle::getString), ev -> {\r\n"
+
" showUserDetailDialog(ui);\r\n" +
" });\r\n" +
"\r\n" +
" return head;\r\n" +
" }\r\n" +
"\r\n" +
"}\r\n" +
"";
}
}
Loading…
Cancel
Save