use of org.apache.wicket.core.util.resource.UrlResourceStream in project wicket by apache.
the class CachingResourceStreamLocatorTest method urlResource.
/**
* Tests UrlResourceStreamReference
*
* @throws Exception
*/
@Test
public void urlResource() throws Exception {
IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class);
UrlResourceStream urs = new UrlResourceStream(new URL("file:///"));
when(resourceStreamLocator.locate(String.class, "path")).thenReturn(urs);
CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator(resourceStreamLocator);
cachingLocator.locate(String.class, "path");
cachingLocator.locate(String.class, "path");
// there is a url resource with that Key so expect just one call to the delegate
verify(resourceStreamLocator, times(1)).locate(String.class, "path");
}
use of org.apache.wicket.core.util.resource.UrlResourceStream in project wicket by apache.
the class CachingResourceStreamLocator method updateCache.
private void updateCache(CacheKey key, IResourceStream stream) {
if (null == stream) {
cache.put(key, NullResourceStreamReference.INSTANCE);
} else if (stream instanceof FileResourceStream) {
FileResourceStream fileResourceStream = (FileResourceStream) stream;
cache.put(key, new FileResourceStreamReference(fileResourceStream));
} else if (stream instanceof UrlResourceStream) {
UrlResourceStream urlResourceStream = (UrlResourceStream) stream;
cache.put(key, new UrlResourceStreamReference(urlResourceStream));
}
}
use of org.apache.wicket.core.util.resource.UrlResourceStream in project wicket by apache.
the class UrlResourceStreamReference method getReference.
@Override
public UrlResourceStream getReference() {
UrlResourceStream resourceStream = new UrlResourceStream(url);
restoreResourceStream(resourceStream);
return resourceStream;
}
use of org.apache.wicket.core.util.resource.UrlResourceStream in project wicket by apache.
the class ResourceStreamLocatorTest method getPath.
/**
* Gets the path of the resource as a string.
*
* @param resource
* the resource
* @return the path of the resource as a string
*/
public static String getPath(IResourceStream resource) {
if (resource instanceof UrlResourceStream) {
try {
URL url = ((UrlResourceStream) resource).getURL();
CharSequence path = new File(new URI(url.toString())).getPath();
path = Strings.replaceAll(path, "\\", "/");
return path.toString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
} else if (resource instanceof FileResourceStream) {
try {
return ((FileResourceStream) resource).getFile().getCanonicalPath();
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
use of org.apache.wicket.core.util.resource.UrlResourceStream in project wicket by apache.
the class UrlResourceStreamTest method loadJustOnce.
/**
* Verifies that a connection is opened just once but each #getInputStream() opens a new one
* and all input streams are closed with UrlResourceStream#close()
*
* https://issues.apache.org/jira/browse/WICKET-3176
* https://issues.apache.org/jira/browse/WICKET-4293
*
* @throws IOException
* @throws ResourceStreamNotFoundException
*/
@Test
public void loadJustOnce() throws IOException, ResourceStreamNotFoundException {
String anyClassInJarFile = "/java/lang/String.class";
URL realURL = getClass().getResource(anyClassInJarFile);
final AtomicInteger connectCounter = new AtomicInteger(0);
final AtomicInteger streamCounter = new AtomicInteger(0);
URL url = new URL(null, "test://anything", new CountingURLStreamHandler(realURL, connectCounter, streamCounter));
UrlResourceStream countingStream = new UrlResourceStream(url);
// assert the call is not made yet
assertEquals(0, connectCounter.get());
assertEquals(0, streamCounter.get());
// assert the connection is loaded lazily
countingStream.length();
assertEquals(1, connectCounter.get());
assertEquals(0, streamCounter.get());
// assert the following calls do not make new connections
countingStream.getInputStream();
assertEquals(1, connectCounter.get());
assertEquals(1, streamCounter.get());
countingStream.getContentType();
assertEquals(1, connectCounter.get());
assertEquals(1, streamCounter.get());
countingStream.getInputStream();
assertEquals(1, connectCounter.get());
assertEquals(2, streamCounter.get());
countingStream.close();
assertEquals(1, connectCounter.get());
assertEquals(2, streamCounter.get());
// assert the connection is re-opened (again lazily) second time,
// but stream is not re-opened yet
countingStream.length();
assertEquals(2, connectCounter.get());
assertEquals(2, streamCounter.get());
// assert stream is re-opened on next getInputStream call
countingStream.getInputStream();
assertEquals(2, connectCounter.get());
assertEquals(3, streamCounter.get());
}
Aggregations