001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.plugins.core.sensors;
021    
022    import org.sonar.api.batch.Decorator;
023    import org.sonar.api.batch.DecoratorContext;
024    import org.sonar.api.batch.DependedUpon;
025    import org.sonar.api.measures.CoreMetrics;
026    import org.sonar.api.measures.Measure;
027    import org.sonar.api.measures.MeasureUtils;
028    import org.sonar.api.measures.Metric;
029    import org.sonar.api.resources.Project;
030    import org.sonar.api.resources.Resource;
031    import org.sonar.api.resources.ResourceUtils;
032    
033    import java.util.Arrays;
034    import java.util.Collection;
035    import java.util.List;
036    
037    public class UnitTestDecorator implements Decorator {
038    
039      @DependedUpon
040      public List<Metric> generatesMetrics() {
041        return Arrays.asList(CoreMetrics.TEST_EXECUTION_TIME, CoreMetrics.TESTS, CoreMetrics.TEST_ERRORS, CoreMetrics.TEST_FAILURES, CoreMetrics.TEST_SUCCESS_DENSITY);
042      }
043    
044      public boolean shouldExecuteOnProject(Project project) {
045        return !Project.AnalysisType.STATIC.equals(project.getAnalysisType());
046      }
047    
048      public boolean shouldDecorateResource(Resource resource, DecoratorContext context) {
049        return context.getMeasure(CoreMetrics.TESTS) == null && (ResourceUtils.isUnitTestClass(resource) || !ResourceUtils.isEntity(resource));
050      }
051    
052      public void decorate(Resource resource, DecoratorContext context) {
053        if (shouldDecorateResource(resource, context)) {
054          sumChildren(context, CoreMetrics.TEST_EXECUTION_TIME);
055          sumChildren(context, CoreMetrics.SKIPPED_TESTS);
056          Double tests = sumChildren(context, CoreMetrics.TESTS);
057          Double errors = sumChildren(context, CoreMetrics.TEST_ERRORS);
058          Double failures = sumChildren(context, CoreMetrics.TEST_FAILURES);
059    
060          if (isPositive(tests, true) && isPositive(errors, false) && isPositive(failures, false)) {
061            Double errorsAndFailuresRatio = (errors + failures) * 100.0 / tests;
062            context.saveMeasure(CoreMetrics.TEST_SUCCESS_DENSITY, 100.0 - errorsAndFailuresRatio);
063          }
064        }
065      }
066    
067      private boolean isPositive(Double d, boolean strict) {
068        return d != null && (strict ? d > 0.0 : d >= 0.0);
069      }
070    
071      private Double sumChildren(DecoratorContext jobContext, Metric metric) {
072        Collection<Measure> childrenMeasures = jobContext.getChildrenMeasures(metric);
073        if (childrenMeasures != null && !childrenMeasures.isEmpty()) {
074          Double sum = 0.0;
075          boolean hasChildrenMeasures = false;
076          for (Measure measure : childrenMeasures) {
077            if (MeasureUtils.hasValue(measure)) {
078              sum += measure.getValue();
079              hasChildrenMeasures = true;
080            }
081          }
082          if (hasChildrenMeasures) {
083            jobContext.saveMeasure(metric, sum);
084            return sum;
085          }
086        }
087        return null;
088      }
089    
090      @Override
091      public String toString() {
092        return getClass().getSimpleName();
093      }
094    }