1 /*
2 * Copyright (c) 2013, 2016, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
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 package jdk.testlibrary;
25
26 import static jdk.testlibrary.Asserts.assertTrue;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.net.InetAddress;
35 import java.net.ServerSocket;
36 import java.net.UnknownHostException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.Objects;
42 import java.util.regex.Pattern;
43 import java.util.regex.Matcher;
44 import java.util.concurrent.TimeUnit;
45 import java.util.function.Function;
46
47 /**
48 * Common library for various test helper functions.
49 */
50 public final class Utils {
51
52 /**
53 * Returns the sequence used by operating system to separate lines.
54 */
55 public static final String NEW_LINE = System.getProperty("line.separator");
56
57 /**
58 * Returns the value of 'test.vm.opts'system property.
59 */
60 public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
61
62 /**
63 * Returns the value of 'test.java.opts'system property.
64 */
65 public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
393 * one, or both, streams may be in an inconsistent state. It is strongly
394 * recommended that both streams be promptly closed if an I/O error occurs.
395 *
396 * @param in the input stream, non-null
397 * @param out the output stream, non-null
398 * @return the number of bytes transferred
399 * @throws IOException if an I/O error occurs when reading or writing
400 * @throws NullPointerException if {@code in} or {@code out} is {@code null}
401 *
402 */
403 public static long transferTo(InputStream in, OutputStream out)
404 throws IOException {
405 long transferred = 0;
406 byte[] buffer = new byte[BUFFER_SIZE];
407 int read;
408 while ((read = in.read(buffer, 0, BUFFER_SIZE)) >= 0) {
409 out.write(buffer, 0, read);
410 transferred += read;
411 }
412 return transferred;
413 }
414 }
|
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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
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 package jdk.testlibrary;
25
26 import static jdk.testlibrary.Asserts.assertTrue;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.net.InetAddress;
35 import java.net.ServerSocket;
36 import java.net.UnknownHostException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Arrays;
40 import java.util.LinkedList;
41 import java.nio.file.Paths;
42 import java.util.Collections;
43 import java.util.Objects;
44 import java.util.regex.Pattern;
45 import java.util.regex.Matcher;
46 import java.util.concurrent.TimeUnit;
47 import java.util.function.Function;
48 import java.nio.file.Files;
49
50 /**
51 * Common library for various test helper functions.
52 */
53 public final class Utils {
54
55 /**
56 * Returns the sequence used by operating system to separate lines.
57 */
58 public static final String NEW_LINE = System.getProperty("line.separator");
59
60 /**
61 * Returns the value of 'test.vm.opts'system property.
62 */
63 public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
64
65 /**
66 * Returns the value of 'test.java.opts'system property.
67 */
68 public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
396 * one, or both, streams may be in an inconsistent state. It is strongly
397 * recommended that both streams be promptly closed if an I/O error occurs.
398 *
399 * @param in the input stream, non-null
400 * @param out the output stream, non-null
401 * @return the number of bytes transferred
402 * @throws IOException if an I/O error occurs when reading or writing
403 * @throws NullPointerException if {@code in} or {@code out} is {@code null}
404 *
405 */
406 public static long transferTo(InputStream in, OutputStream out)
407 throws IOException {
408 long transferred = 0;
409 byte[] buffer = new byte[BUFFER_SIZE];
410 int read;
411 while ((read = in.read(buffer, 0, BUFFER_SIZE)) >= 0) {
412 out.write(buffer, 0, read);
413 transferred += read;
414 }
415 return transferred;
416 }
417
418 // Parses the specified source file for "@{id} breakpoint" tags and returns
419 // list of the line numbers containing the tag.
420 // Example:
421 // System.out.println("BP is here"); // @1 breakpoint
422 public static List<Integer> parseBreakpoints(String filePath, int id) {
423 final String pattern = "@" + id + " breakpoint";
424 int lineNum = 1;
425 List<Integer> result = new LinkedList<>();
426 try {
427 for (String line: Files.readAllLines(Paths.get(filePath))) {
428 if (line.contains(pattern)) {
429 result.add(lineNum);
430 }
431 lineNum++;
432 }
433 } catch (IOException ex) {
434 throw new RuntimeException("failed to parse " + filePath, ex);
435 }
436 return result;
437 }
438
439 // gets full test source path for the given test filename
440 public static String getTestSourcePath(String fileName) {
441 return Paths.get(System.getProperty("test.src")).resolve(fileName).toString();
442 }
443 }
|