Home | History | Annotate | Download | only in littlemock
      1 /*
      2  * Copyright (C) 2011 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.google.testing.littlemock;
     18 
     19 import junit.framework.TestCase;
     20 
     21 import java.io.File;
     22 import java.util.Arrays;
     23 import java.util.HashSet;
     24 import java.util.Set;
     25 
     26 public class AppDataDirGuesserTest extends TestCase {
     27   public void testGuessCacheDir_SimpleExample() {
     28     guessCacheDirFor("/data/app/a.b.c.apk").shouldGive("/data/data/a.b.c/cache");
     29     guessCacheDirFor("/data/app/a.b.c.tests.apk").shouldGive("/data/data/a.b.c.tests/cache");
     30   }
     31 
     32   public void testGuessCacheDir_MultipleResultsSeparatedByColon() {
     33     guessCacheDirFor("/data/app/a.b.c.apk:/data/app/d.e.f.apk")
     34         .shouldGive("/data/data/a.b.c/cache", "/data/data/d.e.f/cache");
     35   }
     36 
     37   public void testGuessCacheDir_NotWriteableSkipped() {
     38     guessCacheDirFor("/data/app/a.b.c.apk:/data/app/d.e.f.apk")
     39         .withNonWriteable("/data/data/a.b.c/cache")
     40         .shouldGive("/data/data/d.e.f/cache");
     41   }
     42 
     43   public void testGuessCacheDir_StripHyphenatedSuffixes() {
     44     guessCacheDirFor("/data/app/a.b.c-2.apk").shouldGive("/data/data/a.b.c/cache");
     45   }
     46 
     47   public void testGuessCacheDir_LeadingAndTrailingColonsIgnored() {
     48     guessCacheDirFor("/data/app/a.b.c.apk:asdf:").shouldGive("/data/data/a.b.c/cache");
     49     guessCacheDirFor(":asdf:/data/app/a.b.c.apk").shouldGive("/data/data/a.b.c/cache");
     50   }
     51 
     52   public void testGuessCacheDir_InvalidInputsGiveEmptyArray() {
     53     guessCacheDirFor("").shouldGive();
     54   }
     55 
     56   public void testGuessCacheDir_JarsIgnored() {
     57     guessCacheDirFor("/data/app/a.b.c.jar").shouldGive();
     58     guessCacheDirFor("/system/framework/android.test.runner.jar").shouldGive();
     59   }
     60 
     61   public void testGuessCacheDir_RealWorldExample() {
     62     String realPath = "/system/framework/android.test.runner.jar:" +
     63         "/data/app/com.google.android.voicesearch.tests-2.apk:" +
     64         "/data/app/com.google.android.voicesearch-1.apk";
     65     guessCacheDirFor(realPath)
     66         .withNonWriteable("/data/data/com.google.android.voicesearch.tests/cache")
     67         .shouldGive("/data/data/com.google.android.voicesearch/cache");
     68   }
     69 
     70   public void testSplitPathList() {
     71     final String[] expected = { "foo", "bar" };
     72     assertTrue(Arrays.equals(expected, AppDataDirGuesser.splitPathList("foo:bar")));
     73     assertTrue(Arrays.equals(expected,
     74               AppDataDirGuesser.splitPathList("dexPath=foo:bar")));
     75     assertTrue(Arrays.equals(expected,
     76               AppDataDirGuesser.splitPathList("dexPath=foo:bar,bazPath=bar:bar2")));
     77   }
     78 
     79   private interface TestCondition {
     80     TestCondition withNonWriteable(String... files);
     81     void shouldGive(String... files);
     82   }
     83 
     84   private TestCondition guessCacheDirFor(final String path) {
     85     final Set<String> notWriteable = new HashSet<String>();
     86     return new TestCondition() {
     87       @Override
     88       public void shouldGive(String... files) {
     89         AppDataDirGuesser guesser = new AppDataDirGuesser() {
     90           @Override
     91           public boolean isWriteableDirectory(File file) {
     92             return !notWriteable.contains(file.getAbsolutePath());
     93           }
     94           @Override
     95           boolean fileOrDirExists(File file) {
     96             return true;
     97           }
     98         };
     99         File[] results = guesser.guessPath(path);
    100         assertNotNull("Null results for " + path, results);
    101         assertEquals("Bad lengths for " + path, files.length, results.length);
    102         for (int i = 0; i < files.length; ++i) {
    103           assertEquals("Element " + i, new File(files[i]), results[i]);
    104         }
    105       }
    106 
    107       @Override
    108       public TestCondition withNonWriteable(String... files) {
    109         notWriteable.addAll(Arrays.asList(files));
    110         return this;
    111       }
    112     };
    113   }
    114 }
    115