/
AndroidLicensePluginTest.groovy
203 lines (166 loc) · 6.69 KB
/
AndroidLicensePluginTest.groovy
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* License added by: GRADLE-LICENSE-PLUGIN
*
* Copyright (C)2011 - Jeroen van Erp <jeroen@javadude.nl>
*
* 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 nl.javadude.gradle.plugins.license
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import static org.hamcrest.CoreMatchers.*
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertThat
@RunWith(Parameterized.class)
class AndroidLicensePluginTest {
Project project
String pluginName
Class pluginClass
@Parameterized.Parameters(name = "Plugin {0}")
public static Collection<Object[]> pluginTypes() {
Object[][] params = [
[ 'com.android.application', AppPlugin ],
[ 'com.android.library', LibraryPlugin ]
];
return Arrays.asList(params);
}
public AndroidLicensePluginTest(pluginName, pluginClass) {
this.pluginName = pluginName
this.pluginClass = pluginClass
}
@Before
public void setupProject() {
project = ProjectBuilder.builder().withProjectDir(new File("testProject")).build()
def plugin = project.plugins.apply(LicensePlugin)
project.apply plugin: pluginName
// Otherwise we'd need a project.evaluate() which would trigger Android SDK detection
plugin.configureAndroidSourceSetRule(pluginClass)
}
@Test
public void shouldAddLicenseTask() {
def tasks = project.tasks.withType(License.class).findAll { true }
assertFalse tasks.isEmpty()
}
@Test
public void shouldPointToLicenseFileByDefault() {
assertThat(project.license.header.name, is("LICENSE"))
}
@Test
public void extensionShouldHaveBooleanDocumentedDefaults() {
assertThat project.license.ignoreFailures, is(false)
assertThat project.license.dryRun, is(false)
assertThat project.license.skipExistingHeaders, is(false)
assertThat project.license.useDefaultMappings, is(true)
assertThat project.license.strictCheck, is(false)
}
@Test
public void extensionShouldNotHaveSourceSets() {
assertThat project.license.sourceSets, is(notNullValue())
assertThat project.license.sourceSets.size(), equalTo(0)
}
@Test
public void shouldBeAbleToConfigureLicenseToOtherFile() {
project.license.header = project.file("OTHERLICENSE")
assertThat(project.license.header.name, is("OTHERLICENSE"))
}
@Test
public void shouldFindTwoLicenseTaskPerSourceSet() {
def tasks = project.tasks.withType(License.class).findAll { true }
assertThat tasks.size(), is(14) // [androidTest, debug, main, release, test, testDebug, testRelease].count * 2
}
@Test
public void shouldFindMainLicenseTask() {
def task = project.tasks.getByName("licenseAndroidMain")
assertThat task, instanceOf(License.class)
}
@Test
public void shouldFindDebugLicenseTask() {
def task = project.tasks.getByName("licenseAndroidDebug")
assertThat task, instanceOf(License.class)
}
@Test
public void shouldFindReleaseLicenseTask() {
def task = project.tasks.getByName("licenseAndroidRelease")
assertThat task, instanceOf(License.class)
}
@Test
public void shouldFindTestLicenseTask() {
def task = project.tasks.getByName("licenseAndroidAndroidTest")
assertThat task, instanceOf(License.class)
}
@Test
public void extensionShouldHaveSourceSetsWithJava() {
assertThat project.license.sourceSets.size(), equalTo(project.sourceSets.size())
}
@Test
public void shouldConfigureLicenseForTasks() {
def task = project.tasks['licenseAndroidMain']
assertThat task.header.name, is("LICENSE")
}
@Test
public void shouldConfigureManuallyConfiguredTask() {
def task = project.tasks.create('licenseManual', License)
assertThat task.header.name, is("LICENSE")
}
@Test
public void manualTaskShouldInheritFromExtension() {
def task = project.tasks.create('licenseManual', License)
assertThat project.license.ignoreFailures, is(false) // Default
assertThat task.ignoreFailures, is(false)
project.license.ignoreFailures = true
assertThat task.isIgnoreFailures(), is(true)
//assertThat task.getIgnoreFailures(), is(true) // GRADLE-2163, fixed in 1.0-rc1
assertThat task.ignoreFailures, is(true)
}
@Test
public void shouldRunLicenseDuringCheck() {
def task = project.tasks.create('licenseManual', License)
Set<Task> dependsOn = project.tasks['check'].getDependsOn()
assertThat dependsOn, hasItem(project.tasks['license'])
// Manual tests don't get registered with check
assertThat dependsOn, not(hasItem(task))
}
@Test
public void shouldRunLicenseFromBaseTasks() {
def task = project.tasks.create('licenseManual', License)
Set<Task> dependsOn = project.tasks['license'].getDependsOn()
assertThat dependsOn, hasItem(project.tasks['licenseAndroidMain'])
assertThat dependsOn, hasItem(project.tasks['licenseAndroidAndroidTest'])
// Manual tests don't get registered with check
assertThat dependsOn, not(hasItem(task))
Set<Task> dependsOnFormat = project.tasks['licenseFormat'].getDependsOn()
assertThat dependsOnFormat, hasItem(project.tasks['licenseFormatAndroidMain'])
assertThat dependsOnFormat, hasItem(project.tasks['licenseFormatAndroidAndroidTest'])
// Manual tests don't get registered with check
assertThat dependsOnFormat, not(hasItem(task))
}
@Test
public void canAddMappingsAtMultipleLevels() {
def task = project.tasks['licenseAndroidMain']
project.license.mapping {
map1='VALUE1'
}
task.mapping {
map2='VALUE2'
}
def mappings = task.combinedMappings()
assert mappings.containsKey('map1')
assert mappings.containsKey('map2')
}
}