-
Notifications
You must be signed in to change notification settings - Fork 139
/
TestSuiteLoaderTest.java
121 lines (108 loc) · 5.1 KB
/
TestSuiteLoaderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2016 Shazam Entertainment Limited
*
* 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.shazam.fork.suite;
import com.shazam.fork.io.DexFileExtractor;
import com.shazam.fork.model.TestCaseEvent;
import org.hamcrest.Matcher;
import org.jf.dexlib.DexFile;
import org.junit.Test;
import javax.annotation.Nonnull;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.shazam.fork.io.FakeDexFileExtractor.fakeDexFileExtractor;
import static com.shazam.fork.io.Files.convertFileToDexFile;
import static com.shazam.fork.model.TestCaseEvent.newTestCase;
import static com.shazam.fork.suite.FakeTestClassMatcher.fakeTestClassMatcher;
import static com.shazam.shazamcrest.MatcherAssert.assertThat;
import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
import static java.util.Arrays.asList;
import static java.util.Collections.*;
import static org.hamcrest.Matchers.containsInAnyOrder;
/**
* This test is based on the <code>tests.dex</code> file, which contains test classes with the following code:
* <blockquote><pre>
*{@literal}@Ignore
*public class IgnoredClassTest {
* {@literal}@Test
* public void methodOfAnIgnoredTestClass() {
* }
*}
*
*public class ClassWithNoIgnoredMethodsTest {
* {@literal}@Test
* public void firstTestMethod() {
* }
*
* {@literal}@Test
* public void secondTestMethod() {
* }
*}
*
*public class ClassWithSomeIgnoredMethodsTest {
* {@literal}@Test
* public void nonIgnoredTestMethod() {
* }
*
* {@literal}@Test
* {@literal}@Ignore
* public void ignoredTestMethod() {
* }
*}
* </pre></blockquote>
*/
public class TestSuiteLoaderTest {
private static final File ANY_INSTRUMENTATION_APK_FILE = null;
private final DexFileExtractor fakeDexFileExtractor = fakeDexFileExtractor().thatReturns(testDexFile());
private final TestClassMatcher fakeTestClassMatcher = fakeTestClassMatcher().thatAlwaysMatches();
private DexFile testDexFile() {
URL testDexResourceUrl = this.getClass().getResource("/tests.dex");
String testDexFile = testDexResourceUrl.getFile();
File file = new File(testDexFile);
return convertFileToDexFile().apply(file);
}
@SuppressWarnings("unchecked")
@Test
public void populatesTestCaseEvents() throws Exception {
Map<String, String> multiPropertiesMap = new HashMap() {{
put("foo", "bar");
put("bux", "poi");
}};
TestSuiteLoader testSuiteLoader = new TestSuiteLoader(ANY_INSTRUMENTATION_APK_FILE, fakeDexFileExtractor,
fakeTestClassMatcher);
assertThat(testSuiteLoader.loadTestSuite(), containsInAnyOrder(
sameTestEventAs("methodOfAnIgnoredTestClass", "com.shazam.forktest.IgnoredClassTest", true),
sameTestEventAs("firstTestMethod", "com.shazam.forktest.ClassWithNoIgnoredMethodsTest", false),
sameTestEventAs("secondTestMethod", "com.shazam.forktest.ClassWithNoIgnoredMethodsTest", false),
sameTestEventAs("nonIgnoredTestMethod", "com.shazam.forktest.ClassWithSomeIgnoredMethodsTest", false),
sameTestEventAs("ignoredTestMethod", "com.shazam.forktest.ClassWithSomeIgnoredMethodsTest", true),
sameTestEventAs("methodAnnotatedWithRevokePermissionsTest", "com.shazam.forktest.RevokePermissionsClassTest",
false, asList("android.permission.RECORD_AUDIO", "android.permission.ACCESS_FINE_LOCATION")),
sameTestEventAs("methodAnnotatedWithEmptyRevokePermissionsTest", "com.shazam.forktest.RevokePermissionsClassTest", false),
sameTestEventAs("methodWithProperties", "com.shazam.forktest.PropertiesClassTest", singletonMap("foo", "bar")),
sameTestEventAs("methodWithMultipleProperties", "com.shazam.forktest.PropertiesClassTest", multiPropertiesMap)
)
);
}
@Nonnull
private Matcher<TestCaseEvent> sameTestEventAs(String testMethod, String testClass, Map<String, String> properties) {
return sameBeanAs(newTestCase(testMethod, testClass, false, emptyList(), properties));
}
@Nonnull
private Matcher<TestCaseEvent> sameTestEventAs(String testMethod, String testClass, boolean isIgnored) {
return sameBeanAs(newTestCase(testMethod, testClass, isIgnored, emptyList(), emptyMap()));
}
@Nonnull
private Matcher<TestCaseEvent> sameTestEventAs(String testMethod, String testClass, boolean isIgnored, List<String> permissions) {
return sameBeanAs(newTestCase(testMethod, testClass, isIgnored, permissions, emptyMap()));
}
}