]> source.dussan.org Git - archiva.git/blob
998efb1479bb82f1fce3c7039666d59e6ca89ed7
[archiva.git] /
1 package org.apache.archiva.common.utils;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.junit.Assume;
23 import org.junit.Test;
24
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;
35 import java.util.Set;
36
37 import static org.junit.Assert.assertFalse;
38 import static org.junit.Assert.assertTrue;
39
40 /**
41  * @author Martin Stockhammer <martin_s@apache.org>
42  */
43 public class FileUtilsTest
44 {
45     @Test
46     public void testDeleteQuietly() throws IOException
47     {
48         Path tf = Files.createTempFile( "FileUtilsTest", ".txt" );
49         assertTrue(Files.exists(tf));
50         FileUtils.deleteQuietly( tf );
51         assertFalse(Files.exists(tf));
52
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));
71
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));
78
79
80     }
81
82     @Test
83     public void testDelete() throws IOException
84     {
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));
103
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));
110
111     }
112
113     @Test
114     public void testDeleteNonExist() throws IOException
115     {
116         Path tf = Paths.get("aaserijdmcjdjhdejeidmdjdlasrjerjnbmckdkdk");
117         assertFalse(Files.exists(tf));
118         FileUtils.deleteDirectory( tf );
119     }
120
121     @Test(expected = IOException.class)
122     public void testDeleteWithException() throws IOException
123     {
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;
130         try
131         {
132             stream = Files.newOutputStream( tmpFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE );
133             stream.write( 1 );
134             stream.close( );
135             assertTrue( Files.exists( tmpFile ) );
136             stream = Files.newOutputStream( tmpFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE );
137             stream.write( 1 );
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 ) );
144         } finally {
145             if (stream!=null) {
146                 stream.close();
147             }
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 );
157         }
158     }
159
160     @Test
161     public void unzip() throws URISyntaxException, IOException {
162         Path destPath = Paths.get("target/unzip");
163         try {
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());
167             }
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")));
180         } finally {
181             org.apache.commons.io.FileUtils.deleteQuietly(destPath.toFile());
182         }
183     }
184
185 }