1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.report.check; 13 14 import static org.junit.Assert.assertEquals; 15 import static org.junit.Assert.assertNull; 16 17 import org.jacoco.core.analysis.CoverageNodeImpl; 18 import org.jacoco.core.analysis.ICounter.CounterValue; 19 import org.jacoco.core.analysis.ICoverageNode.CounterEntity; 20 import org.jacoco.core.internal.analysis.CounterImpl; 21 import org.junit.Before; 22 import org.junit.Test; 23 24 /** 25 * Unit tests for {@link Limit}. 26 */ 27 public class LimitTest { 28 29 private Limit limit; 30 31 @Before 32 public void setup() { 33 limit = new Limit(); 34 } 35 36 @Test 37 public void testDefaults() { 38 assertNull(limit.getMinimum()); 39 assertNull(limit.getMaximum()); 40 assertEquals(CounterEntity.INSTRUCTION, limit.getEntity()); 41 assertEquals(CounterValue.COVEREDRATIO, limit.getValue()); 42 } 43 44 @Test 45 public void testTotalCount() { 46 limit.setValue(CounterValue.TOTALCOUNT.name()); 47 limit.setMaximum("-1"); 48 assertEquals(CounterValue.TOTALCOUNT, limit.getValue()); 49 assertEquals( 50 "instructions total count is 0, but expected maximum is -1", 51 limit.check(new TestNode())); 52 } 53 54 @Test 55 public void testMissedCount() { 56 limit.setValue(CounterValue.MISSEDCOUNT.name()); 57 limit.setMaximum("-1"); 58 assertEquals(CounterValue.MISSEDCOUNT, limit.getValue()); 59 assertEquals( 60 "instructions missed count is 0, but expected maximum is -1", 61 limit.check(new TestNode())); 62 } 63 64 @Test 65 public void testCoveredCount() { 66 limit.setValue(CounterValue.COVEREDCOUNT.name()); 67 limit.setMaximum("-1"); 68 assertEquals(CounterValue.COVEREDCOUNT, limit.getValue()); 69 assertEquals( 70 "instructions covered count is 0, but expected maximum is -1", 71 limit.check(new TestNode())); 72 } 73 74 @Test 75 public void testMissedRatio() { 76 limit.setValue(CounterValue.MISSEDRATIO.name()); 77 limit.setMaximum("-1"); 78 assertEquals(CounterValue.MISSEDRATIO, limit.getValue()); 79 assertEquals( 80 "instructions missed ratio is 0, but expected maximum is -1", 81 limit.check(new TestNode() { 82 { 83 instructionCounter = CounterImpl.COUNTER_0_1; 84 } 85 })); 86 } 87 88 @Test 89 public void testCoveredRatio() { 90 limit.setValue(CounterValue.COVEREDRATIO.name()); 91 limit.setMaximum("-1"); 92 assertEquals(CounterValue.COVEREDRATIO, limit.getValue()); 93 assertEquals( 94 "instructions covered ratio is 0, but expected maximum is -1", 95 limit.check(new TestNode() { 96 { 97 instructionCounter = CounterImpl.COUNTER_1_0; 98 } 99 })); 100 } 101 102 @Test 103 public void testInstruction() { 104 limit.setValue(CounterValue.TOTALCOUNT.name()); 105 limit.setCounter(CounterEntity.INSTRUCTION.name()); 106 limit.setMaximum("-1"); 107 assertEquals(CounterEntity.INSTRUCTION, limit.getEntity()); 108 assertEquals( 109 "instructions total count is 0, but expected maximum is -1", 110 limit.check(new TestNode())); 111 } 112 113 @Test 114 public void testBranch() { 115 limit.setValue(CounterValue.TOTALCOUNT.name()); 116 limit.setCounter(CounterEntity.BRANCH.name()); 117 limit.setMaximum("-1"); 118 assertEquals(CounterEntity.BRANCH, limit.getEntity()); 119 assertEquals("branches total count is 0, but expected maximum is -1", 120 limit.check(new TestNode())); 121 } 122 123 @Test 124 public void testLine() { 125 limit.setValue(CounterValue.TOTALCOUNT.name()); 126 limit.setCounter(CounterEntity.LINE.name()); 127 limit.setMaximum("-1"); 128 assertEquals(CounterEntity.LINE, limit.getEntity()); 129 assertEquals("lines total count is 0, but expected maximum is -1", 130 limit.check(new TestNode())); 131 } 132 133 @Test 134 public void testComlexity() { 135 limit.setValue(CounterValue.TOTALCOUNT.name()); 136 limit.setCounter(CounterEntity.COMPLEXITY.name()); 137 limit.setMaximum("-1"); 138 assertEquals(CounterEntity.COMPLEXITY, limit.getEntity()); 139 assertEquals("complexity total count is 0, but expected maximum is -1", 140 limit.check(new TestNode())); 141 } 142 143 @Test 144 public void testClass() { 145 limit.setValue(CounterValue.TOTALCOUNT.name()); 146 limit.setCounter(CounterEntity.CLASS.name()); 147 limit.setMaximum("-1"); 148 assertEquals(CounterEntity.CLASS, limit.getEntity()); 149 assertEquals("classes total count is 0, but expected maximum is -1", 150 limit.check(new TestNode())); 151 } 152 153 @Test 154 public void testMethod() { 155 limit.setValue(CounterValue.TOTALCOUNT.name()); 156 limit.setCounter(CounterEntity.METHOD.name()); 157 limit.setMaximum("-1"); 158 assertEquals(CounterEntity.METHOD, limit.getEntity()); 159 assertEquals("methods total count is 0, but expected maximum is -1", 160 limit.check(new TestNode())); 161 } 162 163 @Test 164 public void testNoRatio() { 165 assertNull(limit.check(new TestNode() { 166 { 167 instructionCounter = CounterImpl.COUNTER_0_0; 168 } 169 })); 170 } 171 172 @Test 173 public void testNoLimits() { 174 assertNull(limit.check(new TestNode() { 175 { 176 instructionCounter = CounterImpl.getInstance(1000, 0); 177 } 178 })); 179 } 180 181 @Test 182 public void testMin0() { 183 limit.setMinimum("0"); 184 limit.setMinimum((String) null); 185 assertNull(limit.getMinimum()); 186 } 187 188 @Test 189 public void testMin1() { 190 limit.setMinimum("0.35"); 191 assertEquals("0.35", limit.getMinimum()); 192 assertNull(limit.check(new TestNode() { 193 { 194 instructionCounter = CounterImpl.getInstance(65, 35); 195 } 196 })); 197 } 198 199 @Test 200 public void testMin2() { 201 limit.setMinimum("0.35"); 202 assertEquals("0.35", limit.getMinimum()); 203 assertNull(limit.check(new TestNode() { 204 { 205 instructionCounter = CounterImpl.getInstance(64, 36); 206 } 207 })); 208 } 209 210 @Test 211 public void testMin3() { 212 limit.setMinimum("0.3500"); 213 assertEquals("0.3500", limit.getMinimum()); 214 assertEquals( 215 "instructions covered ratio is 0.3400, but expected minimum is 0.3500", 216 limit.check(new TestNode() { 217 { 218 instructionCounter = CounterImpl.getInstance(66, 34); 219 } 220 })); 221 } 222 223 @Test 224 public void testMin4() { 225 limit.setMinimum("0.35"); 226 assertEquals("0.35", limit.getMinimum()); 227 assertEquals( 228 "instructions covered ratio is 0.34, but expected minimum is 0.35", 229 limit.check(new TestNode() { 230 { 231 instructionCounter = CounterImpl.getInstance(65001, 232 34999); 233 } 234 })); 235 } 236 237 @Test 238 public void testMin5() { 239 limit.setMinimum("10000"); 240 limit.setValue(CounterValue.MISSEDCOUNT.name()); 241 assertEquals("10000", limit.getMinimum()); 242 assertEquals( 243 "instructions missed count is 9990, but expected minimum is 10000", 244 limit.check(new TestNode() { 245 { 246 instructionCounter = CounterImpl.getInstance(9990, 0); 247 } 248 })); 249 } 250 251 @Test 252 public void testMin6() { 253 limit.setMinimum("12345"); 254 assertEquals("12345", limit.getMinimum()); 255 assertEquals( 256 "instructions covered ratio is 0, but expected minimum is 12345", 257 limit.check(new TestNode() { 258 { 259 instructionCounter = CounterImpl.getInstance(1, 999); 260 } 261 })); 262 } 263 264 @Test 265 public void testMinPercent() { 266 limit.setMinimum("1.55%"); 267 assertEquals("0.0155", limit.getMinimum()); 268 269 limit.setMinimum("1.5%"); 270 assertEquals("0.015", limit.getMinimum()); 271 272 limit.setMinimum("1.00%"); 273 assertEquals("0.0100", limit.getMinimum()); 274 275 limit.setMinimum("1%"); 276 assertEquals("0.01", limit.getMinimum()); 277 } 278 279 @Test 280 public void testMax0() { 281 limit.setMaximum("0"); 282 limit.setMaximum((String) null); 283 assertNull(limit.getMaximum()); 284 } 285 286 @Test 287 public void testMax1() { 288 limit.setMaximum("12345678"); 289 limit.setValue(CounterValue.MISSEDCOUNT.name()); 290 assertEquals("12345678", limit.getMaximum()); 291 assertNull(limit.check(new TestNode() { 292 { 293 instructionCounter = CounterImpl.getInstance(12345678, 0); 294 } 295 })); 296 } 297 298 @Test 299 public void testMax2() { 300 limit.setMaximum("0.999"); 301 assertEquals("0.999", limit.getMaximum()); 302 assertNull(limit.check(new TestNode() { 303 { 304 instructionCounter = CounterImpl.getInstance(1, 99); 305 } 306 })); 307 } 308 309 @Test 310 public void testMax3() { 311 limit.setMaximum("0.999"); 312 assertEquals("0.999", limit.getMaximum()); 313 assertEquals( 314 "instructions covered ratio is 1.000, but expected maximum is 0.999", 315 limit.check(new TestNode() { 316 { 317 instructionCounter = CounterImpl.getInstance(0, 1); 318 } 319 })); 320 } 321 322 @Test 323 public void testMax4() { 324 limit.setMaximum("0.999"); 325 assertEquals("0.999", limit.getMaximum()); 326 assertEquals( 327 "instructions covered ratio is 1.000, but expected maximum is 0.999", 328 limit.check(new TestNode() { 329 { 330 instructionCounter = CounterImpl.getInstance(999, 331 999001); 332 } 333 })); 334 } 335 336 @Test 337 public void testMaxPercent() { 338 limit.setMaximum("1.55%"); 339 assertEquals("0.0155", limit.getMaximum()); 340 341 limit.setMaximum("1.5%"); 342 assertEquals("0.015", limit.getMaximum()); 343 344 limit.setMaximum("1.00%"); 345 assertEquals("0.0100", limit.getMaximum()); 346 347 limit.setMaximum("1%"); 348 assertEquals("0.01", limit.getMaximum()); 349 } 350 351 private static class TestNode extends CoverageNodeImpl { 352 353 public TestNode() { 354 super(ElementType.CLASS, "Foo"); 355 } 356 357 } 358 359 } 360