Issue #1498 RTResource

Added a test that indicates the existing URLResource is adequate for JRT
This commit is contained in:
Greg Wilkins 2017-08-24 13:01:03 +10:00
parent e462fb2648
commit 5a9e2ea25a
2 changed files with 127 additions and 8 deletions

View File

@ -32,7 +32,7 @@ import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/* ------------------------------------------------------------ */
/** Abstract resource class.
/** URL resource class.
*/
public class URLResource extends Resource
{
@ -179,10 +179,6 @@ public class URLResource extends Resource
return new File(perm.getName());
}
// Try the URL file arg
try {return new File(_url.getFile());}
catch(Exception e) {LOG.ignore(e);}
// Don't know the file
return null;
}
@ -196,7 +192,6 @@ public class URLResource extends Resource
{
return _url.toExternalForm();
}
/* ------------------------------------------------------------ */
/**
@ -209,8 +204,6 @@ public class URLResource extends Resource
{
return getInputStream (true); //backwards compatibility
}
/* ------------------------------------------------------------ */
/**

View File

@ -0,0 +1,126 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.resource;
import java.net.URI;
import java.util.Arrays;
import org.eclipse.jetty.toolchain.test.JDK;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.TypeUtil;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
public class JrtResourceTest
{
private String testResURI = MavenTestingUtils.getTestResourcesDir().getAbsoluteFile().toURI().toASCIIString();
@Test
public void testResourceFromUriForString()
throws Exception
{
Assume.assumeTrue(JDK.IS_9);
URI string_loc = TypeUtil.getLocationOfClass(String.class);
Resource resource = Resource.newResource(string_loc);
assertThat(resource.exists(), is(true));
assertThat(resource.isDirectory(), is(false));
assertThat(IO.readBytes(resource.getInputStream()).length,Matchers.greaterThan(0));
assertThat(IO.readBytes(resource.getInputStream()).length,is((int)resource.length()));
assertThat(resource.getWeakETag("-xxx"),startsWith("W/\""));
assertThat(resource.getWeakETag("-xxx"),endsWith("-xxx\""));
}
@Test
public void testResourceFromStringForString()
throws Exception
{
Assume.assumeTrue(JDK.IS_9);
URI string_loc = TypeUtil.getLocationOfClass(String.class);
Resource resource = Resource.newResource(string_loc.toASCIIString());
assertThat(resource.exists(), is(true));
assertThat(resource.isDirectory(), is(false));
assertThat(IO.readBytes(resource.getInputStream()).length,Matchers.greaterThan(0));
assertThat(IO.readBytes(resource.getInputStream()).length,is((int)resource.length()));
assertThat(resource.getWeakETag("-xxx"),startsWith("W/\""));
assertThat(resource.getWeakETag("-xxx"),endsWith("-xxx\""));
}
@Test
public void testResourceFromURLForString()
throws Exception
{
Assume.assumeTrue(JDK.IS_9);
URI string_loc = TypeUtil.getLocationOfClass(String.class);
Resource resource = Resource.newResource(string_loc.toURL());
assertThat(resource.exists(), is(true));
assertThat(resource.isDirectory(), is(false));
assertThat(IO.readBytes(resource.getInputStream()).length,Matchers.greaterThan(0));
assertThat(IO.readBytes(resource.getInputStream()).length,is((int)resource.length()));
assertThat(resource.getWeakETag("-xxx"),startsWith("W/\""));
assertThat(resource.getWeakETag("-xxx"),endsWith("-xxx\""));
}
@Test
public void testResourceModule()
throws Exception
{
Assume.assumeTrue(JDK.IS_9);
Resource resource = Resource.newResource("jrt:/java.base");
assertThat(resource.exists(), is(false));
assertThat(resource.isDirectory(), is(false));
assertThat(resource.length(),is(-1L));
}
@Test
public void testResourceAllModules()
throws Exception
{
Assume.assumeTrue(JDK.IS_9);
Resource resource = Resource.newResource("jrt:/");
assertThat(resource.exists(), is(false));
assertThat(resource.isDirectory(), is(false));
assertThat(resource.length(),is(-1L));
}
}