AbstractMediaFileNameStrategyImpl implements MediaFileNameStrategy { @Override public String createRollingFileName(final String fullFileName, final String code, final String suffix, final String locale) { final String systemPart = (Constants.NO_IMAGE.equals(code) ? "" : "_" + code) + "_" + (char)(NumberUtils.toInt(suffix) + 'a') + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); final int posExt = fullFileName.lastIndexOf('.'); final String fileName; final String fileExt; if (posExt == -1) { fileName = fullFileName; fileExt = ""; } else { fileName = fullFileName.substring(0, posExt); fileExt = fullFileName.substring(posExt); } final String mainPart; if (fileName.endsWith(systemPart)) { mainPart = fileName.substring(0, fileName.length() - systemPart.length()); } else { mainPart = fileName; } final int posRollingNumber = mainPart.lastIndexOf('-'); if (posRollingNumber == -1 || (mainPart.length() < posRollingNumber + 1) || !NumberUtils.isDigits(mainPart.substring(posRollingNumber + 1))) { return mainPart + "-1" + systemPart + fileExt; } return mainPart.substring(0, posRollingNumber) + "-" + (NumberUtils.toInt(mainPart.substring(posRollingNumber + 1)) + 1) + systemPart + fileExt; } 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 testCreateRollingFileName() throws Exception { final LanguageService languageService = context.mock(LanguageService.class, "languageService"); context.checking(new Expectations() {{ allowing(languageService).getSupportedLanguages(); will(returnValue(Arrays.asList("en", "ru", "uk"))); }}); final MediaFileNameStrategy mediaFileNameStrategy = new AbstractMediaFileNameStrategyImpl("/root/img/", "root", languageService) { @Override public String resolveObjectCodeInternal(final String url) { return null; } }; assertEquals("some-file-name-1_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name.jpg", Constants.NO_IMAGE, "0", null)); assertEquals("some-file-name-2_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-1.jpg", Constants.NO_IMAGE, "0", null)); assertEquals("some-file-name-3_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-2.jpg", Constants.NO_IMAGE, "0", null)); assertEquals("some-file-name-10_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-9.jpg", Constants.NO_IMAGE, "0", null)); assertEquals("some-file-name-11_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-10.jpg", Constants.NO_IMAGE, "0", null)); assertEquals("some-file-name-12_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-11.jpg", Constants.NO_IMAGE, "0", null)); assertEquals("some-file-name-1_code_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name_code_a.jpg", "code", "0", null)); assertEquals("some-file-name-2_code_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-1_code_a.jpg", "code", "0", null)); assertEquals("some-file-name-3_code_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-2_code_a.jpg", "code", "0", null)); assertEquals("some-file-name-10_code_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-9_code_a.jpg", "code", "0", null)); assertEquals("some-file-name-11_code_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-10_code_a.jpg", "code", "0", null)); assertEquals("some-file-name-12_code_a.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-11_code_a.jpg", "code", "0", null)); assertEquals("some-file-name-1_code_b_en.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name_code_b_en.jpg", "code", "1", "en")); assertEquals("some-file-name-2_code_b_en.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-1_code_b_en.jpg", "code", "1", "en")); assertEquals("some-file-name-3_code_b_en.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-2_code_b_en.jpg", "code", "1", "en")); assertEquals("some-file-name-10_code_b_en.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-9_code_b_en.jpg", "code", "1", "en")); assertEquals("some-file-name-11_code_b_en.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-10_code_b_en.jpg", "code", "1", "en")); assertEquals("some-file-name-12_code_b_en.jpg", mediaFileNameStrategy.createRollingFileName("some-file-name-11_code_b_en.jpg", "code", "1", "en")); }