diff options
author | Xavier Ducrohet <xav@android.com> | 2012-12-20 11:46:05 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2012-12-20 11:49:27 -0800 |
commit | 9701da260a42f433d098707f0d6515ee41b024bf (patch) | |
tree | 19f16637e91dc72347c96c10a6bc06227a233ff9 /builder | |
parent | 86fe65512d1230aafd9cb974b733f6fd09ac15ce (diff) | |
download | build-9701da260a42f433d098707f0d6515ee41b024bf.tar.gz |
Add tests for FileManager.
Change-Id: Ib94f5bf3bc3444c83541b657e5551712537fbb84
Diffstat (limited to 'builder')
14 files changed, 252 insertions, 27 deletions
diff --git a/builder/src/main/java/com/android/builder/internal/incremental/FileEntity.java b/builder/src/main/java/com/android/builder/internal/incremental/FileEntity.java index 17eae4c..ba0174a 100644 --- a/builder/src/main/java/com/android/builder/internal/incremental/FileEntity.java +++ b/builder/src/main/java/com/android/builder/internal/incremental/FileEntity.java @@ -227,4 +227,14 @@ class FileEntity { formatter.close(); } } + + @Override + public String toString() { + return "FileEntity{" + + "file=" + file + + ", lastModified=" + lastModified + + ", length=" + length + + ", sha1='" + sha1 + '\'' + + '}'; + } } diff --git a/builder/src/main/java/com/android/builder/internal/incremental/FileManager.java b/builder/src/main/java/com/android/builder/internal/incremental/FileManager.java index ff47c6a..42055e5 100644 --- a/builder/src/main/java/com/android/builder/internal/incremental/FileManager.java +++ b/builder/src/main/java/com/android/builder/internal/incremental/FileManager.java @@ -22,6 +22,7 @@ import com.google.common.collect.Maps; import java.io.*; import java.util.Collection; +import java.util.Collections; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,6 +41,7 @@ class FileManager { private Map<File, FileEntity> mLoadedFiles = Maps.newHashMap(); private Map<File, FileEntity> mProcessedFiles = Maps.newHashMap(); private Map<File, FileStatus> mResults = Maps.newHashMap(); + private Map<File, FileStatus> mReturnedMap = null; public FileManager() { } @@ -163,13 +165,21 @@ class FileManager { */ @NonNull public Map<File, FileStatus> getChangedFiles() { - // at this point, all the files that needed processing have been processed, - // but there may be removed files remaining in the loaded file map. - for (File f : mLoadedFiles.keySet()) { - mResults.put(f, FileStatus.REMOVED); + if (mReturnedMap == null) { + // create a map with the content of the result map. + mReturnedMap = Maps.newHashMap(mResults); + + // at this point, all the files that needed processing have been processed, + // but there may be removed files remaining in the loaded file map. + for (File f : mLoadedFiles.keySet()) { + mReturnedMap.put(f, FileStatus.REMOVED); + } + + // wrap this + mReturnedMap = Collections.unmodifiableMap(mReturnedMap); } - return mResults; + return mReturnedMap; } private void processFile(File file) { diff --git a/builder/src/test/java/com/android/builder/TestUtils.java b/builder/src/test/java/com/android/builder/TestUtils.java new file mode 100644 index 0000000..ad45ce3 --- /dev/null +++ b/builder/src/test/java/com/android/builder/TestUtils.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.builder; + +import junit.framework.TestCase; + +import java.io.File; +import java.io.IOException; + +/** + * Utility methods to deal with loading the test data. + */ +public class TestUtils { + + /** + * returns a File for the subfolder of the test resource data. + * + * This is basically "src/test/resources/testData/$name". + * + * Note that this folder is relative to the root project which is where gradle + * sets the current working dir when running the tests. + * + * If you need a full folder path, use {@link #getCanonicalRoot(String)}. + * + * @param name the name of the subfolder. + * @return a File + */ + public static File getRoot(String name) { + File root = new File("src/test/resources/testData/" + name); + TestCase.assertTrue("Test folder '" + name + "' does not exist!", + root.isDirectory()); + + return root; + } + + /** + * returns a File for the subfolder of the test resource data. + * + * The full path is canonized. + * This is basically ".../src/test/resources/testData/$name". + * + * @param name the name of the subfolder. + * @return a File + */ + public static File getCanonicalRoot(String name) throws IOException { + File root = getRoot(name); + return root.getCanonicalFile(); + } +} diff --git a/builder/src/test/java/com/android/builder/internal/incremental/FileManagerTest.java b/builder/src/test/java/com/android/builder/internal/incremental/FileManagerTest.java new file mode 100644 index 0000000..182d584 --- /dev/null +++ b/builder/src/test/java/com/android/builder/internal/incremental/FileManagerTest.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.builder.internal.incremental; + +import com.android.builder.TestUtils; +import com.android.builder.resources.FileStatus; +import com.google.common.base.Charsets; +import com.google.common.collect.Maps; +import com.google.common.io.Files; +import junit.framework.TestCase; + +import java.io.File; +import java.io.IOException; +import java.util.Map; + +public class FileManagerTest extends TestCase { + + private static FileManager sFileManager = null; + private static File sFilesFolder = null; + + public void testUntouched() throws Exception { + FileManager fileManager = getFileManager(); + Map<File, FileStatus> changedFiles = fileManager.getChangedFiles(); + + File file = new File(sFilesFolder, "untouched.png"); + FileStatus status = changedFiles.get(file); + assertNull(status); + } + + public void testUntouchedDateChanged() throws Exception { + FileManager fileManager = getFileManager(); + Map<File, FileStatus> changedFiles = fileManager.getChangedFiles(); + + File file = new File(sFilesFolder, "untouched_date_changed.png"); + FileStatus status = changedFiles.get(file); + assertNull(status); + } + + public void testContentChanged() throws Exception { + FileManager fileManager = getFileManager(); + Map<File, FileStatus> changedFiles = fileManager.getChangedFiles(); + + File file = new File(sFilesFolder, "content_changed.png"); + FileStatus status = changedFiles.get(file); + assertEquals(FileStatus.CHANGED, status); + } + + public void testSizeChanged() throws Exception { + FileManager fileManager = getFileManager(); + Map<File, FileStatus> changedFiles = fileManager.getChangedFiles(); + + File file = new File(sFilesFolder, "size_changed.png"); + FileStatus status = changedFiles.get(file); + assertEquals(FileStatus.CHANGED, status); + } + + public void testRemoved() throws Exception { + FileManager fileManager = getFileManager(); + Map<File, FileStatus> changedFiles = fileManager.getChangedFiles(); + + File file = new File(sFilesFolder, "removed.png"); + FileStatus status = changedFiles.get(file); + assertEquals(FileStatus.REMOVED, status); + } + + public void testNew() throws Exception { + FileManager fileManager = getFileManager(); + Map<File, FileStatus> changedFiles = fileManager.getChangedFiles(); + + File file = new File(sFilesFolder, "new.png"); + FileStatus status = changedFiles.get(file); + assertEquals(FileStatus.NEW, status); + } + + private FileManager getFileManager() throws IOException { + if (sFileManager == null) { + File root = TestUtils.getCanonicalRoot("changeManager"); + File dataFile = new File(root, "files.data"); + sFilesFolder = new File(root, "files"); + + // update the last modified on some of the files. + Map<String, String> placeHolderMap = Maps.newHashMap(); + String[] files = new String[] { "untouched" }; + for (String filename : files) { + File file = new File(sFilesFolder, filename + ".png"); + placeHolderMap.put( + String.format("\\$lm_%s\\$", filename), + String.format("%d", file.lastModified())); + } + + File trueDataFile = getDataFile(dataFile, sFilesFolder, placeHolderMap); + + sFileManager = new FileManager(); + sFileManager.load(trueDataFile); + + sFileManager.addFile(sFilesFolder); + } + + return sFileManager; + } + + /** + * Returns a data file where the placeholders have been updated with the real folder based + * on where the tests are run from. + * + * @param file the data folder. + * @param targetFolder the targetFolder + * @param placeholders additional placeholders and values to replace. + * + * @return a new data file that's been updated with the targetFolder + * @throws IOException + */ + private static File getDataFile(File file, File targetFolder, Map<String, String> placeholders) + throws IOException { + + String content = Files.toString(file, Charsets.UTF_8); + + // search and replace $TOP$ with the root and $SEP$ with the platform separator. + content = content.replaceAll("\\$TOP\\$", targetFolder.getAbsolutePath()) + .replaceAll("\\$SEP\\$", File.separator); + + // now replace the additional placeholders + for (Map.Entry<String, String> entry : placeholders.entrySet()) { + content = content.replaceAll(entry.getKey(), entry.getValue()); + } + + File tmp = File.createTempFile("android", "getDataFile"); + Files.write(content, tmp, Charsets.UTF_8); + + return tmp; + } +} diff --git a/builder/src/test/java/com/android/builder/resources/BaseTestCase.java b/builder/src/test/java/com/android/builder/resources/BaseTestCase.java index b536ca7..73c6e20 100644 --- a/builder/src/test/java/com/android/builder/resources/BaseTestCase.java +++ b/builder/src/test/java/com/android/builder/resources/BaseTestCase.java @@ -19,25 +19,10 @@ package com.android.builder.resources; import com.google.common.collect.ListMultimap; import junit.framework.TestCase; -import java.io.File; -import java.io.IOException; import java.util.List; public abstract class BaseTestCase extends TestCase { - protected static File getRoot(String name) { - File root = new File("src/test/resources/testData/" + name); - assertTrue("Test folder '" + name + "' does not exist!", - root.isDirectory()); - - return root; - } - - protected static File getCanonicalRoot(String name) throws IOException { - File root = getRoot(name); - return root.getCanonicalFile(); - } - protected void verifyResourceExists(ResourceMap resourceMap, String... resourceKeys) { ListMultimap<String, Resource> map = resourceMap.getResourceMap(); diff --git a/builder/src/test/java/com/android/builder/resources/ResourceMergerTest.java b/builder/src/test/java/com/android/builder/resources/ResourceMergerTest.java index c26f47d..cb11499 100644 --- a/builder/src/test/java/com/android/builder/resources/ResourceMergerTest.java +++ b/builder/src/test/java/com/android/builder/resources/ResourceMergerTest.java @@ -16,6 +16,7 @@ package com.android.builder.resources; +import com.android.builder.TestUtils; import com.android.resources.ResourceFolderType; import com.android.resources.ResourceType; import com.google.common.base.Charsets; @@ -149,7 +150,7 @@ public class ResourceMergerTest extends BaseTestCase { * @throws Exception */ public void testLoadingTestPathReplacement() throws Exception { - File root = getRoot("baseMerge"); + File root = TestUtils.getRoot("baseMerge"); File fakeRoot = getMergedBlobFolder(root); ResourceMerger resourceMerger = new ResourceMerger(); @@ -643,7 +644,7 @@ public class ResourceMergerTest extends BaseTestCase { private static ResourceMerger getResourceMerger() throws DuplicateResourceException, IOException { if (sResourceMerger == null) { - File root = getRoot("baseMerge"); + File root = TestUtils.getRoot("baseMerge"); ResourceSet res = ResourceSetTest.getBaseResourceSet(); @@ -687,7 +688,7 @@ public class ResourceMergerTest extends BaseTestCase { String content = Files.toString(originalMerger, Charsets.UTF_8); - // search and replace $TOP$ with the root + // search and replace $TOP$ with the root and $SEP$ with the platform separator. content = content.replaceAll( "\\$TOP\\$", folder.getAbsolutePath()).replaceAll("\\$SEP\\$", File.separator); @@ -698,7 +699,7 @@ public class ResourceMergerTest extends BaseTestCase { } private File getIncMergeRoot(String name) throws IOException { - File root = getCanonicalRoot("incMergeData"); + File root = TestUtils.getCanonicalRoot("incMergeData"); return new File(root, name); } diff --git a/builder/src/test/java/com/android/builder/resources/ResourceSetTest.java b/builder/src/test/java/com/android/builder/resources/ResourceSetTest.java index 9c0efa1..fa39ebf 100644 --- a/builder/src/test/java/com/android/builder/resources/ResourceSetTest.java +++ b/builder/src/test/java/com/android/builder/resources/ResourceSetTest.java @@ -16,6 +16,8 @@ package com.android.builder.resources; +import com.android.builder.TestUtils; + import java.io.File; import java.io.IOException; @@ -59,7 +61,7 @@ public class ResourceSetTest extends BaseTestCase { } public void testDupResourceSet() throws Exception { - File root = getRoot("dupResourceSet"); + File root = TestUtils.getRoot("dupResourceSet"); ResourceSet set = new ResourceSet("main"); set.addSource(new File(root, "res1")); @@ -76,7 +78,7 @@ public class ResourceSetTest extends BaseTestCase { static ResourceSet getBaseResourceSet() throws DuplicateResourceException, IOException { if (sBaseResourceSet == null) { - File root = getRoot("baseResourceSet"); + File root = TestUtils.getRoot("baseResourceSet"); sBaseResourceSet = new ResourceSet("main"); sBaseResourceSet.addSource(root); diff --git a/builder/src/test/java/com/android/builder/resources/ValueResourceParserTest.java b/builder/src/test/java/com/android/builder/resources/ValueResourceParserTest.java index 022b5d7..96dabfd 100644 --- a/builder/src/test/java/com/android/builder/resources/ValueResourceParserTest.java +++ b/builder/src/test/java/com/android/builder/resources/ValueResourceParserTest.java @@ -16,6 +16,7 @@ package com.android.builder.resources; +import com.android.builder.TestUtils; import com.google.common.collect.Maps; import java.io.File; @@ -69,7 +70,7 @@ public class ValueResourceParserTest extends BaseTestCase { private static List<Resource> getParsedResources() throws IOException { if (sResources == null) { - File root = getRoot("baseResourceSet"); + File root = TestUtils.getRoot("baseResourceSet"); File values = new File(root, "values"); File valuesXml = new File(values, "values.xml"); diff --git a/builder/src/test/resources/testData/changeManager/files.data b/builder/src/test/resources/testData/changeManager/files.data new file mode 100644 index 0000000..7c1c157 --- /dev/null +++ b/builder/src/test/resources/testData/changeManager/files.data @@ -0,0 +1,7 @@ +# golden files created manually to differ form the actual +# files in key ways to test various detectors. +$lm_untouched$ 98 272fbc249474f5c842db3c6273ca94e30e641d06 $TOP$$SEP$untouched.png +1000000000000 98 272fbc249474f5c842db3c6273ca94e30e641d06 $TOP$$SEP$untouched_date_changed.png +1000000000000 98 0000000000000000000000000000000000000000 $TOP$$SEP$content_changed.png +1000000000000 95 272fbc249474f5c842db3c6273ca94e30e641d06 $TOP$$SEP$size_changed.png +1000000000000 2574 272fbc249474f5c842db3c6273ca94e30e641d06 $TOP$$SEP$removed.png diff --git a/builder/src/test/resources/testData/changeManager/files/content_changed.png b/builder/src/test/resources/testData/changeManager/files/content_changed.png Binary files differnew file mode 100644 index 0000000..47e1adf --- /dev/null +++ b/builder/src/test/resources/testData/changeManager/files/content_changed.png diff --git a/builder/src/test/resources/testData/changeManager/files/new.png b/builder/src/test/resources/testData/changeManager/files/new.png Binary files differnew file mode 100644 index 0000000..47e1adf --- /dev/null +++ b/builder/src/test/resources/testData/changeManager/files/new.png diff --git a/builder/src/test/resources/testData/changeManager/files/size_changed.png b/builder/src/test/resources/testData/changeManager/files/size_changed.png Binary files differnew file mode 100644 index 0000000..47e1adf --- /dev/null +++ b/builder/src/test/resources/testData/changeManager/files/size_changed.png diff --git a/builder/src/test/resources/testData/changeManager/files/untouched.png b/builder/src/test/resources/testData/changeManager/files/untouched.png Binary files differnew file mode 100644 index 0000000..47e1adf --- /dev/null +++ b/builder/src/test/resources/testData/changeManager/files/untouched.png diff --git a/builder/src/test/resources/testData/changeManager/files/untouched_date_changed.png b/builder/src/test/resources/testData/changeManager/files/untouched_date_changed.png Binary files differnew file mode 100644 index 0000000..47e1adf --- /dev/null +++ b/builder/src/test/resources/testData/changeManager/files/untouched_date_changed.png |