@Test public void testGetImageAttributeFileNamesWithDefaults() throws Exception { final Attributable attributable = context.mock(Attributable.class, "attributable"); final AttrValue av1 = context.mock(AttrValue.class, "av1"); final AttrValue av2 = context.mock(AttrValue.class, "av2"); final AttrValue av3 = context.mock(AttrValue.class, "av3"); final AttrValue av4 = context.mock(AttrValue.class, "av4"); final List<AttrValue> avs = Arrays.asList(av1, av2, av3, av4); context.checking(new Expectations() {{ oneOf(attributable).getAllAttributes(); will(returnValue(avs)); oneOf(av1).getAttributeCode(); will(returnValue("MY_IMAGE1_en")); exactly(2).of(av1).getVal(); will(returnValue("image1_en.jpg")); oneOf(av2).getAttributeCode(); will(returnValue("MY_IMAGE2")); exactly(2).of(av2).getVal(); will(returnValue("image2.jpg")); oneOf(av3).getAttributeCode(); will(returnValue("MY_IMAGE1")); oneOf(av3).getVal(); will(returnValue("image1.jpg")); oneOf(av4).getAttributeCode(); will(returnValue("MY_IMAGE3_en")); exactly(2).of(av4).getVal(); will(returnValue("image3_en.jpg")); }}); final List<Pair<String, String>> pairs = service.getImageAttributeFileNamesInternal(attributable, "en", "MY_IMAGE"); assertNotNull(pairs); assertEquals(3, pairs.size()); assertEquals("MY_IMAGE1_en", pairs.get(0).getFirst()); assertEquals("image1_en.jpg", pairs.get(0).getSecond()); assertEquals("MY_IMAGE2", pairs.get(1).getFirst()); assertEquals("image2.jpg", pairs.get(1).getSecond()); assertEquals("MY_IMAGE3_en", pairs.get(2).getFirst()); assertEquals("image3_en.jpg", pairs.get(2).getSecond()); context.assertIsSatisfied(); }
List<Pair<String, String>> getImageAttributeFileNamesInternal(final Attributable attributable, final String lang, final String prefix) { final Collection<AttrValue> values = attributable.getAllAttributes(); if (CollectionUtils.isEmpty(values)) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } final Map<String, Pair<String, String>> attrToFileMap = new TreeMap<>(); for (final AttrValue av : values) { final String code = av.getAttributeCode(); if (code.startsWith(prefix) && StringUtils.isNotBlank(av.getVal())) { final String suffix = code.substring(prefix.length()); final String key = suffix.length() == 1 || suffix.indexOf('_') == 1 ? "0" + suffix : suffix; if (code.endsWith(lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); attrToFileMap.remove(key.substring(0, key.length() - lang.length() - 1)); } else { final char lastChar = code.charAt(code.length() - 1); if (lastChar >= '0' && lastChar <= '9' && !attrToFileMap.containsKey(key + "_" + lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); } } } } if (attrToFileMap.isEmpty()) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } return Collections.unmodifiableList(new ArrayList<>(attrToFileMap.values())); }
AbstractImageServiceImpl implements AttributableImageService { List<Pair<String, String>> getImageAttributeFileNamesInternal(final Attributable attributable, final String lang, final String prefix) { final Collection<AttrValue> values = attributable.getAllAttributes(); if (CollectionUtils.isEmpty(values)) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } final Map<String, Pair<String, String>> attrToFileMap = new TreeMap<>(); for (final AttrValue av : values) { final String code = av.getAttributeCode(); if (code.startsWith(prefix) && StringUtils.isNotBlank(av.getVal())) { final String suffix = code.substring(prefix.length()); final String key = suffix.length() == 1 || suffix.indexOf('_') == 1 ? "0" + suffix : suffix; if (code.endsWith(lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); attrToFileMap.remove(key.substring(0, key.length() - lang.length() - 1)); } else { final char lastChar = code.charAt(code.length() - 1); if (lastChar >= '0' && lastChar <= '9' && !attrToFileMap.containsKey(key + "_" + lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); } } } } if (attrToFileMap.isEmpty()) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } return Collections.unmodifiableList(new ArrayList<>(attrToFileMap.values())); } }
AbstractImageServiceImpl implements AttributableImageService { List<Pair<String, String>> getImageAttributeFileNamesInternal(final Attributable attributable, final String lang, final String prefix) { final Collection<AttrValue> values = attributable.getAllAttributes(); if (CollectionUtils.isEmpty(values)) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } final Map<String, Pair<String, String>> attrToFileMap = new TreeMap<>(); for (final AttrValue av : values) { final String code = av.getAttributeCode(); if (code.startsWith(prefix) && StringUtils.isNotBlank(av.getVal())) { final String suffix = code.substring(prefix.length()); final String key = suffix.length() == 1 || suffix.indexOf('_') == 1 ? "0" + suffix : suffix; if (code.endsWith(lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); attrToFileMap.remove(key.substring(0, key.length() - lang.length() - 1)); } else { final char lastChar = code.charAt(code.length() - 1); if (lastChar >= '0' && lastChar <= '9' && !attrToFileMap.containsKey(key + "_" + lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); } } } } if (attrToFileMap.isEmpty()) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } return Collections.unmodifiableList(new ArrayList<>(attrToFileMap.values())); } AbstractImageServiceImpl(); protected  AbstractImageServiceImpl(final CacheManager cacheManager); }
AbstractImageServiceImpl implements AttributableImageService { List<Pair<String, String>> getImageAttributeFileNamesInternal(final Attributable attributable, final String lang, final String prefix) { final Collection<AttrValue> values = attributable.getAllAttributes(); if (CollectionUtils.isEmpty(values)) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } final Map<String, Pair<String, String>> attrToFileMap = new TreeMap<>(); for (final AttrValue av : values) { final String code = av.getAttributeCode(); if (code.startsWith(prefix) && StringUtils.isNotBlank(av.getVal())) { final String suffix = code.substring(prefix.length()); final String key = suffix.length() == 1 || suffix.indexOf('_') == 1 ? "0" + suffix : suffix; if (code.endsWith(lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); attrToFileMap.remove(key.substring(0, key.length() - lang.length() - 1)); } else { final char lastChar = code.charAt(code.length() - 1); if (lastChar >= '0' && lastChar <= '9' && !attrToFileMap.containsKey(key + "_" + lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); } } } } if (attrToFileMap.isEmpty()) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } return Collections.unmodifiableList(new ArrayList<>(attrToFileMap.values())); } AbstractImageServiceImpl(); protected  AbstractImageServiceImpl(final CacheManager cacheManager); @Override String getImageURI(final Object object,
                              final String servletContextPath,
                              final String locale,
                              final String width,
                              final String height,
                              final String imageName); @Override String getImage(final Attributable attributable,
                           final String httpServletContextPath,
                           final String locale,
                           final String width,
                           final String height,
                           final String attrName,
                           String attrVal); @Override List<Pair<String, String>> getImageAttributeFileNames(final Attributable attributable, final String lang); }
AbstractImageServiceImpl implements AttributableImageService { List<Pair<String, String>> getImageAttributeFileNamesInternal(final Attributable attributable, final String lang, final String prefix) { final Collection<AttrValue> values = attributable.getAllAttributes(); if (CollectionUtils.isEmpty(values)) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } final Map<String, Pair<String, String>> attrToFileMap = new TreeMap<>(); for (final AttrValue av : values) { final String code = av.getAttributeCode(); if (code.startsWith(prefix) && StringUtils.isNotBlank(av.getVal())) { final String suffix = code.substring(prefix.length()); final String key = suffix.length() == 1 || suffix.indexOf('_') == 1 ? "0" + suffix : suffix; if (code.endsWith(lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); attrToFileMap.remove(key.substring(0, key.length() - lang.length() - 1)); } else { final char lastChar = code.charAt(code.length() - 1); if (lastChar >= '0' && lastChar <= '9' && !attrToFileMap.containsKey(key + "_" + lang)) { attrToFileMap.put(key, new Pair<>(code, av.getVal())); } } } } if (attrToFileMap.isEmpty()) { return Collections.singletonList(createNoDefaultImagePair(attributable)); } return Collections.unmodifiableList(new ArrayList<>(attrToFileMap.values())); } AbstractImageServiceImpl(); protected  AbstractImageServiceImpl(final CacheManager cacheManager); @Override String getImageURI(final Object object,
                              final String servletContextPath,
                              final String locale,
                              final String width,
                              final String height,
                              final String imageName); @Override String getImage(final Attributable attributable,
                           final String httpServletContextPath,
                           final String locale,
                           final String width,
                           final String height,
                           final String attrName,
                           String attrVal); @Override List<Pair<String, String>> getImageAttributeFileNames(final Attributable attributable, final String lang);  }