PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/EventsTests.groovy

https://bitbucket.org/atlassian/grails-clover-plugin
Groovy | 205 lines | 129 code | 24 blank | 52 comment | 4 complexity | 565cec4180570a7cdd30baaacd126364 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. import org.codehaus.gant.GantBuilder
  2. import org.apache.tools.ant.DefaultLogger
  3. import org.apache.tools.ant.Project
  4. import com.atlassian.clover.api.optimization.StringOptimizable
  5. import com.atlassian.clover.optimization.Snapshot
  6. import com.atlassian.clover.CloverDatabase
  7. import com.atlassian.clover.context.ContextSet
  8. import com.atlassian.clover.CoverageDataSpec
  9. import com.atlassian.clover.util.FileUtils
  10. import com.atlassian.clover.ant.tasks.AntInstrumentationConfig
  11. import com.atlassian.clover.api.optimization.TestOptimizer
  12. import com.atlassian.clover.api.optimization.OptimizationOptions
  13. import org.apache.tools.ant.BuildEvent
  14. /**
  15. * Tests for some methods from _Events.groovy file
  16. */
  17. class EventsTests extends GroovyTestCase {
  18. /** Helper class for logging into buffer */
  19. static class StringLogger extends DefaultLogger {
  20. private final List<String> buffer = new LinkedList<String>()
  21. void log(int level, String message, Throwable t) {
  22. buffer.add("log:$message")
  23. }
  24. void messageLogged(BuildEvent event) {
  25. buffer.add("messageLogged: $event.message")
  26. }
  27. protected void printMessage(String message, PrintStream stream, int priority) {
  28. buffer.add("printMessage:$message")
  29. }
  30. boolean containsFragment(final String fragment) {
  31. for (String line : buffer) {
  32. if (line.indexOf(fragment) >= 0) {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. void clear() {
  39. buffer.clear()
  40. }
  41. }
  42. String projectDir = System.getProperty("project.dir")
  43. String testRunTmpDir = System.getProperty("testrun.tmpdir")
  44. Binding binding
  45. GantBuilder ant
  46. Object script
  47. StringLogger antBuildListener = new StringLogger()
  48. void setUp() {
  49. ant = new GantBuilder()
  50. ant.project.addBuildListener(antBuildListener)
  51. binding = new Binding([projectWorkDir: testRunTmpDir,
  52. basedir: testRunTmpDir,
  53. metadata: [],
  54. ant: ant,
  55. userHome: testRunTmpDir,
  56. grailsWorkDir: testRunTmpDir,
  57. projectTargetDir: testRunTmpDir,
  58. cloverPluginDir: new File(".")
  59. ])
  60. GroovyScriptEngine shell = new GroovyScriptEngine("./scripts/_Events.groovy")
  61. script = shell.run("_Events.groovy", binding)
  62. }
  63. /**
  64. * Test for configureAntInstr()
  65. */
  66. void testConfigureAntInstr() {
  67. // input data
  68. ConfigObject clover = new ConfigObject()
  69. clover.preserve = true
  70. clover.tmpDir = "/some/tmp/dir"
  71. // populate into Ant config
  72. AntInstrumentationConfig antConfig = new AntInstrumentationConfig(ant.project)
  73. script.configureAntInstr(clover, antConfig)
  74. // verify values
  75. assertEquals true, antConfig.isPreserve()
  76. assertEquals new File("/some/tmp/dir").getAbsolutePath(), antConfig.getTmpDir().getAbsolutePath()
  77. }
  78. /**
  79. * Test for configureLicense()
  80. */
  81. void testConfigureLicense() {
  82. ConfigObject config = new ConfigObject()
  83. String origLicenseLoc = System.properties.remove('clover.license.path')
  84. try {
  85. config.license.path = "/path/to/clover.license"
  86. script.configureLicense(config)
  87. assertEquals config.license.path, System.getProperty('clover.license.path')
  88. }
  89. finally {
  90. if (origLicenseLoc) {
  91. System.setProperty('clover.license.path', origLicenseLoc)
  92. }
  93. }
  94. }
  95. /**
  96. * Test for createTestPattern()
  97. */
  98. void testCreateTestPattern() {
  99. assertEquals "Something", script.createTestPattern("Something")
  100. assertEquals "Some", script.createTestPattern("SomeTests")
  101. }
  102. /**
  103. * Test for scanForSourceFiles
  104. */
  105. // void testScanForSourceFiles() {
  106. // // search for the following files
  107. // // /test
  108. // // /unit
  109. // // MyTest.java - OK
  110. // // Something.groovy - OK
  111. // // /integration
  112. // // MyIT.java - OK
  113. // // NotATest.txt - NOT OK
  114. //
  115. // GrailsTestTargetPattern testTargetPattern = new GrailsTestTargetPattern("**/")
  116. //
  117. // List<File> unitTests = script.scanForSourceFiles(testTargetPattern, script.binding, "unit")
  118. // List<File> integrationTests = script.scanForSourceFiles(testTargetPattern, script.binding, "integration")
  119. //
  120. // assertEquals 2, unitTests.size()
  121. // assertEquals 1, integrationTests.size()
  122. // }
  123. /**
  124. * Test for toggleAntLogging()
  125. * @throws Exception
  126. */
  127. void testToggleAntLogging() {
  128. ConfigObject config = new ConfigObject()
  129. config.debug = true
  130. int outputLevel = 0 // this gets modified by toggleAntLogging
  131. def logger = [setMessageOutputLevel: {outputLevel = it}] as DefaultLogger
  132. ant.project.addBuildListener(logger)
  133. script.toggleAntLogging config
  134. println outputLevel
  135. assertTrue outputLevel == Project.MSG_DEBUG
  136. }
  137. /**
  138. * Test for toggleCloverOn()
  139. */
  140. void testToggleCloverOn() {
  141. ConfigObject cloverConfig = new ConfigObject()
  142. cloverConfig.on = true
  143. cloverConfig.initstring = "my_clover.db"
  144. antBuildListener.clear()
  145. script.toggleCloverOn(cloverConfig)
  146. assertTrue antBuildListener.containsFragment("Clover is enabled with initstring")
  147. assertTrue antBuildListener.containsFragment("my_clover.db")
  148. }
  149. /**
  150. * Some calls of Clover API are made through reflections. Check
  151. */
  152. void testReflections() {
  153. // input data
  154. List optimizables = []
  155. optimizables << new StringOptimizable("AbcTest")
  156. // prepare empty database and snapshot file
  157. File dbFile = File.createTempFile("clover", "clover.db")
  158. dbFile.delete()
  159. File snapshotFile = File.createTempFile("clover", "clover.snapshot")
  160. snapshotFile.delete()
  161. CloverDatabase db = new CloverDatabase(dbFile, false, "My Project", new ContextSet(), new CoverageDataSpec())
  162. Snapshot snapshot = Snapshot.generateFor(db, snapshotFile.absolutePath)
  163. snapshot.store()
  164. // run optimization - as there are no classes in snapshot it shall return all input
  165. def builder = new OptimizationOptions.Builder()
  166. def options = builder
  167. .enabled(true)
  168. .initString(dbFile.absolutePath)
  169. .snapshot(snapshotFile)
  170. .build()
  171. def optimizer = new TestOptimizer(options)
  172. List optimizedTests = optimizer.optimizeObjects(optimizables)
  173. // validation: one element having getName() method and returning "AbcTest"
  174. assertEquals 1, optimizedTests.size()
  175. optimizedTests.each {
  176. // Clover Cover<->Clover Grails interface test - check call of getName() via reflections works
  177. final String className = (String)it.getClass().getMethod("getName").invoke(it)
  178. assertEquals "AbcTest", className
  179. }
  180. }
  181. }