AbstractAggregatorImpl extends HttpServlet implements IAggregator { protected void addAlias(String alias, URI res, String initParamName, Map<String, URI> map) { final String sourceMethod = "addAlias"; boolean isTraceLogging = log.isLoggable(Level.FINER); if (isTraceLogging) { log.entering(AbstractAggregatorImpl.class.getName(), sourceMethod, new Object[]{alias, res, initParamName, map}); } String[] parts; if (alias == null || (parts = alias.split("/")).length == 0) { throw new IllegalArgumentException(initParamName + " = " + alias); } List<String> nonEmptyParts = new ArrayList<String>(parts.length); for (String part : parts) { if (part != null && part.length() > 0) { nonEmptyParts.add(part); } } alias = "/" + StringUtils.join(nonEmptyParts, "/"); for (String test : map.keySet()) { if (alias.equals(test)) { throw new IllegalArgumentException("Duplicate alias path: " + alias); } else if (alias.startsWith(test + "/") || test.startsWith(alias + "/")) { throw new IllegalArgumentException("Overlapping alias paths: " + alias + ", " + test); } } map.put(alias, res); log.exiting(AbstractAggregatorImpl.class.getName(), sourceMethod); }  @Override void init(ServletConfig servletConfig); @Override void destroy(); @Override boolean reloadConfig(); @Override IDependencies getDependencies(); @Override String getName(); @Override IConfig getConfig(); @Override ICacheManager getCacheManager(); @Override InitParams getInitParams(); @Override HttpServlet asServlet(); @Override IHttpTransport getTransport(); @Override IResourceFactory getResourceFactory(Mutable<URI> uriRef); @Override IResource newResource(URI uri); @Override IResource runConverters(IResource resource); @Override IModuleBuilder getModuleBuilder(String mid, IResource res); @Override Iterable<IAggregatorExtension> getExtensions(String extensionPointId); @Override IModule newModule(String mid, URI uri); @Override ILayerCache newLayerCache(); @Override IModuleCache newModuleCache(); @Override IGzipCache newGzipCache(); @Override String substituteProps(String str); @Override String substituteProps(String str, SubstitutionTransformer transformer); String getPropValue(String propName); @Override abstract IOptions getOptions(); @Override abstract IExecutors getExecutors(); @Override IPlatformServices getPlatformServices(); void initialize(IConfig config); @Override String setForceError(String forceError); @Override Future<?> buildAsync(final Callable<?> builder, final HttpServletRequest req); @Override HttpServletRequest getCurrentRequest();  }
@Test public void testAddAlias_overlappingPathsValidation() throws Exception { TestAggregatorImpl aggregator = EasyMock.createMockBuilder(TestAggregatorImpl.class).createMock(); Map<String, URI> map = new HashMap<String, URI>(); URI res = new URI("/test/resource"); aggregator.addAlias("/test", res, "", map); try { aggregator.addAlias("/test/sub", res, "", map); Assert.fail("Expected exception"); } catch (IllegalArgumentException e) {} map.clear(); aggregator.addAlias("/test/sub1", res, "", map); aggregator.addAlias("/test/sub2", res, "", map); try { aggregator.addAlias("/test", res, "", map); Assert.fail("Expected exception"); } catch (IllegalArgumentException e) {} try { aggregator.addAlias("/test/sub1/foo", res, "", map); Assert.fail("Expected exception"); } catch (IllegalArgumentException e) {} }
@Test public void testAddAlias_pathDelimFixup() throws Exception { TestAggregatorImpl aggregator = EasyMock.createMockBuilder(TestAggregatorImpl.class).createMock(); Map<String, URI> map = new HashMap<String, URI>(); URI res = new URI("/test/resource"); aggregator.addAlias("/test", res, "", map); Assert.assertTrue(map.containsKey("/test")); map.clear(); aggregator.addAlias("test", res, "", map); Assert.assertTrue(map.containsKey("/test")); map.clear(); aggregator.addAlias("test/", res, "", map); Assert.assertTrue(map.containsKey("/test")); map.clear(); aggregator.addAlias("/test/", res, "", map); Assert.assertTrue(map.containsKey("/test")); map.clear(); aggregator.addAlias("test/foo Assert.assertTrue(map.containsKey("/test/foo/bar")); }