New Test Configuration

master
Markus Kreth 1 month ago
parent 8e9671bec5
commit 0db9fa85cf
  1. 1
      Application_Properties.java
  2. 1
      src/main/java/de/kreth/property2java/Generator.java
  3. 4
      src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java
  4. 20
      src/test/java/de/kreth/property2java/GeneratorTests.java
  5. 55
      src/test/java/de/kreth/property2java/processor/ProcessorConfigurationTest.java

@ -69,7 +69,6 @@ public class Generator {
void generate(Properties properties, Writer out, String fileName, Configuration config)
throws IOException, TemplateException {
Map<String, Object> root = new HashMap<>();
root.put("generator_name", getClass().getName());
root.put("generation_date", dateTimeInstance.format(new Date()));

@ -91,7 +91,7 @@ public class ProcessorConfiguration implements Configuration {
}
static final class Builder {
public GeneratorOptions[] options;
private GeneratorOptions[] options;
private final Filer filer;
private final Element element;
private final List<String> resourcenames;
@ -113,7 +113,7 @@ public class ProcessorConfiguration implements Configuration {
return this;
}
public Builder addAll(String[] resourceNames) {
public Builder addAll(String... resourceNames) {
this.resourcenames.addAll(Arrays.asList(resourceNames));
return this;
}

@ -27,6 +27,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
@ -205,6 +206,24 @@ class GeneratorTests {
assertThat(ex.getCause()).isInstanceOf(TemplateException.class);
}
@Test
void testGenerateProp() throws IOException, TemplateException {
Template template = mock(Template.class);
Properties properties = new Properties();
StringWriter out = new StringWriter();
Generator generator = new Generator(config, template);
String fileName = "de.kreth.messages.properties";
generator.generate(properties, out, fileName , config);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> dataModelCaptor = ArgumentCaptor.forClass(Map.class);
verify(template).process(dataModelCaptor.capture(), any(Writer.class));
assertThat(dataModelCaptor.getValue())
.containsEntry("bundle_base_name", "de.kreth.messages")
.containsEntry("fileName", fileName)
.containsEntry("classname", "De_Kreth_Messages_Properties");
}
@Test
void testMainMethod() throws IOException, GeneratorException {
Path source = Files.createTempFile(getClass().getSimpleName(), ".properties");
@ -243,4 +262,5 @@ class GeneratorTests {
return line.contains("\t" + key + " ");
}
}

@ -0,0 +1,55 @@
package de.kreth.property2java.processor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.StringReader;
import javax.annotation.processing.Filer;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import de.kreth.property2java.Format;
@ExtendWith(MockitoExtension.class)
public class ProcessorConfigurationTest {
@Mock
private Filer filer;
@Mock
private TypeElement element;
@Test
void testPackage() throws IOException {
PackageElement packageElement = mock(PackageElement.class);
Name name = mock(Name.class);
String packageName = "de.kreth.pack.name";
String resourceName = "localization.properties";
FileObject fileObject = mock(FileObject.class);
StringReader input = new StringReader("key1=value1\n"
+ "key2=value2");
when(element.getEnclosingElement()).thenReturn(packageElement);
when(packageElement.getQualifiedName()).thenReturn(name);
when(name.toString()).thenReturn(packageName);
when(filer.getResource(StandardLocation.CLASS_PATH, "", resourceName)).thenReturn(fileObject);
when(fileObject.openReader(false)).thenReturn(input);
ProcessorConfiguration config = new ProcessorConfiguration(
ProcessorConfiguration.builder(filer, element)
.withFormat(Format.WithInnerPropertyLoader)
.addAll(resourceName));
assertThat(config.getPackage()).isEqualTo(packageName);
}
}
Loading…
Cancel
Save