Compare commits
8 Commits
master
...
REL-BRANCH
| Author | SHA1 | Date |
|---|---|---|
|
|
0d0b169fb1 | 7 years ago |
|
|
03f8ffdcd2 | 7 years ago |
|
|
46c2f64ad1 | 7 years ago |
|
|
40314c4b9b | 7 years ago |
|
|
1289c8d6e9 | 7 years ago |
|
|
e6d324477f | 7 years ago |
|
|
8736a2d81f | 7 years ago |
|
|
055ab1a536 | 7 years ago |
@ -0,0 +1,35 @@ |
|||||||
|
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,24 +1,5 @@ |
|||||||
package de.kreth.property2java.config; |
package de.kreth.property2java.config; |
||||||
|
|
||||||
import java.io.IOException; |
public interface FreemarkerConfig { |
||||||
|
|
||||||
import freemarker.template.Configuration; |
|
||||||
import freemarker.template.Template; |
|
||||||
|
|
||||||
public enum FreemarkerConfig { |
|
||||||
|
|
||||||
INSTANCE; |
|
||||||
|
|
||||||
private final Configuration cfg; |
|
||||||
|
|
||||||
public Template getTemplate() throws IOException { |
|
||||||
return cfg.getTemplate("enum_template.tpl"); |
|
||||||
} |
|
||||||
|
|
||||||
private FreemarkerConfig() { |
|
||||||
cfg = new Configuration(Configuration.VERSION_2_3_28); |
|
||||||
cfg.setClassForTemplateLoading(this.getClass(), "/template/"); |
|
||||||
cfg.setDefaultEncoding("UTF-8"); |
|
||||||
} |
|
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,24 @@ |
|||||||
|
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,152 @@ |
|||||||
|
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); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,14 @@ |
|||||||
|
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,55 @@ |
|||||||
|
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,163 @@ |
|||||||
|
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…
Reference in new issue