/** * Given a {@code resourceName} that is relative to {@code contextClass}, returns a {@code URL} * pointing to the named resource. * * @throws IllegalArgumentException if the resource is not found */ public static URL getResource(Class<?> contextClass, String resourceName) { URL url = contextClass.getResource(resourceName); checkArgument( url != null, "resource %s relative to %s not found.", resourceName, contextClass.getName()); return url; } }
public static ImageIcon openIcon(String name) { String iconPath = "/icons-16/" + name + ".png"; URL resource = Utils.class.getResource(iconPath); if (resource == null) { throw new JadxRuntimeException("Icon not found: " + iconPath); } return new ImageIcon(resource); }
private static URL classfile(Class<?> c) { return c.getResource(c.getSimpleName() + ".class"); } }
public JAXBElement<DataHandler> standardClassDataHandler() { DataSource dataSource = new URLDataSource(getClass().getResource("spring-ws.png")); DataHandler dataHandler = new DataHandler(dataSource); return new JAXBElement<>(NAME, DataHandler.class, dataHandler); }
public void testToString() throws IOException { URL resource = getClass().getResource("testdata/i18n.txt"); assertEquals(I18N, Resources.toString(resource, Charsets.UTF_8)); assertThat(Resources.toString(resource, Charsets.US_ASCII)).isNotEqualTo(I18N); }
public void testCopyToOutputStream() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); URL resource = getClass().getResource("testdata/i18n.txt"); Resources.copy(resource, out); assertEquals(I18N, out.toString("UTF-8")); }
@Test(expected = FileNotFoundException.class) public void testInputStreamNotFoundOnFileSystemResource() throws IOException { new FileSystemResource(getClass().getResource("Resource.class").getFile()).createRelative("X").getInputStream(); }
/** Returns the file with the given name under the testdata directory. */ protected final File getTestFile(String name) throws IOException { File file = new File(getTestDir(), name); if (!file.exists()) { URL resourceUrl = IoTestCase.class.getResource("testdata/" + name); if (resourceUrl == null) { return null; } copy(resourceUrl, file); } return file; }
public void testReadLines() throws IOException { // TODO(chrisn): Check in a better resource URL resource = getClass().getResource("testdata/i18n.txt"); assertEquals(ImmutableList.of(I18N), Resources.readLines(resource, Charsets.UTF_8)); }
@Test public void testFileSystemResourceWithFilePath() throws Exception { Path filePath = Paths.get(getClass().getResource("Resource.class").toURI()); Resource resource = new FileSystemResource(filePath); doTestResource(resource); assertEquals(new FileSystemResource(filePath), resource); }
public CallbacksSecurityTests() { // setup security if (System.getSecurityManager() == null) { Policy policy = Policy.getPolicy(); URL policyURL = getClass() .getResource( "/org/springframework/beans/factory/support/security/policy.all"); System.setProperty("java.security.policy", policyURL.toString()); System.setProperty("policy.allowSystemProperty", "true"); policy.refresh(); System.setSecurityManager(new SecurityManager()); } }
@Test public void testFileSystemResource() throws IOException { String file = getClass().getResource("Resource.class").getFile(); Resource resource = new FileSystemResource(file); doTestResource(resource); assertEquals(new FileSystemResource(file), resource); }
@AndroidIncompatible // TODO(cpovirk): How significant is this failure? public void testGetFinalizerUrl() { assertNotNull(getClass().getResource("internal/Finalizer.class")); }
private void doExtensiveTest(String resourceName) throws IOException { Splitter splitter = Splitter.on(CharMatcher.whitespace()); URL url = getClass().getResource(resourceName); for (String line : Resources.readLines(url, UTF_8)) { Iterator<String> iterator = splitter.split(line).iterator(); String input = iterator.next(); String expectedOutput = iterator.next(); assertFalse(iterator.hasNext()); assertEquals(expectedOutput, simplifyPath(input)); } } }
@Test(expected = FileNotFoundException.class) public void testReadableChannelNotFoundOnFileSystemResource() throws IOException { new FileSystemResource(getClass().getResource("Resource.class").getFile()).createRelative("X").readableChannel(); }
@Test // SPR-12624 public void checkRelativeLocation() throws Exception { String locationUrl= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm(); Resource location = new UrlResource(locationUrl.replace("/springframework","/../org/springframework")); List<Resource> locations = singletonList(location); assertNotNull(this.resolver.resolveResource(null, "main.css", locations, null).block(TIMEOUT)); }
@Test public void checkRelativeLocation() throws Exception { String locationUrl= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm(); Resource location = new UrlResource(locationUrl.replace("/springframework","/../org/springframework")); assertNotNull(this.resolver.resolveResource(null, "main.css", Collections.singletonList(location), null)); }
@Test public void testFileSystemResourceWithImport() throws URISyntaxException { String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath(); DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file)); // comes from "resourceImport.xml" xbf.getBean("resource1", ResourceTestBean.class); // comes from "resource.xml" xbf.getBean("resource2", ResourceTestBean.class); }
@Test public void testUrlResourceWithImport() { URL url = getClass().getResource(RESOURCE_CONTEXT.getPath()); DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url)); // comes from "resourceImport.xml" xbf.getBean("resource1", ResourceTestBean.class); // comes from "resource.xml" xbf.getBean("resource2", ResourceTestBean.class); }
@Test public void testUrlResource() throws IOException { Resource resource = new UrlResource(getClass().getResource("Resource.class")); doTestResource(resource); assertEquals(new UrlResource(getClass().getResource("Resource.class")), resource); Resource resource2 = new UrlResource("file:core/io/Resource.class"); assertEquals(resource2, new UrlResource("file:core/../core/io/./Resource.class")); assertEquals("test.txt", new UrlResource("file:/dir/test.txt?argh").getFilename()); assertEquals("test.txt", new UrlResource("file:\\dir\\test.txt?argh").getFilename()); assertEquals("test.txt", new UrlResource("file:\\dir/test.txt?argh").getFilename()); }