diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/FileMappingRuleParser.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/FileMappingRuleParser.java
index 55b39244..97479469 100644
--- a/zanata-client-commands/src/main/java/org/zanata/client/commands/FileMappingRuleParser.java
+++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/FileMappingRuleParser.java
@@ -26,6 +26,7 @@
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.EnumMap;
+import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
@@ -34,10 +35,14 @@
import org.slf4j.LoggerFactory;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleMapping;
+import org.zanata.common.ProjectType;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
import com.google.common.io.Files;
+import static org.zanata.client.commands.TransFileResolver.QualifiedSrcDocName;
+
/**
* @author Patrick Huang pahuang@redhat.com
@@ -46,8 +51,11 @@ public class FileMappingRuleParser {
private static final Logger log =
LoggerFactory.getLogger(FileMappingRuleParser.class);
private final FileMappingRule mappingRule;
+ private final ProjectType projectType;
- public FileMappingRuleParser(FileMappingRule rule) {
+ public FileMappingRuleParser(FileMappingRule rule, ProjectType projectType) {
+ this.projectType = projectType;
+ Preconditions.checkState(isRuleValid(rule.getRule()), "rule defined is not valid: %s", rule.getRule());
Preconditions.checkArgument(isRuleValid(rule.getRule()));
this.mappingRule = rule;
}
@@ -58,20 +66,25 @@ protected static boolean isRuleValid(String rule) {
|| rule.contains(Placeholders.localeWithUnderscore.holder);
}
- public boolean isApplicable(String qualifiedDocName) {
- boolean ruleValid = isRuleValid(mappingRule.getRule());
- if (!ruleValid) {
- return false;
+ public boolean isApplicable(QualifiedSrcDocName qualifiedSrcDocName) {
+ if (Strings.isNullOrEmpty(mappingRule.getPattern())) {
+ return matchFileExtensionWithProjectType(qualifiedSrcDocName);
}
PathMatcher matcher =
FileSystems.getDefault().getPathMatcher("glob:" + mappingRule.getPattern());
- return matcher.matches(Paths.get(qualifiedDocName));
+ return matcher.matches(Paths.get(qualifiedSrcDocName.getFullName()));
+ }
+
+ private boolean matchFileExtensionWithProjectType(QualifiedSrcDocName qualifiedSrcDocName) {
+ List supportedTypes =
+ ProjectType.getSupportedSourceFileTypes(projectType);
+ return supportedTypes.contains(qualifiedSrcDocName.getExtension());
}
- public String getRelativePathFromRule(@Nonnull String sourceFile,
+ public String getRelativePathFromRule(QualifiedSrcDocName qualifiedSrcDocName,
@Nonnull LocaleMapping localeMapping) {
EnumMap map =
- parseToMap(sourceFile, localeMapping);
+ parseToMap(qualifiedSrcDocName.getFullName(), localeMapping);
String temp = mappingRule.getRule();
for (Map.Entry entry : map.entrySet()) {
diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/TransFileResolver.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/TransFileResolver.java
index 18a0c31f..a369e89a 100644
--- a/zanata-client-commands/src/main/java/org/zanata/client/commands/TransFileResolver.java
+++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/TransFileResolver.java
@@ -22,35 +22,147 @@
package org.zanata.client.commands;
import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.base.Throwables;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.io.Files;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleMapping;
+import org.zanata.common.ProjectType;
import java.io.File;
import java.util.List;
+import java.util.Map;
+
+import static org.zanata.client.commands.Messages._;
/**
- * @author Patrick Huang
- * pahuang@redhat.com
+ * @author Patrick Huang pahuang@redhat.com
*/
public class TransFileResolver {
private final ConfigurableProjectOptions opts;
+ private Map defaultProjectTypeToRules =
+ ImmutableMap
+ . builder()
+ .put(ProjectType.File, new FileMappingRule(null,
+ "{locale}/{path}/{filename}.{extension}"))
+ .put(ProjectType.Gettext, new FileMappingRule(null, ""))
+ .build();
public TransFileResolver(ConfigurableProjectOptions opts) {
this.opts = opts;
}
- public Optional getTransFile(String sourceDocWithExtension,
- LocaleMapping localeMapping) {
+ public File getTransFile(QualifiedSrcDocName qualifiedSrcDocName,
+ LocaleMapping localeMapping) {
+ Optional fileOptional =
+ tryGetTransFileFromProjectMappingRules(qualifiedSrcDocName,
+ localeMapping);
+ if (fileOptional.isPresent()) {
+ return fileOptional.get();
+ } else {
+ ProjectType projectType = getProjectType();
+ return getDefaultTransFileFromProjectType(qualifiedSrcDocName,
+ localeMapping, projectType);
+ }
+ }
+
+ public File getTransFile(UnqualifiedSrcDocName unqualifiedSrcDocName,
+ LocaleMapping localeMapping) {
+ QualifiedSrcDocName qualifiedSrcDocName =
+ unqualifiedSrcDocName.toQualifiedDocName(getProjectType());
+ return getTransFile(qualifiedSrcDocName, localeMapping);
+ }
+
+ private ProjectType getProjectType() {
+ try {
+ return ProjectType.getValueOf(
+ opts.getProjectType());
+ } catch (Exception e) {
+ throw Throwables.propagate(e);
+ }
+ }
+
+ private File getDefaultTransFileFromProjectType(
+ QualifiedSrcDocName qualifiedSrcDocName, LocaleMapping localeMapping,
+ ProjectType projectType) {
+ FileMappingRule rule = defaultProjectTypeToRules.get(projectType);
+ Preconditions
+ .checkNotNull(rule, _("no.default.mapping"), projectType);
+ String relativePath = new FileMappingRuleParser(rule, projectType)
+ .getRelativePathFromRule(qualifiedSrcDocName, localeMapping);
+ return new File(opts.getTransDir(), relativePath);
+ }
+
+ private Optional tryGetTransFileFromProjectMappingRules(
+ QualifiedSrcDocName qualifiedSrcDocName, LocaleMapping localeMapping) {
List fileMappingRules = opts.getFileMappingRules();
+ // TODO may need to sort the rules. put rules without pattern to last
for (FileMappingRule rule : fileMappingRules) {
- FileMappingRuleParser parser = new FileMappingRuleParser(rule);
- if (parser.isApplicable(sourceDocWithExtension)) {
+ FileMappingRuleParser parser = new FileMappingRuleParser(rule,
+ getProjectType());
+ if (parser.isApplicable(qualifiedSrcDocName)) {
String relativePath = parser
- .getRelativePathFromRule(sourceDocWithExtension,
- localeMapping);
+ .getRelativePathFromRule(qualifiedSrcDocName,
+ localeMapping);
return Optional.of(new File(opts.getTransDir(), relativePath));
}
}
return Optional.absent();
}
+
+ public static class QualifiedSrcDocName {
+ private final String fullName;
+ private final String extension;
+
+ private QualifiedSrcDocName(String fullName) {
+ this.fullName = fullName;
+ extension = Files.getFileExtension(fullName).toLowerCase();
+ }
+ public static QualifiedSrcDocName from(String qualifiedName) {
+ String extension = Files.getFileExtension(qualifiedName);
+ Preconditions.checkArgument(!Strings.isNullOrEmpty(extension), "expect a qualified document name (with extension)");
+ return new QualifiedSrcDocName(qualifiedName);
+ }
+ public static QualifiedSrcDocName from(String unqualifiedName, String extension) {
+ return new QualifiedSrcDocName(unqualifiedName + "." + extension);
+ }
+
+ public String getFullName() {
+ return fullName;
+ }
+
+ public String getExtension() {
+ return extension;
+ }
+ }
+ public static class UnqualifiedSrcDocName {
+ private final String name;
+ private UnqualifiedSrcDocName(String name) {
+ this.name = name;
+ }
+ public static UnqualifiedSrcDocName from(String docName) {
+ String extension = Files.getFileExtension(docName);
+ Preconditions.checkArgument(Strings.isNullOrEmpty(extension), "expect an unqualifed document name (without extension)");
+ return new UnqualifiedSrcDocName(docName);
+ }
+ public QualifiedSrcDocName toQualifiedDocName(ProjectType projectType) {
+ switch (projectType) {
+ case Utf8Properties:
+ case Properties:
+ return QualifiedSrcDocName.from(name, ".properties");
+ case Gettext:
+ case Podir:
+ return QualifiedSrcDocName.from(name, ".pot");
+ case Xliff:
+ case Xml:
+ return QualifiedSrcDocName.from(name, ".xml");
+ case File:
+ throw new IllegalArgumentException("You can not using unqualified document name in file type project");
+ }
+ throw new IllegalStateException("Can not convert unqualified document name for this project type: " + projectType);
+ }
+ }
}
diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushStrategy.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushStrategy.java
index 42f13004..87b5c842 100644
--- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushStrategy.java
+++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushStrategy.java
@@ -21,11 +21,10 @@
package org.zanata.client.commands.push;
import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.zanata.client.commands.TransFileResolver;
import org.zanata.client.config.LocaleMapping;
/**
@@ -61,9 +60,10 @@ public void visitTranslationFiles(String sourceDocument,
return;
}
for (LocaleMapping localeMapping : getOpts().getLocaleMapList()) {
- String locale = localeMapping.getLocalLocale();
- File localeDir = new File(getOpts().getTransDir(), locale);
- File translationFile = new File(localeDir, sourceDocument);
+ File translationFile = new TransFileResolver(getOpts())
+ .getTransFile(TransFileResolver.QualifiedSrcDocName.from(
+ sourceDocument), localeMapping);
+
if (translationFile.canRead()) {
visitor.visit(localeMapping, translationFile);
} else {
diff --git a/zanata-client-commands/src/main/resources/prompts.properties b/zanata-client-commands/src/main/resources/prompts.properties
index 90f817dc..9f99a2e4 100644
--- a/zanata-client-commands/src/main/resources/prompts.properties
+++ b/zanata-client-commands/src/main/resources/prompts.properties
@@ -84,3 +84,6 @@ update.marker.hint=You can edit the file to change when and how your client chec
no.check.update.prompt=true|false. Whether to ask before performing an update.
latest.version.confirm=You are running the latest version of Zanata client.
create.file.failure=Failed to create file at %s. Check permissions.
+
+no.default.mapping=Can not find default translation file mapping rule for project type %s
+
diff --git a/zanata-client-commands/src/test/java/org/zanata/client/TempTransFileRule.java b/zanata-client-commands/src/test/java/org/zanata/client/TempTransFileRule.java
new file mode 100644
index 00000000..628dd536
--- /dev/null
+++ b/zanata-client-commands/src/test/java/org/zanata/client/TempTransFileRule.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2014, Red Hat, Inc. and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.zanata.client;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.rules.TemporaryFolder;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * @author Patrick Huang
+ * pahuang@redhat.com
+ */
+public class TempTransFileRule extends TemporaryFolder {
+ private File transDir;
+
+ @Override
+ protected void before() throws Throwable {
+ super.before();
+ if (transDir == null) {
+ transDir = getRoot();
+ }
+ }
+
+ public File createTransFileRelativeToTransDir(String path)
+ throws IOException {
+ File file = new File(transDir, path);
+ assertThat(file.getParentFile().mkdirs(), is(true));
+ assertThat(file.createNewFile(), is(true));
+ return file;
+ }
+
+ public File getTransDir() {
+ return transDir;
+ }
+}
diff --git a/zanata-client-commands/src/test/java/org/zanata/client/TestUtils.java b/zanata-client-commands/src/test/java/org/zanata/client/TestUtils.java
new file mode 100644
index 00000000..79cd870a
--- /dev/null
+++ b/zanata-client-commands/src/test/java/org/zanata/client/TestUtils.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2014, Red Hat, Inc. and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.zanata.client;
+
+import org.zanata.client.commands.ConfigurableProjectOptions;
+import org.zanata.client.config.LocaleMapping;
+import com.google.common.base.Optional;
+
+/**
+ * @author Patrick Huang
+ * pahuang@redhat.com
+ */
+public class TestUtils {
+ /**
+ * Creates a LocaleMapping with optional map from and then add to options.
+ * @return the created LocaleMapping
+ */
+ public static LocaleMapping createAndAddLocaleMapping(String localeId,
+ Optional optionalMapFrom, ConfigurableProjectOptions opts) {
+ LocaleMapping mapping = new LocaleMapping(localeId);
+ if (optionalMapFrom.isPresent()) {
+ mapping.setMapFrom(optionalMapFrom.get());
+ }
+ opts.getLocaleMapList().add(mapping);
+ return mapping;
+ }
+}
diff --git a/zanata-client-commands/src/test/java/org/zanata/client/commands/FileMappingRuleParserTest.java b/zanata-client-commands/src/test/java/org/zanata/client/commands/FileMappingRuleParserTest.java
index 9b648d61..388f47e2 100644
--- a/zanata-client-commands/src/test/java/org/zanata/client/commands/FileMappingRuleParserTest.java
+++ b/zanata-client-commands/src/test/java/org/zanata/client/commands/FileMappingRuleParserTest.java
@@ -29,6 +29,7 @@
import org.junit.Test;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleMapping;
+import org.zanata.common.ProjectType;
public class FileMappingRuleParserTest {
@@ -50,20 +51,36 @@ public void canCheckSyntaxErrorInTheRule() {
@Test
public void willReturnTransFileRelativePath() {
assertThat(getTransFile("pot/message.pot", "fr",
- "{path}/../{locale}/{filename}.po"),
+ "{path}/../{locale}/{filename}.po", ProjectType.Podir),
Matchers.equalTo("fr/message.po"));
assertThat(getTransFile("./message.pot", "fr",
- "{path}/{locale_with_underscore}.po"),
+ "{path}/{locale_with_underscore}.po", ProjectType.Gettext),
Matchers.equalTo("fr.po"));
assertThat(getTransFile("a/path/message.odt", "de-DE",
- "{path}/{locale_with_underscore}_{filename}.{extension}"),
+ "{path}/{locale_with_underscore}_{filename}.{extension}",
+ ProjectType.File),
Matchers.equalTo("a/path/de_DE_message.odt"));
}
- private String getTransFile(String sourceFile, String locale, String rule) {
+ @Test
+ public void ifNoPatternWillUseProjectType() {
+ FileMappingRuleParser parser =
+ new FileMappingRuleParser(
+ new FileMappingRule(null,
+ "{path}/{locale_with_underscore}.po"),
+ ProjectType.Gettext);
+ assertThat(parser.getRelativePathFromRule(
+ TransFileResolver.QualifiedSrcDocName.from("message.pot"),
+ new LocaleMapping("zh")), Matchers.equalTo("zh.po"));
+ }
+
+ private String getTransFile(String sourceFile, String locale, String rule,
+ ProjectType projectType) {
FileMappingRuleParser parser = new FileMappingRuleParser(
- new FileMappingRule("**/*", rule));
- return parser.getRelativePathFromRule(sourceFile, new LocaleMapping(locale));
+ new FileMappingRule("**/*", rule), projectType);
+ return parser.getRelativePathFromRule(
+ TransFileResolver.QualifiedSrcDocName.from(sourceFile),
+ new LocaleMapping(locale));
}
}
diff --git a/zanata-client-commands/src/test/java/org/zanata/client/commands/TransFileResolverTest.java b/zanata-client-commands/src/test/java/org/zanata/client/commands/TransFileResolverTest.java
index 95963ac6..d515379c 100644
--- a/zanata-client-commands/src/test/java/org/zanata/client/commands/TransFileResolverTest.java
+++ b/zanata-client-commands/src/test/java/org/zanata/client/commands/TransFileResolverTest.java
@@ -1,11 +1,10 @@
package org.zanata.client.commands;
-import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
-import org.zanata.client.commands.init.InitOptionsImpl;
+import org.zanata.client.commands.push.PushOptionsImpl;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleMapping;
@@ -21,33 +20,41 @@ public class TransFileResolverTest {
@Before
public void setUp() {
- // we choose init option as the implementation just because it's simple
- opts = new InitOptionsImpl();
+ opts = new PushOptionsImpl();
resolver = new TransFileResolver(opts);
}
@Test
public void canGetTransFileUsingRule() {
opts.setTransDir(new File("."));
+ opts.setProjectType("podir");
opts.setFileMappingRules(Lists.newArrayList(
new FileMappingRule("**/*.pot",
"{path}/{locale_with_underscore}.po"),
new FileMappingRule("**/*.properties",
"{path}/{filename}_{locale_with_underscore}.{extension}")));
- Optional gettext =
- resolver.getTransFile("gcc/po/gcc.pot", new LocaleMapping("de-DE"));
+ File gettext =
+ resolver.getTransFile(TransFileResolver.QualifiedSrcDocName.from(
+ "gcc/po/gcc.pot"), new LocaleMapping("de-DE"));
- assertThat(gettext.get().getPath(), equalTo("./gcc/po/de_DE.po"));
+ assertThat(gettext.getPath(), equalTo("./gcc/po/de_DE.po"));
- Optional prop = resolver
- .getTransFile("src/main/resources/messages.properties",
- new LocaleMapping("zh"));
- assertThat(prop.get().getPath(), equalTo(
+ File prop = resolver
+ .getTransFile(TransFileResolver.QualifiedSrcDocName.from(
+ "src/main/resources/messages.properties"),
+ new LocaleMapping("zh"));
+ assertThat(prop.getPath(), equalTo(
"./src/main/resources/messages_zh.properties"));
+ }
- Optional noMatching = resolver
- .getTransFile("doc/marketting.odt", new LocaleMapping("ja"));
- assertThat(noMatching.isPresent(), equalTo(false));
+ @Test
+ public void canGetTransFileUsingProjectTypeIfNoRuleIsApplicable() {
+ opts.setTransDir(new File("."));
+ opts.setProjectType("file");
+ File noMatching = resolver
+ .getTransFile(TransFileResolver.QualifiedSrcDocName.from(
+ "doc/marketing.odt"), new LocaleMapping("ja"));
+ assertThat(noMatching.getPath(), equalTo("./ja/doc/marketing.odt"));
}
}
\ No newline at end of file
diff --git a/zanata-client-commands/src/test/java/org/zanata/client/commands/push/RawPushStrategyTest.java b/zanata-client-commands/src/test/java/org/zanata/client/commands/push/RawPushStrategyTest.java
new file mode 100644
index 00000000..f1b737ee
--- /dev/null
+++ b/zanata-client-commands/src/test/java/org/zanata/client/commands/push/RawPushStrategyTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2014, Red Hat, Inc. and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.zanata.client.commands.push;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.zanata.client.TestUtils.createAndAddLocaleMapping;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.zanata.client.TempTransFileRule;
+import org.zanata.client.config.FileMappingRule;
+import org.zanata.client.config.LocaleList;
+import org.zanata.client.config.LocaleMapping;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
+
+public class RawPushStrategyTest {
+ @Rule
+ public TempTransFileRule tempFileRule = new TempTransFileRule();
+ private RawPushStrategy strategy;
+ private PushOptionsImpl opts;
+ @Captor
+ private ArgumentCaptor fileCaptor;
+ @Mock
+ private RawPushStrategy.TranslationFilesVisitor visitor;
+
+ @Before
+ public void setUp() throws IOException {
+ MockitoAnnotations.initMocks(this);
+ strategy = new RawPushStrategy();
+ opts = new PushOptionsImpl();
+ opts.setLocaleMapList(new LocaleList());
+ strategy.setPushOptions(opts);
+ opts.setTransDir(tempFileRule.getTransDir());
+ opts.setProjectType("file");
+ }
+
+ @Test
+ public void canVisitTranslationFileWithoutFileMapping() throws IOException {
+ // with translation
+ LocaleMapping deMapping = createAndAddLocaleMapping("de",
+ Optional.absent(), opts);
+ // with translation and has map-from
+ LocaleMapping zhMapping =
+ createAndAddLocaleMapping("zh-CN",
+ Optional.of("zh-Hans"),
+ opts);
+ // no translation
+ opts.getLocaleMapList().add(new LocaleMapping("ja"));
+
+ File deTransFile =
+ tempFileRule.createTransFileRelativeToTransDir(
+ "de/src/test.odt");
+ File zhTransFile =
+ tempFileRule.createTransFileRelativeToTransDir(
+ "zh-Hans/src/test.odt");
+
+ strategy.visitTranslationFiles("src/test.odt", visitor);
+
+ verify(visitor).visit(eq(deMapping), fileCaptor.capture());
+ assertThat(fileCaptor.getValue(), equalTo(deTransFile));
+
+ verify(visitor).visit(eq(zhMapping), fileCaptor.capture());
+ assertThat(fileCaptor.getValue(), equalTo(zhTransFile));
+
+ verifyNoMoreInteractions(visitor);
+ }
+
+ @Test
+ public void canVisitTranslationFileUsingFileMapping() throws IOException {
+ // with translation
+ LocaleMapping deMapping = createAndAddLocaleMapping("de",
+ Optional.absent(), opts);
+ // with translation and has map-from
+ LocaleMapping zhMapping =
+ createAndAddLocaleMapping("zh-CN",
+ Optional.of("zh-Hans"),
+ opts);
+ // no translation
+ opts.getLocaleMapList().add(new LocaleMapping("ja"));
+
+ File deTransFile =
+ tempFileRule.createTransFileRelativeToTransDir("de/test.odt");
+ File zhTransFile =
+ tempFileRule.createTransFileRelativeToTransDir(
+ "zh-Hans/test.odt");
+
+ opts.setFileMappingRules(Lists.newArrayList(
+ new FileMappingRule("**/*.odt",
+ "{locale}/{filename}.{extension}")));
+
+ strategy.visitTranslationFiles("src/test.odt", visitor);
+
+ verify(visitor).visit(eq(deMapping), fileCaptor.capture());
+ assertThat(fileCaptor.getValue(), equalTo(deTransFile));
+
+ verify(visitor).visit(eq(zhMapping), fileCaptor.capture());
+ assertThat(fileCaptor.getValue(), equalTo(zhTransFile));
+
+ verifyNoMoreInteractions(visitor);
+ }
+}
diff --git a/zanata-rest-client/src/main/java/org/zanata/rest/client/ClientUtility.java b/zanata-rest-client/src/main/java/org/zanata/rest/client/ClientUtility.java
index a99a26cb..fef8f53f 100644
--- a/zanata-rest-client/src/main/java/org/zanata/rest/client/ClientUtility.java
+++ b/zanata-rest-client/src/main/java/org/zanata/rest/client/ClientUtility.java
@@ -48,4 +48,10 @@ public static void checkResult(ClientResponse> response, URI uri) {
throw new RuntimeException(msg);
}
}
+
+ public static void checkResultAndReleaseConnection(
+ ClientResponse> clientResponse) {
+ checkResult(clientResponse, null);
+ clientResponse.releaseConnection();
+ }
}