1 /******************************************************************************* 2 * Copyright (c) 2009, 2015 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.core.test.validation; 13 14 import org.jacoco.core.analysis.ICounter; 15 import org.jacoco.core.test.validation.targets.Target01; 16 import org.junit.Test; 17 18 /** 19 * Tests of basic Java control structures. 20 */ 21 public class ControlStructuresTest extends ValidationTestBase { 22 23 public ControlStructuresTest() { 24 super(Target01.class); 25 } 26 27 @Override 28 protected void run(final Class<?> targetClass) throws Exception { 29 final Object instance = targetClass.newInstance(); 30 ((Runnable) instance).run(); 31 } 32 33 @Test 34 public void testCoverageResult() { 35 36 // 1. Direct unconditional execution 37 assertLine("unconditional", ICounter.FULLY_COVERED); 38 39 // 2. Missed if block 40 assertLine("iffalse", ICounter.FULLY_COVERED, 1, 1); 41 assertLine("missedif", ICounter.NOT_COVERED); 42 assertLine("executedelse", ICounter.FULLY_COVERED); 43 44 // 3. Executed if block 45 assertLine("iftrue", ICounter.FULLY_COVERED, 1, 1); 46 assertLine("executedif", ICounter.FULLY_COVERED); 47 assertLine("missedelse", ICounter.NOT_COVERED); 48 49 // 4. Missed while block 50 // ECJ and javac produce different status here 51 assertLine("whilefalse", 1, 1); 52 assertLine("missedwhile", ICounter.NOT_COVERED); 53 54 // 5. Always true while block 55 assertLine("whiletrue", ICounter.FULLY_COVERED, 1, 1); 56 57 // 6. Executed while block 58 assertLine("whiletruefalse", ICounter.FULLY_COVERED, 0, 2); 59 assertLine("executedwhile", ICounter.FULLY_COVERED); 60 61 // 7. Executed do while block 62 assertLine("executeddowhile", ICounter.FULLY_COVERED); 63 64 // 8. Missed for block 65 assertLine("missedforincrementer", ICounter.PARTLY_COVERED, 1, 1); 66 assertLine("missedfor", ICounter.NOT_COVERED); 67 68 // 9. Executed for block 69 assertLine("executedforincrementer", ICounter.FULLY_COVERED, 0, 2); 70 assertLine("executedfor", ICounter.FULLY_COVERED); 71 72 // 10. Missed for each block 73 assertLine("missedforeachincrementer", ICounter.PARTLY_COVERED, 1, 1); 74 assertLine("missedforeach", ICounter.NOT_COVERED); 75 76 // 11. Executed for each block 77 assertLine("executedforeachincrementer", ICounter.FULLY_COVERED, 0, 2); 78 assertLine("executedforeach", ICounter.FULLY_COVERED); 79 80 // 12. Table switch with hit 81 assertLine("tswitch1", ICounter.FULLY_COVERED, 3, 1); 82 assertLine("tswitch1case1", ICounter.NOT_COVERED); 83 assertLine("tswitch1case2", ICounter.FULLY_COVERED); 84 assertLine("tswitch1case3", ICounter.NOT_COVERED); 85 assertLine("tswitch1default", ICounter.NOT_COVERED); 86 87 // 13. Continued table switch with hit 88 assertLine("tswitch2", ICounter.FULLY_COVERED, 3, 1); 89 assertLine("tswitch2case1", ICounter.NOT_COVERED); 90 assertLine("tswitch2case2", ICounter.FULLY_COVERED); 91 assertLine("tswitch2case3", ICounter.FULLY_COVERED); 92 assertLine("tswitch2default", ICounter.FULLY_COVERED); 93 94 // 14. Table switch without hit 95 assertLine("tswitch2", ICounter.FULLY_COVERED, 3, 1); 96 assertLine("tswitch3case1", ICounter.NOT_COVERED); 97 assertLine("tswitch3case2", ICounter.NOT_COVERED); 98 assertLine("tswitch3case3", ICounter.NOT_COVERED); 99 assertLine("tswitch3default", ICounter.FULLY_COVERED); 100 101 // 15. Lookup switch with hit 102 assertLine("lswitch1", ICounter.FULLY_COVERED, 3, 1); 103 assertLine("lswitch1case1", ICounter.NOT_COVERED); 104 assertLine("lswitch1case2", ICounter.FULLY_COVERED); 105 assertLine("lswitch1case3", ICounter.NOT_COVERED); 106 assertLine("lswitch1default", ICounter.NOT_COVERED); 107 108 // 16. Continued lookup switch with hit 109 assertLine("lswitch2", ICounter.FULLY_COVERED, 3, 1); 110 assertLine("lswitch2case1", ICounter.NOT_COVERED); 111 assertLine("lswitch2case2", ICounter.FULLY_COVERED); 112 assertLine("lswitch2case3", ICounter.FULLY_COVERED); 113 assertLine("lswitch2default", ICounter.FULLY_COVERED); 114 115 // 17. Lookup switch without hit 116 assertLine("lswitch3", ICounter.FULLY_COVERED, 3, 1); 117 assertLine("lswitch3case1", ICounter.NOT_COVERED); 118 assertLine("lswitch3case2", ICounter.NOT_COVERED); 119 assertLine("lswitch3case3", ICounter.NOT_COVERED); 120 assertLine("lswitch3default", ICounter.FULLY_COVERED); 121 122 // 18. Break statement 123 assertLine("executedbreak", ICounter.FULLY_COVERED); 124 assertLine("missedafterbreak", ICounter.NOT_COVERED); 125 126 // 19. Continue statement 127 assertLine("executedcontinue", ICounter.FULLY_COVERED); 128 assertLine("missedaftercontinue", ICounter.NOT_COVERED); 129 130 // 20. Return statement 131 assertLine("return", ICounter.FULLY_COVERED); 132 assertLine("afterreturn", ICounter.NOT_COVERED); 133 134 // 21. Implicit return 135 assertLine("implicitreturn", ICounter.FULLY_COVERED); 136 137 } 138 139 } 140