Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn committed Nov 27, 2021
1 parent 1b948c1 commit ec9b1d8
Show file tree
Hide file tree
Showing 22 changed files with 990 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tools/i18n-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@
<artifactId>org.openhab.core.thing.xml</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bom</groupId>
<artifactId>org.openhab.core.bom.test</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openhab.core.bom</groupId>
<artifactId>org.openhab.core.bom.test-index</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ protected void readAddonInfo() throws IOException {
BundleInfoReader bundleInfoReader = new BundleInfoReader(getLog());
bundleInfo = bundleInfoReader.readBundleInfo(ohinfDirectory.toPath());
}

void setOhinfDirectory(File ohinfDirectory) {
this.ohinfDirectory = ohinfDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,12 @@ private void writeDefaultTranslations(String translationsString) throws MojoFail
throw new MojoFailureException("Failed to write generated translations to: " + translationsPath, e);
}
}

void setTargetDirectory(File targetDirectory) {
this.targetDirectory = targetDirectory;
}

void setGenerationMode(DefaultTranslationsGenerationMode generationMode) {
this.generationMode = generationMode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.tools.i18n.plugin;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;
import java.nio.file.Path;

import org.apache.maven.plugin.logging.SystemStreamLog;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.core.binding.xml.internal.BindingInfoXmlResult;

/**
* Tests {@link BundleInfoReader}.
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public class BundleInfoReaderTest {

@Test
public void readBindingInfo() throws IOException {
BundleInfoReader reader = new BundleInfoReader(new SystemStreamLog());
BundleInfo bundleInfo = reader.readBundleInfo(Path.of("src/test/resources/acmeweather.bundle/OH-INF"));

BindingInfoXmlResult bindingInfoXml = bundleInfo.getBindingInfoXml();
assertThat(bindingInfoXml, is(notNullValue()));
if (bindingInfoXml != null) {
assertThat(bindingInfoXml.getBindingInfo().getName(), is("ACME Weather Binding"));
assertThat(bindingInfoXml.getBindingInfo().getDescription(),
is("ACME Weather - Current weather and forecasts in your city."));
}

assertThat(bundleInfo.getBindingId(), is("acmeweather"));
assertThat(bundleInfo.getChannelGroupTypesXml().size(), is(1));
assertThat(bundleInfo.getChannelTypesXml().size(), is(2));
assertThat(bundleInfo.getConfigDescriptions().size(), is(1));
assertThat(bundleInfo.getThingTypesXml().size(), is(2));
}

@Test
public void readGenericBundleInfo() throws IOException {
BundleInfoReader reader = new BundleInfoReader(new SystemStreamLog());
BundleInfo bundleInfo = reader.readBundleInfo(Path.of("src/test/resources/acmetts.bundle/OH-INF"));

assertThat(bundleInfo.getBindingInfoXml(), is(nullValue()));
assertThat(bundleInfo.getBindingId(), is(""));
assertThat(bundleInfo.getChannelGroupTypesXml().size(), is(0));
assertThat(bundleInfo.getChannelTypesXml().size(), is(0));
assertThat(bundleInfo.getConfigDescriptions().size(), is(1));
assertThat(bundleInfo.getThingTypesXml().size(), is(0));
}

@Test
public void readPathWithoutAnyInfo() throws IOException {
BundleInfoReader reader = new BundleInfoReader(new SystemStreamLog());
BundleInfo bundleInfo = reader.readBundleInfo(Path.of("src/test/resources/infoless.bundle/OH-INF"));

assertThat(bundleInfo.getBindingInfoXml(), is(nullValue()));
assertThat(bundleInfo.getBindingId(), is(""));
assertThat(bundleInfo.getChannelGroupTypesXml().size(), is(0));
assertThat(bundleInfo.getChannelTypesXml().size(), is(0));
assertThat(bundleInfo.getConfigDescriptions().size(), is(0));
assertThat(bundleInfo.getThingTypesXml().size(), is(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.tools.i18n.plugin;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.openhab.core.tools.i18n.plugin.DefaultTranslationsGenerationMode.*;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Tests {@link GenerateDefaultTranslationsMojo}.
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public class GenerateDefaultTranslationsMojoTest {

@TempDir
public @NonNullByDefault({}) Path tempPath;
public @NonNullByDefault({}) Path tempI18nPath;

public static final Path TTS_RESOURCES_PATH = Path.of("src/test/resources/acmetts.bundle");
public static final Path TTS_I18N_RESOURCES_PATH = TTS_RESOURCES_PATH.resolve("OH-INF/i18n");
public static final Path TTS_ALL_PROPERTIES_PATH = TTS_I18N_RESOURCES_PATH.resolve("acmetts.properties");
public static final Path TTS_ALL_DE_PROPERTIES_PATH = TTS_I18N_RESOURCES_PATH.resolve("acmetts_de.properties");
public static final Path TTS_GENERATED_PROPERTIES_PATH = TTS_I18N_RESOURCES_PATH
.resolve("acmetts.generated.properties");
public static final Path TTS_PARTIAL_PROPERTIES_PATH = TTS_I18N_RESOURCES_PATH
.resolve("acmetts.partial.properties");

public static final Path WEATHER_RESOURCES_PATH = Path.of("src/test/resources/acmeweather.bundle");
public static final Path WEATHER_I18N_RESOURCES_PATH = WEATHER_RESOURCES_PATH.resolve("OH-INF/i18n");
public static final Path WEATHER_ALL_PROPERTIES_PATH = WEATHER_I18N_RESOURCES_PATH
.resolve("acmeweather.properties");
public static final Path WEATHER_ALL_DE_PROPERTIES_PATH = WEATHER_I18N_RESOURCES_PATH
.resolve("acmeweather_de.properties");
public static final Path WEATHER_GENERATED_PROPERTIES_PATH = WEATHER_I18N_RESOURCES_PATH
.resolve("acmeweather.generated.properties");
public static final Path WEATHER_PARTIAL_PROPERTIES_PATH = WEATHER_I18N_RESOURCES_PATH
.resolve("acmeweather.partial.properties");

public static final Path INFOLESS_RESOURCES_PATH = Path.of("src/test/resources/infoless.bundle");

private @NonNullByDefault({}) GenerateDefaultTranslationsMojo mojo;

private void copyPath(Path src, Path dest) throws IOException {
try (Stream<Path> stream = Files.walk(src)) {
stream.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
}
}

private void copy(Path source, Path dest) {
try {
Files.copy(source, dest, REPLACE_EXISTING);
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
}
}

private void deleteTempI18nPath() throws IOException {
try (DirectoryStream<Path> entries = Files.newDirectoryStream(tempI18nPath)) {
for (Path entry : entries) {
Files.delete(entry);
}
}

Files.delete(tempI18nPath);
}

@BeforeEach
public void before() {
tempI18nPath = tempPath.resolve("OH-INF/i18n");

mojo = new GenerateDefaultTranslationsMojo();
mojo.setLog(new SystemStreamLog());
mojo.setOhinfDirectory(tempPath.resolve("OH-INF").toFile());
mojo.setTargetDirectory(tempI18nPath.toFile());
}

private void assertSameProperties(Path expectedPath, Path actualPath) throws IOException {
String expected = Files.readString(expectedPath);
String actual = Files.readString(actualPath);
assertThat(expected, equalTo(actual));
}

@Test
public void addMissingBindingTranslationsWithoutI18nPath() throws IOException, MojoFailureException {
copyPath(WEATHER_RESOURCES_PATH, tempPath);
deleteTempI18nPath();

mojo.setGenerationMode(ADD_MISSING_TRANSLATIONS);
mojo.execute();

assertSameProperties(WEATHER_GENERATED_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather.properties"));
}

@Test
public void addMissingBindingTranslationsNoChanges() throws IOException, MojoFailureException {
copyPath(WEATHER_RESOURCES_PATH, tempPath);

mojo.setGenerationMode(ADD_MISSING_TRANSLATIONS);
mojo.execute();

assertSameProperties(WEATHER_ALL_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather.properties"));
assertSameProperties(WEATHER_ALL_DE_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather_de.properties"));
}

@Test
public void addMissingBindingTranslationsToPartialTranslation() throws IOException, MojoFailureException {
copyPath(WEATHER_RESOURCES_PATH, tempPath);
Files.move(tempI18nPath.resolve("acmeweather.partial.properties"),
tempI18nPath.resolve("acmeweather.properties"), REPLACE_EXISTING);

mojo.setGenerationMode(ADD_MISSING_TRANSLATIONS);
mojo.execute();

assertSameProperties(WEATHER_ALL_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather.properties"));
assertSameProperties(WEATHER_ALL_DE_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather_de.properties"));
}

@Test
public void skipTranslationsBindingTranslationsGeneratingWithExistingTranslations()
throws IOException, MojoFailureException {
copyPath(WEATHER_RESOURCES_PATH, tempPath);
Files.move(tempI18nPath.resolve("acmeweather.partial.properties"),
tempI18nPath.resolve("acmeweather.properties"), REPLACE_EXISTING);

mojo.setGenerationMode(ADD_MISSING_FILES);
mojo.execute();

assertSameProperties(WEATHER_PARTIAL_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather.properties"));
assertSameProperties(WEATHER_ALL_DE_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather_de.properties"));
}

@Test
public void regenerateBindingTranslations() throws IOException, MojoFailureException {
copyPath(WEATHER_RESOURCES_PATH, tempPath);

mojo.setGenerationMode(REGENERATE_FILES);
mojo.execute();

assertSameProperties(WEATHER_GENERATED_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather.properties"));
assertSameProperties(WEATHER_ALL_DE_PROPERTIES_PATH, tempI18nPath.resolve("acmeweather_de.properties"));
}

@Test
public void addMissingGenericBundleTranslationsWithoutI18nPath() throws IOException, MojoFailureException {
copyPath(TTS_RESOURCES_PATH, tempPath);
deleteTempI18nPath();

mojo.setGenerationMode(ADD_MISSING_TRANSLATIONS);
mojo.execute();

assertSameProperties(TTS_GENERATED_PROPERTIES_PATH, tempI18nPath.resolve("acmetts.properties"));
}

@Test
public void addMissingGenericBundleTranslationsNoChanges() throws IOException, MojoFailureException {
copyPath(TTS_RESOURCES_PATH, tempPath);

mojo.setGenerationMode(ADD_MISSING_TRANSLATIONS);
mojo.execute();

assertSameProperties(TTS_ALL_PROPERTIES_PATH, tempI18nPath.resolve("acmetts.properties"));
assertSameProperties(TTS_ALL_DE_PROPERTIES_PATH, tempI18nPath.resolve("acmetts_de.properties"));
}

@Test
public void addMissingGenericBundleTranslationsToPartialTranslation() throws IOException, MojoFailureException {
copyPath(TTS_RESOURCES_PATH, tempPath);
Files.move(tempI18nPath.resolve("acmetts.partial.properties"), tempI18nPath.resolve("acmetts.properties"),
REPLACE_EXISTING);

mojo.setGenerationMode(ADD_MISSING_TRANSLATIONS);
mojo.execute();

assertSameProperties(TTS_ALL_PROPERTIES_PATH, tempI18nPath.resolve("acmetts.properties"));
assertSameProperties(TTS_ALL_DE_PROPERTIES_PATH, tempI18nPath.resolve("acmetts_de.properties"));
}

@Test
public void skipTranslationsGenericBundleTranslationsGeneratingWithExistingTranslations()
throws IOException, MojoFailureException {
copyPath(TTS_RESOURCES_PATH, tempPath);
Files.move(tempI18nPath.resolve("acmetts.partial.properties"), tempI18nPath.resolve("acmetts.properties"),
REPLACE_EXISTING);

mojo.setGenerationMode(ADD_MISSING_FILES);
mojo.execute();

assertSameProperties(TTS_PARTIAL_PROPERTIES_PATH, tempI18nPath.resolve("acmetts.properties"));
assertSameProperties(TTS_ALL_DE_PROPERTIES_PATH, tempI18nPath.resolve("acmetts_de.properties"));
}

@Test
public void regenerateGenericBundleTranslations() throws IOException, MojoFailureException {
copyPath(TTS_RESOURCES_PATH, tempPath);

mojo.setGenerationMode(REGENERATE_FILES);
mojo.execute();

assertSameProperties(TTS_GENERATED_PROPERTIES_PATH, tempI18nPath.resolve("acmetts.properties"));
assertSameProperties(TTS_ALL_DE_PROPERTIES_PATH, tempI18nPath.resolve("acmetts_de.properties"));
}

@Test
public void addMissingTranslationsWithoutOhInfPath() throws IOException, MojoFailureException {
copyPath(INFOLESS_RESOURCES_PATH, tempPath);

mojo.setGenerationMode(ADD_MISSING_TRANSLATIONS);
mojo.execute();
}
}
Loading

0 comments on commit ec9b1d8

Please sign in to comment.