@Override public byte[] readAllBytes(final Path path) throws IOException, OutOfMemoryError, SecurityException { return Files.readAllBytes(path); }
@Override public String readAllString(final Path path, final Charset cs) throws IllegalArgumentException, NoSuchFileException, IOException { final byte[] result = Files.readAllBytes(path); if (result == null || result.length == 0) { return ""; } return new String(result, cs); }
private Model getPOMModel(org.uberfire.java.nio.file.Path filePath) throws IOException, XmlPullParserException { return reader.read(new ByteArrayInputStream(Files.readAllBytes(filePath))); }
public Model getModel(Path pom) throws IOException, XmlPullParserException { return reader.read(new ByteArrayInputStream(Files.readAllBytes(pom))); }
private Collection<String> directoryContent(final String directory, final String glob) { String realPath = PluginUtils.getRealPath(directory); if (realPath == null) { LOGGER.info("Not listing directory content for " + directory + "/" + glob + " because getRealPath() is returning null. (This app is probably deployed in an unexploded .war)"); return Collections.emptyList(); } final Collection<String> result = new ArrayList<>(); final Path pluginsRootPath = Paths.get(URI.create("file://" + realPath)); if (Files.isDirectory(pluginsRootPath)) { final DirectoryStream<Path> stream = Files.newDirectoryStream(pluginsRootPath, glob); for (final Path activeJS : stream) { result.add(new String(Files.readAllBytes(activeJS))); } } return result; }
public static String readLockedBy(final Path path) { org.uberfire.java.nio.file.Path lock = convert(PathFactory.newLock(path)); if (!Files.exists(lock)) { return null; } else { return new String(Files.readAllBytes(lock)); } }
@Test public void readAllBytesNull() { assertThatThrownBy(() -> Files.readAllBytes(null)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'path' should be not null!"); }
@Override public String getTemplateContent(String url) { String pluginTemplateContent = getRuntimePluginTemplateContent(url); if (isAJarPluginTemplate(pluginTemplateContent)) { return pluginTemplateContent; } String realPath = PluginUtils.getRealPath("plugins"); if (realPath == null) { LOGGER.info("Not fetching template content for " + url + " because getRealPath() is" + " returning null. (This app is probably deployed in an unexploded .war)"); return ""; } final Path template; if (url.startsWith("/")) { template = Paths.get(URI.create("file://" + realPath + url)); } else { template = Paths.get(URI.create("file://" + realPath + "/" + url)); } if (Files.isRegularFile(template)) { return new String(Files.readAllBytes(template)); } return ""; }
public PomPlaceHolder readSingle(Path pom) { PomPlaceHolder holder = new PomPlaceHolder(); try { Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(pom))); holder = new PomPlaceHolder(pom.toAbsolutePath().toString(), model.getArtifactId(), model.getGroupId(), model.getVersion(), model.getPackaging()); } catch (Exception e) { logger.error(e.getMessage()); } return holder; }
@Test public void readAllBytesDir() { assertThatThrownBy(() -> Files.readAllBytes(newTempDir())) .isInstanceOf(NoSuchFileException.class); }
@Test public void readAllBytesFileNotExists() { assertThatThrownBy(() -> Files.readAllBytes(newTempDir().resolve("file.big"))) .isInstanceOf(NoSuchFileException.class); }
public static List<Artifact> resolveDependenciesFromMultimodulePrj(List<String> pomsPaths) { MavenXpp3Reader reader = new MavenXpp3Reader(); Set<Artifact> deps = new HashSet<>(); try { for (String pomx : pomsPaths) { Path pom = Paths.get(URI.create("default:///" + pomx)); Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(pom))); if (model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null) { createArtifacts(model.getDependencyManagement().getDependencies(), deps); } if (model.getDependencies() != null) { createArtifacts(model.getDependencies(), deps); } } } catch (Exception ex) { logger.error(ex.getMessage()); return Collections.emptyList(); } return new ArrayList<>(deps); }
@Test public void readAllBytes() throws IOException { final Path dir = newTempDir(); try (final BufferedWriter writer = Files.newBufferedWriter(dir.resolve("myfile.txt"), Charset.defaultCharset())) { assertThat(writer).isNotNull(); writer.write("content"); } final byte[] result = Files.readAllBytes(dir.resolve("myfile.txt")); assertThat(result) .hasSize("content".getBytes().length) .isEqualTo("content".getBytes()); }
@Test(expected = OutOfMemoryError.class) @Ignore public void readAllBytesOutOfMemory() throws IOException { final Path file = newTempDir().resolve("file.big"); final RandomAccessFile f = new RandomAccessFile(file.toFile(), "rw"); f.setLength(Integer.MAX_VALUE + 1L); f.close(); Files.readAllBytes(file); }
@Test public void write() { final Path dir = newTempDir(); Path file = dir.resolve("file.txt"); Files.write(file, "content".getBytes()); assertThat(Files.readAllBytes(file)) .hasSize("content".getBytes().length) .isEqualTo("content".getBytes()); }
@Test public void onNEwDynamicDependencyEventTest() throws Exception { MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + File.separator + POM)))); assertThat(model.getDependencies()).hasSize(0); editor = new PomStructureEditor(); AddPomDependencyEvent event = new AddPomDependencyEvent(DependencyType.JPA, PathFactory.newPath(tmp.getFileName().toString(), tmp.toUri().toString() + File.separator + POM)); editor.onNewDynamicDependency(event); model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + File.separator + POM)))); assertThat(model.getDependencies()).hasSize(1); Dependency dep = model.getDependencies().get(0); assertThat(dep.getGroupId()).containsOnlyOnce("org.hibernate.javax.persistence"); assertThat(dep.getArtifactId()).containsOnlyOnce("hibernate-jpa-2.1-api"); assertThat(dep.getVersion()).containsOnlyOnce("1.0.2.Final"); } }
@Test public void onNEwDynamicDependencyEventTest() throws Exception { MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + File.separator + POM)))); assertThat(model.getDependencies()).hasSize(0); editor = new PomStructureEditor(); AddPomDependencyEvent event = new AddPomDependencyEvent(DependencyType.JPA, PathFactory.newPath(tmp.getFileName().toString(), tmp.toUri().toString() + File.separator + POM)); editor.onNewDynamicDependency(event); model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + File.separator + POM)))); assertThat(model.getDependencies()).hasSize(1); Dependency dep = model.getDependencies().get(0); assertThat(dep.getGroupId()).containsOnlyOnce("javax.persistence"); assertThat(dep.getArtifactId()).containsOnlyOnce("javax.persistence-api"); assertThat(dep.getVersion()).containsOnlyOnce("2.2"); } }
@Test public void addAndOverrideVersionDepTest() throws Exception { //During the scan of the pom if a dep is founded present will be override the version with the version in the json file tmp = TestUtil.createAndCopyToDirectory(tmpRoot, "dummyOverride", "target/test-classes/dummy"); DynamicPomDependency dep = new DynamicPomDependency("org.hibernate.javax.persistence", "hibernate-jpa-2.1-api", "1.0.3.Final", ""); List<DynamicPomDependency> deps = Arrays.asList(dep); boolean result = editor.addDependencies(deps, PathFactory.newPath(tmp.toAbsolutePath().toString() + File.separator + POM, tmp.toUri().toString() + File.separator + POM)); assertThat(result).isTrue(); MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + File.separator + POM)))); Dependency changedDep = getDependency(model.getDependencies(), "org.hibernate.javax.persistence", "hibernate-jpa-2.1-api"); assertThat(changedDep.getVersion()).isEqualTo("1.0.3.Final"); }
@Test public void addAndOverrideVersionDepTest() throws Exception { //During the scan of the pom if a dep is founded present will be override the version with the version in the json file tmp = TestUtil.createAndCopyToDirectory(tmpRoot, "dummyOverride", "target/test-classes/dummy"); DynamicPomDependency dep = new DynamicPomDependency("javax.persistence", "javax.persistence-api", "1.0.3.Final", ""); List<DynamicPomDependency> deps = Arrays.asList(dep); boolean result = editor.addDependencies(deps, PathFactory.newPath(tmp.toAbsolutePath().toString() + File.separator + POM, tmp.toUri().toString() + File.separator + POM)); assertThat(result).isTrue(); MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + File.separator + POM)))); Dependency changedDep = getDependency(model.getDependencies(), "javax.persistence", "javax.persistence-api"); assertThat(changedDep.getVersion()).isEqualTo("1.0.3.Final"); }
Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + File.separator + POM)))); Dependency changedDep = getDependency(model.getDependencies(), "javax.persistence",