1   package javax.jdo;
2   
3   import org.apache.tools.ant.AntClassLoader;
4   
5   import java.net.URL;
6   import java.net.URLClassLoader;
7   import java.net.MalformedURLException;
8   import java.io.File;
9   import java.util.StringTokenizer;
10  import java.util.List;
11  import java.util.ArrayList;
12  import java.util.Iterator;
13  
14  /**
15   * A class loader used to ensure that classpath URLs added in JUnit tests
16   * aren't included in subsequent JUnit tests.
17   */
18  public class JDOConfigTestClassLoader extends URLClassLoader {
19  
20      public JDOConfigTestClassLoader(
21              String partialPathToIgnore,
22              ClassLoader unparent
23      ) {
24          this(new String[]{partialPathToIgnore}, unparent);
25      }
26  
27      public JDOConfigTestClassLoader(
28              String[] partialPathsToIgnore,
29              ClassLoader unparent
30      ) {
31          super(new URL[]{}, null);
32          
33          if (unparent instanceof URLClassLoader) {
34              addNonTestURLs(
35                      partialPathsToIgnore == null
36                              ? new String[]{}
37                              : partialPathsToIgnore,
38                      (URLClassLoader) unparent);
39          }
40          else if (unparent instanceof AntClassLoader) {
41              addNonTestURLs(
42                      partialPathsToIgnore == null
43                              ? new String[]{}
44                              : partialPathsToIgnore,
45                      (AntClassLoader) unparent);
46          }
47          else {
48              throw new RuntimeException(
49                      "unknown ClassLoader type: "
50                              + unparent.getClass().getName());
51          }
52      }
53  
54      // HACK:  need to identify a better way of controlling test classpath
55      protected void addNonTestURLs(
56              String[] partialPathsToIgnore,
57              URLClassLoader unparent
58      ) {
59          URL[] urls = unparent.getURLs();
60          for (int i = 0; i < urls.length; i++) {
61              URL url = urls[i];
62              String urlString = url.toString();
63              for (int j = 0; j < partialPathsToIgnore.length; j++) {
64                  if (urlString.indexOf(partialPathsToIgnore[j]) == -1) {
65                      addURL(url);
66                  }
67              }
68          }
69      }
70  
71      protected void addNonTestURLs(
72              String[] partialPathsToIgnore,
73              AntClassLoader unparent
74      ) {
75          List<String> elements = new ArrayList<String>();
76          String classpath = unparent.getClasspath();
77          StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
78          while (st.hasMoreTokens()) {
79              String nextToken = st.nextToken();
80              if(!nextToken.endsWith(".jar")) {
81                  nextToken = nextToken.concat(File.separator);
82              }
83              elements.add("file://" + nextToken);
84          }
85          Iterator<String> i = elements.iterator();
86          while (i.hasNext()) {
87              String element = i.next();
88              for (int j = 0; j < partialPathsToIgnore.length; j++) {
89                  if (element.indexOf(partialPathsToIgnore[j]) == -1) {
90                      try {
91                          addURL(new URL(element));
92                      }
93                      catch (MalformedURLException e) {
94                          throw new RuntimeException(e);
95                      }
96                  }
97              }
98          }
99      }
100 }