AbstractMediaFileNameStrategyImpl implements MediaFileNameStrategy { @Override public String resolveLocale(final String url) { if (StringUtils.isNotBlank(url)) { final int extPos = url.lastIndexOf('.'); if (extPos > -1) { final String urlNoExt = url.substring(0, extPos); if (urlNoExt.indexOf('_') > -1) { final List<String> languages = languageService.getSupportedLanguages(); for (final String language : languages) { if (urlNoExt.endsWith(language) && urlNoExt.charAt(urlNoExt.length() - language.length() - 1) == '_') { return language; } } } } } return null; } protected  AbstractMediaFileNameStrategyImpl(final String urlPath,
                                                final String relativeInternalRootDirectory,
                                                final LanguageService languageService); @Override String getUrlPath(); @Override String getRelativeInternalRootDirectory(); @Override String resolveObjectCode(final String url); @Override String resolveLocale(final String url); @Override String resolveSuffix(final String url); @Override String resolveFileName(final String url); @Override String resolveRelativeInternalFileNamePath(final String fileName, final String code, final String locale); @Override String resolveRelativeInternalFileNamePath(final String fileName, final String code, final String locale, final String width, final String height); @Override String createRollingFileName(final String fullFileName, final String code, final String suffix, final String locale); void setAttributePrefix(final String attributePrefix);  }
@Test public void testGetLocale() { final LanguageService languageService = context.mock(LanguageService.class, "languageService"); context.checking(new Expectations() {{ allowing(languageService).getSupportedLanguages(); will(returnValue(Arrays.asList("en", "ru", "uk"))); }}); final String[][] testFilenames = { { "some_seo_image_file-name_PRODUCT-or-SKU-CODE_a_ru.jpeg", "ru" }, { "some_seo_image_file-name_PRODUCT1_b_en.jpeg", "en" }, { "some_seo_image_file-name_PROD+UCT1_c_uk.jpeg", "uk" }, { "some_seo_image_file_name_КОД-ПРОДУКТА_d.jpeg", null }, { "some-seo-image-file-name_ЕЩЕ-КОД-пРОДУКТА_e_zu.jpg", null }, { "очень-вкусная-сосиска-с-камнями-от-Сваровски_-КОД-Сосики-_ru.jpg", "ru" }, { "Очень-Вкусная-Сосиска3-с-камнями-от-Булыжникова_ЕЩЕ-КОД-ПРОДУКТА!_ru.jpg", "ru" } }; final MediaFileNameStrategy mediaFileNameStrategy = new AbstractMediaFileNameStrategyImpl("/root/img/", "root", languageService) { @Override public String resolveObjectCodeInternal(final String url) { return null; } }; for (String[] fileName : testFilenames) { String language = mediaFileNameStrategy.resolveLocale(fileName[0]); assertEquals(fileName[0] + " not in expectations. The code is " + language, language, fileName[1]); } assertEquals("en", mediaFileNameStrategy.resolveLocale("seo_name_code_en.jpg")); assertEquals("en", mediaFileNameStrategy.resolveLocale("_code_en.jpg")); assertEquals("en", mediaFileNameStrategy.resolveLocale("code_en.jpg")); assertEquals(null, mediaFileNameStrategy.resolveLocale("code_zu.jpg")); }