AppDataDirGuesserTest.java revision 524c023fb37b41e06b69f1b696100dd465acb353
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.dexmaker; 18 19import java.io.File; 20import java.util.Arrays; 21import java.util.HashSet; 22import java.util.Set; 23import junit.framework.TestCase; 24 25public final class AppDataDirGuesserTest extends TestCase { 26 public void testGuessCacheDir_SimpleExample() { 27 guessCacheDirFor("/data/app/a.b.c.apk").shouldGive("/data/data/a.b.c/cache"); 28 guessCacheDirFor("/data/app/a.b.c.tests.apk").shouldGive("/data/data/a.b.c.tests/cache"); 29 } 30 31 public void testGuessCacheDir_MultipleResultsSeparatedByColon() { 32 guessCacheDirFor("/data/app/a.b.c.apk:/data/app/d.e.f.apk") 33 .shouldGive("/data/data/a.b.c/cache", "/data/data/d.e.f/cache"); 34 } 35 36 public void testGuessCacheDir_NotWriteableSkipped() { 37 guessCacheDirFor("/data/app/a.b.c.apk:/data/app/d.e.f.apk") 38 .withNonWriteable("/data/data/a.b.c/cache") 39 .shouldGive("/data/data/d.e.f/cache"); 40 } 41 42 public void testGuessCacheDir_StripHyphenatedSuffixes() { 43 guessCacheDirFor("/data/app/a.b.c-2.apk").shouldGive("/data/data/a.b.c/cache"); 44 } 45 46 public void testGuessCacheDir_LeadingAndTrailingColonsIgnored() { 47 guessCacheDirFor("/data/app/a.b.c.apk:asdf:").shouldGive("/data/data/a.b.c/cache"); 48 guessCacheDirFor(":asdf:/data/app/a.b.c.apk").shouldGive("/data/data/a.b.c/cache"); 49 } 50 51 public void testGuessCacheDir_InvalidInputsGiveEmptyArray() { 52 guessCacheDirFor("").shouldGive(); 53 } 54 55 public void testGuessCacheDir_JarsIgnored() { 56 guessCacheDirFor("/data/app/a.b.c.jar").shouldGive(); 57 guessCacheDirFor("/system/framework/android.test.runner.jar").shouldGive(); 58 } 59 60 public void testGuessCacheDir_RealWorldExample() { 61 String realPath = "/system/framework/android.test.runner.jar:" + 62 "/data/app/com.google.android.voicesearch.tests-2.apk:" + 63 "/data/app/com.google.android.voicesearch-1.apk"; 64 guessCacheDirFor(realPath) 65 .withNonWriteable("/data/data/com.google.android.voicesearch.tests/cache") 66 .shouldGive("/data/data/com.google.android.voicesearch/cache"); 67 } 68 69 private interface TestCondition { 70 TestCondition withNonWriteable(String... files); 71 void shouldGive(String... files); 72 } 73 74 private TestCondition guessCacheDirFor(final String path) { 75 final Set<String> notWriteable = new HashSet<String>(); 76 return new TestCondition() { 77 public void shouldGive(String... files) { 78 AppDataDirGuesser guesser = new AppDataDirGuesser() { 79 @Override 80 public boolean isWriteableDirectory(File file) { 81 return !notWriteable.contains(file.getAbsolutePath()); 82 } 83 @Override 84 boolean fileOrDirExists(File file) { 85 return true; 86 } 87 }; 88 File[] results = guesser.guessPath(path); 89 assertNotNull("Null results for " + path, results); 90 assertEquals("Bad lengths for " + path, files.length, results.length); 91 for (int i = 0; i < files.length; ++i) { 92 assertEquals("Element " + i, new File(files[i]), results[i]); 93 } 94 } 95 96 public TestCondition withNonWriteable(String... files) { 97 notWriteable.addAll(Arrays.asList(files)); 98 return this; 99 } 100 }; 101 } 102} 103