private File getGenerationTargetDirectory() { URL url = getResourceAsUrl(getEffectiveConfigFile(), getClass(), true, true); File targetDirectory = new File(FileUtils.toFile(url).getParentFile(), "META-INF"); if (!targetDirectory.exists() && !targetDirectory.mkdir()) { throw new RuntimeException("Could not create target directory " + targetDirectory.getAbsolutePath()); } return targetDirectory; }
private void setupUserManagerFactory(FtpServerFactory serverFactory) throws IOException, URISyntaxException { PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); URL usersFile = IOUtils.getResourceAsUrl("users.properties", getClass()); if (usersFile == null) { throw new IOException("users.properties file not found in the classpath"); } userManagerFactory.setFile(new File(usersFile.toURI())); serverFactory.setUserManager(userManagerFactory.createUserManager()); }
private void updateExpectedJson(String json) throws URISyntaxException, IOException { File root = new File(getResourceAsUrl("models/" + expectedSource, getClass()).toURI()).getParentFile() .getParentFile().getParentFile().getParentFile(); File testDir = new File(root, "src/test/resources/models"); File target = new File(testDir, expectedSource); stringToFile(target.getAbsolutePath(), json); System.out.println(expectedSource + " fixed"); }
private void updateExpectedJson(String json) throws URISyntaxException, IOException { File root = new File(getResourceAsUrl(expectedSource, getClass()).toURI()).getParentFile() .getParentFile().getParentFile().getParentFile().getParentFile(); File testDir = new File(root, "src/test/resources/"); File target = new File(testDir, expectedSource); stringToFile(target.getAbsolutePath(), json); System.out.println(target.getAbsolutePath() + " was fixed"); }
@Test public void testUnzipFileToSameFolderTwice() throws Exception { URL resourceAsUrl = IOUtils.getResourceAsUrl("testFolder.zip", getClass()); File zipFile = new File(resourceAsUrl.toURI()); File outputDir = FileUtils.newFile(TEST_DIRECTORY); for (int i = 0; i < 2; i++) { unzip(zipFile, outputDir); File testFile = new File(UNZIPPED_FILE_PATH); assertTrue(testFile.exists()); } }
@Test public void generate() throws Exception { String schema = generator.generate(extensionUnderTest, new SchemaTestDslContext()); try { compareXML(expectedSchema, schema); } catch (Throwable t) { if (shouldUpdateExpectedFilesOnError()) { File root = new File(getResourceAsUrl("schemas/" + expectedXSD, getClass()).toURI()).getParentFile() .getParentFile().getParentFile().getParentFile(); File testDir = new File(root, "src/test/resources/schemas"); File target = new File(testDir, expectedXSD); stringToFile(target.getAbsolutePath(), schema); System.out.println(expectedXSD + " fixed"); } throw t; } }
/** * Attempts to load a resource from the file system or from the classpath, in that order. * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object * @return an URL to the resource or null if resource not found */ public static URL getResourceAsUrl(final String resourceName, final Class callingClass) { return getResourceAsUrl(resourceName, callingClass, true, true); }
public ConfigResource(String resourceName) throws IOException { this.resourceName = resourceName; url = IOUtils.getResourceAsUrl(resourceName, getClass(), true, true); if (url == null) { throw new FileNotFoundException(resourceName); } }
/** * Attempts to load a resource from the file system, from a URL, or from the classpath, in that order. * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object * @param tryAsFile - try to load the resource from the local file system * @param tryAsUrl - try to load the resource as a URL * @return an InputStream to the resource or null if resource not found * @throws java.io.IOException IO error */ public static InputStream getResourceAsStream(final String resourceName, final Class callingClass, boolean tryAsFile, boolean tryAsUrl) throws IOException { URL url = getResourceAsUrl(resourceName, callingClass, tryAsFile, tryAsUrl); if (url == null) { return null; } else { return url.openStream(); } }
public static String getResourcePath(String resourceName, Class callingClass, String encoding) throws IOException { if (resourceName == null) { // no name return null; } URL url = IOUtils.getResourceAsUrl(resourceName, callingClass); if (url == null) { // not found return null; } return normalizeFilePath(url, encoding); }
private void updateExpectedJson(String json) throws URISyntaxException, IOException { File root = new File(getResourceAsUrl("models/" + expectedSource, getClass()).toURI()).getParentFile() .getParentFile().getParentFile().getParentFile(); File testDir = new File(root, "src/test/resources/models"); File target = new File(testDir, expectedSource); stringToFile(target.getAbsolutePath(), json); System.out.println(expectedSource + " fixed"); }
@Test public void testUnzipFileToSameFolderTwice() throws Exception { URL resourceAsUrl = IOUtils.getResourceAsUrl("testFolder.zip", getClass()); File zipFile = new File(resourceAsUrl.toURI()); File outputDir = FileUtils.newFile(TEST_DIRECTORY); for (int i = 0; i < 2; i++) { unzip(zipFile, outputDir); File testFile = new File(UNZIPPED_FILE_PATH); assertTrue(testFile.exists()); } }
@Test public void generate() throws Exception { String schema = generator.generate(extensionUnderTest, new SchemaTestDslContext()); try { compareXML(expectedSchema, schema); } catch (Throwable t) { if (shouldUpdateExpectedFilesOnError()) { File root = new File(getResourceAsUrl("schemas/" + expectedXSD, getClass()).toURI()).getParentFile() .getParentFile().getParentFile().getParentFile(); File testDir = new File(root, "src/test/resources/schemas"); File target = new File(testDir, expectedXSD); stringToFile(target.getAbsolutePath(), schema); System.out.println(expectedXSD + " fixed"); } throw t; } }