1 package org.robolectric.internal.dependency; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.junit.Assert.fail; 5 import static org.mockito.Mockito.mock; 6 import static org.mockito.Mockito.when; 7 8 import java.io.File; 9 import java.io.IOException; 10 import java.io.Writer; 11 import java.net.URL; 12 import java.nio.charset.StandardCharsets; 13 import java.nio.file.Files; 14 import java.nio.file.Path; 15 import java.nio.file.Paths; 16 import java.util.Properties; 17 import org.junit.Before; 18 import org.junit.Rule; 19 import org.junit.Test; 20 import org.junit.rules.TemporaryFolder; 21 import org.junit.runner.RunWith; 22 import org.junit.runners.JUnit4; 23 24 @RunWith(JUnit4.class) 25 public class PropertiesDependencyResolverTest { 26 27 @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); 28 private DependencyJar exampleDep; 29 private DependencyResolver mock; 30 private boolean cColonBackslash; 31 32 @Before setUp()33 public void setUp() throws Exception { 34 exampleDep = new DependencyJar("com.group", "example", "1.3", null); 35 mock = mock(DependencyResolver.class); 36 cColonBackslash = File.separatorChar == '\\'; 37 } 38 39 @Test whenAbsolutePathIsProvidedInProperties_shouldReturnFileUrl()40 public void whenAbsolutePathIsProvidedInProperties_shouldReturnFileUrl() throws Exception { 41 String absolutePath = cColonBackslash ? "c:\\tmp\\file.jar" : "/tmp/file.jar"; 42 DependencyResolver resolver = 43 new PropertiesDependencyResolver( 44 propsFile("com.group:example:1.3", new File(absolutePath).getAbsoluteFile()), mock); 45 46 URL url = resolver.getLocalArtifactUrl(exampleDep); 47 if (cColonBackslash) { 48 assertThat(url).isEqualTo(Paths.get("c:\\tmp\\file.jar").toUri().toURL()); 49 } else { 50 assertThat(url).isEqualTo(Paths.get("/tmp/file.jar").toUri().toURL()); 51 } 52 } 53 54 @Test whenRelativePathIsProvidedInProperties_shouldReturnFileUrl()55 public void whenRelativePathIsProvidedInProperties_shouldReturnFileUrl() throws Exception { 56 DependencyResolver resolver = 57 new PropertiesDependencyResolver( 58 propsFile("com.group:example:1.3", new File("path", "1")), mock); 59 60 URL url = resolver.getLocalArtifactUrl(exampleDep); 61 assertThat(url) 62 .isEqualTo(temporaryFolder.getRoot().toPath().resolve("path").resolve("1").toUri().toURL()); 63 } 64 65 @Test whenMissingFromProperties_shouldDelegate()66 public void whenMissingFromProperties_shouldDelegate() throws Exception { 67 DependencyResolver resolver = 68 new PropertiesDependencyResolver(propsFile("nothing", new File("interesting")), mock); 69 70 when(mock.getLocalArtifactUrl(exampleDep)).thenReturn(new URL("file:///path/3")); 71 URL url = resolver.getLocalArtifactUrl(exampleDep); 72 assertThat(url).isEqualTo(new URL("file:///path/3")); 73 } 74 75 @Test whenDelegateIsNull_shouldGiveGoodMessage()76 public void whenDelegateIsNull_shouldGiveGoodMessage() throws Exception { 77 DependencyResolver resolver = 78 new PropertiesDependencyResolver(propsFile("nothing", new File("interesting")), null); 79 80 try { 81 resolver.getLocalArtifactUrl(exampleDep); 82 fail("should have failed"); 83 } catch (Exception e) { 84 assertThat(e.getMessage()).contains("no artifacts found for " + exampleDep); 85 } 86 } 87 88 ////////////////// 89 propsFile(String key, File value)90 private Path propsFile(String key, File value) throws IOException { 91 Properties properties = new Properties(); 92 properties.setProperty(key, value.toString()); 93 return propsFile(properties); 94 } 95 propsFile(Properties contents)96 private Path propsFile(Properties contents) throws IOException { 97 File file = temporaryFolder.newFile("file.properties"); 98 try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) { 99 contents.store(out, "for tests"); 100 } 101 return file.toPath(); 102 } 103 } 104