@Test public void testGetCodeForIncorrectFileName() { 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(Constants.NO_IMAGE, mediaFileNameStrategy.resolveObjectCode(null)); assertEquals(Constants.NO_IMAGE, mediaFileNameStrategy.resolveObjectCode("")); assertEquals(Constants.NO_IMAGE, mediaFileNameStrategy.resolveObjectCode("some-incorrect-code-in-file-name")); }
@Override public String resolveObjectCode(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("_"); final String candidate = nameParts[nameParts.length - 2]; if (nameParts[nameParts.length - 1].length() == 1) { final char csuf = nameParts[nameParts.length - 1].charAt(0); if (csuf >= 'a' && csuf <= 'z') { return candidate; } } } } } final String code = resolveObjectCodeInternal(url); if (code != null) { return code; } } return Constants.NO_IMAGE; }
AbstractMediaFileNameStrategyImpl implements MediaFileNameStrategy { @Override public String resolveObjectCode(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("_"); final String candidate = nameParts[nameParts.length - 2]; if (nameParts[nameParts.length - 1].length() == 1) { final char csuf = nameParts[nameParts.length - 1].charAt(0); if (csuf >= 'a' && csuf <= 'z') { return candidate; } } } } } final String code = resolveObjectCodeInternal(url); if (code != null) { return code; } } return Constants.NO_IMAGE; } }
AbstractMediaFileNameStrategyImpl implements MediaFileNameStrategy { @Override public String resolveObjectCode(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("_"); final String candidate = nameParts[nameParts.length - 2]; if (nameParts[nameParts.length - 1].length() == 1) { final char csuf = nameParts[nameParts.length - 1].charAt(0); if (csuf >= 'a' && csuf <= 'z') { return candidate; } } } } } final String code = resolveObjectCodeInternal(url); if (code != null) { return code; } } return Constants.NO_IMAGE; } protected  AbstractMediaFileNameStrategyImpl(final String urlPath,
                                                final String relativeInternalRootDirectory,
                                                final LanguageService languageService); }
AbstractMediaFileNameStrategyImpl implements MediaFileNameStrategy { @Override public String resolveObjectCode(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("_"); final String candidate = nameParts[nameParts.length - 2]; if (nameParts[nameParts.length - 1].length() == 1) { final char csuf = nameParts[nameParts.length - 1].charAt(0); if (csuf >= 'a' && csuf <= 'z') { return candidate; } } } } } final String code = resolveObjectCodeInternal(url); if (code != null) { return code; } } return Constants.NO_IMAGE; } 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); }
AbstractMediaFileNameStrategyImpl implements MediaFileNameStrategy { @Override public String resolveObjectCode(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("_"); final String candidate = nameParts[nameParts.length - 2]; if (nameParts[nameParts.length - 1].length() == 1) { final char csuf = nameParts[nameParts.length - 1].charAt(0); if (csuf >= 'a' && csuf <= 'z') { return candidate; } } } } } final String code = resolveObjectCodeInternal(url); if (code != null) { return code; } } return Constants.NO_IMAGE; } 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);  }