jdk/test/java/net/URLClassLoader/closetest/CloseTest.java
changeset 7983 fb83b663cadc
parent 7668 d4a77089c587
child 9035 1255eb81cc2f
equal deleted inserted replaced
7973:dffe8439eb20 7983:fb83b663cadc
    34 import java.io.*;
    34 import java.io.*;
    35 import java.net.*;
    35 import java.net.*;
    36 import java.lang.reflect.*;
    36 import java.lang.reflect.*;
    37 import com.sun.net.httpserver.*;
    37 import com.sun.net.httpserver.*;
    38 
    38 
    39 public class CloseTest {
    39 public class CloseTest extends Common {
    40 
       
    41     static void copyFile (String src, String dst) {
       
    42         copyFile (new File(src), new File(dst));
       
    43     }
       
    44 
       
    45     static void copyDir (String src, String dst) {
       
    46         copyDir (new File(src), new File(dst));
       
    47     }
       
    48 
       
    49     static void copyFile (File src, File dst) {
       
    50         try {
       
    51             if (!src.isFile()) {
       
    52                 throw new RuntimeException ("File not found: " + src.toString());
       
    53             }
       
    54             dst.delete();
       
    55             dst.createNewFile();
       
    56             FileInputStream i = new FileInputStream (src);
       
    57             FileOutputStream o = new FileOutputStream (dst);
       
    58             byte[] buf = new byte [1024];
       
    59             int count;
       
    60             while ((count=i.read(buf)) >= 0) {
       
    61                 o.write (buf, 0, count);
       
    62             }
       
    63             i.close();
       
    64             o.close();
       
    65         } catch (IOException e) {
       
    66             throw new RuntimeException (e);
       
    67         }
       
    68     }
       
    69 
       
    70     static void rm_minus_rf (File path) {
       
    71         if (!path.exists()) {
       
    72             return;
       
    73         }
       
    74         if (path.isFile()) {
       
    75             if (!path.delete()) {
       
    76                 throw new RuntimeException ("Could not delete " + path);
       
    77             }
       
    78         } else if (path.isDirectory ()) {
       
    79             String[] names = path.list();
       
    80             File[] files = path.listFiles();
       
    81             for (int i=0; i<files.length; i++) {
       
    82                 rm_minus_rf (new File(path, names[i]));
       
    83             }
       
    84             if (!path.delete()) {
       
    85                 throw new RuntimeException ("Could not delete " + path);
       
    86             }
       
    87         } else {
       
    88             throw new RuntimeException ("Trying to delete something that isn't a file or a directory");
       
    89         }
       
    90     }
       
    91 
       
    92     static void copyDir (File src, File dst) {
       
    93         if (!src.isDirectory()) {
       
    94             throw new RuntimeException ("Dir not found: " + src.toString());
       
    95         }
       
    96         if (dst.exists()) {
       
    97             throw new RuntimeException ("Dir exists: " + dst.toString());
       
    98         }
       
    99         dst.mkdir();
       
   100         String[] names = src.list();
       
   101         File[] files = src.listFiles();
       
   102         for (int i=0; i<files.length; i++) {
       
   103             String f = names[i];
       
   104             if (files[i].isDirectory()) {
       
   105                 copyDir (files[i], new File (dst, f));
       
   106             } else {
       
   107                 copyFile (new File (src, f), new File (dst, f));
       
   108             }
       
   109         }
       
   110     }
       
   111 
       
   112     /* expect is true if you expect to find it, false if you expect not to */
       
   113     static Class loadClass (String name, URLClassLoader loader, boolean expect){
       
   114         try {
       
   115             Class clazz = Class.forName (name, true, loader);
       
   116             if (!expect) {
       
   117                 throw new RuntimeException ("loadClass: "+name+" unexpected");
       
   118             }
       
   119             return clazz;
       
   120         } catch (ClassNotFoundException e) {
       
   121             if (expect) {
       
   122                 throw new RuntimeException ("loadClass: " +name + " not found");
       
   123             }
       
   124         }
       
   125         return null;
       
   126     }
       
   127 
    40 
   128 //
    41 //
   129 // needs two jar files test1.jar and test2.jar with following structure
    42 // needs two jar files test1.jar and test2.jar with following structure
   130 //
    43 //
   131 // com/foo/TestClass
    44 // com/foo/TestClass