OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.testing.local; |
| 6 |
| 7 import org.robolectric.DependencyJar; |
| 8 import org.robolectric.DependencyResolver; |
| 9 |
| 10 import java.io.File; |
| 11 import java.net.MalformedURLException; |
| 12 import java.net.URL; |
| 13 import java.util.regex.Pattern; |
| 14 |
| 15 /** |
| 16 * A Robolectric dependency resolver that looks for the Robolectric dependencies |
| 17 * in the Java classpath. |
| 18 */ |
| 19 public class RobolectricClasspathDependencyResolver implements DependencyResolve
r { |
| 20 private static final Pattern COLON = Pattern.compile(":"); |
| 21 private final String[] mClassPathJars; |
| 22 |
| 23 /** |
| 24 * Creates a {@link ClasspathDependencyResolver}. |
| 25 */ |
| 26 public RobolectricClasspathDependencyResolver() { |
| 27 mClassPathJars = COLON.split(System.getProperty("java.class.path")); |
| 28 } |
| 29 |
| 30 /** |
| 31 * Returns the {@link URL} for a Robolectric dependency. It looks through th
e jars |
| 32 * in the classpath to find the dependency's filepath. |
| 33 */ |
| 34 @Override |
| 35 public URL getLocalArtifactUrl(DependencyJar dependency) { |
| 36 // Jar filenames are constructed identically to how they are built in Ro
bolectric's |
| 37 // own LocalDependencyResolver. |
| 38 String dependencyJar = dependency.getArtifactId() + "-" + dependency.get
Version() + "." |
| 39 + dependency.getType(); |
| 40 |
| 41 for (String jarPath : mClassPathJars) { |
| 42 if (jarPath.endsWith(dependencyJar)) { |
| 43 return fileToUrl(new File(jarPath)); |
| 44 } |
| 45 } |
| 46 throw new IllegalStateException( |
| 47 String.format("Robolectric jar %s was not found in classpath.",
dependencyJar)); |
| 48 } |
| 49 |
| 50 /** |
| 51 * Returns the {@link URL} for a list of Robolectric dependencies. |
| 52 */ |
| 53 @Override |
| 54 public URL[] getLocalArtifactUrls(DependencyJar... dependencies) { |
| 55 URL[] urls = new URL[dependencies.length]; |
| 56 |
| 57 for (int i = 0; i < dependencies.length; i++) { |
| 58 urls[i] = getLocalArtifactUrl(dependencies[i]); |
| 59 } |
| 60 |
| 61 return urls; |
| 62 } |
| 63 |
| 64 private static URL fileToUrl(File file) { |
| 65 try { |
| 66 return file.toURI().toURL(); |
| 67 } catch (MalformedURLException e) { |
| 68 throw new IllegalArgumentException( |
| 69 String.format("File \"%s\" cannot be represented as a URL: %
s", file, e)); |
| 70 } |
| 71 } |
| 72 } |
OLD | NEW |