| 1 |
/* |
| 2 |
* Copyright (C) 2014 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 |
package com.android.ide.common.res2; |
| 17 |
|
| 18 |
import com.android.annotations.NonNull; |
| 19 |
import com.android.annotations.Nullable; |
| 20 |
import com.android.utils.ILogger; |
| 21 |
import static com.android.ide.common.res2.DataSet.isIgnored; |
| 22 |
import static java.io.File.separator; |
| 23 |
import static org.junit.Assert.assertEquals; |
| 24 |
import static org.junit.Assert.assertFalse; |
| 25 |
import static org.junit.Assert.assertNull; |
| 26 |
import static org.junit.Assert.assertTrue; |
| 27 |
|
| 28 |
import com.android.testutils.TestUtils; |
| 29 |
|
| 30 |
import java.io.File; |
| 31 |
import org.junit.Assert; |
| 32 |
import org.junit.Rule; |
| 33 |
import org.junit.Test; |
| 34 |
import org.junit.rules.TemporaryFolder; |
| 35 |
import org.w3c.dom.Node; |
| 36 |
|
| 37 |
public class DataSetTest { |
| 38 |
|
| 39 |
@Rule |
| 40 |
public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); |
| 41 |
|
| 42 |
@Test |
| 43 |
public void testIsIgnored() throws Exception { |
| 44 |
assertNull("Environment variable $ANDROID_AAPT_IGNORE should not be set while " |
| 45 |
+ "running test; can interfere with results", |
| 46 |
System.getenv("ANDROID_AAPT_IGNORE")); |
| 47 |
assertFalse(isIgnored(new File("a."))); |
| 48 |
assertFalse(isIgnored(new File("foo"))); |
| 49 |
assertFalse(isIgnored(new File("foo" + separator + "bar"))); |
| 50 |
assertFalse(isIgnored(new File("foo"))); |
| 51 |
assertFalse(isIgnored(new File("foo" + separator + "bar"))); |
| 52 |
assertFalse(isIgnored(new File("layout" + separator + "main.xml"))); |
| 53 |
assertFalse(isIgnored(new File("res" + separator + "drawable" + separator + "foo.png"))); |
| 54 |
assertFalse(isIgnored(new File(""))); |
| 55 |
|
| 56 |
assertTrue(isIgnored(new File("."))); |
| 57 |
assertTrue(isIgnored(new File(".."))); |
| 58 |
assertTrue(isIgnored(new File(".git"))); |
| 59 |
assertTrue(isIgnored(new File("foo" + separator + ".git"))); |
| 60 |
assertTrue(isIgnored(new File(".svn"))); |
| 61 |
assertTrue(isIgnored(new File("thumbs.db"))); |
| 62 |
assertTrue(isIgnored(new File("Thumbs.db"))); |
| 63 |
assertTrue(isIgnored(new File("foo" + separator + "Thumbs.db"))); |
| 64 |
|
| 65 |
// Suffix |
| 66 |
assertTrue(isIgnored(new File("foo~"))); |
| 67 |
assertTrue(isIgnored(new File("foo.scc"))); |
| 68 |
assertTrue(isIgnored(new File("foo" + separator + "foo.scc"))); |
| 69 |
|
| 70 |
// Prefix |
| 71 |
assertTrue(isIgnored(new File(".test"))); |
| 72 |
assertTrue(isIgnored(new File("foo" + separator + ".test"))); |
| 73 |
|
| 74 |
// Don't match on non-directory |
| 75 |
assertFalse(isIgnored(new File("_test"))); |
| 76 |
File dir = new File(TestUtils.createTempDirDeletedOnExit(), "_test"); |
| 77 |
assertTrue(dir.mkdirs()); |
| 78 |
assertTrue(isIgnored(dir)); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void testLongestPath() { |
| 83 |
DataSet dataSet = new DataSet("foo", false) { |
| 84 |
|
| 85 |
@Override |
| 86 |
protected DataSet createSet(String name) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
protected DataFile createFileAndItemsFromXml(@NonNull File file, @NonNull Node fileNode) |
| 92 |
throws MergingException { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
protected void readSourceFolder(File sourceFolder, ILogger logger) |
| 98 |
throws MergingException { |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
@Nullable |
| 103 |
@Override |
| 104 |
protected DataFile createFileAndItems(File sourceFolder, File file, ILogger logger) |
| 105 |
throws MergingException { |
| 106 |
return null; |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
File res = new File(mTemporaryFolder.getRoot(), "res"); |
| 111 |
assertTrue(res.mkdirs()); |
| 112 |
|
| 113 |
File foo = new File(mTemporaryFolder.getRoot(), "foo"); |
| 114 |
assertTrue(foo.mkdirs()); |
| 115 |
|
| 116 |
File customRes = new File(mTemporaryFolder.getRoot(), "res/layouts/shared"); |
| 117 |
assertTrue(customRes.mkdirs()); |
| 118 |
|
| 119 |
dataSet.addSource(res); |
| 120 |
dataSet.addSource(foo); |
| 121 |
dataSet.addSource(customRes); |
| 122 |
|
| 123 |
assertEquals(res.getAbsolutePath(), dataSet.findMatchingSourceFile( |
| 124 |
new File(mTemporaryFolder.getRoot(), "res/any.xml")).getPath()); |
| 125 |
assertEquals(res.getAbsolutePath(), dataSet.findMatchingSourceFile( |
| 126 |
new File(mTemporaryFolder.getRoot(), "res/layout/activity.xml")).getPath()); |
| 127 |
assertEquals(res.getAbsolutePath(), dataSet.findMatchingSourceFile( |
| 128 |
new File(mTemporaryFolder.getRoot(), "res/layout/foo/bar/activity.xml")).getPath()); |
| 129 |
assertEquals(customRes.getAbsolutePath(), dataSet.findMatchingSourceFile( |
| 130 |
new File(mTemporaryFolder.getRoot(), "res/layouts/shared/any.xml")).getPath()); |
| 131 |
assertEquals(customRes.getAbsolutePath(), dataSet.findMatchingSourceFile( |
| 132 |
new File(mTemporaryFolder.getRoot(), |
| 133 |
"res/layouts/shared/layout/activity.xml")).getPath()); |
| 134 |
assertEquals(customRes.getAbsolutePath(), dataSet.findMatchingSourceFile( |
| 135 |
new File(mTemporaryFolder.getRoot(), |
| 136 |
"res/layouts/shared/layout/foo/bar/activity.xml")).getPath()); |
| 137 |
assertNull(dataSet.findMatchingSourceFile( |
| 138 |
new File(mTemporaryFolder.getRoot(), "funky/shared/layout/foo/bar/activity.xml"))); |
| 139 |
} |
| 140 |
} |