Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / net / LoadUtility.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.test.java.net;
20
21 import java.io.File;
22
23 import gov.nasa.jpf.util.test.TestJPF;
24
25 /**
26  * This is used to hide the package "classloader_specific_tests" from JPF 
27  * which is need to test costum class loaders
28  * 
29  * @author Nastaran Shafiei
30  */
31 public class LoadUtility extends TestJPF{
32
33   protected String user_dir = System.getProperty("user.dir");
34   protected String pkg = "classloader_specific_tests";
35
36   protected String originalPath = user_dir + "/build/tests/" + pkg;
37   protected String tempPath = user_dir + "/build/" + pkg;
38
39   protected String jarUrl = "jar:file:" + user_dir + "/build/" + pkg + ".jar!/";
40   protected String dirUrl = "file:" + user_dir + "/build";
41
42   /**
43    * move the package, to avoid systemClassLoader loading its classes
44    */
45   protected void movePkgOut() {
46     if(!TestJPF.isJPFRun()) {
47       movePkg(originalPath, tempPath);
48     }
49   }
50
51   /**
52    * move the package back to its original place
53    */
54   protected void movePkgBack() {
55     if(!TestJPF.isJPFRun()) {
56       movePkg(tempPath, originalPath);
57     }
58   }
59
60   protected void movePkg(String from, String to) {
61     File dstFile = new File(to);
62     if(!dstFile.exists()) {
63       dstFile = new File(from);
64       assertTrue(dstFile.renameTo(new File(to)));
65     } else {
66       File srcFile = new File(from);
67       if(srcFile.exists()) {
68         // empty the directory
69         for(String name: srcFile.list()) {
70           assertTrue((new File(from + "/" + name)).delete());
71         }
72         // remove the directory
73         assertTrue(srcFile.delete());
74       }
75     }
76   }
77 }