1 /* 2 * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.testdoc.core.doc; 17 18 import java.io.Serializable; 19 20 import org.apache.commons.lang.NullArgumentException; 21 import org.apache.commons.lang.StringUtils; 22 23 import de.smartics.testdoc.core.BlankArgumentException; 24 import de.smartics.util.source.MethodInfo; 25 import de.smartics.util.source.SourceCodeLocation; 26 27 /** 28 * Provides information about a test method of a scenario. 29 */ 30 public class TestMethodDoc implements Serializable, MethodInfo 31 { 32 // ********************************* Fields ********************************* 33 34 // --- constants ------------------------------------------------------------ 35 36 /** 37 * The class version identifier. 38 * <p> 39 * The value of this constant is {@value}. 40 */ 41 private static final long serialVersionUID = 1L; 42 43 // --- members -------------------------------------------------------------- 44 45 /** 46 * The name of the test. Must not be <code>null</code>. 47 * 48 * @serial 49 */ 50 private final String testName; 51 52 /** 53 * The name of the test translated to a human readable sentence. 54 * 55 * @serial 56 */ 57 private final String testSentence; 58 59 /** 60 * The location of the test. This is the location of the test method. 61 * 62 * @serial 63 */ 64 private final SourceCodeLocation location; 65 66 /** 67 * The optional Javadoc comment of the test method. May be <code>null</code>. 68 * 69 * @serial 70 * @impl If the comment contains only white-spaces or is empty, the value is 71 * silently set to <code>null</code> during construction. 72 */ 73 private final String javadocComment; 74 75 // ****************************** Initializer ******************************* 76 77 // ****************************** Constructors ****************************** 78 79 /** 80 * Default constructor. 81 * 82 * @param testName the name of the test. 83 * @param testSentence the name of the test translated to a human readable 84 * sentence. 85 * @param location the location of the test. 86 * @param javadocComment the optional Javadoc comment of the test method. May 87 * be <code>null</code>. If it contains only whitespaces or is empty, 88 * the value is silently set to <code>null</code>. 89 * @throws IllegalArgumentException if <code>testName</code> or 90 * <code>testSentence</code> is blank or <code>location</code> is 91 * <code>null</code>. 92 */ 93 public TestMethodDoc(final String testName, final String testSentence, 94 final SourceCodeLocation location, final String javadocComment) 95 throws IllegalArgumentException 96 { 97 checkArguments(testName, testSentence, location); 98 99 this.testName = testName; 100 this.testSentence = testSentence; 101 this.location = location; 102 this.javadocComment = 103 StringUtils.isBlank(javadocComment) ? null : javadocComment; 104 } 105 106 // ****************************** Inner Classes ***************************** 107 108 // ********************************* Methods ******************************** 109 110 // --- init ----------------------------------------------------------------- 111 112 private static void checkArguments(final String testName, 113 final String testSentence, final SourceCodeLocation location) 114 { 115 if (StringUtils.isBlank(testName)) 116 { 117 throw new BlankArgumentException("testName"); 118 } 119 if (StringUtils.isBlank(testSentence)) 120 { 121 throw new BlankArgumentException("testSentence"); 122 } 123 if (location == null) 124 { 125 throw new NullArgumentException("location"); 126 } 127 } 128 129 // --- get&set -------------------------------------------------------------- 130 131 /** 132 * Returns the name of the test. Must not be <code>null</code>. 133 * 134 * @return the name of the test. 135 */ 136 public String getTestName() 137 { 138 return testName; 139 } 140 141 /** 142 * Returns the signature of the test method. This includes the test name and 143 * the parameter types list (with brackets). 144 * 145 * @return the signature of the test method. 146 * @todo Currently assumes that the test method has no arguments. 147 */ 148 public String getMethodSignature() 149 { 150 return StringUtils.isNotBlank(testName) ? testName + "()" : null; 151 } 152 153 /** 154 * Returns the name of the test translated to a human readable sentence. 155 * 156 * @return the name of the test translated to a human readable sentence. 157 */ 158 public String getTestSentence() 159 { 160 return testSentence; 161 } 162 163 /** 164 * Returns the location of the test. This is the location of the test method. 165 * 166 * @return the location of the test. 167 */ 168 public SourceCodeLocation getLocation() 169 { 170 return location; 171 } 172 173 /** 174 * Returns the optional Javadoc comment of the test method. May be 175 * <code>null</code>. 176 * 177 * @return the optional Javadoc comment of the test method. 178 */ 179 public String getJavadocComment() 180 { 181 return javadocComment; 182 } 183 184 // --- business ------------------------------------------------------------- 185 186 // --- object basics -------------------------------------------------------- 187 188 /** 189 * Delegates call to {@link java.lang.String#hashCode()}. 190 * 191 * @return the result of the call to {@link java.lang.String#hashCode()}. 192 * @see java.lang.String#hashCode() 193 */ 194 @Override 195 public int hashCode() 196 { 197 int result = 17; 198 result = 37 * result + testName.hashCode(); 199 200 return result; 201 } 202 203 /** 204 * Returns <code>true</code> if the given object is semantically equal to the 205 * given object, <code>false</code> otherwise. 206 * 207 * @param object the instance to compare to. 208 * @return <code>true</code> if the given object is semantically equal to the 209 * given object, <code>false</code> otherwise. 210 */ 211 @Override 212 public boolean equals(final Object object) 213 { 214 if (this == object) 215 { 216 return true; 217 } 218 else if (object == null || getClass() != object.getClass()) 219 { 220 return false; 221 } 222 223 final TestMethodDoc other = (TestMethodDoc) object; 224 225 return (this.testName.equals(other.testName) && this.testSentence 226 .equals(other.testSentence)); 227 } 228 229 /** 230 * Delegates call to {@link java.lang.StringBuilder#toString()}. 231 * 232 * @return the result of the call to 233 * {@link java.lang.StringBuilder#toString()}. 234 * @see java.lang.StringBuilder#toString() 235 */ 236 @Override 237 public String toString() 238 { 239 final StringBuilder buffer = new StringBuilder(); 240 241 buffer.append("Test name : ").append(testName).append(", "); 242 buffer.append("\nSentence : ").append(testSentence); 243 if (location != SourceCodeLocation.UNKNOWN_LOCATION) 244 { 245 buffer.append("\nLocation : ").append(location); 246 } 247 if (javadocComment != null) 248 { 249 buffer.append("\nAPI : ").append(javadocComment); 250 } 251 return buffer.toString(); 252 } 253 }