author | erikj |
Tue, 12 Sep 2017 19:03:56 +0200 | |
changeset 47217 | 72e3ae9a25eb |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 |
||
26 |
package jdk.nashorn.internal.test.framework; |
|
27 |
||
28 |
import java.io.BufferedReader; |
|
29 |
import java.io.ByteArrayOutputStream; |
|
30 |
import java.io.File; |
|
31 |
import java.io.FileReader; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.PrintStream; |
|
34 |
import java.io.Reader; |
|
35 |
||
36 |
/** |
|
37 |
* Simple utilities to deal with build-dir, read/dump files etc. |
|
38 |
*/ |
|
27361 | 39 |
@SuppressWarnings("javadoc") |
16147 | 40 |
public abstract class TestHelper { |
41 |
||
47217 | 42 |
public static final String TEST_ROOT = "test" + File.separator + "nashorn"; |
25675
556f675a6a12
8051019: Separate src and test execution sandbox directories
sundar
parents:
16151
diff
changeset
|
43 |
public static final String BUILD_ROOT = |
556f675a6a12
8051019: Separate src and test execution sandbox directories
sundar
parents:
16151
diff
changeset
|
44 |
System.getProperty("build.dir", "build") + File.separator + "test"; |
16147 | 45 |
public static final String TEST_PREFIX = TEST_ROOT + File.separator; |
46 |
||
47 |
private TestHelper() { |
|
48 |
// empty |
|
49 |
} |
|
50 |
||
51 |
protected static File makeBuildDir(final File testFile) { |
|
52 |
final File buildDir = getBuildDir(testFile); |
|
53 |
if (!new File(BUILD_ROOT).exists()) { |
|
54 |
throw new IllegalArgumentException("no " + BUILD_ROOT + " directory in " + new File(".").getAbsolutePath()); |
|
55 |
} |
|
56 |
buildDir.mkdirs(); |
|
57 |
return buildDir; |
|
58 |
} |
|
59 |
||
60 |
protected static File getBuildDir(final File testFile) { |
|
61 |
if (!testFile.getPath().startsWith(TEST_PREFIX)) { |
|
62 |
throw new IllegalArgumentException("test file path not a relative pathname"); |
|
63 |
} |
|
64 |
final File buildDir = new File(BUILD_ROOT + File.separator + testFile.getParent().substring(TEST_PREFIX.length())); |
|
65 |
return buildDir; |
|
66 |
} |
|
67 |
||
68 |
// return the first line of the given file |
|
69 |
protected static String firstLine(final File file) throws IOException { |
|
70 |
return content(file, true); |
|
71 |
} |
|
72 |
||
73 |
// return the full content of the file as a String |
|
74 |
protected static String fullContent(final File file) throws IOException { |
|
75 |
return content(file, false); |
|
76 |
} |
|
77 |
||
78 |
private static String content(final File file, final boolean firstLine) throws IOException { |
|
79 |
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|
80 |
final PrintStream ps = new PrintStream(bos); |
|
81 |
dumpFile(ps, file); |
|
82 |
final String contents = bos.toString(); |
|
83 |
||
84 |
if (! firstLine) { |
|
85 |
return contents; |
|
86 |
} |
|
87 |
||
88 |
// else return only the first line, stripping the trailing cr, lf or cr+lf, |
|
89 |
// if present |
|
90 |
final int cr = contents.indexOf('\r'); |
|
91 |
if (cr > 0) { |
|
92 |
return contents.substring(0, cr); |
|
93 |
} |
|
94 |
final int lf = contents.indexOf('\n'); |
|
95 |
if (lf > 0) { |
|
96 |
return contents.substring(0, lf); |
|
97 |
} |
|
98 |
return contents; |
|
99 |
} |
|
100 |
||
101 |
// dump the content of given reader on standard output |
|
102 |
protected static void dumpFile(final Reader rdr) throws IOException { |
|
103 |
dumpFile(System.out, rdr); |
|
104 |
} |
|
105 |
||
106 |
// dump the content of given reader on given output stream |
|
107 |
protected static void dumpFile(final PrintStream output, final Reader rdr) throws IOException { |
|
108 |
try (BufferedReader reader = new BufferedReader(rdr)) { |
|
109 |
while (true) { |
|
110 |
final String line = reader.readLine(); |
|
111 |
if (line == null) { |
|
112 |
break; |
|
113 |
} |
|
114 |
output.println(line); |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
// dump the content of given file on standard output |
|
120 |
protected static void dumpFile(final File file) throws IOException { |
|
121 |
dumpFile(System.out, file); |
|
122 |
} |
|
123 |
||
124 |
// dump the content of given file on given output stream |
|
125 |
protected static void dumpFile(final PrintStream output, final File file) throws IOException { |
|
126 |
try (Reader rdr = new FileReader(file); BufferedReader reader = new BufferedReader(rdr)) { |
|
127 |
while (true) { |
|
128 |
final String line = reader.readLine(); |
|
129 |
if (line == null) { |
|
130 |
break; |
|
131 |
} |
|
132 |
output.println(line); |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
} |