下面列出了怎么用java.nio.file.CopyOption的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testCopyReplaceNonEmptyDirAllowedDifferentFileSystems() throws IOException {
addDirectory("/foo");
addFile("/foo/bar");
addFile("/baz");
CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
FileSystemException exception = assertThrows(FileSystemException.class,
() -> fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo"), options));
assertEquals("/foo", exception.getFile());
verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertTrue(Files.isRegularFile(getPath("/baz")));
}
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
throw new AtomicMoveNotSupportedException(source.toString(), target.toString(),
"ATOMIC_MOVE not supported yet");
}
if (Files.isDirectory(source)) {
MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
MCRDirectory srcRootDirectory = MCRFileSystemUtils.getFileCollection(src.getOwner());
if (srcRootDirectory.hasChildren()) {
throw new IOException("Directory is not empty");
}
}
copy(source, target, options);
delete(source);
}
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
throw new AtomicMoveNotSupportedException(source.toString(), target.toString(),
"ATOMIC_MOVE not supported yet");
}
if (Files.isDirectory(source)) {
MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
MCRDirectory srcRootDirectory = getRootDirectory(src);
if (srcRootDirectory.hasChildren()) {
throw new IOException("Directory is not empty");
}
}
copy(source, target, options);
delete(source);
}
@Test
public void testMoveNonEmptyDirDifferentFileSystem() throws IOException {
addDirectory("/foo");
addDirectory("/baz");
addFile("/baz/qux");
CopyOption[] options = {};
FileSystemException exception = assertThrows(FileSystemException.class,
() -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options));
assertEquals("/baz", exception.getFile());
verify(getExceptionFactory()).createDeleteException(eq("/baz"), any(SftpException.class), eq(true));
assertTrue(Files.isDirectory(getPath("/baz")));
assertTrue(Files.isRegularFile(getPath("/baz/qux")));
assertEquals(1, getChildCount("/baz"));
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isDirectory(getPath("/foo/bar")));
assertFalse(Files.isRegularFile(getPath("/foo/bar/qux")));
assertEquals(1, getChildCount("/foo"));
assertEquals(0, getChildCount("/foo/bar"));
}
@Test
public void testMoveReplaceFileDifferentFileSystem() throws IOException {
addDirectory("/foo");
addDirectory("/foo/bar");
addFile("/baz");
CopyOption[] options = {};
FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class,
() -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options));
assertEquals("/foo/bar", exception.getFile());
verify(getExceptionFactory(), never()).createMoveException(anyString(), anyString(), any(SftpException.class));
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isDirectory(getPath("/foo/bar")));
assertTrue(Files.isRegularFile(getPath("/baz")));
}
@Test
public void testCopySFTPFailure() throws IOException {
addDirectory("/foo");
addFile("/foo/bar");
// failure: target parent does not exist
CopyOption[] options = {};
NoSuchFileException exception = assertThrows(NoSuchFileException.class,
() -> fileSystem.copy(createPath("/foo/bar"), createPath("/baz/bar"), options));
assertEquals("/baz/bar", exception.getFile());
verify(getExceptionFactory()).createNewOutputStreamException(eq("/baz/bar"), any(SftpException.class), anyCollection());
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertFalse(Files.exists(getPath("/baz")));
assertFalse(Files.exists(getPath("/baz/bar")));
}
@Test
public void testCopyReplaceNonEmptyDirAllowed() throws IOException {
addDirectory("/foo");
addFile("/foo/bar");
addFile("/baz");
CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
FileSystemException exception = assertThrows(FileSystemException.class,
() -> fileSystem.copy(createPath("/baz"), createPath("/foo"), options));
assertEquals("/foo", exception.getFile());
verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertTrue(Files.isRegularFile(getPath("/baz")));
}
@Test
public void testMoveSFTPFailure() throws IOException {
addDirectory("/foo");
addFile("/foo/bar");
// failure: non-existing target parent
CopyOption[] options = {};
FileSystemException exception = assertThrows(FileSystemException.class,
() -> fileSystem.move(createPath("/foo/bar"), createPath("/baz/bar"), options));
assertEquals("/foo/bar", exception.getFile());
assertEquals("/baz/bar", exception.getOtherFile());
verify(getExceptionFactory()).createMoveException(eq("/foo/bar"), eq("/baz/bar"), any(SftpException.class));
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertFalse(Files.exists(getPath("/baz")));
}
CopyMoveVisitor(Path before, Path after, @Nullable PathMatcher pathMatcher, boolean overwrite, boolean isCopy) {
this.before = before;
this.after = after;
this.pathMatcher = pathMatcher;
this.isCopy = isCopy;
if (overwrite) {
moveMode = new CopyOption[]{LinkOption.NOFOLLOW_LINKS, StandardCopyOption.REPLACE_EXISTING};
} else {
moveMode = new CopyOption[]{LinkOption.NOFOLLOW_LINKS};
}
}
@Test
public void testMoveReplaceFileAllowed() throws IOException {
addDirectory("/foo");
addFile("/foo/bar");
addFile("/baz");
CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertFalse(Files.exists(getPath("/baz")));
}
@Test
public void testCopyReplaceEmptyDirAllowedDifferentFileSystems() throws IOException {
addDirectory("/foo");
addDirectory("/baz");
CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isDirectory(getPath("/foo")));
}
/**
* Copy source file to target location.
*
* @return
*/
static boolean copyFile(Path source, Path target) {
CopyOption[] options = new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING };
target = getUnique(target);
try {
Files.copy(source, target, options);
return true;
} catch (Exception x) {
System.err.format("Unable to copy: %s: %s%n", source, x);
return false;
}
}
protected void createAPKs(String testSuitePath, String appPackage, String appMainActivity, String extractorClass,
String toolsPath, String debugKeyStorePath, String autAPK, String tempPath) {
// replace strings
println("Editing 'Configuration.java'");
replaceStringsInFile(
testSuitePath + "/smali/it/unina/android/ripper/configuration/Configuration.smali.template",
testSuitePath + "/smali/it/unina/android/ripper/configuration/Configuration.smali", appPackage,
appMainActivity);
println("Editing 'AndroidManifest.xml'");
replaceStringsInFile(testSuitePath + "/AndroidManifest.xml.template", testSuitePath + "/AndroidManifest.xml",
appPackage, appMainActivity);
try {
println("Cleaning apks...");
println("Building AndroidRipper...");
execCommand("java -jar " + toolsPath + "apktool.jar b " + testSuitePath + " -o " + tempPath + "/ar.apk");
println("Signing AndroidRipper...");
execCommand("jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore " + debugKeyStorePath
+ "/debug.keystore -storepass android -keypass android " + tempPath + "/ar.apk androiddebugkey");
execCommand("zipalign 4 " + tempPath + "/ar.apk " + tempPath + "/ripper.apk");
Files.copy(FileSystems.getDefault().getPath(autAPK),
FileSystems.getDefault().getPath(tempPath + "/temp.apk"),
(CopyOption) StandardCopyOption.REPLACE_EXISTING);
println("Signing AUT...");
ZipUtils.deleteFromZip(tempPath + "/temp.apk");
execCommand("jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore " + debugKeyStorePath
+ "/debug.keystore -storepass android -keypass android " + tempPath
+ "/temp.apk androiddebugkey");
execCommand("jarsigner -verify " + tempPath + "/temp.apk");
execCommand("zipalign -v 4 " + tempPath + "/temp.apk " + tempPath + "/aut.apk");
} catch (Throwable t) {
throw new RipperRuntimeException(AndroidRipperStarter.class, "createAPKs", "apk build failed", t);
}
}
@Test
public void testMoveNonEmptyDirSameParent() throws IOException {
addDirectory("/foo");
addFile("/foo/bar");
CopyOption[] options = {};
try {
fileSystem.move(createPath("/foo"), createPath("/baz"), options);
} finally {
assertFalse(Files.exists(getPath("/foo")));
assertTrue(Files.isDirectory(getPath("/baz")));
assertTrue(Files.isRegularFile(getPath("/baz/bar")));
}
}
@Test
public void testMoveFileDifferentFileSystem() throws IOException {
addDirectory("/foo");
addFile("/baz");
CopyOption[] options = {};
fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertFalse(Files.exists(getPath("/baz")));
}
@Test
public void testMoveFile() throws IOException {
addDirectory("/foo");
addFile("/baz");
CopyOption[] options = {};
fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertFalse(Files.exists(getPath("/baz")));
}
@Test
public void testCopyEmptyDir() throws IOException {
addDirectory("/foo");
addDirectory("/baz");
CopyOption[] options = {};
fileSystem.copy(createPath("/baz"), createPath("/foo/bar"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isDirectory(getPath("/foo/bar")));
assertTrue(Files.isDirectory(getPath("/baz")));
assertEquals(0, getChildCount("/foo/bar"));
}
/**
* Convert a given resource to a temporary {@link File}.
* @param resourceName the resource to convert
* @return the {@link File} object pointing to the temporary File.
* @throws Exception
*/
public static File resourceToTemporaryFile(final String resourceName) throws IOException {
InputStream inputStream = FileUtils.class.getResourceAsStream(resourceName);
File tempFile = File.createTempFile(resourceName, ".tmp");
Files.copy(inputStream, tempFile.toPath(), new CopyOption[]{StandardCopyOption.REPLACE_EXISTING});
inputStream.close();
return tempFile;
}
@Test
public void testMoveEmptyDir() throws IOException {
addDirectory("/foo");
addDirectory("/baz");
CopyOption[] options = {};
fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isDirectory(getPath("/foo/bar")));
assertFalse(Files.exists(getPath("/baz")));
}
@Test
public void testCopyFileDifferentFileSystems() throws IOException {
addDirectory("/foo");
addFile("/baz");
CopyOption[] options = {};
fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertTrue(Files.isRegularFile(getPath("/baz")));
}
@Test
public void testMoveReplaceEmptyDirDifferentFileSystem() throws IOException {
addDirectory("/foo");
addFile("/baz");
CopyOption[] options = {};
FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class,
() -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo"), options));
assertEquals("/foo", exception.getFile());
verify(getExceptionFactory(), never()).createMoveException(anyString(), anyString(), any(SftpException.class));
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/baz")));
}
@Test
public void testMoveEmptyDirDifferentFileSystem() throws IOException {
addDirectory("/foo");
addDirectory("/baz");
CopyOption[] options = {};
fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isDirectory(getPath("/foo/bar")));
assertFalse(Files.exists(getPath("/baz")));
}
public static boolean renameTo( Path from, Path to, CopyOption... options )
{
try
{
return Files.move( from, to, options ) != null;
}
catch( IOException e )
{
return false;
}
}
@Test
public void testMoveNonEmptyRoot() throws IOException {
addDirectory("/foo");
CopyOption[] options = {};
DirectoryNotEmptyException exception = assertThrows(DirectoryNotEmptyException.class,
() -> fileSystem.move(createPath("/"), createPath("/baz"), options));
assertEquals("/", exception.getFile());
assertTrue(Files.isDirectory(getPath("/foo")));
assertFalse(Files.exists(getPath("/baz")));
}
private void checkCopyOptions(CopyOption[] options) {
for (CopyOption option : options) {
if (!SUPPORTED_COPY_OPTIONS.contains(option)) {
throw new UnsupportedOperationException("Unsupported copy option: " + option);
}
}
}
@Test
public void testMoveReplaceEmptyDir() throws IOException {
addDirectory("/foo");
addFile("/baz");
CopyOption[] options = {};
FileSystemException exception = assertThrows(FileSystemException.class,
() -> fileSystem.move(createPath("/baz"), createPath("/foo"), options));
assertEquals("/baz", exception.getFile());
assertEquals("/foo", exception.getOtherFile());
verify(getExceptionFactory()).createMoveException(eq("/baz"), eq("/foo"), any(SftpException.class));
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/baz")));
}
@Override
public void copy(Path source, Path target, CopyOption... options)
throws IOException
{
JPath jSource = (JPath) source;
JPath jTarget = (JPath) target;
try (InputStream is = jSource.getBfsFile().openRead()) {
if (is == null) {
throw new IOException(L.l("unable to read from file {0}", source.toUri()));
}
ArrayList<WriteOption> bfsOptionList = new ArrayList<>();
for (CopyOption option : options) {
if (option == StandardCopyOption.REPLACE_EXISTING) {
bfsOptionList.add(WriteOption.Standard.OVERWRITE);
}
}
WriteOption []bfsOptions = new WriteOption[bfsOptionList.size()];
bfsOptionList.toArray(bfsOptions);
try (OutputStream os = jTarget.getBfsFile().openWrite(bfsOptions)) {
if (os == null) {
throw new IOException(L.l("unable to write to file {0}", target.toUri()));
}
IoUtil.copy(is, os);
}
}
}
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
synchronized (openFiles) {
checkDeleteAccess(source);
Object key = getKeyOrNull(target);
super.move(source, target, options);
if (key != null) {
Object newKey = getKey(target);
if (newKey.equals(key) == false) {
// we need to transfer ownership here if we have open files on this file since the getKey() method will
// return a different i-node next time we call it with the target path and our onClose method will
// trip an assert
Map<Path, Integer> map = openFiles.get(key);
if (map != null) {
Integer v = map.remove(target);
if (v != null) {
Map<Path, Integer> pathIntegerMap = openFiles.computeIfAbsent(newKey, k -> new HashMap<>());
Integer existingValue = pathIntegerMap.getOrDefault(target, 0);
pathIntegerMap.put(target, existingValue + v);
}
if (map.isEmpty()) {
openFiles.remove(key);
}
}
}
}
}
}
@Test
public void testCopyReplaceFileAllowedDifferentFileSystems() throws IOException {
addDirectory("/foo");
addFile("/foo/bar");
addFile("/baz");
CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);
assertTrue(Files.isDirectory(getPath("/foo")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
assertTrue(Files.isRegularFile(getPath("/foo/bar")));
}
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
Throwable exception = null;
try {
super.copy(source, target, options);
} catch (Throwable t) {
exception = t;
} finally {
sop("copy" + Arrays.toString(options) + ": " + path(source) + " -> " + path(target), exception);
}
}