|
| 1 | +/* |
| 2 | + * SonarQube Java |
| 3 | + * Copyright (C) 2012-2016 SonarSource SA |
| 4 | + * mailto:contact AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.java.se; |
| 21 | + |
| 22 | +import com.google.common.collect.Lists; |
| 23 | +import org.junit.Test; |
| 24 | +import org.sonar.java.cfg.CFG; |
| 25 | +import org.sonar.java.se.checks.NullDereferenceCheck; |
| 26 | +import org.sonar.java.se.checks.SECheck; |
| 27 | +import org.sonar.plugins.java.api.JavaFileScannerContext; |
| 28 | +import org.sonar.plugins.java.api.tree.Tree; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.fest.assertions.Assertions.assertThat; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | + |
| 35 | +public class CheckerDispatcherTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void test_only_one_execution_of_post_statement_by_check() { |
| 39 | + List<SECheck> checks = Lists.newArrayList(new NullDereferenceCheck(), new CheckTest(), new CheckTest(), new CheckTest()); |
| 40 | + CheckerDispatcher checkerDispatcher = new CheckerDispatcher(mockExplodedGraphWalker(), mock(JavaFileScannerContext.class), checks); |
| 41 | + checkerDispatcher.executeCheckPostStatement(mock(Tree.class)); |
| 42 | + for (SECheck check : checks) { |
| 43 | + if(check instanceof CheckTest) { |
| 44 | + assertThat(((CheckTest) check).postStatementExecution).isEqualTo(1); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static ExplodedGraphWalker mockExplodedGraphWalker() { |
| 50 | + ExplodedGraphWalker explodedGraphWalker = mock(ExplodedGraphWalker.class); |
| 51 | + explodedGraphWalker.programPosition = new ExplodedGraph.ProgramPoint(new CFG.Block(1), 0); |
| 52 | + explodedGraphWalker.programState = mock(ProgramState.class); |
| 53 | + explodedGraphWalker.node = new ExplodedGraph.Node(explodedGraphWalker.programPosition, explodedGraphWalker.programState); |
| 54 | + return explodedGraphWalker; |
| 55 | + } |
| 56 | + |
| 57 | + private static class CheckTest extends SECheck { |
| 58 | + int postStatementExecution = 0; |
| 59 | + |
| 60 | + @Override |
| 61 | + public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) { |
| 62 | + postStatementExecution++; |
| 63 | + return mock(ProgramState.class); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments