Best Mockito code snippet using org.mockitoutil.ClassLoaders.pathsToURLs
Source:Math_72_rank-2_new.java
...44 privateCopyPrefixes.addAll(asList(privatePrefixes));45 return this;46 }47 public IsolatedURLClassLoaderBuilder withCodeSourceUrls(String... urls) {48 codeSourceUrls.addAll(pathsToURLs(urls));49 return this;50 }51 public IsolatedURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {52 for (Class<?> clazz : classes) {53 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));54 }55 return this;56 }57 public IsolatedURLClassLoaderBuilder withCurrentCodeSourceUrls() {58 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));59 return this;60 }61 public ClassLoader build() {62 return new LocalIsolatedURLClassLoader(63 jdkClassLoader(),64 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),65 privateCopyPrefixes66 );67 }68 }69 static class LocalIsolatedURLClassLoader extends URLClassLoader {70 private final ArrayList<String> privateCopyPrefixes;71 public LocalIsolatedURLClassLoader(ClassLoader classLoader, URL[] urls, ArrayList<String> privateCopyPrefixes) {72 super(urls, classLoader);73 this.privateCopyPrefixes = privateCopyPrefixes;74 }75 @Override76 public Class<?> findClass(String name) throws ClassNotFoundException {77 if(classShouldBePrivate(name)) return super.findClass(name);78 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);79 }80 private boolean classShouldBePrivate(String name) {81 for (String prefix : privateCopyPrefixes) {82 if (name.startsWith(prefix)) return true;83 }84 return false;85 }86 }87 public static class ExcludingURLClassLoaderBuilder extends ClassLoaders {88 private final ArrayList<String> privateCopyPrefixes = new ArrayList<String>();89 private final ArrayList<URL> codeSourceUrls = new ArrayList<URL>();90 public ExcludingURLClassLoaderBuilder without(String... privatePrefixes) {91 privateCopyPrefixes.addAll(asList(privatePrefixes));92 return this;93 }94 public ExcludingURLClassLoaderBuilder withCodeSourceUrls(String... urls) {95 codeSourceUrls.addAll(pathsToURLs(urls));96 return this;97 }98 public ExcludingURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {99 for (Class<?> clazz : classes) {100 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));101 }102 return this;103 }104 public ExcludingURLClassLoaderBuilder withCurrentCodeSourceUrls() {105 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));106 return this;107 }108 public ClassLoader build() {109 return new LocalExcludingURLClassLoader(110 jdkClassLoader(),111 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),112 privateCopyPrefixes113 );114 }115 }116 static class LocalExcludingURLClassLoader extends URLClassLoader {117 private final ArrayList<String> privateCopyPrefixes;118 public LocalExcludingURLClassLoader(ClassLoader classLoader, URL[] urls, ArrayList<String> privateCopyPrefixes) {119 super(urls, classLoader);120 this.privateCopyPrefixes = privateCopyPrefixes;121 }122 @Override123 public Class<?> findClass(String name) throws ClassNotFoundException {124 if(classShouldBePrivate(name)) throw new ClassNotFoundException("classes with prefix : " + privateCopyPrefixes + " are excluded");125 return super.findClass(name);126 }127 private boolean classShouldBePrivate(String name) {128 for (String prefix : privateCopyPrefixes) {129 if (name.startsWith(prefix)) return true;130 }131 return false;132 }133 }134 public static class InMemoryClassLoaderBuilder extends ClassLoaders {135 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();136 public InMemoryClassLoaderBuilder withParent(ClassLoader parent) {137 this.parent = parent;138 return this;139 }140 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {141 inMemoryClassObjects.put(name, classDefinition);142 return this;143 }144 public ClassLoader build() {145 return new InMemoryClassLoader(parent, inMemoryClassObjects);146 }147 }148 static class InMemoryClassLoader extends ClassLoader {149 public static final String SCHEME = "mem";150 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();151 public InMemoryClassLoader(ClassLoader parent, Map<String, byte[]> inMemoryClassObjects) {152 super(parent);153 this.inMemoryClassObjects = inMemoryClassObjects;154 }155 protected Class findClass(String name) throws ClassNotFoundException {156 byte[] classDefinition = inMemoryClassObjects.get(name);157 if (classDefinition != null) {158 return defineClass(name, classDefinition, 0, classDefinition.length);159 }160 throw new ClassNotFoundException(name);161 }162 @Override163 public Enumeration<URL> getResources(String ignored) throws IOException {164 return inMemoryOnly();165 }166 private Enumeration<URL> inMemoryOnly() {167 final Set<String> names = inMemoryClassObjects.keySet();168 return new Enumeration<URL>() {169 private final MemHandler memHandler = new MemHandler(InMemoryClassLoader.this);170 private final Iterator<String> it = names.iterator();171 public boolean hasMoreElements() {172 return it.hasNext();173 }174 public URL nextElement() {175 try {176 return new URL(null, SCHEME + ":" + it.next(), memHandler);177 } catch (MalformedURLException rethrown) {178 throw new IllegalStateException(rethrown);179 }180 }181 };182 }183 }184 public static class MemHandler extends URLStreamHandler {185 private InMemoryClassLoader inMemoryClassLoader;186 public MemHandler(InMemoryClassLoader inMemoryClassLoader) {187 this.inMemoryClassLoader = inMemoryClassLoader;188 }189 @Override190 protected URLConnection openConnection(URL url) throws IOException {191 return new MemURLConnection(url, inMemoryClassLoader);192 }193 private static class MemURLConnection extends URLConnection {194 private final InMemoryClassLoader inMemoryClassLoader;195 private String qualifiedName;196 public MemURLConnection(URL url, InMemoryClassLoader inMemoryClassLoader) {197 super(url);198 this.inMemoryClassLoader = inMemoryClassLoader;199 qualifiedName = url.getPath();200 }201 @Override202 public void connect() throws IOException { }203 @Override204 public InputStream getInputStream() throws IOException {205 return new ByteArrayInputStream(inMemoryClassLoader.inMemoryClassObjects.get(qualifiedName));206 }207 }208 }209 protected URL obtainClassPathOf(String className) {210 String path = className.replace('.', '/') + ".class";211 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();212 try {213 return new URL(url.substring(0, url.length() - path.length()));214 } catch (MalformedURLException e) {215 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);216 }217 }218 protected List<URL> pathsToURLs(String... codeSourceUrls) {219 return pathsToURLs(Arrays.asList(codeSourceUrls));220 }221 private List<URL> pathsToURLs(List<String> codeSourceUrls) {222 ArrayList<URL> urls = new ArrayList<URL>(codeSourceUrls.size());223 for (String codeSourceUrl : codeSourceUrls) {224 URL url = pathToUrl(codeSourceUrl);225 urls.add(url);226 }227 return urls;228 }229 private URL pathToUrl(String path) {230 try {231 return new File(path).getAbsoluteFile().toURI().toURL();232 } catch (MalformedURLException e) {233 throw new IllegalArgumentException("Path is malformed", e);234 }235 }...
Source:Math_72_rank-1_new.java
...44 privateCopyPrefixes.addAll(asList(privatePrefixes));45 return this;46 }47 public IsolatedURLClassLoaderBuilder withCodeSourceUrls(String... urls) {48 codeSourceUrls.addAll(pathsToURLs(urls));49 return this;50 }51 public IsolatedURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {52 for (Class<?> clazz : classes) {53 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));54 }55 return this;56 }57 public IsolatedURLClassLoaderBuilder withCurrentCodeSourceUrls() {58 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));59 return this;60 }61 public ClassLoader build() {62 return new LocalIsolatedURLClassLoader(63 jdkClassLoader(),64 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),65 privateCopyPrefixes66 );67 }68 }69 static class LocalIsolatedURLClassLoader extends URLClassLoader {70 private final ArrayList<String> privateCopyPrefixes;71 public LocalIsolatedURLClassLoader(ClassLoader classLoader, URL[] urls, ArrayList<String> privateCopyPrefixes) {72 super(urls, classLoader);73 this.privateCopyPrefixes = privateCopyPrefixes;74 }75 @Override76 public Class<?> findClass(String name) throws ClassNotFoundException {77 if(classShouldBePrivate(name)) return super.findClass(name);78 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);79 }80 private boolean classShouldBePrivate(String name) {81 for (String prefix : privateCopyPrefixes) {82 if (name.startsWith(prefix)) return true;83 }84 return false;85 }86 }87 public static class ExcludingURLClassLoaderBuilder extends ClassLoaders {88 private final ArrayList<String> privateCopyPrefixes = new ArrayList<String>();89 private final ArrayList<URL> codeSourceUrls = new ArrayList<URL>();90 public ExcludingURLClassLoaderBuilder without(String... privatePrefixes) {91 privateCopyPrefixes.addAll(asList(privatePrefixes));92 return this;93 }94 public ExcludingURLClassLoaderBuilder withCodeSourceUrls(String... urls) {95 codeSourceUrls.addAll(pathsToURLs(urls));96 return this;97 }98 public ExcludingURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {99 for (Class<?> clazz : classes) {100 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));101 }102 return this;103 }104 public ExcludingURLClassLoaderBuilder withCurrentCodeSourceUrls() {105 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));106 return this;107 }108 public ClassLoader build() {109 return new LocalExcludingURLClassLoader(110 jdkClassLoader(),111 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),112 privateCopyPrefixes113 );114 }115 }116 static class LocalExcludingURLClassLoader extends URLClassLoader {117 private final ArrayList<String> privateCopyPrefixes;118 public LocalExcludingURLClassLoader(ClassLoader classLoader, URL[] urls, ArrayList<String> privateCopyPrefixes) {119 super(urls, classLoader);120 this.privateCopyPrefixes = privateCopyPrefixes;121 }122 @Override123 public Class<?> findClass(String name) throws ClassNotFoundException {124 if(classShouldBePrivate(name)) throw new ClassNotFoundException("classes with prefix : " + privateCopyPrefixes + " are excluded");125 return super.findClass(name);126 }127 private boolean classShouldBePrivate(String name) {128 for (String prefix : privateCopyPrefixes) {129 if (name.startsWith(prefix)) return true;130 }131 return false;132 }133 }134 public static class InMemoryClassLoaderBuilder extends ClassLoaders {135 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();136 public InMemoryClassLoaderBuilder withParent(ClassLoader parent) {137 this.parent = parent;138 return this;139 }140 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {141 inMemoryClassObjects.put(name, classDefinition);142 return this;143 }144 public ClassLoader build() {145 return new InMemoryClassLoader(parent, inMemoryClassObjects);146 }147 }148 static class InMemoryClassLoader extends ClassLoader {149 public static final String SCHEME = "mem";150 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();151 public InMemoryClassLoader(ClassLoader parent, Map<String, byte[]> inMemoryClassObjects) {152 super(parent);153 this.inMemoryClassObjects = inMemoryClassObjects;154 }155 protected Class findClass(String name) throws ClassNotFoundException {156 byte[] classDefinition = inMemoryClassObjects.get(name);157 if (classDefinition != null) {158 return defineClass(name, classDefinition, 0, classDefinition.length);159 }160 throw new ClassNotFoundException(name);161 }162 @Override163 public Enumeration<URL> getResources(String ignored) throws IOException {164 return inMemoryOnly();165 }166 private Enumeration<URL> inMemoryOnly() {167 final Set<String> names = inMemoryClassObjects.keySet();168 return new Enumeration<URL>() {169 private final MemHandler memHandler = new MemHandler(InMemoryClassLoader.this);170 private final Iterator<String> it = names.iterator();171 public boolean hasMoreElements() {172 return it.hasNext();173 }174 public URL nextElement() {175 try {176 return new URL(null, SCHEME + ":" + it.next(), memHandler);177 } catch (MalformedURLException rethrown) {178 throw new IllegalStateException(rethrown);179 }180 }181 };182 }183 }184 public static class MemHandler extends URLStreamHandler {185 private InMemoryClassLoader inMemoryClassLoader;186 public MemHandler(InMemoryClassLoader inMemoryClassLoader) {187 this.inMemoryClassLoader = inMemoryClassLoader;188 }189 @Override190 protected URLConnection openConnection(URL url) throws IOException {191 return new MemURLConnection(url, inMemoryClassLoader);192 }193 private static class MemURLConnection extends URLConnection {194 private final InMemoryClassLoader inMemoryClassLoader;195 private String qualifiedName;196 public MemURLConnection(URL url, InMemoryClassLoader inMemoryClassLoader) {197 super(url);198 this.inMemoryClassLoader = inMemoryClassLoader;199 qualifiedName = url.getPath();200 }201 @Override202 public void connect() throws IOException { }203 @Override204 public InputStream getInputStream() throws IOException {205 return new ByteArrayInputStream(inMemoryClassLoader.inMemoryClassObjects.get(qualifiedName));206 }207 }208 }209 protected URL obtainClassPathOf(String className) {210 String path = className.replace('.', '/') + ".class";211 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();212 try {213 return new URL(url.substring(0, url.length() - path.length()));214 } catch (MalformedURLException e) {215 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);216 }217 }218 protected List<URL> pathsToURLs(String... codeSourceUrls) {219 return pathsToURLs(Arrays.asList(codeSourceUrls));220 }221 private List<URL> pathsToURLs(List<String> codeSourceUrls) {222 ArrayList<URL> urls = new ArrayList<URL>(codeSourceUrls.size());223 for (String codeSourceUrl : codeSourceUrls) {224 URL url = pathToUrl(codeSourceUrl);225 urls.add(url);226 }227 return urls;228 }229 private URL pathToUrl(String path) {230 try {231 return new File(path).getAbsoluteFile().toURI().toURL();232 } catch (MalformedURLException e) {233 throw new IllegalArgumentException("Path is malformed", e);234 }235 }...
Source:Math_72_rank-2_old.java
...23 privateCopyPrefixes.addAll(asList(privatePrefixes));24 return this;25 }26 public IsolatedURLClassLoaderBuilder withCodeSourceUrls(String... urls) {27 codeSourceUrls.addAll(pathsToURLs(urls));28 return this;29 }30 public IsolatedURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {31 for (Class<?> clazz : classes) {32 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));33 }34 return this;35 }36 public IsolatedURLClassLoaderBuilder withCurrentCodeSourceUrls() {37 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));38 return this;39 }40 public ClassLoader build() {41 return new LocalIsolatedURLClassLoader(42 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),43 privateCopyPrefixes44 );45 }46 }47 static class LocalIsolatedURLClassLoader extends URLClassLoader {48 private final ArrayList<String> privateCopyPrefixes;49 public LocalIsolatedURLClassLoader(URL[] urls, ArrayList<String> privateCopyPrefixes) {50 super(urls, null);51 this.privateCopyPrefixes = privateCopyPrefixes;52 }53 @Override54 public Class<?> findClass(String name) throws ClassNotFoundException {55 if(classShouldBePrivate(name)) return super.findClass(name);56 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);57 }58 private boolean classShouldBePrivate(String name) {59 for (String prefix : privateCopyPrefixes) {60 if (name.startsWith(prefix)) return true;61 }62 return false;63 }64 }65 public static class ExcludingURLClassLoaderBuilder extends ClassLoaders {66 private final ArrayList<String> privateCopyPrefixes = new ArrayList<String>();67 private final ArrayList<URL> codeSourceUrls = new ArrayList<URL>();68 public ExcludingURLClassLoaderBuilder without(String... privatePrefixes) {69 privateCopyPrefixes.addAll(asList(privatePrefixes));70 return this;71 }72 public ExcludingURLClassLoaderBuilder withCodeSourceUrls(String... urls) {73 codeSourceUrls.addAll(pathsToURLs(urls));74 return this;75 }76 public ExcludingURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {77 for (Class<?> clazz : classes) {78 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));79 }80 return this;81 }82 public ExcludingURLClassLoaderBuilder withCurrentCodeSourceUrls() {83 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));84 return this;85 }86 public ClassLoader build() {87 return new LocalExcludingURLClassLoader(88 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),89 privateCopyPrefixes90 );91 }92 }93 static class LocalExcludingURLClassLoader extends URLClassLoader {94 private final ArrayList<String> privateCopyPrefixes;95 public LocalExcludingURLClassLoader(URL[] urls, ArrayList<String> privateCopyPrefixes) {96 super(urls, null);97 this.privateCopyPrefixes = privateCopyPrefixes;98 }99 @Override100 public Class<?> findClass(String name) throws ClassNotFoundException {101 if(classShouldBePrivate(name)) throw new ClassNotFoundException("classes with prefix : " + privateCopyPrefixes + " are excluded");102 return super.findClass(name);103 }104 private boolean classShouldBePrivate(String name) {105 for (String prefix : privateCopyPrefixes) {106 if (name.startsWith(prefix)) return true;107 }108 return false;109 }110 }111 public static class InMemoryClassLoaderBuilder extends ClassLoaders {112 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();113 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {114 inMemoryClassObjects.put(name, classDefinition);115 return this;116 }117 public ClassLoader build() {118 return new InMemoryClassLoader(inMemoryClassObjects);119 }120 }121 static class InMemoryClassLoader extends ClassLoader {122 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();123 public InMemoryClassLoader(Map<String, byte[]> inMemoryClassObjects) {124 this.inMemoryClassObjects = inMemoryClassObjects;125 }126 protected Class findClass(String name) throws ClassNotFoundException {127 byte[] classDefinition = inMemoryClassObjects.get(name);128 if (classDefinition != null) {129 return defineClass(name, classDefinition, 0, classDefinition.length);130 }131 throw new ClassNotFoundException(name);132 }133 }134 protected URL obtainClassPathOf(String className) {135 String path = className.replace('.', '/') + ".class";136 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();137 try {138 return new URL(url.substring(0, url.length() - path.length()));139 } catch (MalformedURLException e) {140 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);141 }142 }143 protected List<URL> pathsToURLs(String... codeSourceUrls) {144 return pathsToURLs(Arrays.asList(codeSourceUrls));145 }146 private List<URL> pathsToURLs(List<String> codeSourceUrls) {147 ArrayList<URL> urls = new ArrayList<URL>(codeSourceUrls.size());148 for (String codeSourceUrl : codeSourceUrls) {149 URL url = pathToUrl(codeSourceUrl);150 urls.add(url);151 }152 return urls;153 }154 private URL pathToUrl(String path) {155 try {156 return new File(path).getAbsoluteFile().toURI().toURL();157 } catch (MalformedURLException e) {158 throw new IllegalArgumentException("Path is malformed", e);159 }160 }...
Source:Math_72_rank-1_old.java
...23 privateCopyPrefixes.addAll(asList(privatePrefixes));24 return this;25 }26 public IsolatedURLClassLoaderBuilder withCodeSourceUrls(String... urls) {27 codeSourceUrls.addAll(pathsToURLs(urls));28 return this;29 }30 public IsolatedURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {31 for (Class<?> clazz : classes) {32 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));33 }34 return this;35 }36 public IsolatedURLClassLoaderBuilder withCurrentCodeSourceUrls() {37 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));38 return this;39 }40 public ClassLoader build() {41 return new LocalIsolatedURLClassLoader(42 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),43 privateCopyPrefixes44 );45 }46 }47 static class LocalIsolatedURLClassLoader extends URLClassLoader {48 private final ArrayList<String> privateCopyPrefixes;49 public LocalIsolatedURLClassLoader(URL[] urls, ArrayList<String> privateCopyPrefixes) {50 super(urls, null);51 this.privateCopyPrefixes = privateCopyPrefixes;52 }53 @Override54 public Class<?> findClass(String name) throws ClassNotFoundException {55 if(classShouldBePrivate(name)) return super.findClass(name);56 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);57 }58 private boolean classShouldBePrivate(String name) {59 for (String prefix : privateCopyPrefixes) {60 if (name.startsWith(prefix)) return true;61 }62 return false;63 }64 }65 public static class ExcludingURLClassLoaderBuilder extends ClassLoaders {66 private final ArrayList<String> privateCopyPrefixes = new ArrayList<String>();67 private final ArrayList<URL> codeSourceUrls = new ArrayList<URL>();68 public ExcludingURLClassLoaderBuilder without(String... privatePrefixes) {69 privateCopyPrefixes.addAll(asList(privatePrefixes));70 return this;71 }72 public ExcludingURLClassLoaderBuilder withCodeSourceUrls(String... urls) {73 codeSourceUrls.addAll(pathsToURLs(urls));74 return this;75 }76 public ExcludingURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {77 for (Class<?> clazz : classes) {78 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));79 }80 return this;81 }82 public ExcludingURLClassLoaderBuilder withCurrentCodeSourceUrls() {83 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));84 return this;85 }86 public ClassLoader build() {87 return new LocalExcludingURLClassLoader(88 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),89 privateCopyPrefixes90 );91 }92 }93 static class LocalExcludingURLClassLoader extends URLClassLoader {94 private final ArrayList<String> privateCopyPrefixes;95 public LocalExcludingURLClassLoader(URL[] urls, ArrayList<String> privateCopyPrefixes) {96 super(urls, null);97 this.privateCopyPrefixes = privateCopyPrefixes;98 }99 @Override100 public Class<?> findClass(String name) throws ClassNotFoundException {101 if(classShouldBePrivate(name)) throw new ClassNotFoundException("classes with prefix : " + privateCopyPrefixes + " are excluded");102 return super.findClass(name);103 }104 private boolean classShouldBePrivate(String name) {105 for (String prefix : privateCopyPrefixes) {106 if (name.startsWith(prefix)) return true;107 }108 return false;109 }110 }111 public static class InMemoryClassLoaderBuilder extends ClassLoaders {112 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();113 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {114 inMemoryClassObjects.put(name, classDefinition);115 return this;116 }117 public ClassLoader build() {118 return new InMemoryClassLoader(inMemoryClassObjects);119 }120 }121 static class InMemoryClassLoader extends ClassLoader {122 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();123 public InMemoryClassLoader(Map<String, byte[]> inMemoryClassObjects) {124 this.inMemoryClassObjects = inMemoryClassObjects;125 }126 protected Class findClass(String name) throws ClassNotFoundException {127 byte[] classDefinition = inMemoryClassObjects.get(name);128 if (classDefinition != null) {129 return defineClass(name, classDefinition, 0, classDefinition.length);130 }131 throw new ClassNotFoundException(name);132 }133 }134 protected URL obtainClassPathOf(String className) {135 String path = className.replace('.', '/') + ".class";136 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();137 try {138 return new URL(url.substring(0, url.length() - path.length()));139 } catch (MalformedURLException e) {140 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);141 }142 }143 protected List<URL> pathsToURLs(String... codeSourceUrls) {144 return pathsToURLs(Arrays.asList(codeSourceUrls));145 }146 private List<URL> pathsToURLs(List<String> codeSourceUrls) {147 ArrayList<URL> urls = new ArrayList<URL>(codeSourceUrls.size());148 for (String codeSourceUrl : codeSourceUrls) {149 URL url = pathToUrl(codeSourceUrl);150 urls.add(url);151 }152 return urls;153 }154 private URL pathToUrl(String path) {155 try {156 return new File(path).getAbsoluteFile().toURI().toURL();157 } catch (MalformedURLException e) {158 throw new IllegalArgumentException("Path is malformed", e);159 }160 }...
Source:ClassLoaders.java
...24 privateCopyPrefixes.addAll(asList(privatePrefixes));25 return this;26 }27 public IsolatedURLClassLoaderBuilder withCodeSourceUrls(String... urls) {28 codeSourceUrls.addAll(pathsToURLs(urls));29 return this;30 }31 public IsolatedURLClassLoaderBuilder withCurrentCodeSourceUrls() {32 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));33 return this;34 }35 public ClassLoader build() {36 return new LocalIsolatedURLClassLoader(37 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),38 privateCopyPrefixes39 );40 }41 }42 static class LocalIsolatedURLClassLoader extends URLClassLoader {43 private final ArrayList<String> privateCopyPrefixes;44 public LocalIsolatedURLClassLoader(URL[] urls, ArrayList<String> privateCopyPrefixes) {45 super(urls, null);46 this.privateCopyPrefixes = privateCopyPrefixes;47 }48 @Override49 public Class<?> findClass(String name) throws ClassNotFoundException {50 if(classShouldBePrivate(name)) return super.findClass(name);51 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);52 }53 private boolean classShouldBePrivate(String name) {54 for (String prefix : privateCopyPrefixes) {55 if (name.startsWith(prefix)) return true;56 }57 return false;58 }59 }60 public static class InMemoryClassLoaderBuilder extends ClassLoaders {61 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();62 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {63 inMemoryClassObjects.put(name, classDefinition);64 return this;65 }66 public ClassLoader build() {67 return new InMemoryClassLoader(inMemoryClassObjects);68 }69 }70 static class InMemoryClassLoader extends ClassLoader {71 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();72 public InMemoryClassLoader(Map<String, byte[]> inMemoryClassObjects) {73 this.inMemoryClassObjects = inMemoryClassObjects;74 }75 protected Class findClass(String name) throws ClassNotFoundException {76 byte[] classDefinition = inMemoryClassObjects.get(name);77 if (classDefinition != null) {78 return defineClass(name, classDefinition, 0, classDefinition.length);79 }80 throw new ClassNotFoundException(name);81 }82 }83 protected URL obtainClassPathOf(String className) {84 String path = className.replace('.', '/') + ".class";85 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();86 try {87 return new URL(url.substring(0, url.length() - path.length()));88 } catch (MalformedURLException e) {89 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);90 }91 }92 protected List<URL> pathsToURLs(String... codeSourceUrls) {93 return pathsToURLs(Arrays.asList(codeSourceUrls));94 }95 private List<URL> pathsToURLs(List<String> codeSourceUrls) {96 ArrayList<URL> urls = new ArrayList<URL>(codeSourceUrls.size());97 for (String codeSourceUrl : codeSourceUrls) {98 URL url = pathToUrl(codeSourceUrl);99 urls.add(url);100 }101 return urls;102 }103 private URL pathToUrl(String path) {104 try {105 return new File(path).getAbsoluteFile().toURI().toURL();106 } catch (MalformedURLException e) {107 throw new IllegalArgumentException("Path is malformed", e);108 }109 }...
pathsToURLs
Using AI Code Generation
1package org.mockitoutil;2import java.io.File;3import java.io.IOException;4import java.net.URL;5import java.net.URLClassLoader;6import java.util.ArrayList;7import java.util.List;8public class ClassLoaders {9 public static URLClassLoader inMemoryClassLoader(Class<?>... classes) throws IOException {10 URLClassLoader classLoader = new URLClassLoader(toURLs(classes));11 return classLoader;12 }13 public static URL[] toURLs(Class<?>... classes) throws IOException {14 List<URL> urls = new ArrayList<URL>();15 for (Class<?> clazz : classes) {16 String classResource = clazz.getName().replace('.', '/') + ".class";17 URL classURL = clazz.getClassLoader().getResource(classResource);18 if (classURL == null) {19 throw new IllegalArgumentException("Cannot find class resource " + classResource);20 }21 String classFile = classURL.getFile();22 if (!classFile.endsWith(classResource)) {23 throw new IllegalArgumentException("Class resource " + classResource + " not found in " + classFile);24 }25 String path = classFile.substring(0, classFile.length() - classResource.length());26 if (path.startsWith("file:")) {27 path = path.substring("file:".length());28 }29 urls.add(new File(path).toURI().toURL());30 }31 return urls.toArray(new URL[urls.size()]);32 }33}34import java.io.File;35import java.io.IOException;36import java.net.URL;37import java.net.URLClassLoader;38import org.junit.Test;39import org.mockitoutil.ClassLoaders;40public class ClassLoadersTest {41 public void test() throws IOException {42 URLClassLoader classLoader = ClassLoaders.inMemoryClassLoader(ClassLoadersTest.class);43 System.out.println(classLoader);44 }45}46import java.io.File;47import java.io.IOException;48import java.net.URL;49import java.net.URLClassLoader;50import org.junit.Test;51import org.mockitoutil.ClassLoaders;52public class ClassLoadersTest {53 public void test() throws IOException {54 URLClassLoader classLoader = ClassLoaders.inMemoryClassLoader(ClassLoadersTest.class);55 System.out.println(classLoader);56 }57}
pathsToURLs
Using AI Code Generation
1import org.junit.Test;2import org.junit.runner.RunWith;3import org.junit.runners.JUnit4;4import org.mockitoutil.ClassLoaders;5import java.net.URL;6import java.net.URLClassLoader;7import java.util.List;8import static org.junit.Assert.assertEquals;9import static org.junit.Assert.assertTrue;10import static org.junit.Assert.fail;11@RunWith(JUnit4.class)12public class 1 {13 public void testPathToURLs() {14 String path = "C:\\Users\\user\\Documents\\NetBeansProjects\\Mockito\\src\\org\\mockitoutil\\ClassLoaders.java";15 List<URL> urls = ClassLoaders.pathsToURLs(path);16 assertEquals(1, urls.size());17 assertTrue(urls.get(0).toString().endsWith("ClassLoaders.java"));18 }19}20 at org.junit.Assert.assertEquals(Assert.java:115)21 at org.junit.Assert.assertEquals(Assert.java:144)22 at 1.testPathToURLs(1.java:23)
pathsToURLs
Using AI Code Generation
1import org.junit.*;2import org.junit.runner.*;3import org.junit.runners.*;4import org.junit.runners.Parameterized.*;5import org.mockitoutil.*;6import java.net.*;7import java.util.*;8@RunWith(Parameterized.class)
pathsToURLs
Using AI Code Generation
1package com.mycompany.app;2import java.net.URL;3import java.util.List;4import org.mockitoutil.ClassLoaders;5public class App {6 public static void main(String[] args) {7 List<URL> urls = ClassLoaders.pathsToURLs("C:\\Users\\User\\Desktop\\Mockito\\1.jar");8 System.out.println(urls);9 }10}11package com.mycompany.app;12import java.net.URL;13import java.util.List;14import org.mockitoutil.ClassLoaders;15public class App {16 public static void main(String[] args) {17 List<URL> urls = ClassLoaders.pathsToURLs("C:\\Users\\User\\Desktop\\Mockito\\1.jar", "C:\\Users\\User\\Desktop\\Mockito\\2.jar");18 System.out.println(urls);19 }20}21package com.mycompany.app;22import java.net.URL;23import java.util.List;24import org.mockitoutil.ClassLoaders;25public class App {26 public static void main(String[] args) {27 List<URL> urls = ClassLoaders.pathsToURLs("C:\\Users\\User\\Desktop\\Mockito\\1.jar", "C:\\Users\\User\\Desktop\\Mockito\\2.jar", "C:\\Users\\User\\Desktop\\Mockito\\3.jar");28 System.out.println(urls);29 }30}31package com.mycompany.app;32import java.net.URL;33import java.util.List;34import org.mockitoutil.ClassLoaders;35public class App {36 public static void main(String[] args) {37 List<URL> urls = ClassLoaders.pathsToURLs("C:\\Users\\User\\Desktop\\Mockito\\1.jar", "C:\\Users\\
pathsToURLs
Using AI Code Generation
1import org.junit.*;2import org.mockitoutil.*;3import java.net.*;4import java.util.*;5public class Test1 {6 public void test1() throws Exception {7 List<URL> urls = ClassLoaders.pathsToURLs("c:\\temp\\test1");8 for (URL url : urls) {9 System.out.println(url);10 }11 }12}13import org.junit.*;14import org.mockitoutil.*;15import java.net.*;16import java.util.*;17public class Test2 {18 public void test1() throws Exception {19 List<URL> urls = ClassLoaders.pathsToURLs("c:\\temp\\test1", "c:\\temp\\test2");20 for (URL url : urls) {21 System.out.println(url);22 }23 }24}25import org.junit.*;26import org.mockitoutil.*;27import java.net.*;28import java.util.*;29public class Test3 {30 public void test1() throws Exception {31 List<URL> urls = ClassLoaders.pathsToURLs("c:\\temp\\test1", "c:\\temp\\test2", "c:\\temp\\test3");32 for (URL url : urls) {33 System.out.println(url);34 }35 }36}37import org.junit.*;38import org.mockitoutil.*;39import java.net.*;40import java.util.*;41public class Test4 {42 public void test1() throws Exception {43 List<URL> urls = ClassLoaders.pathsToURLs("c:\\temp\\test1", "c:\\temp\\test2", "c:\\temp\\test3", "c:\\temp\\test4");44 for (URL url : urls) {45 System.out.println(url);46 }47 }48}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!