PageRenderTime 95ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/subprojects/integ-test/src/integTest/groovy/org/gradle/integtests/ApplicationIntegrationSpec.groovy

https://github.com/andrewhj-mn/gradle
Groovy | 447 lines | 405 code | 26 blank | 16 comment | 12 complexity | 03e7b40f1325491402ed090c2afcab3f MD5 | raw file
  1. /*
  2. * Copyright 2011 the original author or authors.
  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 org.gradle.integtests
  17. import org.gradle.integtests.fixtures.AbstractIntegrationSpec
  18. import org.gradle.integtests.fixtures.ScriptExecuter
  19. import org.gradle.integtests.fixtures.executer.GradleContextualExecuter
  20. import org.gradle.internal.os.OperatingSystem
  21. import org.gradle.test.fixtures.file.TestFile
  22. import org.gradle.util.TextUtil
  23. import spock.lang.IgnoreIf
  24. import spock.lang.Unroll
  25. import static org.hamcrest.Matchers.startsWith
  26. class ApplicationIntegrationSpec extends AbstractIntegrationSpec{
  27. def setup() {
  28. file('settings.gradle') << 'rootProject.name = "application"'
  29. buildFile << """
  30. apply plugin: 'application'
  31. mainClassName = 'org.gradle.test.Main'
  32. """
  33. }
  34. def canUseEnvironmentVariableToPassMultipleOptionsToJvmWhenRunningScript() {
  35. file('src/main/java/org/gradle/test/Main.java') << '''
  36. package org.gradle.test;
  37. class Main {
  38. public static void main(String[] args) {
  39. if (!"value".equals(System.getProperty("testValue"))) {
  40. throw new RuntimeException("Expected system property not specified");
  41. }
  42. if (!"some value".equals(System.getProperty("testValue2"))) {
  43. throw new RuntimeException("Expected system property not specified");
  44. }
  45. if (!"some value".equals(System.getProperty("testValue3"))) {
  46. throw new RuntimeException("Expected system property not specified");
  47. }
  48. }
  49. }
  50. '''
  51. when:
  52. run 'installDist'
  53. def builder = new ScriptExecuter()
  54. builder.workingDir file('build/install/application/bin')
  55. builder.executable "application"
  56. if (OperatingSystem.current().windows) {
  57. builder.environment('APPLICATION_OPTS', '-DtestValue=value -DtestValue2="some value" -DtestValue3="some value"')
  58. } else {
  59. builder.environment('APPLICATION_OPTS', '-DtestValue=value -DtestValue2=\'some value\' -DtestValue3=some\\ value')
  60. }
  61. def result = builder.run()
  62. then:
  63. result.assertNormalExitValue()
  64. }
  65. def canUseDefaultJvmArgsToPassMultipleOptionsToJvmWhenRunningScript() {
  66. file("build.gradle") << '''
  67. applicationDefaultJvmArgs = ['-DtestValue=value', '-DtestValue2=some value', '-DtestValue3=some value']
  68. '''
  69. file('src/main/java/org/gradle/test/Main.java') << '''
  70. package org.gradle.test;
  71. class Main {
  72. public static void main(String[] args) {
  73. if (!"value".equals(System.getProperty("testValue"))) {
  74. throw new RuntimeException("Expected system property not specified");
  75. }
  76. if (!"some value".equals(System.getProperty("testValue2"))) {
  77. throw new RuntimeException("Expected system property not specified");
  78. }
  79. if (!"some value".equals(System.getProperty("testValue3"))) {
  80. throw new RuntimeException("Expected system property not specified");
  81. }
  82. }
  83. }
  84. '''
  85. when:
  86. run 'installDist'
  87. def builder = new ScriptExecuter()
  88. builder.workingDir file('build/install/application/bin')
  89. builder.executable "application"
  90. def result = builder.run()
  91. then:
  92. result.assertNormalExitValue()
  93. }
  94. def canUseBothDefaultJvmArgsAndEnvironmentVariableToPassOptionsToJvmWhenRunningScript() {
  95. file("build.gradle") << '''
  96. applicationDefaultJvmArgs = ['-Dvar1=value1', '-Dvar2=some value2']
  97. '''
  98. file('src/main/java/org/gradle/test/Main.java') << '''
  99. package org.gradle.test;
  100. class Main {
  101. public static void main(String[] args) {
  102. if (!"value1".equals(System.getProperty("var1"))) {
  103. throw new RuntimeException("Expected system property not specified");
  104. }
  105. if (!"some value2".equals(System.getProperty("var2"))) {
  106. throw new RuntimeException("Expected system property not specified");
  107. }
  108. if (!"value3".equals(System.getProperty("var3"))) {
  109. throw new RuntimeException("Expected system property not specified");
  110. }
  111. }
  112. }
  113. '''
  114. when:
  115. run 'installDist'
  116. def builder = new ScriptExecuter()
  117. builder.workingDir file('build/install/application/bin')
  118. builder.executable "application"
  119. builder.environment('APPLICATION_OPTS', '-Dvar3=value3')
  120. def result = builder.run()
  121. then:
  122. result.assertNormalExitValue()
  123. }
  124. def canUseDefaultJvmArgsToPassMultipleOptionsWithShellMetacharactersToJvmWhenRunningScript() {
  125. //even in single-quoted multi-line strings, backslashes must still be quoted
  126. file("build.gradle") << '''
  127. applicationDefaultJvmArgs = ['-DtestValue=value',
  128. /-DtestValue2=s\\o"me val'ue/ + '$PATH',
  129. /-DtestValue3=so\\"me value%PATH%/,
  130. ]
  131. '''
  132. file('src/main/java/org/gradle/test/Main.java') << '''
  133. package org.gradle.test;
  134. class Main {
  135. public static void main(String[] args) {
  136. if (!"value".equals(System.getProperty("testValue"))) {
  137. throw new RuntimeException("Expected system property not specified (testValue)");
  138. }
  139. if (!"s\\\\o\\"me val'ue$PATH".equals(System.getProperty("testValue2"))) {
  140. throw new RuntimeException("Expected system property not specified (testValue2)");
  141. }
  142. if (!"so\\\\\\"me value%PATH%".equals(System.getProperty("testValue3"))) {
  143. throw new RuntimeException("Expected system property not specified (testValue3)");
  144. }
  145. }
  146. }
  147. '''
  148. when:
  149. run 'installDist'
  150. def builder = new ScriptExecuter()
  151. builder.workingDir file('build/install/application/bin')
  152. builder.executable "application"
  153. def result = builder.run()
  154. then:
  155. result.assertNormalExitValue()
  156. }
  157. def canUseDefaultJvmArgsInRunTask() {
  158. file("build.gradle") << '''
  159. applicationDefaultJvmArgs = ['-Dvar1=value1', '-Dvar2=value2']
  160. '''
  161. file('src/main/java/org/gradle/test/Main.java') << '''
  162. package org.gradle.test;
  163. class Main {
  164. public static void main(String[] args) {
  165. if (!"value1".equals(System.getProperty("var1"))) {
  166. throw new RuntimeException("Expected system property not specified (var1)");
  167. }
  168. if (!"value2".equals(System.getProperty("var2"))) {
  169. throw new RuntimeException("Expected system property not specified (var2)");
  170. }
  171. }
  172. }
  173. '''
  174. expect:
  175. run 'run'
  176. }
  177. def "can customize application name"() {
  178. file('build.gradle') << '''
  179. applicationName = 'mega-app'
  180. '''
  181. file('src/main/java/org/gradle/test/Main.java') << '''
  182. package org.gradle.test;
  183. class Main {
  184. public static void main(String[] args) {
  185. }
  186. }
  187. '''
  188. when:
  189. run 'installDist', 'distZip', 'distTar'
  190. then:
  191. def installDir = file('build/install/mega-app')
  192. installDir.assertIsDir()
  193. checkApplicationImage('mega-app', installDir)
  194. def distZipFile = file('build/distributions/mega-app.zip')
  195. distZipFile.assertIsFile()
  196. def distZipDir = file('build/unzip')
  197. distZipFile.usingNativeTools().unzipTo(distZipDir)
  198. checkApplicationImage('mega-app', distZipDir.file('mega-app'))
  199. def distTarFile = file('build/distributions/mega-app.tar')
  200. distTarFile.assertIsFile()
  201. def distTarDir = file('build/untar')
  202. distTarFile.usingNativeTools().untarTo(distTarDir)
  203. checkApplicationImage('mega-app', distTarDir.file('mega-app'))
  204. }
  205. def "check distribution contents when all defaults used"() {
  206. file('src/main/java/org/gradle/test/Main.java') << '''
  207. package org.gradle.test;
  208. class Main {
  209. public static void main(String[] args) {
  210. }
  211. }
  212. '''
  213. when:
  214. run 'installDist', 'distZip', 'distTar'
  215. then:
  216. def installDir = file('build/install/application')
  217. installDir.assertIsDir()
  218. checkApplicationImage('application', installDir)
  219. def distZipFile = file('build/distributions/application.zip')
  220. distZipFile.assertIsFile()
  221. def distZipDir = file('build/unzip')
  222. distZipFile.usingNativeTools().unzipTo(distZipDir)
  223. checkApplicationImage('application', distZipDir.file('application'))
  224. def distTarFile = file('build/distributions/application.tar')
  225. distTarFile.assertIsFile()
  226. def distTarDir = file('build/untar')
  227. distTarFile.usingNativeTools().untarTo(distTarDir)
  228. checkApplicationImage('application', distTarDir.file('application'))
  229. }
  230. @Unroll
  231. def "#installTask complains if install directory exists and doesn't look like previous install"() {
  232. setup:
  233. if(deprecatedTask){
  234. executer.withDeprecationChecksDisabled()
  235. }
  236. file('build.gradle') << """
  237. ${installTask}.destinationDir = buildDir
  238. """
  239. when:
  240. runAndFail installTask
  241. then:
  242. result.assertThatCause(startsWith("The specified installation directory '${file('build')}' is neither empty nor does it contain an installation"))
  243. where:
  244. installTask | deprecatedTask
  245. "installApp" | true
  246. "installDist" | false
  247. }
  248. def "startScripts respect OS dependent line separators"() {
  249. file('build.gradle') << '''
  250. installDist.destinationDir = buildDir
  251. '''
  252. when:
  253. run 'startScripts'
  254. then:
  255. File generatedWindowsStartScript = file("build/scripts/application.bat")
  256. generatedWindowsStartScript.exists()
  257. assertLineSeparators(generatedWindowsStartScript, TextUtil.windowsLineSeparator, 90);
  258. File generatedLinuxStartScript = file("build/scripts/application")
  259. generatedLinuxStartScript.exists()
  260. assertLineSeparators(generatedLinuxStartScript, TextUtil.unixLineSeparator, 164);
  261. assertLineSeparators(generatedLinuxStartScript, TextUtil.windowsLineSeparator, 1)
  262. file("build/scripts/application").exists()
  263. }
  264. def "application packages are built when running the assemble task"() {
  265. file('src/main/java/org/gradle/test/Main.java') << '''
  266. package org.gradle.test;
  267. class Main {
  268. public static void main(String[] args) {
  269. }
  270. }
  271. '''
  272. when:
  273. run 'assemble'
  274. then:
  275. def distributionsDir = file('build/distributions')
  276. distributionsDir.assertIsDir()
  277. def distZipFile = file('build/distributions/application.zip')
  278. distZipFile.assertIsFile()
  279. def distZipDir = file('build/unzip')
  280. distZipFile.usingNativeTools().unzipTo(distZipDir)
  281. checkApplicationImage('application', distZipDir.file('application'))
  282. def distTarFile = file('build/distributions/application.tar')
  283. distTarFile.assertIsFile()
  284. def distTarDir = file('build/untar')
  285. distTarFile.usingNativeTools().untarTo(distTarDir)
  286. checkApplicationImage('application', distTarDir.file('application'))
  287. }
  288. def "conventional resources are including in dist"() {
  289. when:
  290. file("src/dist/dir").with {
  291. file("r1.txt") << "r1"
  292. file("r2.txt") << "r2"
  293. }
  294. then:
  295. succeeds "installDist"
  296. and:
  297. def distBase = file("build/install/application")
  298. distBase.file("dir").directory
  299. distBase.file("dir/r1.txt").text == "r1"
  300. distBase.file("dir/r2.txt").text == "r2"
  301. }
  302. def "configure the distribution spec to source from a different dir"() {
  303. when:
  304. file("src/somewhere-else/dir").with {
  305. file("r1.txt") << "r1"
  306. file("r2.txt") << "r2"
  307. }
  308. and:
  309. buildFile << """
  310. applicationDistribution.from("src/somewhere-else") {
  311. include "**/r2.*"
  312. }
  313. """
  314. then:
  315. succeeds "installDist"
  316. and:
  317. def distBase = file("build/install/application")
  318. distBase.file("dir").directory
  319. !distBase.file("dir/r1.txt").exists()
  320. distBase.file("dir/r2.txt").text == "r2"
  321. }
  322. @IgnoreIf({GradleContextualExecuter.parallel})
  323. def "distribution file producing tasks are run automatically"() {
  324. when:
  325. buildFile << """
  326. task createDocs {
  327. def docs = file("\$buildDir/docs")
  328. outputs.dir docs
  329. doLast {
  330. assert docs.mkdirs()
  331. new File(docs, "readme.txt") << "Read me!!!"
  332. }
  333. }
  334. applicationDistribution.from(createDocs) {
  335. into "docs"
  336. rename 'readme(.*)', 'READ-ME\$1'
  337. }
  338. """
  339. then:
  340. succeeds "installDist"
  341. and:
  342. ":createDocs" in nonSkippedTasks
  343. and:
  344. def distBase = file("build/install/application")
  345. distBase.file("docs").directory
  346. !distBase.file("docs/readme.txt").exists()
  347. distBase.file("docs/READ-ME.txt").text == "Read me!!!"
  348. }
  349. private void checkApplicationImage(String applicationName, TestFile installDir) {
  350. installDir.file("bin/${applicationName}").assertIsFile()
  351. installDir.file("bin/${applicationName}.bat").assertIsFile()
  352. installDir.file("lib/application.jar").assertIsFile()
  353. def builder = new ScriptExecuter()
  354. builder.workingDir installDir.file('bin')
  355. builder.executable applicationName
  356. builder.standardOutput = new ByteArrayOutputStream()
  357. builder.errorOutput = new ByteArrayOutputStream()
  358. def result = builder.run()
  359. result.assertNormalExitValue()
  360. }
  361. def assertLineSeparators(TestFile testFile, String lineSeparator, expectedLineCount) {
  362. assert testFile.text.split(lineSeparator).length == expectedLineCount
  363. true
  364. }
  365. }