13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 6244047
27 * @author Jim Gish
28 * @summary throw more precise IOException when pattern specifies invalid directory
29 *
30 * @run main/othervm CheckLockLocationTest
31 */
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.AccessDeniedException;
35 import java.nio.file.FileSystemException;
36 import java.nio.file.Files;
37 import java.nio.file.NoSuchFileException;
38 import java.nio.file.Path;
39 import java.nio.file.attribute.UserPrincipal;
40 import java.util.logging.FileHandler;
41 public class CheckLockLocationTest {
42
43 private static final String NON_WRITABLE_DIR = "non-writable-dir";
44 private static final String NOT_A_DIR = "not-a-dir";
45 private static final String WRITABLE_DIR = "writable-dir";
46 private static final String NON_EXISTENT_DIR = "non-existent-dir";
47 private static boolean runNonWritableDirTest;
48
49 public static void main(String... args) throws IOException {
50 // we'll base all file creation attempts on the system temp directory,
51 // %t and also try specifying non-existent directories and plain files
52 // that should be directories, and non-writable directories,
53 // to exercise all code paths of checking the lock location
54 // Note that on platforms like Windows that don't support
55 // setWritable() on a directory, we'll skip the non-writable
56 // directory test if setWritable(false) returns false.
57 //
58 File writableDir = setup();
59 // we now have three files/directories to work with:
61 // notAdir
62 // nonWritableDir (may not be possible on some platforms)
63 // nonExistentDir (which doesn't exist)
64 runTests(writableDir);
65 }
66
67 /**
68 * @param writableDir in which log and lock file are created
69 * @throws SecurityException
70 * @throws RuntimeException
71 * @throws IOException
72 */
73 private static void runTests(File writableDir) throws SecurityException,
74 RuntimeException, IOException {
75 // Test 1: make sure we can create FileHandler in writable directory
76 try {
77 new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
78 } catch (IOException ex) {
79 throw new RuntimeException("Test failed: should have been able"
80 + " to create FileHandler for " + "%t/" + WRITABLE_DIR
81 + "/log.log in writable directory.", ex);
82 } finally {
83 // the above test leaves files in the directory. Get rid of the
84 // files created and the directory
85 delete(writableDir);
86 }
87
88 // Test 2: creating FileHandler in non-writable directory should fail
89 if (runNonWritableDirTest) {
90 try {
91 new FileHandler("%t/" + NON_WRITABLE_DIR + "/log.log");
92 throw new RuntimeException("Test failed: should not have been able"
93 + " to create FileHandler for " + "%t/" + NON_WRITABLE_DIR
94 + "/log.log in non-writable directory.");
95 } catch (AccessDeniedException ex) {
96 // the right exception was thrown, so continue.
97 } catch (IOException ex) {
98 throw new RuntimeException(
99 "Test failed: Expected exception was not an "
100 + "AccessDeniedException", ex);
101 }
132 * Setup all the files and directories needed for the tests
133 *
134 * @return writable directory created that needs to be deleted when done
135 * @throws RuntimeException
136 */
137 private static File setup() throws RuntimeException {
138 // First do some setup in the temporary directory (using same logic as
139 // FileHandler for %t pattern)
140 String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
141 if (tmpDir == null) {
142 tmpDir = System.getProperty("user.home");
143 }
144 File tmpOrHomeDir = new File(tmpDir);
145 // Create a writable directory here (%t/writable-dir)
146 File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
147 if (!createFile(writableDir, true)) {
148 throw new RuntimeException("Test setup failed: unable to create"
149 + " writable working directory "
150 + writableDir.getAbsolutePath() );
151 }
152 // writableDirectory and its contents will be deleted after the test
153 // that uses it
154
155 // Create a plain file which we will attempt to use as a directory
156 // (%t/not-a-dir)
157 File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
158 if (!createFile(notAdir, false)) {
159 throw new RuntimeException("Test setup failed: unable to a plain"
160 + " working file " + notAdir.getAbsolutePath() );
161 }
162 notAdir.deleteOnExit();
163
164 // Create a non-writable directory (%t/non-writable-dir)
165 File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
166 if (!createFile(nonWritableDir, true)) {
167 throw new RuntimeException("Test setup failed: unable to create"
168 + " a non-"
169 + "writable working directory "
170 + nonWritableDir.getAbsolutePath() );
171 }
172 nonWritableDir.deleteOnExit();
173
|
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 6244047
27 * @author Jim Gish
28 * @summary throw more precise IOException when pattern specifies invalid directory
29 *
30 * @run main/othervm CheckLockLocationTest
31 */
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.io.OutputStream;
36 import java.nio.file.AccessDeniedException;
37 import java.nio.file.FileSystemException;
38 import java.nio.file.Files;
39 import java.nio.file.NoSuchFileException;
40 import java.nio.file.Path;
41 import java.nio.file.attribute.UserPrincipal;
42 import java.util.UUID;
43 import java.util.logging.FileHandler;
44 public class CheckLockLocationTest {
45
46 private static final String NON_WRITABLE_DIR = "non-writable-dir";
47 private static final String NOT_A_DIR = "not-a-dir";
48 private static final String WRITABLE_DIR = "writable-dir";
49 private static final String NON_EXISTENT_DIR = "non-existent-dir";
50 private static boolean runNonWritableDirTest;
51
52 public static void main(String... args) throws IOException {
53 // we'll base all file creation attempts on the system temp directory,
54 // %t and also try specifying non-existent directories and plain files
55 // that should be directories, and non-writable directories,
56 // to exercise all code paths of checking the lock location
57 // Note that on platforms like Windows that don't support
58 // setWritable() on a directory, we'll skip the non-writable
59 // directory test if setWritable(false) returns false.
60 //
61 File writableDir = setup();
62 // we now have three files/directories to work with:
64 // notAdir
65 // nonWritableDir (may not be possible on some platforms)
66 // nonExistentDir (which doesn't exist)
67 runTests(writableDir);
68 }
69
70 /**
71 * @param writableDir in which log and lock file are created
72 * @throws SecurityException
73 * @throws RuntimeException
74 * @throws IOException
75 */
76 private static void runTests(File writableDir) throws SecurityException,
77 RuntimeException, IOException {
78 // Test 1: make sure we can create FileHandler in writable directory
79 try {
80 new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
81 } catch (IOException ex) {
82 throw new RuntimeException("Test failed: should have been able"
83 + " to create FileHandler for " + "%t/" + WRITABLE_DIR
84 + "/log.log in writable directory"
85 + (!writableDir.canRead() // concurrent tests running or user conf issue?
86 ? ": directory not readable.\n\tPlease check your "
87 + "environment and machine configuration."
88 : "."), ex);
89 } finally {
90 // the above test leaves files in the directory. Get rid of the
91 // files created and the directory
92 delete(writableDir);
93 }
94
95 // Test 2: creating FileHandler in non-writable directory should fail
96 if (runNonWritableDirTest) {
97 try {
98 new FileHandler("%t/" + NON_WRITABLE_DIR + "/log.log");
99 throw new RuntimeException("Test failed: should not have been able"
100 + " to create FileHandler for " + "%t/" + NON_WRITABLE_DIR
101 + "/log.log in non-writable directory.");
102 } catch (AccessDeniedException ex) {
103 // the right exception was thrown, so continue.
104 } catch (IOException ex) {
105 throw new RuntimeException(
106 "Test failed: Expected exception was not an "
107 + "AccessDeniedException", ex);
108 }
139 * Setup all the files and directories needed for the tests
140 *
141 * @return writable directory created that needs to be deleted when done
142 * @throws RuntimeException
143 */
144 private static File setup() throws RuntimeException {
145 // First do some setup in the temporary directory (using same logic as
146 // FileHandler for %t pattern)
147 String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
148 if (tmpDir == null) {
149 tmpDir = System.getProperty("user.home");
150 }
151 File tmpOrHomeDir = new File(tmpDir);
152 // Create a writable directory here (%t/writable-dir)
153 File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
154 if (!createFile(writableDir, true)) {
155 throw new RuntimeException("Test setup failed: unable to create"
156 + " writable working directory "
157 + writableDir.getAbsolutePath() );
158 }
159
160 if (!writableDir.canRead()) {
161 throw new RuntimeException("Test setup failed: can't read "
162 + " writable working directory "
163 + writableDir.getAbsolutePath() );
164 }
165
166 // writableDirectory and its contents will be deleted after the test
167 // that uses it.
168
169 // check that we can write in the new writable dir.
170 File dummyFile = new File(writableDir, UUID.randomUUID().toString() + ".txt" );
171 try {
172 if (!dummyFile.createNewFile()) {
173 throw new RuntimeException("Test setup failed: can't create "
174 + " dummy file in writable working directory "
175 + dummyFile.getAbsolutePath() );
176 }
177 try (OutputStream os = new FileOutputStream(dummyFile)) {
178 os.write('A');
179 } finally {
180 dummyFile.delete();
181 }
182 if (dummyFile.canRead()) {
183 throw new RuntimeException("Test setup failed: can't delete "
184 + " dummy file in writable working directory "
185 + dummyFile.getAbsolutePath() );
186 }
187 System.out.println("Successfully created and deleted dummy file: " +
188 dummyFile.getAbsolutePath());
189 } catch(IOException x) {
190 throw new RuntimeException("Test setup failed: can't write "
191 + " or delete dummy file in writable working directory "
192 + dummyFile.getAbsolutePath(), x);
193 }
194
195 // Create a plain file which we will attempt to use as a directory
196 // (%t/not-a-dir)
197 File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
198 if (!createFile(notAdir, false)) {
199 throw new RuntimeException("Test setup failed: unable to a plain"
200 + " working file " + notAdir.getAbsolutePath() );
201 }
202 notAdir.deleteOnExit();
203
204 // Create a non-writable directory (%t/non-writable-dir)
205 File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
206 if (!createFile(nonWritableDir, true)) {
207 throw new RuntimeException("Test setup failed: unable to create"
208 + " a non-"
209 + "writable working directory "
210 + nonWritableDir.getAbsolutePath() );
211 }
212 nonWritableDir.deleteOnExit();
213
|