grails-clover-plugin /test/unit/EventsTests.groovy

Language Groovy Lines 206
MD5 Hash 565cec4180570a7cdd30baaacd126364
Repository https://bitbucket.org/atlassian/grails-clover-plugin View Raw File View Project SPDX
  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
204
205
import org.codehaus.gant.GantBuilder
import org.apache.tools.ant.DefaultLogger
import org.apache.tools.ant.Project
import com.atlassian.clover.api.optimization.StringOptimizable
import com.atlassian.clover.optimization.Snapshot
import com.atlassian.clover.CloverDatabase
import com.atlassian.clover.context.ContextSet
import com.atlassian.clover.CoverageDataSpec
import com.atlassian.clover.util.FileUtils
import com.atlassian.clover.ant.tasks.AntInstrumentationConfig
import com.atlassian.clover.api.optimization.TestOptimizer
import com.atlassian.clover.api.optimization.OptimizationOptions
import org.apache.tools.ant.BuildEvent

/**
 * Tests for some methods from _Events.groovy file
 */
class EventsTests extends GroovyTestCase {

    /** Helper class for logging into buffer */
    static class StringLogger extends DefaultLogger {
        private final List<String> buffer = new LinkedList<String>()

        void log(int level, String message, Throwable t) {
            buffer.add("log:$message")
        }

        void messageLogged(BuildEvent event) {
            buffer.add("messageLogged: $event.message")
        }

        protected void printMessage(String message, PrintStream stream, int priority) {
            buffer.add("printMessage:$message")
        }

        boolean containsFragment(final String fragment) {
            for (String line : buffer) {
                if (line.indexOf(fragment) >= 0) {
                    return true
                }
            }
            return false
        }

        void clear() {
            buffer.clear()
        }
    }

    String projectDir = System.getProperty("project.dir")
    String testRunTmpDir = System.getProperty("testrun.tmpdir")
    Binding binding
    GantBuilder ant
    Object script
    StringLogger antBuildListener = new StringLogger()

    void setUp() {
        ant = new GantBuilder()
        ant.project.addBuildListener(antBuildListener)

        binding = new Binding([projectWorkDir: testRunTmpDir,
                basedir: testRunTmpDir,
                metadata: [],
                ant: ant,
                userHome: testRunTmpDir,
                grailsWorkDir: testRunTmpDir,
                projectTargetDir: testRunTmpDir,
                cloverPluginDir: new File(".")
        ])
        GroovyScriptEngine shell = new GroovyScriptEngine("./scripts/_Events.groovy")
        script = shell.run("_Events.groovy", binding)
    }

    /**
     * Test for configureAntInstr()
     */
    void testConfigureAntInstr() {
        // input data
        ConfigObject clover = new ConfigObject()
        clover.preserve = true
        clover.tmpDir = "/some/tmp/dir"

        // populate into Ant config
        AntInstrumentationConfig antConfig = new AntInstrumentationConfig(ant.project)
        script.configureAntInstr(clover, antConfig)

        // verify values
        assertEquals true, antConfig.isPreserve()
        assertEquals new File("/some/tmp/dir").getAbsolutePath(), antConfig.getTmpDir().getAbsolutePath()
    }

    /**
     * Test for configureLicense()
     */
    void testConfigureLicense() {
        ConfigObject config = new ConfigObject()
        String origLicenseLoc = System.properties.remove('clover.license.path')
        try {
            config.license.path = "/path/to/clover.license"
            script.configureLicense(config)
            assertEquals config.license.path, System.getProperty('clover.license.path')
        }
        finally {
            if (origLicenseLoc) {
                System.setProperty('clover.license.path', origLicenseLoc)
            }
        }
    }

    /**
     * Test for createTestPattern()
     */
    void testCreateTestPattern() {
        assertEquals "Something", script.createTestPattern("Something")
        assertEquals "Some", script.createTestPattern("SomeTests")
    }

    /**
     * Test for scanForSourceFiles
     */
//    void testScanForSourceFiles() {
//        // search for the following files
//        //  /test
//        //     /unit
//        //        MyTest.java        - OK
//        //        Something.groovy   - OK
//        //     /integration
//        //        MyIT.java          - OK
//        //        NotATest.txt       - NOT OK
//
//        GrailsTestTargetPattern testTargetPattern = new GrailsTestTargetPattern("**/")
//
//        List<File> unitTests = script.scanForSourceFiles(testTargetPattern, script.binding, "unit")
//        List<File> integrationTests = script.scanForSourceFiles(testTargetPattern, script.binding, "integration")
//
//        assertEquals 2, unitTests.size()
//        assertEquals 1, integrationTests.size()
//    }

    /**
     * Test for toggleAntLogging()
     * @throws Exception
     */
    void testToggleAntLogging() {
        ConfigObject config = new ConfigObject()
        config.debug = true
        int outputLevel = 0 // this gets modified by toggleAntLogging
        def logger = [setMessageOutputLevel: {outputLevel = it}] as DefaultLogger
        ant.project.addBuildListener(logger)
        script.toggleAntLogging config
        println outputLevel
        assertTrue outputLevel == Project.MSG_DEBUG
    }


    /**
     * Test for toggleCloverOn()
     */
    void testToggleCloverOn() {
        ConfigObject cloverConfig = new ConfigObject()
        cloverConfig.on = true
        cloverConfig.initstring = "my_clover.db"

        antBuildListener.clear()
        script.toggleCloverOn(cloverConfig)
        assertTrue antBuildListener.containsFragment("Clover is enabled with initstring")
        assertTrue antBuildListener.containsFragment("my_clover.db")
    }

    /**
     * Some calls of Clover API are made through reflections. Check
     */
    void testReflections() {
        // input data
        List optimizables = []
        optimizables << new StringOptimizable("AbcTest")

        // prepare empty database and snapshot file
        File dbFile = File.createTempFile("clover", "clover.db")
        dbFile.delete()
        File snapshotFile = File.createTempFile("clover", "clover.snapshot")
        snapshotFile.delete()
        CloverDatabase db = new CloverDatabase(dbFile, false, "My Project", new ContextSet(), new CoverageDataSpec())
        Snapshot snapshot = Snapshot.generateFor(db, snapshotFile.absolutePath)
        snapshot.store()

        // run optimization - as there are no classes in snapshot it shall return all input
        def builder = new OptimizationOptions.Builder()
        def options = builder
                .enabled(true)
                .initString(dbFile.absolutePath)
                .snapshot(snapshotFile)
                .build()
        def optimizer = new TestOptimizer(options)
        List optimizedTests = optimizer.optimizeObjects(optimizables)

        // validation: one element having getName() method and returning "AbcTest"
        assertEquals 1, optimizedTests.size()
        optimizedTests.each {
            // Clover Cover<->Clover Grails interface test - check call of getName() via reflections works
            final String className = (String)it.getClass().getMethod("getName").invoke(it)
            assertEquals "AbcTest", className
        }
    }
}
Back to Top