TestUtil.java revision 28dc94ecb7ac94834b97e3f6be778b3f2bb734fd
1package com.xtremelabs.robolectric.util; 2 3import com.xtremelabs.robolectric.RobolectricConfig; 4 5import java.io.File; 6import java.io.FileInputStream; 7import java.util.Collection; 8import java.util.Properties; 9 10import static org.junit.Assert.assertTrue; 11 12public abstract class TestUtil { 13 public static File testDirLocation; 14 15 public static void assertEquals(Collection<?> expected, Collection<?> actual) { 16 org.junit.Assert.assertEquals(stringify(expected), stringify(actual)); 17 } 18 19 public static String stringify(Collection<?> collection) { 20 StringBuilder buf = new StringBuilder(); 21 for (Object o : collection) { 22 if (buf.length() > 0) buf.append("\n"); 23 buf.append(o); 24 } 25 return buf.toString(); 26 } 27 28 public static <T> void assertInstanceOf(Class<? extends T> expectedClass, T object) { 29 Class actualClass = object.getClass(); 30 assertTrue(expectedClass + " should be assignable from " + actualClass, 31 expectedClass.isAssignableFrom(actualClass)); 32 } 33 34 public static File file(String... pathParts) { 35 return file(new File("."), pathParts); 36 } 37 38 public static File file(File f, String... pathParts) { 39 for (String pathPart : pathParts) { 40 f = new File(f, pathPart); 41 } 42 return f; 43 } 44 45 public static File resourcesBaseDir() { 46 if (testDirLocation == null) { 47 File testDir = file("src", "test", "resources"); 48 if (hasTestManifest(testDir)) return testDirLocation = testDir; 49 50 File roboTestDir = file("robolectric", "src", "test", "resources"); 51 if (hasTestManifest(roboTestDir)) return testDirLocation = roboTestDir; 52 53 File submoduleDir = file("submodules", "robolectric", "src", "test", "resources"); 54 if (hasTestManifest(submoduleDir)) return testDirLocation = submoduleDir; 55 56 throw new RuntimeException("can't find your TestAndroidManifest.xml in " 57 + testDir.getAbsolutePath() + " or " + roboTestDir.getAbsolutePath()); 58 } else { 59 return testDirLocation; 60 } 61 } 62 63 private static boolean hasTestManifest(File testDir) { 64 return new File(testDir, "TestAndroidManifest.xml").isFile(); 65 } 66 67 public static File resourceFile(String... pathParts) { 68 return file(resourcesBaseDir(), pathParts); 69 } 70 71 public static RobolectricConfig newConfig(String androidManifestFile) { 72 return new RobolectricConfig(resourceFile(androidManifestFile), null, null); 73 } 74 75 public static File getSystemResourceValuesDir() throws Exception { 76 Properties localProperties = new Properties(); 77 localProperties.load(new FileInputStream(new File("local.properties"))); 78 PropertiesHelper.doSubstitutions(localProperties); 79 return new File(localProperties.getProperty("sdk.dir"), "platforms/android-10/data/res/values"); 80 } 81} 82