/src/test/groovy/execute/ExecuteTest.groovy

https://github.com/groovy/groovy-core · Groovy · 150 lines · 117 code · 9 blank · 24 comment · 8 complexity · a5546cccc04e89f3f46de7573137ac06 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package groovy.execute
  20. /**
  21. * Cross platform tests for the DGM#execute() family of methods.
  22. *
  23. * @author Paul King
  24. */
  25. class ExecuteTest extends GroovyTestCase {
  26. private String getCmd() {
  27. def cmd = "ls -l"
  28. if (System.properties.'os.name'.startsWith('Windows ')) {
  29. cmd = "cmd /c dir"
  30. }
  31. return cmd
  32. }
  33. void testExecuteCommandLineProcessUsingAString() {
  34. println "Executing String command: $cmd"
  35. StringBuffer sbout = new StringBuffer()
  36. StringBuffer sberr = new StringBuffer()
  37. def process = cmd.execute()
  38. process.waitForProcessOutput sbout, sberr
  39. def value = process.exitValue()
  40. int count = sbout.toString().readLines().size()
  41. println "Exit value: $value, Err lines: ${sberr.toString().readLines().size()}, Out lines: $count"
  42. assert count > 1
  43. assert value == 0
  44. }
  45. void testExecuteCommandLineProcessUsingAStringArray() {
  46. def cmdArray = cmd.split(' ')
  47. println "Executing String[] command: $cmdArray"
  48. StringBuffer sbout = new StringBuffer()
  49. StringBuffer sberr = new StringBuffer()
  50. def process = cmdArray.execute()
  51. process.waitForProcessOutput sbout, sberr
  52. def value = process.exitValue()
  53. int count = sbout.toString().readLines().size()
  54. println "Exit value: $value, Err lines: ${sberr.toString().readLines().size()}, Out lines: $count"
  55. assert count > 1
  56. assert value == 0
  57. }
  58. void testExecuteCommandLineProcessUsingAList() {
  59. List<String> cmdList = Arrays.asList(cmd.split(' '))
  60. println "Executing List<String> command: $cmdList"
  61. StringBuffer sbout = new StringBuffer()
  62. StringBuffer sberr = new StringBuffer()
  63. def process = cmdList.execute()
  64. process.waitForProcessOutput sbout, sberr
  65. def value = process.exitValue()
  66. int count = sbout.toString().readLines().size()
  67. println "Exit value: $value, Err lines: ${sberr.toString().readLines().size()}, Out lines: $count"
  68. assert count > 1
  69. assert value == 0
  70. }
  71. void testExecuteCommandLineProcessAndUseWaitForOrKill() {
  72. String[] java = [System.getProperty('java.home') + "/bin/java",
  73. "-classpath",
  74. System.getProperty('java.class.path'),
  75. "groovy.ui.GroovyMain",
  76. "-e",
  77. "sleep(2000); println('Done'); System.exit(0)"]
  78. println "Executing this command for two cases:\n${java.join(' ')}"
  79. StringBuffer sbout = new StringBuffer()
  80. StringBuffer sberr = new StringBuffer()
  81. def process = java.execute()
  82. def tout = process.consumeProcessOutputStream(sbout)
  83. def terr = process.consumeProcessErrorStream(sberr)
  84. process.waitForOrKill(60000)
  85. tout.join()
  86. terr.join()
  87. def value = process.exitValue()
  88. int count = sbout.toString().readLines().size()
  89. println "Heaps of time case: Exit value: $value, Err lines: ${sberr.toString().readLines().size()}, Out lines: $count"
  90. System.err.println 'std err: ' + sberr.toString()
  91. System.err.println 'std out: ' + sbout.toString()
  92. // assert sbout.toString().contains('Done'), "Expected 'Done' but found: " + sbout.toString() + " with error: " + sberr.toString()
  93. assert value == 0
  94. sbout = new StringBuffer()
  95. sberr = new StringBuffer()
  96. process = java.execute()
  97. process.pipeTo(process)
  98. tout = process.consumeProcessOutputStream(sbout)
  99. terr = process.consumeProcessErrorStream(sberr)
  100. process.waitForOrKill(500)
  101. tout.join()
  102. terr.join()
  103. value = process.exitValue()
  104. count = sbout.toString().readLines().size()
  105. println "Insufficient time case: Exit value: $value, Err lines: ${sberr.toString().readLines().size()}, Out lines: $count"
  106. assert !sbout.toString().contains('Done')
  107. assert value != 0 // should have been killed
  108. }
  109. void testExecuteCommandLineUnderWorkingDirectory() {
  110. println "Executing command in dir '..': $cmd"
  111. StringBuffer sbout = new StringBuffer()
  112. StringBuffer sberr = new StringBuffer()
  113. def process = cmd.execute(null, new File('..'))
  114. process.waitForProcessOutput sbout, sberr
  115. def value = process.exitValue()
  116. int count = sbout.toString().readLines().size()
  117. println "Exit value: $value, Err lines: ${sberr.toString().readLines().size()}, Out lines: $count"
  118. assert count > 1
  119. assert value == 0
  120. }
  121. void testExecuteCommandLineWithEnvironmentProperties() {
  122. String[] java = [System.getProperty('java.home') + "/bin/java",
  123. "-classpath",
  124. System.getProperty('java.class.path'),
  125. "groovy.ui.GroovyMain",
  126. "-e",
  127. "println(System.getenv('foo'))"]
  128. println "Executing this command:\n${java.join(' ')}"
  129. def props = ["foo=bar"]
  130. println "With these props: $props"
  131. StringBuffer sbout = new StringBuffer()
  132. StringBuffer sberr = new StringBuffer()
  133. def process = java.execute(props, null)
  134. process.waitForProcessOutput sbout, sberr
  135. def value = process.exitValue()
  136. int count = sbout.toString().readLines().size()
  137. println "Exit value: $value, Err lines: ${sberr.toString().readLines().size()}, Out lines: $count"
  138. assert sbout.toString().contains('bar')
  139. assert value == 0
  140. }
  141. }