Search in sources :

Example 1 with Statistics

use of com.disney.groovity.stats.GroovityStatistics.Statistics in project groovity by disney.

the class GroovityTestMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        if (skip) {
            return;
        }
        getLog().info("STARTING Groovity test");
        populateSystemProperties();
        ClassLoader testLoader = createClassLoader(ClassLoaderScope.TEST);
        GroovityServletContainerBuilder builder = new GroovityServletContainerBuilder().setPort(Integer.valueOf(port)).setWebappDirectory(new File(project.getBasedir(), "src/main/webapp")).setClassLoader(testLoader);
        if (groovitySourceDirectory != null && groovitySourceDirectory.exists()) {
            builder.addSourceLocation(groovitySourceDirectory.toURI(), true);
        }
        if (groovityTestDirectory != null && groovityTestDirectory.exists()) {
            builder.addSourceLocation(groovityTestDirectory.toURI(), true);
        }
        if (sources != null) {
            for (File source : sources) {
                builder.addSourceLocation(source.toURI(), true);
            }
        }
        GroovityStatistics.reset();
        GroovityServletContainer container = builder.build();
        container.start();
        Groovity groovity = container.getGroovity();
        ArrayList<String> appSources = new ArrayList<String>();
        try {
            if (failOnError) {
                validateFactory(groovity);
            }
            if (skipTests) {
                return;
            }
            GroovitySourceLocator[] sourceLocators = groovity.getSourceLocators();
            for (GroovitySourceLocator locator : sourceLocators) {
                if (!interactive && ((FileGroovitySourceLocator) locator).getDirectory().equals(groovityTestDirectory)) {
                    if (path != null) {
                        String[] pathParts = path.split("\\s*,\\s*");
                        for (String pathPart : pathParts) {
                            container.run(pathPart);
                        }
                    } else {
                        for (GroovitySource source : locator) {
                            String scriptPath = source.getPath();
                            scriptPath = scriptPath.substring(0, scriptPath.lastIndexOf("."));
                            String scriptName = scriptPath.substring(scriptPath.lastIndexOf('/') + 1);
                            if (scriptName.startsWith("test")) {
                                container.run(scriptPath);
                            }
                        }
                    }
                }
                if (((FileGroovitySourceLocator) locator).getDirectory().equals(groovitySourceDirectory)) {
                    for (GroovitySource source : locator) {
                        appSources.add(source.getPath());
                    }
                }
            }
            if (interactive) {
                // in interactive mode we wait for instructions and compile each time to allow
                // real-time development
                container.enterConsole();
            }
        } finally {
            container.stop();
        }
        Map<String, CodeCoverage> coverageMap = new TreeMap<String, GroovityTestMojo.CodeCoverage>();
        Collection<Class<Script>> scriptClasses = groovity.getGroovityScriptClasses();
        for (Class<Script> sc : scriptClasses) {
            String sourcePath = groovity.getSourcePath(sc);
            if (appSources.contains(sourcePath)) {
                String scriptLabel = sourcePath.substring(0, sourcePath.length() - 5);
                CodeCoverage cc = new CodeCoverage(sc, scriptLabel);
                if (cc.isCoverable()) {
                    coverageMap.put(scriptLabel, cc);
                }
                for (Class<?> c : ((GroovityClassLoader) sc.getClassLoader()).getLoadedClasses()) {
                    if (!c.equals(sc) && !(Closure.class.isAssignableFrom(c)) && !c.isInterface()) {
                        String cname = getClassLabel(c);
                        int p = 0;
                        if ((p = cname.indexOf("$Trait")) > 0) {
                            cname = cname.substring(0, p);
                        }
                        String classLabel = scriptLabel + "->" + cname;
                        CodeCoverage icc = new CodeCoverage(c, classLabel);
                        if (icc.isCoverable()) {
                            coverageMap.put(classLabel, icc);
                        }
                    }
                }
            }
        }
        List<Statistics> stats = GroovityStatistics.getStatistics();
        for (Statistics stat : stats) {
            String sks = stat.key.toString();
            int dot = sks.indexOf(".");
            if (dot > 0) {
                String className = sks.substring(0, dot);
                String method = sks.substring(dot + 1);
                CodeCoverage cc = coverageMap.get(className);
                if (cc != null) {
                    if (method.equals("run()") && cc.runnable) {
                        cc.ran = true;
                    } else {
                        if (cc.methods.containsKey(method)) {
                            cc.methods.put(method, true);
                        }
                    }
                }
            }
        }
        Collection<CodeCoverage> ccs = coverageMap.values();
        double total = 0;
        for (CodeCoverage cc : ccs) {
            total += cc.getCoverage();
        }
        total /= ccs.size();
        getLog().info("TEST COVERAGE " + ((int) (100 * total)) + "% TOTAL");
        for (Entry<String, CodeCoverage> entry : coverageMap.entrySet()) {
            CodeCoverage cc = entry.getValue();
            double covered = cc.getCoverage();
            getLog().info(" " + ((int) (100 * covered)) + "% coverage for " + cc.label);
            if (covered < 1.0) {
                if (cc.runnable && !cc.ran) {
                    getLog().warn("   Script body did not run during tests");
                }
                List<String> uncovered = cc.getUncoveredMethods();
                if (!uncovered.isEmpty()) {
                    for (String m : cc.getUncoveredMethods()) {
                        getLog().warn("   " + m + " did not execute during tests");
                    }
                }
            }
        /*
				for(String m: cc.getCoveredMethods()) {
					getLog().info("   " + m + " executed during tests");
				}
				*/
        }
    } catch (MojoFailureException e) {
        throw e;
    } catch (Throwable e) {
        getLog().error("ERROR in Groovity test", e);
        throw new MojoFailureException(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) FileGroovitySourceLocator(com.disney.groovity.source.FileGroovitySourceLocator) Groovity(com.disney.groovity.Groovity) GroovityClassLoader(com.disney.groovity.compile.GroovityClassLoader) GroovitySource(com.disney.groovity.source.GroovitySource) GroovityServletContainer(com.disney.groovity.servlet.container.GroovityServletContainer) Script(groovy.lang.Script) GroovityServletContainerBuilder(com.disney.groovity.servlet.container.GroovityServletContainerBuilder) MojoFailureException(org.apache.maven.plugin.MojoFailureException) GroovityClassLoader(com.disney.groovity.compile.GroovityClassLoader) TreeMap(java.util.TreeMap) GroovityStatistics(com.disney.groovity.stats.GroovityStatistics) Statistics(com.disney.groovity.stats.GroovityStatistics.Statistics) GatherStatistics(com.disney.groovity.compile.GatherStatistics) GroovitySourceLocator(com.disney.groovity.source.GroovitySourceLocator) FileGroovitySourceLocator(com.disney.groovity.source.FileGroovitySourceLocator) File(java.io.File)

Aggregations

Groovity (com.disney.groovity.Groovity)1 GatherStatistics (com.disney.groovity.compile.GatherStatistics)1 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)1 GroovityServletContainer (com.disney.groovity.servlet.container.GroovityServletContainer)1 GroovityServletContainerBuilder (com.disney.groovity.servlet.container.GroovityServletContainerBuilder)1 FileGroovitySourceLocator (com.disney.groovity.source.FileGroovitySourceLocator)1 GroovitySource (com.disney.groovity.source.GroovitySource)1 GroovitySourceLocator (com.disney.groovity.source.GroovitySourceLocator)1 GroovityStatistics (com.disney.groovity.stats.GroovityStatistics)1 Statistics (com.disney.groovity.stats.GroovityStatistics.Statistics)1 Script (groovy.lang.Script)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1