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 | |
jbudorick
2015/04/02 14:21:43
If this is specific to Robolectric, should that be
mikecase (-- gone --)
2015/04/02 17:11:40
Done.
| |
17 * in the Java classpath. | |
18 */ | |
19 public class ClasspathDependencyResolver implements DependencyResolver { | |
20 private static final Pattern COLON = Pattern.compile(":"); | |
21 private final String[] mClassPathJars; | |
22 | |
23 /** | |
24 * Creates a {@link ClasspathDependencyResolver}. | |
25 */ | |
26 public ClasspathDependencyResolver() { | |
27 super(); | |
jbudorick
2015/04/02 14:21:43
I _think_ this is unnecessary.
mikecase (-- gone --)
2015/04/02 17:11:39
Done.
| |
28 mClassPathJars = COLON.split(System.getProperty("java.class.path")); | |
29 } | |
30 | |
31 /** | |
32 * Returns the {@link URL} for a Robolectric dependency. It looks through th e jars | |
33 * in the classpath to find the dependency's filepath. | |
34 */ | |
35 @Override | |
36 public URL getLocalArtifactUrl(DependencyJar dependency) { | |
37 // Jar filenames are constructed identically to how they are built in Ro bolectric's | |
38 // own LocalDependencyResolver. | |
39 String dependencyJar = dependency.getArtifactId() + "-" + dependency.get Version() + "." | |
40 + dependency.getType(); | |
41 | |
42 for (String jarPath : mClassPathJars) { | |
43 if (jarPath.endsWith(dependencyJar)) { | |
44 return fileToUrl(new File(jarPath)); | |
45 } | |
46 } | |
47 throw new IllegalStateException( | |
48 String.format("Robolectric jar %s was not found in classpath.", dependencyJar)); | |
49 } | |
50 | |
51 /** | |
52 * Returns the {@link URL} for a list of Robolectric dependencies. | |
53 */ | |
54 @Override | |
55 public URL[] getLocalArtifactUrls(DependencyJar... dependencies) { | |
56 URL[] urls = new URL[dependencies.length]; | |
57 | |
58 for (int i = 0; i < dependencies.length; i++) { | |
59 urls[i] = getLocalArtifactUrl(dependencies[i]); | |
60 } | |
61 | |
62 return urls; | |
63 } | |
64 | |
65 private static URL fileToUrl(File file) { | |
66 try { | |
67 return file.toURI().toURL(); | |
68 } catch (MalformedURLException e) { | |
69 throw new IllegalArgumentException( | |
70 String.format("File \"%s\" cannot be represented as a URL: % s", file, e)); | |
71 } | |
72 } | |
73 } | |
OLD | NEW |