Compare commits

...

2 Commits

  1. 3
      pom.xml
  2. 6
      src/main/java/de/kreth/property2java/Configuration.java
  3. 3
      src/main/java/de/kreth/property2java/Generator.java
  4. 8
      src/main/java/de/kreth/property2java/GeneratorException.java
  5. 21
      src/main/java/de/kreth/property2java/config/FreemarkerConfig.java
  6. 24
      src/main/java/de/kreth/property2java/config/FreemarkerConfigImpl.java
  7. 23
      src/test/java/de/kreth/property2java/GeneratorTests.java
  8. 55
      src/test/java/de/kreth/property2java/cli/ArgumentConfigurationTest.java

@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.kreth.property2java</groupId>
<artifactId>PropertyToJavaGenerator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2</version>
<properties>

@ -10,7 +10,9 @@ 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;
public interface Configuration {
@ -43,4 +45,8 @@ public interface Configuration {
return path;
}
default Template getTemplate() throws IOException {
return FreemarkerConfigImpl.INSTANCE.getTemplate();
}
}

@ -13,7 +13,6 @@ 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;
@ -28,7 +27,7 @@ public class Generator {
public Generator(Configuration config) {
this.config = config;
try {
template = FreemarkerConfig.INSTANCE.getTemplate();
template = config.getTemplate();
}
catch (IOException e) {
throw new IllegalStateException("Unable to load freemarker template", e);

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

@ -1,24 +1,5 @@
package de.kreth.property2java.config;
import java.io.IOException;
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");
}
public interface FreemarkerConfig {
}

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

@ -3,8 +3,11 @@ 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;
@ -27,6 +30,9 @@ 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";
@ -43,6 +49,7 @@ class GeneratorTests {
config = mock(Configuration.class);
when(config.getInput()).thenReturn(input);
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
when(config.getTemplate()).thenCallRealMethod();
generator = new Generator(config);
}
@ -94,6 +101,22 @@ 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 {

@ -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());
}
}
Loading…
Cancel
Save