1/*
2 * Copyright (c) 2011, 2017, 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
24import java.io.*;
25import java.net.*;
26import java.nio.file.Files;
27import jdk.test.lib.util.FileUtils;
28import static java.nio.file.StandardCopyOption.*;
29
30public class Common {
31
32    static void copyFile (String src, String dst) {
33        copyFile (new File(src), new File(dst));
34    }
35
36    static void copyDir (String src, String dst) {
37        copyDir (new File(src), new File(dst));
38    }
39
40    static void copyFile (File src, File dst) {
41        try {
42            if (!src.isFile()) {
43                throw new RuntimeException ("File not found: " + src.toString());
44            }
45            Files.copy(src.toPath(), dst.toPath(), REPLACE_EXISTING);
46        } catch (IOException e) {
47            throw new RuntimeException (e);
48        }
49    }
50
51    static void rm_minus_rf (File path) throws IOException, InterruptedException {
52        if (!path.exists())
53            return;
54        FileUtils.deleteFileTreeWithRetry(path.toPath());
55    }
56
57    static void copyDir (File src, File dst) {
58        if (!src.isDirectory()) {
59            throw new RuntimeException ("Dir not found: " + src.toString());
60        }
61        if (dst.exists()) {
62            throw new RuntimeException ("Dir exists: " + dst.toString());
63        }
64        dst.mkdir();
65        String[] names = src.list();
66        File[] files = src.listFiles();
67        for (int i=0; i<files.length; i++) {
68            String f = names[i];
69            if (files[i].isDirectory()) {
70                copyDir (files[i], new File (dst, f));
71            } else {
72                copyFile (new File (src, f), new File (dst, f));
73            }
74        }
75    }
76
77    /* expect is true if you expect to find it, false if you expect not to */
78    static Class loadClass (String name, URLClassLoader loader, boolean expect){
79        try {
80            Class clazz = Class.forName (name, true, loader);
81            if (!expect) {
82                throw new RuntimeException ("loadClass: "+name+" unexpected");
83            }
84            return clazz;
85        } catch (ClassNotFoundException e) {
86            if (expect) {
87                throw new RuntimeException ("loadClass: " +name + " not found");
88            }
89        }
90        return null;
91    }
92}
93