Deprecated as of 4.3.13, in favor of selective use of ClassPathResource.ClassPathResource(String, ClassLoader) vs ClassPathResource.ClassPathResource(String, Class)
private String findConfig(String[] locations) { for (String location : locations) { ClassPathResource resource = new ClassPathResource(location, this.classLoader); if (resource.exists()) { return "classpath:" + location; } } return null; }
/** * Detect a default properties file for the supplied class, as specified * in the class-level Javadoc for {@link TestPropertySource}. */ private static String detectDefaultPropertiesFile(Class<?> testClass) { String resourcePath = ClassUtils.convertClassNameToResourcePath(testClass.getName()) + ".properties"; String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath; ClassPathResource classPathResource = new ClassPathResource(resourcePath); if (classPathResource.exists()) { if (logger.isInfoEnabled()) { logger.info(String.format("Detected default properties file \"%s\" for test class [%s]", prefixedResourcePath, testClass.getName())); } return prefixedResourcePath; } else { String msg = String.format("Could not detect default properties file for test [%s]: " + "%s does not exist. Either declare the 'locations' or 'properties' attributes " + "of @TestPropertySource or make the default properties file available.", testClass.getName(), classPathResource); logger.error(msg); throw new IllegalStateException(msg); } }
ClassPathResource classPathResource = new ClassPathResource(resourcePath); if (classPathResource.exists()) { if (logger.isInfoEnabled()) { logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]",
@Override public boolean checkForResourceOnClassPath(String name) { ClassPathResource resource = lookupResourceOnClassPath(name); return (resource != null && resource.exists()); }
String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath; ClassPathResource classPathResource = new ClassPathResource(resourcePath); if (classPathResource.exists()) { if (logger.isInfoEnabled()) { logger.info(String.format("Detected default resource location \"%s\" for test class [%s]",
protected ClassPathResource lookupResourceOnClassPath(String name) { if (fileServiceClasspathDirectory != null && !"".equals(fileServiceClasspathDirectory)) { try { String resourceName = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(fileServiceClasspathDirectory + '/' + name)); ClassPathResource resource = new ClassPathResource(resourceName); if (resource.exists()) { return resource; } } catch (Exception e) { LOG.error("Error getting resource from classpath", e); } } return null; }
private String fetchMacro(String name) throws Exception { ClassPathResource resource = new ClassPathResource("test.ftl", getClass()); assertTrue(resource.exists()); String all = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); all = all.replace("\r\n", "\n"); String[] macros = StringUtils.delimitedListToStringArray(all, "\n\n"); for (String macro : macros) { if (macro.startsWith(name)) { return macro.substring(macro.indexOf("\n")).trim(); } } return null; }
@Before public void init() { String prefix = server.getServletPrefix(); http.setPort(port); http.setPrefix(prefix); if (new ClassPathResource("sample.jks").exists()) { http.setProtocol("https"); } }
@Override public InputStream getClasspathResource(String name) { try { ClassPathResource resource = lookupResourceOnClassPath(name); if (resource != null && resource.exists()) { InputStream assetFile = resource.getInputStream(); BufferedInputStream bufferedStream = new BufferedInputStream(assetFile); // Wrapping the buffered input stream with a globally shared stream allows us to // vary the way the file names are generated on the file system. // This benefits us (mainly in our demo site but their could be other uses) when we // have assets that are shared across sites that we also need to resize. GloballySharedInputStream globallySharedStream = new GloballySharedInputStream(bufferedStream); globallySharedStream.mark(0); return globallySharedStream; } else { return null; } } catch (Exception e) { LOG.error("Error getting resource from classpath", e); } return null; }
private ClassPathResource resolveScript() { ClassPathResource script = new ClassPathResource("scripts/db/" + dbType + ".create.ddl.sql"); if (!script.exists()) { throw new IllegalArgumentException("No ddl script found: " + script); } return script; } }
private ClassPathResource resolveScript() { ClassPathResource script = new ClassPathResource("scripts/db/" + dbType + ".create.ddl.sql"); if (!script.exists()) { throw new IllegalArgumentException("No ddl script found: " + script); } return script; } }
public void addClassPathResource(String location) { ClassPathResource classPathResource = new ClassPathResource(location); if (classPathResource.exists()) { resources.add(classPathResource); } }
public void addClassPathResource(String location) { ClassPathResource classPathResource = new ClassPathResource(location); if (classPathResource.exists()) { resources.add(classPathResource); } }
public void loadPrivateKey() { ClassPathResource res = new ClassPathResource(this.priKeyPath); if(res.exists()) { try { this.privateKey = IOUtils.toString(res.getInputStream(), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } } }
protected Properties readPropertyResource(String resourcePathName) { Properties props = new Properties(); try { ClassPathResource resource = new ClassPathResource(resourcePathName); if (resource.exists()) { props.load(resource.getInputStream()); log.info("Loaded "+props.size()+" config properties from resource: " + resource); } } catch (Exception e) { log.warn("Failed to load config properties from resource ("+resourcePathName+"): " + e.getMessage(), e); } return props; }
public Properties getPropertiesFromClassPath(String... files) throws IOException { Properties properties = new Properties(); for (String file : files) { Properties fileProperties = new Properties(); ClassPathResource classPathResource = new ClassPathResource(file); if (!classPathResource.exists()) { continue; } fileProperties.load(classPathResource.getInputStream()); for (String property : fileProperties.stringPropertyNames()) { properties.put(property, fileProperties.getProperty(property)); } } return properties; }
public static Resource resolve(String path) throws IOException { if (path != null) { ClassPathResource r1 = new ClassPathResource(path); if (r1.exists()) { return r1; } UrlResource r2 = new UrlResource(path); if (r2.exists()) { return r2; } throw new IOException(String.format("resource %s does not exists", path)); } return null; }
private String readJSONFromFile(String filename) throws IOException { ClassPathResource classPathResource = new ClassPathResource(filename); if (!classPathResource.exists()) { Assert.fail("No resource found " + filename); } return FileCopyUtils.copyToString(new InputStreamReader(classPathResource.getInputStream())); } }
protected byte[] getFileBytes(String path) throws IOException { ClassPathResource resource = new ClassPathResource(path); assertTrue(resource.exists(), path + " must exist."); return StreamUtils.copyToByteArray(resource.getInputStream()); }