1 package org.apache.archiva.common.utils;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.junit.Assume;
23 import org.junit.Test;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.net.URISyntaxException;
28 import java.nio.file.FileSystems;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.nio.file.StandardOpenOption;
33 import java.nio.file.attribute.PosixFilePermission;
34 import java.util.HashSet;
37 import static org.junit.Assert.assertFalse;
38 import static org.junit.Assert.assertTrue;
41 * @author Martin Stockhammer <martin_s@apache.org>
43 public class FileUtilsTest
46 public void testDeleteQuietly() throws IOException
48 Path tf = Files.createTempFile( "FileUtilsTest", ".txt" );
49 assertTrue(Files.exists(tf));
50 FileUtils.deleteQuietly( tf );
51 assertFalse(Files.exists(tf));
53 Path td = Files.createTempDirectory( "FileUtilsTest" );
54 Path f1 = td.resolve("file1.txt");
55 Path f2 = td.resolve("file2.txt");
56 Path d1 = td.resolve("dir1");
57 Files.createDirectory( d1 );
58 Path d11 = d1.resolve("dir11");
59 Files.createDirectory( d11 );
60 Path f111 = d11.resolve("file111.txt");
61 Path f112 = d11.resolve("file112.txt");
62 Files.write(f1,"file1".getBytes());
63 Files.write(f2, "file2".getBytes());
64 Files.write(f111, "file111".getBytes());
65 Files.write(f112, "file112".getBytes());
66 assertTrue(Files.exists(d1));
67 assertTrue(Files.exists(f1));
68 assertTrue(Files.exists(f2));
69 assertTrue(Files.exists(f111));
70 assertTrue(Files.exists(f112));
72 FileUtils.deleteQuietly( td );
73 assertFalse(Files.exists(f1));
74 assertFalse(Files.exists(f2));
75 assertFalse(Files.exists(f111));
76 assertFalse(Files.exists(f112));
77 assertFalse(Files.exists(d1));
83 public void testDelete() throws IOException
85 Path td = Files.createTempDirectory( "FileUtilsTest" );
86 Path f1 = td.resolve("file1.txt");
87 Path f2 = td.resolve("file2.txt");
88 Path d1 = td.resolve("dir1");
89 Files.createDirectory( d1 );
90 Path d11 = d1.resolve("dir11");
91 Files.createDirectory( d11 );
92 Path f111 = d11.resolve("file111.txt");
93 Path f112 = d11.resolve("file112.txt");
94 Files.write(f1,"file1".getBytes());
95 Files.write(f2, "file2".getBytes());
96 Files.write(f111, "file111".getBytes());
97 Files.write(f112, "file112".getBytes());
98 assertTrue(Files.exists(d1));
99 assertTrue(Files.exists(f1));
100 assertTrue(Files.exists(f2));
101 assertTrue(Files.exists(f111));
102 assertTrue(Files.exists(f112));
104 FileUtils.deleteDirectory( td );
105 assertFalse(Files.exists(f1));
106 assertFalse(Files.exists(f2));
107 assertFalse(Files.exists(f111));
108 assertFalse(Files.exists(f112));
109 assertFalse(Files.exists(d1));
114 public void testDeleteNonExist() throws IOException
116 Path tf = Paths.get("aaserijdmcjdjhdejeidmdjdlasrjerjnbmckdkdk");
117 assertFalse(Files.exists(tf));
118 FileUtils.deleteDirectory( tf );
121 @Test(expected = IOException.class)
122 public void testDeleteWithException() throws IOException
124 Assume.assumeTrue( FileSystems.getDefault().supportedFileAttributeViews().contains("posix") );
125 Path tmpDir = Files.createTempDirectory( "FileUtilsTest" );
126 Path tmpDir2 = tmpDir.resolve("testdir1");
127 Files.createDirectories( tmpDir2 );
128 Path tmpFile = tmpDir2.resolve("testfile1.txt");
129 OutputStream stream = null;
132 stream = Files.newOutputStream( tmpFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE );
135 assertTrue( Files.exists( tmpFile ) );
136 stream = Files.newOutputStream( tmpFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE );
138 Set<PosixFilePermission> perms = new HashSet<>( );
139 Files.setPosixFilePermissions( tmpFile, perms );
140 Files.setPosixFilePermissions( tmpDir2, perms );
141 FileUtils.deleteDirectory( tmpDir );
142 assertFalse( Files.exists( tmpDir ) );
143 assertFalse( Files.exists( tmpFile ) );
148 Set<PosixFilePermission> perms = new HashSet<>( );
149 perms.add(PosixFilePermission.OWNER_READ);
150 perms.add(PosixFilePermission.OWNER_WRITE);
151 perms.add(PosixFilePermission.OWNER_EXECUTE);
152 Files.setPosixFilePermissions( tmpDir2, perms );
153 Files.setPosixFilePermissions( tmpFile, perms );
154 Files.deleteIfExists( tmpFile );
155 Files.deleteIfExists( tmpDir2 );
156 Files.deleteIfExists( tmpDir );
161 public void unzip() throws URISyntaxException, IOException {
162 Path destPath = Paths.get("target/unzip");
164 Path zipFile = Paths.get(Thread.currentThread().getContextClassLoader().getResource("test-repository.zip").toURI());
165 if (Files.exists(destPath)) {
166 org.apache.commons.io.FileUtils.deleteQuietly(destPath.toFile());
168 FileUtils.unzip(zipFile, destPath);
169 assertTrue(Files.exists(destPath.resolve("org/apache/maven/A/1.0/A-1.0.pom")));
170 assertTrue(Files.isRegularFile(destPath.resolve("org/apache/maven/A/1.0/A-1.0.pom")));
171 assertTrue(Files.exists(destPath.resolve("org/apache/maven/A/1.0/A-1.0.war")));
172 assertTrue(Files.isRegularFile(destPath.resolve("org/apache/maven/A/1.0/A-1.0.war")));
173 assertTrue(Files.exists(destPath.resolve("org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar")));
174 assertTrue(Files.isRegularFile(destPath.resolve("org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar")));
175 assertTrue(Files.exists(destPath.resolve("KEYS")));
176 assertTrue(Files.isRegularFile(destPath.resolve("KEYS")));
177 assertTrue(Files.isDirectory(destPath.resolve("invalid")));
178 assertTrue(Files.isDirectory(destPath.resolve("javax")));
179 assertTrue(Files.isDirectory(destPath.resolve("org")));
181 org.apache.commons.io.FileUtils.deleteQuietly(destPath.toFile());