StandaloneServletContextUriLocator extends ServletContextUriLocator implements StandaloneContextAware { @Override public InputStream locate(final String uri) throws IOException { validState(standaloneContext != null, "Locator was not initialized properly. StandaloneContext missing."); Exception lastException = null; final String[] contextFolders = standaloneContext.getContextFolders(); for(final String contextFolder : contextFolders) { try { return locateStreamWithContextFolder(uri, contextFolder); } catch(final IOException e) { lastException = e; LOG.debug("Could not locate: {} using contextFolder: {}", uri, contextFolder); } } final String exceptionMessage = String.format("No valid resource '%s' found inside any of contextFolders: %s", uri, Arrays.toString(standaloneContext.getContextFolders())); throw new IOException(exceptionMessage, lastException); } StandaloneServletContextUriLocator(); @Override InputStream locate(final String uri); void initialize(final StandaloneContext standaloneContext);  }
@Test(expected = IllegalStateException.class) public void shouldFailWhenStandaloneContextIsNotInitialized() throws Exception { victim = new StandaloneServletContextUriLocator(); victim.locate(""); }
@Test(expected = IOException.class) public void cannotLocateInvalidResource() throws Exception { victim.locate("invalid"); }
@Test public void shouldLocateValidResource() throws Exception { final String validResource = TestStandaloneServletContextUriLocator.class.getSimpleName() + ".class"; assertNotNull(victim.locate(validResource)); }
@Test public void shouldLocateValidResourceWhenMultipleContextFoldersProvided() throws Exception { final String defaultContextFolder = standaloneContext.getContextFoldersAsCSV(); standaloneContext.setContextFoldersAsCSV("invalid," + defaultContextFolder); final String validResource = TestStandaloneServletContextUriLocator.class.getSimpleName() + ".class"; assertNotNull(victim.locate(validResource)); }
@Test(expected = IOException.class) public void cannotLocateInvalidResourceWhenMultipleContextFoldersProvided() throws Exception { final String defaultContextFolder = standaloneContext.getContextFoldersAsCSV(); standaloneContext.setContextFoldersAsCSV("invalid," + defaultContextFolder); assertNotNull(victim.locate("invalid")); }