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