示例#1
0
 /**
  * Tests {@link PackageResourceGuard}.
  *
  * @throws Exception
  */
 @Test
 public void packageResourceGuard() throws Exception {
   PackageResourceGuard guard = new PackageResourceGuard();
   assertTrue(guard.acceptExtension("txt"));
   assertFalse(guard.acceptExtension("java"));
   assertTrue(guard.accept("foo/Bar.txt"));
   assertFalse(guard.accept("foo/Bar.java"));
   assertTrue(guard.accept(Packages.absolutePath(PackageResourceTest.class, "Bar.txt")));
   assertTrue(guard.accept(Packages.absolutePath(PackageResourceTest.class, "Bar.txt.")));
   assertTrue(guard.accept(Packages.absolutePath(PackageResourceTest.class, ".Bar.txt")));
   assertTrue(guard.accept(Packages.absolutePath(PackageResourceTest.class, ".Bar.txt.")));
   assertTrue(guard.accept(Packages.absolutePath(PackageResourceTest.class, ".Bar")));
   assertTrue(guard.accept(Packages.absolutePath(PackageResourceTest.class, ".java")));
   assertFalse(guard.accept(Packages.absolutePath(PackageResourceTest.class, "Bar.java")));
 }
 private static String readResourceNoCache(Class scope, String file) {
   InputStream inputStream = null;
   try {
     final String path = Packages.absolutePath(scope, file);
     final IResourceStream resourceStream =
         Application.get().getResourceSettings().getResourceStreamLocator().locate(scope, path);
     inputStream = resourceStream.getInputStream();
     return Streams.readString(inputStream, "utf-8");
   } catch (ResourceStreamNotFoundException e) {
     throw new RuntimeException(
         String.format("Can't find resource \"%s.%s\"", scope.getName(), file), e);
   } catch (IOException e) {
     throw new RuntimeException(
         String.format("Can't read resource \"%s.%s\"", scope.getName(), file), e);
   } finally {
     Closeables.closeQuietly(inputStream);
   }
 }