1 package org.robolectric.internal.dependency; 2 3 import static org.assertj.core.api.Assertions.assertThat; 4 import static org.assertj.core.api.Assertions.fail; 5 import static org.mockito.Mockito.mock; 6 import static org.mockito.Mockito.when; 7 8 import com.google.common.base.Charsets; 9 import com.google.common.io.Files; 10 import java.io.File; 11 import java.io.IOException; 12 import java.net.URL; 13 import org.junit.Before; 14 import org.junit.Rule; 15 import org.junit.Test; 16 import org.junit.rules.TemporaryFolder; 17 import org.junit.runner.RunWith; 18 import org.junit.runners.JUnit4; 19 import org.robolectric.res.Fs; 20 import org.robolectric.res.FsFile; 21 22 @RunWith(JUnit4.class) 23 public class PropertiesDependencyResolverTest { 24 25 @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); 26 private DependencyResolver mock; 27 28 @Before 29 public void setUp() throws Exception { 30 mock = mock(DependencyResolver.class); 31 } 32 33 @Test 34 public void whenAbsolutePathIsProvidedInProperties_shouldReturnFileUrl() throws Exception { 35 DependencyResolver resolver = new PropertiesDependencyResolver( 36 propsFile("com.group\\:example\\:1.3: /path/1\n"), mock); 37 38 URL url = resolver.getLocalArtifactUrl(new DependencyJar("com.group", "example", "1.3", null)); 39 assertThat(url).isEqualTo(new URL("file:///path/1")); 40 } 41 42 @Test 43 public void whenRelativePathIsProvidedInProperties_shouldReturnFileUrl() throws Exception { 44 DependencyResolver resolver = new PropertiesDependencyResolver( 45 propsFile("com.group\\:example\\:1.3: path/1\n"), mock); 46 47 URL url = resolver.getLocalArtifactUrl(new DependencyJar("com.group", "example", "1.3", null)); 48 assertThat(url).isEqualTo(new URL("file://" + temporaryFolder.getRoot() + "/path/1")); 49 } 50 51 @Test 52 public void whenMissingFromProperties_shouldDelegate() throws Exception { 53 DependencyResolver resolver = new PropertiesDependencyResolver( 54 propsFile("nothing: interesting"), mock); 55 56 DependencyJar dependencyJar = new DependencyJar("com.group", "example", "1.3", null); 57 when(mock.getLocalArtifactUrl(dependencyJar)).thenReturn(new URL("file:///path/3")); 58 URL url = resolver.getLocalArtifactUrl(dependencyJar); 59 assertThat(url).isEqualTo(new URL("file:///path/3") 60 ); 61 } 62 63 @Test 64 public void whenDelegateIsNull_shouldGiveGoodMessage() throws Exception { 65 DependencyResolver resolver = new PropertiesDependencyResolver( 66 propsFile("nothing: interesting"), null); 67 68 DependencyJar dependencyJar = new DependencyJar("com.group", "example", "1.3", null); 69 try { 70 resolver.getLocalArtifactUrl(dependencyJar); 71 fail("should have failed"); 72 } catch (Exception e) { 73 assertThat(e.getMessage()).contains("no artifacts found for " + dependencyJar); 74 } 75 } 76 77 ////////////////// 78 79 private FsFile propsFile(String contents) throws IOException { 80 File file = temporaryFolder.newFile("file.properties"); 81 Files.write(contents, file, Charsets.UTF_8); 82 return Fs.newFile(file); 83 } 84 }