50113
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package jdk.test.lib.jfr;
|
|
26 |
|
|
27 |
import java.io.File;
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.nio.file.AccessDeniedException;
|
|
30 |
import java.nio.file.Files;
|
|
31 |
import java.nio.file.Path;
|
|
32 |
import java.nio.file.Paths;
|
|
33 |
import java.util.List;
|
|
34 |
|
|
35 |
import jdk.jfr.consumer.RecordedEvent;
|
|
36 |
import jdk.jfr.consumer.RecordingFile;
|
|
37 |
import jdk.test.lib.Asserts;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Common helper class.
|
|
41 |
*/
|
|
42 |
public class FileHelper {
|
|
43 |
|
|
44 |
public static Path getDest(String subPath) throws IOException {
|
|
45 |
Path path = Paths.get(subPath + "/test.jfr");
|
|
46 |
Path parent = path.getParent();
|
|
47 |
if (parent == null) {
|
|
48 |
throw new IOException("No parent cound be found for path " + subPath);
|
|
49 |
}
|
|
50 |
Files.createDirectories(parent);
|
|
51 |
return path;
|
|
52 |
}
|
|
53 |
|
|
54 |
public static Path createLongDir(Path root) throws IOException {
|
|
55 |
final int minPathLength = 400;
|
|
56 |
StringBuilder buff = new StringBuilder();
|
|
57 |
buff.append(root.toString());
|
|
58 |
while (buff.length() < minPathLength) {
|
|
59 |
buff.append("/veryLongPath012345678901234567890123456789");
|
|
60 |
}
|
|
61 |
Path path = Paths.get(buff.toString());
|
|
62 |
System.out.println("long dir=" + path);
|
|
63 |
Files.createDirectories(path);
|
|
64 |
return path;
|
|
65 |
}
|
|
66 |
|
|
67 |
public static Path getDestReadOnly(String subPath) throws IOException {
|
|
68 |
final Path path = getDest(subPath);
|
|
69 |
Path parent = path.getParent();
|
|
70 |
if (parent == null) {
|
|
71 |
throw new IOException("No parent cound be found for path " + subPath);
|
|
72 |
}
|
|
73 |
parent.toFile().setReadOnly();
|
|
74 |
return path;
|
|
75 |
}
|
|
76 |
|
|
77 |
public static Path createReadOnlyFile(Path path) throws IOException {
|
|
78 |
final Path createdPath = Files.createFile(path);
|
|
79 |
createdPath.toFile().setReadOnly();
|
|
80 |
return createdPath;
|
|
81 |
}
|
|
82 |
|
|
83 |
public static Path createReadOnlyDir(Path path) throws IOException {
|
|
84 |
final Path createdPath = Files.createDirectories(path);
|
|
85 |
createdPath.toFile().setReadOnly();
|
|
86 |
return createdPath;
|
|
87 |
}
|
|
88 |
|
|
89 |
public static Path getDestNotExist() {
|
|
90 |
return Paths.get(".", "thisDirDoesNotExist/test.jfr");
|
|
91 |
}
|
|
92 |
|
|
93 |
public static boolean isReadOnlyPath(Path path) throws IOException {
|
|
94 |
// Files.isWritable(path) can not really be trusted. At least not on Windows.
|
|
95 |
// If path is a directory, we try to create a new file in it.
|
|
96 |
if (Files.isDirectory(path)) {
|
|
97 |
try {
|
|
98 |
Path f = Files.createFile(Paths.get(path.toString(), "dummyFileToCheckReadOnly"));
|
|
99 |
System.out.printf("Dir is not read-only, created %s, exists=%b%n", f, Files.exists(f));
|
|
100 |
return false;
|
|
101 |
} catch (AccessDeniedException e) {
|
|
102 |
System.out.printf("'%s' verified read-only by %s%n", path, e.toString());
|
|
103 |
return true;
|
|
104 |
}
|
|
105 |
} else {
|
|
106 |
boolean isReadOnly = !Files.isWritable(path);
|
|
107 |
System.out.format("isReadOnly '%s': %b%n", path, isReadOnly);
|
|
108 |
return isReadOnly;
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
public static void verifyRecording(File file) throws Exception {
|
|
113 |
Asserts.assertTrue(file.exists(), file.getAbsolutePath() + " does not exist");
|
|
114 |
Asserts.assertTrue(file.isFile(), file.getAbsolutePath() + " is not a file");
|
|
115 |
Asserts.assertGreaterThan(file.length(), 0L, "Size of recording is 0.");
|
|
116 |
List<RecordedEvent> events = RecordingFile.readAllEvents(file.toPath());
|
|
117 |
for (RecordedEvent event : events) {
|
|
118 |
System.out.printf("First event in recording '%s':%n%s", file.getName(), event);
|
|
119 |
return;
|
|
120 |
}
|
|
121 |
Asserts.fail("No events in file " + file.getName());
|
|
122 |
}
|
|
123 |
|
|
124 |
}
|