001 /* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube 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 * SonarQube 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 License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020 package org.sonar.api.batch.sensor.test.internal; 021 022 import com.google.common.base.Preconditions; 023 import org.apache.commons.lang.StringUtils; 024 import org.apache.commons.lang.builder.EqualsBuilder; 025 import org.apache.commons.lang.builder.HashCodeBuilder; 026 import org.sonar.api.batch.fs.InputFile; 027 import org.sonar.api.batch.sensor.SensorStorage; 028 import org.sonar.api.batch.sensor.internal.DefaultStorable; 029 import org.sonar.api.batch.sensor.test.TestCaseCoverage; 030 031 import java.util.Collections; 032 import java.util.List; 033 034 public class DefaultTestCaseCoverage extends DefaultStorable implements TestCaseCoverage { 035 036 private InputFile testFile; 037 private InputFile mainFile; 038 private String name; 039 private List<Integer> lines; 040 041 public DefaultTestCaseCoverage() { 042 super(null); 043 } 044 045 public DefaultTestCaseCoverage(SensorStorage storage) { 046 super(storage); 047 } 048 049 @Override 050 public InputFile testFile() { 051 return testFile; 052 } 053 054 @Override 055 public DefaultTestCaseCoverage testFile(InputFile testFile) { 056 Preconditions.checkNotNull(testFile, "TestFile cannot be null"); 057 Preconditions.checkArgument(testFile.type() == InputFile.Type.TEST, "Should be a test file: " + testFile); 058 this.testFile = testFile; 059 return this; 060 } 061 062 @Override 063 public InputFile coveredFile() { 064 return mainFile; 065 } 066 067 @Override 068 public DefaultTestCaseCoverage cover(InputFile mainFile) { 069 Preconditions.checkNotNull(mainFile, "InputFile cannot be null"); 070 Preconditions.checkArgument(mainFile.type() == InputFile.Type.MAIN, "Should be a main file: " + mainFile); 071 this.mainFile = mainFile; 072 return this; 073 } 074 075 @Override 076 public DefaultTestCaseCoverage testName(String name) { 077 Preconditions.checkArgument(StringUtils.isNotBlank(name), "Test name is mandatory and should not be blank"); 078 this.name = name; 079 return this; 080 } 081 082 @Override 083 public String testName() { 084 return name; 085 } 086 087 @Override 088 public List<Integer> coveredLines() { 089 return Collections.unmodifiableList(lines); 090 } 091 092 @Override 093 public DefaultTestCaseCoverage onLines(List<Integer> lines) { 094 Preconditions.checkNotNull(lines, "Lines list cannot be null"); 095 Preconditions.checkArgument(!lines.isEmpty(), "No need to register test coverage if no line is covered"); 096 this.lines = lines; 097 return this; 098 } 099 100 @Override 101 public void doSave() { 102 Preconditions.checkNotNull(testFile, "TestFile is mandatory"); 103 Preconditions.checkNotNull(mainFile, "MainFile is mandatory"); 104 Preconditions.checkNotNull(name, "Test name is mandatory"); 105 Preconditions.checkNotNull(lines, "Lines are mandatory"); 106 storage.store(this); 107 } 108 109 // Just for unit tests 110 @Override 111 public boolean equals(Object obj) { 112 if (obj == null) { 113 return false; 114 } 115 if (obj == this) { 116 return true; 117 } 118 if (obj.getClass() != getClass()) { 119 return false; 120 } 121 DefaultTestCaseCoverage rhs = (DefaultTestCaseCoverage) obj; 122 return new EqualsBuilder() 123 .append(testFile, rhs.testFile) 124 .append(name, rhs.name) 125 .append(mainFile, rhs.mainFile) 126 .append(lines.toArray(), rhs.lines.toArray()) 127 .isEquals(); 128 } 129 130 @Override 131 public int hashCode() { 132 return new HashCodeBuilder(13, 43) 133 .append(testFile) 134 .append(name) 135 .append(mainFile) 136 .toHashCode(); 137 } 138 }