ScreenshotCommand extends Command { Locale getLocaleFromImagePath(Path path) throws InvalidLocaleException { try { String localeStr = getFirstElementInPath(path); if (localeMappings != null && localeMappings.containsKey(localeStr)) { localeStr = localeMappings.get(localeStr); } if (!repositoryLocales.containsKey(localeStr)) { throw new IllegalArgumentException("The locale: " + localeStr + " is not supported by the repository"); } return repositoryLocales.get(localeStr); } catch (IllegalArgumentException iae) { throw new InvalidLocaleException("The image: " + path.toString() + " must be included in a directory which path starts with a suported locale", iae); } }  @Override void execute();  }
@Test public void testGetLocaleFromImagePathNoMapping() throws Exception { Repository repository = createTestRepoUsingRepoService(); com.box.l10n.mojito.rest.entity.Repository repositoryByName = repositoryClient.getRepositoryByName(repository.getName()); ScreenshotCommand screenshotCommand = new ScreenshotCommand(); screenshotCommand.repositoryLocales = commandHelper.getSortedRepositoryLocales(repositoryByName); Locale localeFromImagePath = screenshotCommand.getLocaleFromImagePath(Paths.get("fr-FR/sub/file.png")); assertEquals("fr-FR", localeFromImagePath.getBcp47Tag()); }
@Test(expected = ScreenshotCommand.InvalidLocaleException.class) public void testGetLocaleFromImagePathNoMappingUnsupportedLocale() throws Exception { Repository repository = createTestRepoUsingRepoService(); com.box.l10n.mojito.rest.entity.Repository repositoryByName = repositoryClient.getRepositoryByName(repository.getName()); ScreenshotCommand screenshotCommand = new ScreenshotCommand(); screenshotCommand.repositoryLocales = commandHelper.getSortedRepositoryLocales(repositoryByName); Locale localeFromImagePath = screenshotCommand.getLocaleFromImagePath(Paths.get("en-ZA/sub/file.png")); assertEquals("fr-FR", localeFromImagePath.getBcp47Tag()); }
@Test public void testGetLocaleFromImagePathMappedLocale() throws Exception { Repository repository = createTestRepoUsingRepoService(); com.box.l10n.mojito.rest.entity.Repository repositoryByName = repositoryClient.getRepositoryByName(repository.getName()); ScreenshotCommand screenshotCommand = new ScreenshotCommand(); screenshotCommand.localeMappings = localeMappingHelper.getLocaleMapping("fr:fr-FR"); screenshotCommand.repositoryLocales = commandHelper.getSortedRepositoryLocales(repositoryByName); Locale localeFromImagePath = screenshotCommand.getLocaleFromImagePath(Paths.get("fr/sub/file.png")); assertEquals("fr-FR", localeFromImagePath.getBcp47Tag()); }