AbstractMediaFileNameStrategyImpl implements MediaFileNameStrategy { @Override public String resolveSuffix(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 String locale = resolveLocale(url); final String urlNoLocale; if (locale != null) { urlNoLocale = urlNoExt.substring(0, urlNoExt.length() - locale.length() - 1); } else { urlNoLocale = urlNoExt; } if (urlNoLocale.indexOf('_') > -1 && StringUtils.countMatches(urlNoLocale, "_") > 1) { final String[] nameParts = urlNoLocale.split("_"); if (nameParts[nameParts.length - 1].length() == 1) { final char csuf = nameParts[nameParts.length - 1].charAt(0); if (csuf >= 'a' && csuf <= 'z') { return String.valueOf(0 + csuf - 'a'); } } } } } } return "0"; } 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 testGetSuffix() { 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.jpeg", "0" }, { "some_seo_image_file-name_PRODUCT-or-SKU-CODE_a_ru.jpeg", "0" }, { "some_seo_image_file-name_PRODUCT1_b.jpeg", "1" }, { "some_seo_image_file-name_PRODUCT1_b_en.jpeg", "1" }, { "some_seo_image_file-name_PROD+UCT1_c.jpeg", "2" }, { "some_seo_image_file-name_PROD+UCT1_c_uk.jpeg", "2" }, { "some_seo_image_file_name_КОД-ПРОДУКТА_d.jpeg", "3" }, { "some_seo_image_file_name_КОД-ПРОДУКТА_d_ru.jpeg", "3" }, { "some-seo-image-file-name_ЕЩЕ-КОД-пРОДУКТА_e.jpg", "4" }, { "some-seo-image-file-name_ЕЩЕ-КОД-пРОДУКТА_e_en.jpg", "4" }, { "очень-вкусная-сосиска-с-камнями-от-Сваровски_-КОД-Сосики-_f.jpg", "5" }, { "очень-вкусная-сосиска-с-камнями-от-Сваровски_-КОД-Сосики-_f_uk.jpg", "5" }, { "Очень-Вкусная-Сосиска3-с-камнями-от-Булыжникова_ЕЩЕ-КОД-ПРОДУКТА!_g.jpg", "6" }, { "Очень-Вкусная-Сосиска3-с-камнями-от-Булыжникова_ЕЩЕ-КОД-ПРОДУКТА!_g_ru.jpg", "6" } }; final MediaFileNameStrategy mediaFileNameStrategy = new AbstractMediaFileNameStrategyImpl("/root/img/", "root", languageService) { @Override public String resolveObjectCodeInternal(final String url) { return null; } }; for (String[] fileName : testFilenames) { String code = mediaFileNameStrategy.resolveSuffix(fileName[0]); assertNotNull(code); assertFalse("Contains _ ", code.indexOf('_') > -1); assertEquals(fileName[0] + " not in expectations. The code is " + code, code, fileName[1]); } assertEquals("2", mediaFileNameStrategy.resolveSuffix("seo_name_code_c.jpg")); assertEquals("2", mediaFileNameStrategy.resolveSuffix("seo_name_code_c_ru.jpg")); assertEquals("2", mediaFileNameStrategy.resolveSuffix("_code_c.jpg")); assertEquals("2", mediaFileNameStrategy.resolveSuffix("_code_c_ru.jpg")); assertEquals("0", mediaFileNameStrategy.resolveSuffix("code.jpg")); assertEquals("0", mediaFileNameStrategy.resolveSuffix("code_ru.jpg")); }