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.TestCaseExecution; 030 031 import javax.annotation.CheckForNull; 032 import javax.annotation.Nullable; 033 034 public class DefaultTestCaseExecution extends DefaultStorable implements TestCaseExecution { 035 036 private InputFile testFile; 037 private String name; 038 private Long duration; 039 private TestCaseExecution.Status status = Status.OK; 040 private String message; 041 private TestCaseExecution.Type type = Type.UNIT; 042 private String stackTrace; 043 044 public DefaultTestCaseExecution() { 045 super(null); 046 } 047 048 public DefaultTestCaseExecution(SensorStorage storage) { 049 super(storage); 050 } 051 052 @Override 053 public DefaultTestCaseExecution inTestFile(InputFile testFile) { 054 Preconditions.checkNotNull(testFile, "TestFile cannot be null"); 055 Preconditions.checkArgument(testFile.type() == InputFile.Type.TEST, "Should be a test file: " + testFile); 056 this.testFile = testFile; 057 return this; 058 } 059 060 @Override 061 public DefaultTestCaseExecution name(String name) { 062 Preconditions.checkArgument(StringUtils.isNotBlank(name), "Test name is mandatory and should not be blank"); 063 this.name = name; 064 return this; 065 } 066 067 @Override 068 public DefaultTestCaseExecution durationInMs(long duration) { 069 Preconditions.checkArgument(duration >= 0, "Test duration must be positive (got: " + duration + ")"); 070 this.duration = duration; 071 return this; 072 } 073 074 @Override 075 public DefaultTestCaseExecution status(TestCaseExecution.Status status) { 076 Preconditions.checkNotNull(status); 077 this.status = status; 078 return this; 079 } 080 081 @Override 082 public DefaultTestCaseExecution message(@Nullable String message) { 083 this.message = message; 084 return this; 085 } 086 087 @Override 088 public DefaultTestCaseExecution ofType(TestCaseExecution.Type type) { 089 Preconditions.checkNotNull(type); 090 this.type = type; 091 return this; 092 } 093 094 @Override 095 public DefaultTestCaseExecution stackTrace(@Nullable String stackTrace) { 096 this.stackTrace = stackTrace; 097 return this; 098 } 099 100 @Override 101 public InputFile testFile() { 102 return testFile; 103 } 104 105 @CheckForNull 106 @Override 107 public Long durationInMs() { 108 return duration; 109 } 110 111 @Override 112 public Type type() { 113 return type; 114 } 115 116 @Override 117 public Status status() { 118 return status; 119 } 120 121 @Override 122 public String name() { 123 return name; 124 } 125 126 @CheckForNull 127 @Override 128 public String message() { 129 return message; 130 } 131 132 @CheckForNull 133 @Override 134 public String stackTrace() { 135 return stackTrace; 136 } 137 138 @Override 139 public void doSave() { 140 Preconditions.checkNotNull(testFile, "TestFile is mandatory"); 141 Preconditions.checkNotNull(name, "Test name is mandatory"); 142 storage.store(this); 143 } 144 145 // Just for unit tests 146 @Override 147 public boolean equals(Object obj) { 148 if (obj == null) { 149 return false; 150 } 151 if (obj == this) { 152 return true; 153 } 154 if (obj.getClass() != getClass()) { 155 return false; 156 } 157 DefaultTestCaseExecution rhs = (DefaultTestCaseExecution) obj; 158 return new EqualsBuilder() 159 .append(testFile, rhs.testFile) 160 .append(name, rhs.name) 161 .append(duration, rhs.duration) 162 .append(status, rhs.status) 163 .append(message, rhs.message) 164 .append(type, rhs.type) 165 .append(stackTrace, rhs.stackTrace) 166 .isEquals(); 167 } 168 169 @Override 170 public int hashCode() { 171 return new HashCodeBuilder(13, 43) 172 .append(testFile) 173 .append(name) 174 .append(duration) 175 .append(status) 176 .append(message) 177 .append(type) 178 .append(stackTrace) 179 .toHashCode(); 180 } 181 }