Search in sources :

Example 1 with UrlResourceStream

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");
}
Also used : UrlResourceStream(org.apache.wicket.core.util.resource.UrlResourceStream) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) URL(java.net.URL) Test(org.junit.Test)

Example 2 with UrlResourceStream

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));
    }
}
Also used : UrlResourceStream(org.apache.wicket.core.util.resource.UrlResourceStream) FileResourceStream(org.apache.wicket.util.resource.FileResourceStream)

Example 3 with 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;
}
Also used : UrlResourceStream(org.apache.wicket.core.util.resource.UrlResourceStream)

Example 4 with UrlResourceStream

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;
    }
}
Also used : UrlResourceStream(org.apache.wicket.core.util.resource.UrlResourceStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) URL(java.net.URL)

Example 5 with UrlResourceStream

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());
}
Also used : UrlResourceStream(org.apache.wicket.core.util.resource.UrlResourceStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) URL(java.net.URL) Test(org.junit.Test)

Aggregations

UrlResourceStream (org.apache.wicket.core.util.resource.UrlResourceStream)6 URL (java.net.URL)4 Test (org.junit.Test)3 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)1 CachingResourceStreamLocator (org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator)1 Bytes (org.apache.wicket.util.lang.Bytes)1 FileResourceStream (org.apache.wicket.util.resource.FileResourceStream)1