Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/blame/MergingLogTest.java

raw
1 /*
2 * Copyright (C) 2015 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
17 package com.android.ide.common.blame;
18
19 import com.google.common.collect.Maps;
20
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.TemporaryFolder;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.Map;
30
31 public class MergingLogTest {
32
33 @Rule
34 public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
35
36 @Test
37 public void testMergingLog() throws IOException {
38
39 final SourceFilePosition position1 = new SourceFilePosition(
40 new SourceFile(absoluteFile("exploded/a/values/values.xml")),
41 new SourcePosition(7, 8, 20));
42
43 final SourceFilePosition position2 = new SourceFilePosition(
44 new SourceFile(absoluteFile("exploded/b/values/values.xml")),
45 new SourcePosition(2, 3, 14));
46
47 File tempDir = mTemporaryFolder.newFolder();
48 MergingLog mergingLog = new MergingLog(tempDir);
49
50 mergingLog.logCopy(absoluteFile("exploded/layout/a"), absoluteFile("merged/layout/a"));
51 mergingLog.logCopy(absoluteFile("exploded/layout-land/a"),
52 absoluteFile("merged/layout-land/a"));
53
54 Map<SourcePosition, SourceFilePosition> map = Maps.newLinkedHashMap();
55 map.put(new SourcePosition(1, 2, 3, 7, 1, 120), position1);
56 map.put(new SourcePosition(4, 1, 34, 6, 20, 100), position2);
57 mergingLog.logSource(new SourceFile(absoluteFile("merged/values/values.xml")), map);
58
59 Map<SourcePosition, SourceFilePosition> map2 = Maps.newLinkedHashMap();
60 map2.put(
61 new SourcePosition(3, 4, 34),
62 new SourceFilePosition(
63 new SourceFile(absoluteFile("exploded/values-de/values.xml")),
64 new SourcePosition(0, 5, 5)));
65 mergingLog.logSource(new SourceFile(absoluteFile("merged/values-de/values.xml")), map2);
66
67 // Write and then reload (won't load anything immediately).
68 mergingLog.write();
69 mergingLog = new MergingLog(tempDir);
70
71 mergingLog.logRemove(new SourceFile(absoluteFile("merged/layout/a")));
72
73 mergingLog.write();
74 mergingLog = new MergingLog(tempDir);
75
76 Assert.assertEquals("", new SourceFile(absoluteFile("exploded/layout-land/a")),
77 mergingLog.find(new SourceFile(absoluteFile("merged/layout-land/a"))));
78
79
80 /*
81 Test
82 |---search query----|
83 |---------target----------|
84 */
85 Assert.assertEquals("", position2,
86 mergingLog.find(new SourceFilePosition(
87 new SourceFile(absoluteFile("merged/values/values.xml")),
88 new SourcePosition(4, 1, 35, 4, 2, 36))));
89
90 /*
91 Test
92 |---search query----|
93 |------------target-------------|
94 |-----wrong----|
95 */
96 Assert.assertEquals("", position1,
97 mergingLog.find(new SourceFilePosition(
98 new SourceFile(absoluteFile("merged/values/values.xml")),
99 new SourcePosition(5, 20, 35, 6, 25, 105))));
100
101 // Check that an unknown file returns itself.
102 SourceFilePosition noMatch1 = new SourceFilePosition(
103 new SourceFile(absoluteFile("unknownFile")),
104 new SourcePosition(1, 2, 3));
105 Assert.assertEquals("", noMatch1, mergingLog.find(noMatch1));
106
107 // And that a position that is not mapped in the file also returns itself.
108 SourceFilePosition noMatch2 = new SourceFilePosition(
109 new SourceFile(absoluteFile("merged/values/values.xml")),
110 new SourcePosition(100, 0, 3000));
111 Assert.assertEquals("", noMatch2, mergingLog.find(noMatch2));
112
113 mergingLog.write();
114 }
115
116 private File testPath;
117
118 @Before
119 public void setupTestPath() throws IOException {
120 testPath = mTemporaryFolder.newFolder();
121 }
122
123 private File absoluteFile(String path) {
124 return new File(testPath, path);
125 }
126 }