├── .github ├── FUNDING.yml └── workflows │ └── codeql-analysis.yml ├── .gitignore ├── .travis.yml ├── JDepend-LICENSE ├── LICENSE ├── README.md ├── code-assert-core ├── images │ └── packages.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── guru │ │ └── nidi │ │ └── codeassert │ │ ├── Analyzer.java │ │ ├── AnalyzerException.java │ │ ├── AnalyzerResult.java │ │ ├── config │ │ ├── Action.java │ │ ├── ActionResult.java │ │ ├── AnalyzerConfig.java │ │ ├── BaseCollector.java │ │ ├── CollectorConfig.java │ │ ├── CollectorTemplate.java │ │ ├── For.java │ │ ├── Ignore.java │ │ ├── In.java │ │ ├── Language.java │ │ ├── Location.java │ │ ├── LocationMatcher.java │ │ ├── LocationNameMatcher.java │ │ ├── Minima.java │ │ ├── NamedLocation.java │ │ ├── ProjectLayout.java │ │ ├── UsageCounter.java │ │ └── ValuedLocation.java │ │ ├── dependency │ │ ├── AmbiguousRuleException.java │ │ ├── CodeElement.java │ │ ├── Dependencies.java │ │ ├── DependencyAnalyzer.java │ │ ├── DependencyCollector.java │ │ ├── DependencyCycleMatcher.java │ │ ├── DependencyEntry.java │ │ ├── DependencyMap.java │ │ ├── DependencyResult.java │ │ ├── DependencyResultMatcher.java │ │ ├── DependencyRule.java │ │ ├── DependencyRuler.java │ │ ├── DependencyRules.java │ │ ├── MatcherUtils.java │ │ ├── RuleAccessor.java │ │ ├── Tarjan.java │ │ └── Usage.java │ │ ├── io │ │ ├── ModelVisualizer.java │ │ └── Visualized.java │ │ ├── jacoco │ │ ├── Coverage.java │ │ ├── CoverageCollector.java │ │ ├── CoverageMatcher.java │ │ ├── CoverageType.java │ │ ├── Coverages.java │ │ ├── JacocoAnalyzer.java │ │ └── JacocoResult.java │ │ ├── junit │ │ ├── CodeAssertCoreJunit5Test.java │ │ ├── CodeAssertCoreMatchers.java │ │ ├── CodeAssertCoreTest.java │ │ ├── CodeAssertCoreTestBase.java │ │ ├── CodeAssertTestType.java │ │ └── UnusedActionsMatcher.java │ │ ├── model │ │ ├── AttributeInfo.java │ │ ├── ClassFileParser.java │ │ ├── CodeClass.java │ │ ├── CodeClassBuilder.java │ │ ├── CodePackage.java │ │ ├── Constant.java │ │ ├── ConstantPool.java │ │ ├── InternalTypeInPublicApiMatcher.java │ │ ├── MemberInfo.java │ │ ├── Model.java │ │ ├── ModelBuilder.java │ │ ├── PublicMemberInInternalTypeMatcher.java │ │ ├── Scope.java │ │ ├── SignatureParser.java │ │ ├── SourceFileParser.java │ │ ├── UsingElement.java │ │ └── UsingElementMatcher.java │ │ └── util │ │ ├── CountSet.java │ │ ├── CountingInputStream.java │ │ ├── ListUtils.java │ │ ├── ProxyInputStream.java │ │ └── ResultMatcher.java │ └── test │ ├── java │ ├── CodeCoverage.java │ └── guru │ │ └── nidi │ │ └── codeassert │ │ ├── EatYourOwnDogfoodTest.java │ │ ├── config │ │ ├── AnalyzerConfigTest.java │ │ ├── LocationMatcherTest.java │ │ └── LocationNameMatcherTest.java │ │ ├── dependency │ │ ├── CycleTest.java │ │ ├── DependencyRulesTest.java │ │ ├── a │ │ │ ├── A1.java │ │ │ ├── A2.java │ │ │ ├── a │ │ │ │ └── Aa1.java │ │ │ └── b │ │ │ │ └── Ab1.java │ │ ├── b │ │ │ ├── B1.java │ │ │ ├── B2.java │ │ │ ├── a │ │ │ │ ├── Ba1.java │ │ │ │ └── Ba2.java │ │ │ └── b │ │ │ │ └── Bb1.java │ │ └── c │ │ │ ├── C1.java │ │ │ ├── C2.java │ │ │ ├── a │ │ │ └── Ca1.java │ │ │ └── b │ │ │ └── Cb1.java │ │ ├── io │ │ └── ModelVisualizerTest.java │ │ ├── jacoco │ │ └── JacocoTest.java │ │ ├── junit │ │ ├── EmptyCodeAssertCoreJunit5Test.java │ │ └── EmptyCodeAssertCoreTest.java │ │ ├── model │ │ ├── AnalyzerTest.java │ │ ├── ClassFileParserTest.java │ │ ├── ExampleAbstractClass.java │ │ ├── ExampleAnnotation.java │ │ ├── ExampleConcreteClass.java │ │ ├── ExampleInterface.java │ │ ├── Path.java │ │ ├── SourceFileParserTest.java │ │ ├── p1 │ │ │ └── ExampleInnerAnnotation.java │ │ ├── p2 │ │ │ └── ExampleEnum.java │ │ ├── p3 │ │ │ └── ExampleSecondEnum.java │ │ ├── p4 │ │ │ ├── GenericParameters.java │ │ │ ├── SubGenericParameters.java │ │ │ ├── p1 │ │ │ │ └── Type1.java │ │ │ ├── p10 │ │ │ │ └── Type10.java │ │ │ ├── p2 │ │ │ │ └── Type2.java │ │ │ ├── p3 │ │ │ │ └── Type3.java │ │ │ ├── p4 │ │ │ │ └── Type4.java │ │ │ ├── p5 │ │ │ │ └── Type5.java │ │ │ ├── p6 │ │ │ │ └── Type6.java │ │ │ ├── p7 │ │ │ │ └── Type7.java │ │ │ ├── p8 │ │ │ │ └── Type8.java │ │ │ └── p9 │ │ │ │ └── Type9.java │ │ └── p5 │ │ │ ├── Annotations.java │ │ │ ├── ClassRetentionAnnotation.java │ │ │ ├── SourceRetentionAnnotation.java │ │ │ └── package-info.java │ │ ├── snippets │ │ ├── CodeCoverage.java │ │ ├── CodeTest.java │ │ └── DependencyTest.java │ │ └── util │ │ └── ListUtilsTest.java │ └── resources │ ├── example_class1.bin │ ├── example_class2.bin │ ├── jacoco.csv │ ├── test.jar │ └── test.zip ├── code-assert-gui ├── pom.xml └── src │ ├── main │ ├── java │ │ └── guru │ │ │ └── nidi │ │ │ └── codeassert │ │ │ └── gui │ │ │ ├── AppController.java │ │ │ ├── Application.java │ │ │ ├── CodeClassSerializer.java │ │ │ └── CodePackageSerializer.java │ └── resources │ │ └── public │ │ ├── app.js │ │ └── index.html │ └── test │ ├── java │ └── guru │ │ └── nidi │ │ └── codeassert │ │ └── gui │ │ └── CodeTest.java │ └── resources │ └── logback-test.xml ├── code-assert-maven-plugin ├── pom.xml └── src │ └── main │ └── java │ └── guru │ └── nidi │ └── codeassert │ ├── AssertMojo.java │ └── PrepareMojo.java ├── code-assert ├── pom.xml └── src │ ├── main │ ├── java │ │ └── guru │ │ │ └── nidi │ │ │ └── codeassert │ │ │ ├── checkstyle │ │ │ ├── CheckstyleAnalyzer.java │ │ │ ├── CheckstyleConfigs.java │ │ │ ├── CheckstyleMatcher.java │ │ │ ├── CheckstyleResult.java │ │ │ ├── StyleChecks.java │ │ │ └── StyleEventCollector.java │ │ │ ├── detekt │ │ │ ├── DetektAnalyzer.java │ │ │ ├── DetektCollector.java │ │ │ ├── DetektMatcher.java │ │ │ ├── DetektResult.java │ │ │ └── TypedDetektFinding.java │ │ │ ├── findbugs │ │ │ ├── BugCollector.java │ │ │ ├── FindBugsAnalyzer.java │ │ │ ├── FindBugsConfigs.java │ │ │ ├── FindBugsMatcher.java │ │ │ ├── FindBugsResult.java │ │ │ └── PluginLoader.java │ │ │ ├── junit │ │ │ ├── CodeAssertJunit5Test.java │ │ │ ├── CodeAssertMatchers.java │ │ │ ├── CodeAssertTest.java │ │ │ ├── CodeAssertTestBase.java │ │ │ └── kotlin │ │ │ │ └── KotlinCodeAssertMatchers.java │ │ │ ├── ktlint │ │ │ ├── KtlintAnalyzer.java │ │ │ ├── KtlintCollector.java │ │ │ ├── KtlintMatcher.java │ │ │ ├── KtlintResult.java │ │ │ └── LocatedLintError.java │ │ │ └── pmd │ │ │ ├── CpdAnalyzer.java │ │ │ ├── CpdMatchCollector.java │ │ │ ├── CpdMatcher.java │ │ │ ├── CpdResult.java │ │ │ ├── JavaUtilLoggerConfigurer.java │ │ │ ├── PmdAnalyzer.java │ │ │ ├── PmdConfigs.java │ │ │ ├── PmdMatcher.java │ │ │ ├── PmdResult.java │ │ │ ├── PmdRuleset.java │ │ │ ├── PmdRulesets.java │ │ │ ├── PmdUtils.java │ │ │ ├── PmdViolationCollector.java │ │ │ └── PropertyField.java │ └── resources │ │ └── guru │ │ └── nidi │ │ └── codeassert │ │ ├── checkstyle │ │ ├── google_checks.xml │ │ └── sun_checks.xml │ │ └── detekt │ │ └── default-detekt-config.yml │ └── test │ ├── java │ ├── CodeCoverage.java │ └── guru │ │ └── nidi │ │ └── codeassert │ │ ├── Bugs.java │ │ ├── Bugs2.java │ │ ├── EatYourOwnDogfoodTest.java │ │ ├── checkstyle │ │ └── CheckstyleTest.java │ │ ├── detekt │ │ └── DetektAnalyzerTest.java │ │ ├── findbugs │ │ └── FindBugsTest.java │ │ ├── ktlint │ │ └── KtlintAnalyzerTest.java │ │ ├── pmd │ │ ├── PmdTest.java │ │ └── RegexMatcher.java │ │ └── snippets │ │ ├── CheckstyleTest.java │ │ ├── CodeCoverage.java │ │ ├── CodeTest.java │ │ ├── DependencyTest.java │ │ ├── DetektTest.java │ │ ├── FindBugsTest.java │ │ ├── KtlintTest.java │ │ ├── PmdTest.java │ │ └── ReuseTest.java │ └── kotlin │ └── guru │ └── nidi │ └── codeassert │ └── Linker.kt ├── owasp-suppression.xml └── pom.xml /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [nidi3] 4 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ master ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ master ] 20 | schedule: 21 | - cron: '17 13 * * 1' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | 28 | strategy: 29 | fail-fast: false 30 | matrix: 31 | language: [ 'java', 'javascript' ] 32 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 33 | # Learn more: 34 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 35 | 36 | steps: 37 | - name: Checkout repository 38 | uses: actions/checkout@v2 39 | 40 | # Initializes the CodeQL tools for scanning. 41 | - name: Initialize CodeQL 42 | uses: github/codeql-action/init@v1 43 | with: 44 | languages: ${{ matrix.language }} 45 | # If you wish to specify custom queries, you can do so here or in a config file. 46 | # By default, queries listed here will override any specified in a config file. 47 | # Prefix the list here with "+" to use these queries and those in the config file. 48 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 49 | 50 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 51 | # If this step fails, then you should remove it and run the build manually (see below) 52 | - name: Autobuild 53 | uses: github/codeql-action/autobuild@v1 54 | 55 | # ℹ️ Command-line programs to run using the OS shell. 56 | # 📚 https://git.io/JvXDl 57 | 58 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 59 | # and modify them (or add more) to build your code if your project 60 | # uses a compiled language 61 | 62 | #- run: | 63 | # make bootstrap 64 | # make release 65 | 66 | - name: Perform CodeQL Analysis 67 | uses: github/codeql-action/analyze@v1 68 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | *.iml 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: bash 2 | dist: xenial 3 | os: 4 | - windows 5 | - linux 6 | 7 | env: 8 | - JDK="adopt@1.8.0-242" 9 | 10 | cache: 11 | directories: 12 | - $HOME/.m2 13 | 14 | before_install: 15 | - curl "https://raw.githubusercontent.com/nidi3/Mavis-CI/master/mavis.sh" --output mavis.sh 16 | - source mavis.sh 17 | - install_jdk 18 | - install_maven 19 | 20 | before_script: 21 | before_script 22 | 23 | script: 24 | script 25 | 26 | after_success: 27 | - bash <(curl -s https://codecov.io/bash) 28 | -------------------------------------------------------------------------------- /JDepend-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 1999-2004 Clarkware Consulting, Inc. 2 | All Rights Reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 15 | 3. Neither the name of Clarkware Consulting, Inc. nor the names of its 16 | contributors may be used to endorse or promote products derived 17 | from this software without prior written permission. For written 18 | permission, please contact clarkware@clarkware.com. 19 | 20 | THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 21 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 22 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 23 | CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 26 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 29 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /code-assert-core/images/packages.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nidi3/code-assert/a722c20b1541cabc42f08eb4d33fd46e9745da84/code-assert-core/images/packages.png -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/Analyzer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert; 17 | 18 | public interface Analyzer { 19 | AnalyzerResult analyze(); 20 | } 21 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/AnalyzerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert; 17 | 18 | public class AnalyzerException extends RuntimeException { 19 | public AnalyzerException(String message) { 20 | super(message); 21 | } 22 | 23 | public AnalyzerException(String message, Throwable cause) { 24 | super(message, cause); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/AnalyzerResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert; 17 | 18 | import java.util.List; 19 | 20 | public class AnalyzerResult { 21 | private final Analyzer analyzer; 22 | private final T findings; 23 | private final List unusedActions; 24 | 25 | public AnalyzerResult(Analyzer analyzer, T findings, List unusedActions) { 26 | this.analyzer = analyzer; 27 | this.findings = findings; 28 | this.unusedActions = unusedActions; 29 | } 30 | 31 | public Analyzer analyzer() { 32 | return analyzer; 33 | } 34 | 35 | public T findings() { 36 | return findings; 37 | } 38 | 39 | public List unusedActions() { 40 | return unusedActions; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/Action.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | /** 19 | * Actions are created using the In and For classes. 20 | * 21 | * @param the type of issues this action works on. 22 | * @see In 23 | * @see For 24 | */ 25 | public interface Action { 26 | ActionResult accept(T issue); 27 | } 28 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/ActionResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | public class ActionResult { 19 | final boolean accept; 20 | final Action action; 21 | final int quality; 22 | 23 | public ActionResult(boolean accept, Action action, int quality) { 24 | this.accept = accept; 25 | this.action = action; 26 | this.quality = quality; 27 | } 28 | 29 | public static ActionResult accept(Action action, int quality) { 30 | return new ActionResult(true, action, quality); 31 | } 32 | 33 | public static ActionResult reject(Action action, int quality) { 34 | return new ActionResult(false, action, quality); 35 | } 36 | 37 | public static ActionResult undecided(Action action) { 38 | return new ActionResult(false, action, 0); 39 | } 40 | 41 | public static ActionResult acceptIfTrue(boolean accept, Action action, int quality) { 42 | return accept ? accept(action, quality) : undecided(action); 43 | } 44 | 45 | public static ActionResult rejectIfFalse(boolean accept, Action action, int quality) { 46 | return accept ? undecided(action) : reject(action, quality); 47 | } 48 | 49 | public boolean isUndecided() { 50 | return quality == 0; 51 | } 52 | 53 | public ActionResult orMoreQuality(ActionResult result) { 54 | return quality >= result.quality ? this : result; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/CollectorConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | import java.util.List; 19 | 20 | import static guru.nidi.codeassert.util.ListUtils.andJoin; 21 | import static java.util.Arrays.asList; 22 | 23 | public final class CollectorConfig { 24 | public final String reason; 25 | public final boolean ignoreUnused; 26 | public final List actions; 27 | 28 | private CollectorConfig(String reason, boolean ignoreUnused, List actions) { 29 | this.reason = reason; 30 | this.ignoreUnused = ignoreUnused; 31 | this.actions = actions; 32 | } 33 | 34 | public CollectorConfig ignoringUnused() { 35 | return new CollectorConfig<>(reason, true, actions); 36 | } 37 | 38 | @SafeVarargs 39 | public static CollectorConfig because(String reason, A... actions) { 40 | return new CollectorConfig<>(reason, false, asList(actions)); 41 | } 42 | 43 | @SafeVarargs 44 | public static CollectorConfig just(A... actions) { 45 | return because(null, actions); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return andJoin(" " + (reason == null ? "just" : ("because " + reason)), actions); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/CollectorTemplate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | import java.util.*; 19 | 20 | public final class CollectorTemplate 21 | extends BaseCollector> implements Iterable> { 22 | private final List> configs = new ArrayList<>(); 23 | 24 | private CollectorTemplate() { 25 | } 26 | 27 | public static CollectorTemplate forA(Class> coll) { 28 | return new CollectorTemplate<>(); 29 | } 30 | 31 | public static CollectorTemplate of(Class action) { 32 | return new CollectorTemplate<>(); 33 | } 34 | 35 | @Override 36 | public Iterator> iterator() { 37 | return configs.iterator(); 38 | } 39 | 40 | @Override 41 | protected CollectorTemplate config(CollectorConfig... configs) { 42 | for (final CollectorConfig config : configs) { 43 | this.configs.add(config.ignoringUnused()); 44 | } 45 | return this; 46 | } 47 | 48 | @Override 49 | public ActionResult accept(Object issue) { 50 | throw new UnsupportedOperationException( 51 | "This is just a template. Apply it to a real Collector using .configs()"); 52 | } 53 | 54 | @Override 55 | protected ActionResult doAccept(Object issue, A action) { 56 | throw new UnsupportedOperationException( 57 | "This is just a template. Apply it to a real Collector using .configs()"); 58 | } 59 | 60 | @Override 61 | protected List unused(UsageCounter counter) { 62 | throw new UnsupportedOperationException( 63 | "This is just a template. Apply it to a real Collector using .configs()"); 64 | } 65 | 66 | @Override 67 | public String toString() { 68 | return configs.toString(); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/For.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | public final class For { 19 | private final Location loc; 20 | private final String pack; 21 | private final String clazz; 22 | 23 | private For(Location loc, String pack, String clazz) { 24 | this.loc = loc; 25 | this.pack = pack; 26 | this.clazz = clazz; 27 | } 28 | 29 | public static For global() { 30 | return new For(null, "", ""); 31 | } 32 | 33 | public static For allPackages() { 34 | return new For(null, "*", ""); 35 | } 36 | 37 | public static For allClasses() { 38 | return new For(null, "*", "*"); 39 | } 40 | 41 | public static For thePackage(String pack) { 42 | return new For(null, pack, ""); 43 | } 44 | 45 | public static For allInPackage(String pack) { 46 | return new For(null, pack, "*"); 47 | } 48 | 49 | public static For loc(String loc) { 50 | return new For(Location.of(loc), null, null); 51 | } 52 | 53 | public static For clazz(Class clazz) { 54 | return loc(clazz.getName()); 55 | } 56 | 57 | public Minima setMinima(int... values) { 58 | return new Minima(loc, pack, clazz, values); 59 | } 60 | 61 | public Minima setNoMinima() { 62 | return new Minima(loc, pack, clazz); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/Ignore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | import java.util.List; 19 | 20 | public class Ignore implements Action { 21 | private final LocationNameMatcher matcher; 22 | 23 | Ignore(List locs, List names) { 24 | matcher = new LocationNameMatcher(locs, names); 25 | } 26 | 27 | @Override 28 | public ActionResult accept(NamedLocation namedLocation) { 29 | final boolean matches = matcher.matches(namedLocation.name, namedLocation.language, 30 | namedLocation.className, namedLocation.method, namedLocation.strictNameMatch); 31 | return ActionResult.rejectIfFalse(!matches, this, 5); 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return " ignore " + matcher.toString(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/Language.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | import java.util.List; 19 | 20 | import static java.util.Arrays.asList; 21 | 22 | public enum Language { 23 | JAVA("java", asList(".java")), 24 | KOTLIN("kotlin", asList(".kt", ".kts")), 25 | SCALA("scala", asList(".scala")), 26 | GROOVY("groovy", asList(".groovy", ".gvy", ".gy", ".gsh")); 27 | 28 | final String path; 29 | final List suffices; 30 | 31 | Language(String path, List suffices) { 32 | this.path = path; 33 | this.suffices = suffices; 34 | } 35 | 36 | public static Language byFilename(String filename) { 37 | final String suffix = filename.substring(filename.lastIndexOf('.')); 38 | for (final Language lang : values()) { 39 | if (lang.suffices.contains(suffix)) { 40 | return lang; 41 | } 42 | } 43 | return null; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/NamedLocation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | public class NamedLocation { 19 | final String name; 20 | final Language language; 21 | final String className; 22 | final String method; 23 | final boolean strictNameMatch; 24 | 25 | public NamedLocation(String name, Language language, String className, String method, boolean strictNameMatch) { 26 | this.name = name; 27 | this.language = language; 28 | this.className = className; 29 | this.method = method; 30 | this.strictNameMatch = strictNameMatch; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/UsageCounter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | 21 | public class UsageCounter { 22 | private final Map, Integer> usage = new HashMap<>(); 23 | 24 | public int getCount(Action action) { 25 | final Integer c = usage.get(action); 26 | return c == null ? 0 : c; 27 | } 28 | 29 | public boolean accept(ActionResult accept) { 30 | final Integer count = usage.get(accept.action); 31 | usage.put(accept.action, (count == null ? 0 : count) + 1); 32 | return accept.accept; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/config/ValuedLocation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.config; 17 | 18 | public class ValuedLocation { 19 | final String pack; 20 | final String clazz; 21 | final double[] values; 22 | final double[] appliedLimits; 23 | 24 | public ValuedLocation(String pack, String clazz, double[] values) { 25 | this.pack = pack; 26 | this.clazz = clazz; 27 | this.values = values; 28 | appliedLimits = new double[values.length]; 29 | } 30 | 31 | public String getPack() { 32 | return pack; 33 | } 34 | 35 | public String getClazz() { 36 | return clazz; 37 | } 38 | 39 | public double[] getValues() { 40 | return values; 41 | } 42 | 43 | public double[] getAppliedLimits() { 44 | return appliedLimits; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/dependency/AmbiguousRuleException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency; 17 | 18 | import guru.nidi.codeassert.model.UsingElement; 19 | 20 | public class AmbiguousRuleException extends RuntimeException { 21 | private final DependencyRule rule; 22 | private final UsingElement from; 23 | private final UsingElement to; 24 | 25 | AmbiguousRuleException(DependencyRule rule, UsingElement from, UsingElement to) { 26 | this.rule = rule; 27 | this.from = from; 28 | this.to = to; 29 | } 30 | 31 | public DependencyRule getRule() { 32 | return rule; 33 | } 34 | 35 | public UsingElement getFrom() { 36 | return from; 37 | } 38 | 39 | public UsingElement getTo() { 40 | return to; 41 | } 42 | 43 | @Override 44 | public String getMessage() { 45 | return rule + " is ambiguous for dependency " + from + " -> " + to; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/dependency/CodeElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency; 17 | 18 | import guru.nidi.codeassert.config.Location; 19 | import guru.nidi.codeassert.config.LocationMatcher; 20 | import guru.nidi.codeassert.model.UsingElement; 21 | import guru.nidi.codeassert.model.UsingElementMatcher; 22 | 23 | public class CodeElement implements UsingElementMatcher { 24 | final LocationMatcher pattern; 25 | final boolean allowAll; 26 | 27 | CodeElement(String pattern, boolean allowAll) { 28 | final int starPos = pattern.indexOf('*'); 29 | final int plusPos = pattern.indexOf('+'); 30 | if ((starPos >= 0 && starPos != pattern.length() - 1) || (plusPos >= 0 && plusPos != pattern.length() - 1)) { 31 | throw new IllegalArgumentException("Wildcards are allowed at the end (e.g. java.*)"); 32 | } 33 | this.pattern = new LocationMatcher(Location.of(pattern)); 34 | this.allowAll = allowAll; 35 | } 36 | 37 | public DependencyRule andAllSub() { 38 | return sub("*"); 39 | } 40 | 41 | public DependencyRule allSubOf() { 42 | return sub("+"); 43 | } 44 | 45 | public DependencyRule sub(String name) { 46 | final String newPattern = pattern.toString() + (pattern.toString().endsWith(".") ? "" : ".") + name; 47 | return DependencyRules.addRuleToCurrent(new DependencyRule(newPattern, allowAll)); 48 | } 49 | 50 | public DependencyRule rule() { 51 | final String newPattern = pattern.toString(); 52 | return DependencyRules.addRuleToCurrent(new DependencyRule(newPattern, allowAll)); 53 | } 54 | 55 | public boolean matches(UsingElement elem) { 56 | return elem.isMatchedBy(pattern); 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "The java element " + pattern; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/dependency/DependencyCollector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency; 17 | 18 | import guru.nidi.codeassert.config.*; 19 | 20 | import java.util.List; 21 | 22 | import static guru.nidi.codeassert.util.ListUtils.andJoin; 23 | 24 | public class DependencyCollector extends BaseCollector { 25 | public static final String MISSING = "MISSING"; 26 | public static final String DENIED = "DENIED"; 27 | public static final String NOT_EXISTING = "NOT_EXISTING"; 28 | public static final String UNDEFINED = "UNDEFINED"; 29 | public static final String CYCLE = "CYCLE"; 30 | 31 | @Override 32 | public DependencyCollector config(final CollectorConfig... configs) { 33 | return new DependencyCollector() { 34 | @Override 35 | public ActionResult accept(DependencyEntry result) { 36 | return accept(result, DependencyCollector.this, configs); 37 | } 38 | 39 | public List unused(UsageCounter counter) { 40 | return unused(counter, DependencyCollector.this, configs); 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return andJoin(DependencyCollector.this, configs); 46 | } 47 | }; 48 | } 49 | 50 | @Override 51 | public ActionResult accept(DependencyEntry result) { 52 | return new ActionResult(true, null, 1); 53 | } 54 | 55 | @Override 56 | protected ActionResult doAccept(DependencyEntry result, Ignore action) { 57 | final NamedLocation loc = new NamedLocation(result.name, null, result.className, "", true); 58 | return action.accept(loc); 59 | } 60 | 61 | @Override 62 | public List unused(UsageCounter counter) { 63 | return unusedNullAction(counter, false); 64 | } 65 | 66 | @Override 67 | public String toString() { 68 | return ""; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/dependency/DependencyEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency; 17 | 18 | public class DependencyEntry { 19 | final String name; 20 | final String className; 21 | 22 | DependencyEntry(String name, String className) { 23 | this.name = name; 24 | this.className = className; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/dependency/DependencyResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency; 17 | 18 | import guru.nidi.codeassert.Analyzer; 19 | import guru.nidi.codeassert.AnalyzerResult; 20 | 21 | import java.util.List; 22 | 23 | public class DependencyResult extends AnalyzerResult { 24 | DependencyResult(Analyzer analyzer, 25 | Dependencies findings, List unusedActions) { 26 | super(analyzer, findings, unusedActions); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/dependency/MatcherUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency; 17 | 18 | import guru.nidi.codeassert.config.LocationMatcher; 19 | 20 | import java.util.*; 21 | 22 | import static java.lang.System.lineSeparator; 23 | import static java.util.Collections.sort; 24 | 25 | final class MatcherUtils { 26 | private MatcherUtils() { 27 | } 28 | 29 | static String deps(String prefix, Map deps) { 30 | final StringBuilder s = new StringBuilder(); 31 | for (final String dep : sorted(deps.keySet())) { 32 | s.append(prefix).append(dep); 33 | final Set by = deps.get(dep).getVias(); 34 | if (!by.isEmpty()) { 35 | s.append(" (by ").append(join(by)).append(')'); 36 | } 37 | s.append(lineSeparator()); 38 | } 39 | return s.toString(); 40 | } 41 | 42 | static String join(Collection packs) { 43 | final StringBuilder s = new StringBuilder(); 44 | for (final String pack : sorted(packs)) { 45 | s.append(", ").append(pack); 46 | } 47 | return s.length() > 0 ? s.substring(2) : s.toString(); 48 | } 49 | 50 | static List sorted(Collection ss) { 51 | final List sorted = new ArrayList<>(ss); 52 | sort(sorted); 53 | return sorted; 54 | } 55 | 56 | static List sortedPatterns(Collection patterns) { 57 | final List ss = new ArrayList<>(); 58 | for (final LocationMatcher pattern : patterns) { 59 | ss.add(pattern.toString()); 60 | } 61 | return sorted(ss); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/dependency/RuleAccessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency; 17 | 18 | import guru.nidi.codeassert.config.LocationMatcher; 19 | 20 | import java.util.Set; 21 | 22 | interface RuleAccessor { 23 | RuleAccessor MAY_BE_USED = rule -> rule.usedBy.may; 24 | RuleAccessor MUST_BE_USED = rule -> rule.usedBy.must; 25 | RuleAccessor MUST_NOT_BE_USED = rule -> rule.usedBy.mustNot; 26 | 27 | Set access(DependencyRule rule); 28 | } 29 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/dependency/Usage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency; 17 | 18 | import guru.nidi.codeassert.config.LocationMatcher; 19 | 20 | import java.util.HashSet; 21 | import java.util.Set; 22 | 23 | class Usage { 24 | final Set must = new HashSet<>(); 25 | final Set may = new HashSet<>(); 26 | final Set mustNot = new HashSet<>(); 27 | 28 | Usage copy() { 29 | final Usage u = new Usage(); 30 | u.must.addAll(must); 31 | u.may.addAll(may); 32 | u.mustNot.addAll(mustNot); 33 | return u; 34 | } 35 | 36 | void must(CodeElement... rules) { 37 | for (final CodeElement rule : rules) { 38 | must.add(rule.pattern); 39 | } 40 | } 41 | 42 | void may(CodeElement... rules) { 43 | for (final CodeElement rule : rules) { 44 | may.add(rule.pattern); 45 | } 46 | } 47 | 48 | void mustNot(CodeElement... rules) { 49 | for (final CodeElement rule : rules) { 50 | mustNot.add(rule.pattern); 51 | } 52 | } 53 | 54 | boolean isEmpty() { 55 | return must.isEmpty() && may.isEmpty() && mustNot.isEmpty(); 56 | } 57 | 58 | @Override 59 | public String toString() { 60 | return "must " + must + ", may " + may + ", must not " + mustNot; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/io/Visualized.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.io; 17 | 18 | import guru.nidi.graphviz.engine.Format; 19 | import guru.nidi.graphviz.engine.Graphviz; 20 | 21 | import java.awt.image.BufferedImage; 22 | import java.io.File; 23 | import java.io.IOException; 24 | 25 | public class Visualized { 26 | private final Graphviz graphviz; 27 | 28 | Visualized(Graphviz graphviz) { 29 | this.graphviz = graphviz; 30 | } 31 | 32 | public void toFile(File target) throws IOException { 33 | graphviz.render(Format.PNG).toFile(target); 34 | } 35 | 36 | public BufferedImage asImage() { 37 | return graphviz.render(Format.PNG).toImage(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/jacoco/CoverageCollector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.jacoco; 17 | 18 | import guru.nidi.codeassert.config.*; 19 | 20 | import java.util.List; 21 | 22 | import static guru.nidi.codeassert.util.ListUtils.andJoin; 23 | 24 | public class CoverageCollector extends BaseCollector { 25 | final CoverageType[] types; 26 | 27 | public CoverageCollector(CoverageType... types) { 28 | this.types = types; 29 | } 30 | 31 | @Override 32 | public CoverageCollector config(final CollectorConfig... configs) { 33 | for (final CollectorConfig config : configs) { 34 | for (final Minima minima : config.actions) { 35 | if (minima.getValueCount() > types.length) { 36 | throw new IllegalArgumentException("Given " + minima.getValueCount() 37 | + " values, but expected only " + types.length); 38 | } 39 | } 40 | } 41 | 42 | return new CoverageCollector(types) { 43 | @Override 44 | public ActionResult accept(ValuedLocation issue) { 45 | return accept(issue, CoverageCollector.this, configs); 46 | } 47 | 48 | public List unused(UsageCounter counter) { 49 | return unused(counter, CoverageCollector.this, configs); 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return andJoin(CoverageCollector.this, configs); 55 | } 56 | }; 57 | } 58 | 59 | @Override 60 | public ActionResult accept(ValuedLocation issue) { 61 | return ActionResult.reject(null, 1); 62 | } 63 | 64 | @Override 65 | protected ActionResult doAccept(ValuedLocation issue, Minima action) { 66 | return action.accept(issue); 67 | } 68 | 69 | @Override 70 | public List unused(UsageCounter counter) { 71 | return unusedNullAction(counter, true); 72 | } 73 | 74 | @Override 75 | public String toString() { 76 | return "CoverageCollector"; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/jacoco/CoverageType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.jacoco; 17 | 18 | public enum CoverageType { 19 | INSTRUCTION, BRANCH, LINE, COMPLEXITY, METHOD 20 | } 21 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/jacoco/Coverages.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.jacoco; 17 | 18 | import java.util.*; 19 | import java.util.stream.Stream; 20 | 21 | public class Coverages { 22 | final Set coverages = new TreeSet<>(); 23 | final Map perPackage = new TreeMap<>(); 24 | Coverage global; 25 | 26 | public void add(Coverage coverage) { 27 | coverages.add(coverage); 28 | final Coverage pc = perPackage.get(coverage.pack); 29 | perPackage.put(coverage.pack, pc == null ? coverage.withClazz("") : pc.combined(coverage)); 30 | global = global == null ? coverage : global.combined(coverage); 31 | } 32 | 33 | Stream asStream() { 34 | return Stream.concat(Stream.concat( 35 | Stream.of(global), perPackage.values().stream()), coverages.stream()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/jacoco/JacocoResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.jacoco; 17 | 18 | import guru.nidi.codeassert.Analyzer; 19 | import guru.nidi.codeassert.AnalyzerResult; 20 | import guru.nidi.codeassert.config.ValuedLocation; 21 | 22 | import java.util.List; 23 | 24 | public class JacocoResult extends AnalyzerResult> { 25 | private final CoverageType[] types; 26 | 27 | JacocoResult(Analyzer> analyzer, List findings, 28 | List unusedActions, CoverageType[] types) { 29 | super(analyzer, findings, unusedActions); 30 | this.types = types; 31 | } 32 | 33 | public CoverageType[] getTypes() { 34 | return types; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/junit/CodeAssertCoreJunit5Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | import org.junit.jupiter.api.Disabled; 19 | import org.junit.jupiter.api.Test; 20 | 21 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.*; 22 | import static guru.nidi.codeassert.junit.CodeAssertTestType.*; 23 | import static org.hamcrest.MatcherAssert.assertThat; 24 | import static org.junit.jupiter.api.Assumptions.assumeFalse; 25 | import static org.junit.jupiter.api.Assumptions.assumeTrue; 26 | 27 | @Disabled("This is made to be subclassed") 28 | public class CodeAssertCoreJunit5Test extends CodeAssertCoreTestBase { 29 | @Test 30 | void dependencies() { 31 | assumeTrue(defaultTests().contains(DEPENDENCIES), "Dependencies test excluded."); 32 | assumeFalse(dependencyResult() == null, "analyzeDependencies() not implemented."); 33 | assertThat(dependencyResult(), matchesRulesExactly()); 34 | } 35 | 36 | @Test 37 | void circularDependencies() { 38 | assumeTrue(defaultTests().contains(CIRCULAR_DEPENDENCIES), "Circular dependencies test excluded."); 39 | assumeFalse(dependencyResult() == null, "analyzeDependencies() not implemented."); 40 | assertThat(dependencyResult(), hasNoCycles()); 41 | } 42 | 43 | @Test 44 | void codeStructure() { 45 | assumeTrue(defaultTests().contains(STRUCTURE), "Structure tests excluded."); 46 | assumeFalse(createModel() == null, "createModel() not implemented."); 47 | assertThat(createModel(), exposesNoInternalTypes()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/junit/CodeAssertCoreMatchers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | import guru.nidi.codeassert.AnalyzerResult; 19 | import guru.nidi.codeassert.dependency.*; 20 | import guru.nidi.codeassert.jacoco.CoverageMatcher; 21 | import guru.nidi.codeassert.jacoco.JacocoResult; 22 | import guru.nidi.codeassert.model.*; 23 | import org.hamcrest.Matcher; 24 | 25 | public final class CodeAssertCoreMatchers { 26 | private CodeAssertCoreMatchers() { 27 | } 28 | 29 | public static Matcher matchesRules() { 30 | return new DependencyResultMatcher(false, false); 31 | } 32 | 33 | public static Matcher matchesRulesExactly() { 34 | return new DependencyResultMatcher(true, true); 35 | } 36 | 37 | public static Matcher matchesRulesIgnoringNonExisting() { 38 | return new DependencyResultMatcher(false, true); 39 | } 40 | 41 | public static Matcher matchesRulesIgnoringUndefined() { 42 | return new DependencyResultMatcher(true, false); 43 | } 44 | 45 | public static Matcher exposesNoInternalTypes() { 46 | return new InternalTypeInPublicApiMatcher(); 47 | } 48 | 49 | public static Matcher hasNoPublicMembersInInternalTypes() { 50 | return new PublicMemberInInternalTypeMatcher(); 51 | } 52 | 53 | public static Matcher hasNoCycles() { 54 | return new DependencyCycleMatcher(); 55 | } 56 | 57 | public static > Matcher hasNoUnusedActions() { 58 | return new UnusedActionsMatcher<>(); 59 | } 60 | 61 | public static Matcher hasEnoughCoverage() { 62 | return new CoverageMatcher(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/junit/CodeAssertCoreTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | import org.junit.Ignore; 19 | import org.junit.Test; 20 | 21 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.*; 22 | import static guru.nidi.codeassert.junit.CodeAssertTestType.*; 23 | import static org.hamcrest.MatcherAssert.assertThat; 24 | import static org.junit.Assume.assumeFalse; 25 | import static org.junit.Assume.assumeTrue; 26 | 27 | @Ignore("This is made to be subclassed") 28 | public class CodeAssertCoreTest extends CodeAssertCoreTestBase { 29 | @Test 30 | public void dependencies() { 31 | assumeTrue("Dependencies test excluded.", defaultTests().contains(DEPENDENCIES)); 32 | assumeFalse("analyzeDependencies() not implemented.", dependencyResult() == null); 33 | assertThat(dependencyResult(), matchesRulesExactly()); 34 | } 35 | 36 | @Test 37 | public void circularDependencies() { 38 | assumeTrue("Circular dependencies test excluded.", defaultTests().contains(CIRCULAR_DEPENDENCIES)); 39 | assumeFalse("analyzeDependencies() not implemented.", dependencyResult() == null); 40 | assertThat(dependencyResult(), hasNoCycles()); 41 | } 42 | 43 | @Test 44 | public void codeStructure() { 45 | assumeTrue("Structure tests excluded.", defaultTests().contains(STRUCTURE)); 46 | assumeFalse("createModel() not implemented.", createModel() == null); 47 | assertThat(createModel(), exposesNoInternalTypes()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/junit/CodeAssertCoreTestBase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | import guru.nidi.codeassert.dependency.DependencyResult; 19 | import guru.nidi.codeassert.model.Model; 20 | 21 | import java.util.EnumSet; 22 | 23 | public class CodeAssertCoreTestBase { 24 | private Model model; 25 | private DependencyResult dependencyResult; 26 | 27 | protected EnumSet defaultTests() { 28 | return EnumSet.allOf(CodeAssertTestType.class); 29 | } 30 | 31 | protected Model createModel() { 32 | return null; 33 | } 34 | 35 | protected DependencyResult analyzeDependencies() { 36 | return null; 37 | } 38 | 39 | protected synchronized Model model() { 40 | if (model == null) { 41 | model = createModel(); 42 | } 43 | return model; 44 | } 45 | 46 | protected synchronized DependencyResult dependencyResult() { 47 | if (dependencyResult == null) { 48 | dependencyResult = analyzeDependencies(); 49 | } 50 | return dependencyResult; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/junit/CodeAssertTestType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | public enum CodeAssertTestType { 19 | DEPENDENCIES, CIRCULAR_DEPENDENCIES, STRUCTURE, 20 | FIND_BUGS, FIND_BUGS_UNUSED_ACTIONS, 21 | PMD, PMD_UNUSED_ACTIONS, 22 | CPD, CPD_UNUSED_ACTIONS, 23 | CHECKSTYLE, CHECKSTYLE_UNUSED_ACTIONS 24 | } 25 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/junit/UnusedActionsMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | import guru.nidi.codeassert.AnalyzerResult; 19 | import org.hamcrest.Description; 20 | import org.hamcrest.TypeSafeMatcher; 21 | 22 | import static java.lang.System.lineSeparator; 23 | 24 | public class UnusedActionsMatcher> extends TypeSafeMatcher { 25 | @Override 26 | protected boolean matchesSafely(T item) { 27 | return item.unusedActions().isEmpty(); 28 | } 29 | 30 | public void describeTo(Description description) { 31 | description.appendText("Has no unused actions"); 32 | } 33 | 34 | @Override 35 | protected void describeMismatchSafely(T item, Description description) { 36 | description.appendText("Found unused actions:"); 37 | for (final String action : item.unusedActions()) { 38 | description.appendText(lineSeparator()).appendText(action); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/model/AttributeInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | import java.io.DataInputStream; 19 | import java.io.IOException; 20 | 21 | final class AttributeInfo { 22 | final String name; 23 | final byte[] value; 24 | 25 | private AttributeInfo(String name, byte[] value) { 26 | this.name = name; 27 | this.value = value; 28 | } 29 | 30 | static AttributeInfo fromData(DataInputStream in, ConstantPool constantPool) throws IOException { 31 | final String name = constantPool.getUtf8(in.readUnsignedShort()); 32 | final int attributeLength = in.readInt(); 33 | final byte[] value = new byte[attributeLength]; 34 | for (int b = 0; b < attributeLength; b++) { 35 | value[b] = in.readByte(); 36 | } 37 | 38 | return new AttributeInfo(name, value); 39 | } 40 | 41 | boolean isAnnotation() { 42 | return "RuntimeVisibleAnnotations".equals(name) || "RuntimeInvisibleAnnotations".equals(name); 43 | } 44 | 45 | boolean isSignature() { 46 | return "Signature".equals(name); 47 | } 48 | 49 | boolean isSource() { 50 | return "SourceFile".equals(name); 51 | } 52 | 53 | boolean isCode() { 54 | return "Code".equals(name); 55 | } 56 | 57 | String sourceFile(ConstantPool constantPool) throws IOException { 58 | final int b0 = value[0] < 0 ? value[0] + 256 : value[0]; 59 | final int b1 = value[1] < 0 ? value[1] + 256 : value[1]; 60 | final int pe = b0 * 256 + b1; 61 | return constantPool.getUtf8(pe); 62 | } 63 | 64 | int u2(int index) { 65 | return (value[index] << 8 & 0xFF00) | (value[index + 1] & 0xFF); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/model/ModelBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | import java.io.File; 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import static java.util.Arrays.asList; 23 | 24 | public class ModelBuilder { 25 | private final List files = new ArrayList<>(); 26 | private final List ignorePackages = new ArrayList<>(); 27 | private final List mergePackages = new ArrayList<>(); 28 | 29 | ModelBuilder() { 30 | } 31 | 32 | public ModelBuilder and(File... files) { 33 | return and(asList(files)); 34 | } 35 | 36 | public ModelBuilder and(List files) { 37 | this.files.addAll(files); 38 | return this; 39 | } 40 | 41 | public ModelBuilder ignoringPackages(String... packages) { 42 | return ignoringPackages(asList(packages)); 43 | } 44 | 45 | public ModelBuilder ignoringPackages(List packages) { 46 | ignorePackages.addAll(packages); 47 | return this; 48 | } 49 | 50 | public ModelBuilder mergingPackages(String... packages) { 51 | return mergingPackages(asList(packages)); 52 | } 53 | 54 | public ModelBuilder mergingPackages(List packages) { 55 | mergePackages.addAll(packages); 56 | return this; 57 | } 58 | 59 | public Model read() { 60 | return new Model(ignorePackages, mergePackages).read(files); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/model/PublicMemberInInternalTypeMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | import org.hamcrest.Description; 19 | import org.hamcrest.TypeSafeMatcher; 20 | 21 | import java.lang.reflect.Modifier; 22 | import java.util.*; 23 | import java.util.Map.Entry; 24 | 25 | //TODO exclude interface implementing methods 26 | public class PublicMemberInInternalTypeMatcher extends TypeSafeMatcher { 27 | private final Map> findings = new HashMap<>(); 28 | 29 | @Override 30 | protected boolean matchesSafely(Model model) { 31 | for (final CodeClass clazz : model.getClasses()) { 32 | if (!Modifier.isPublic(clazz.flags)) { 33 | checkMembers(clazz); 34 | } 35 | } 36 | return findings.isEmpty(); 37 | } 38 | 39 | private void checkMembers(CodeClass clazz) { 40 | for (final MemberInfo member : clazz.getMembers()) { 41 | if (Modifier.isPublic(member.getAccessFlags())) { 42 | final List classFindings = findings.computeIfAbsent(clazz.getName(), s -> new ArrayList<>()); 43 | classFindings.add(member.getName()); 44 | } 45 | } 46 | } 47 | 48 | public void describeTo(Description description) { 49 | description.appendText("Internal types have no public members."); 50 | } 51 | 52 | @Override 53 | protected void describeMismatchSafely(Model model, Description description) { 54 | description.appendText("Found these public members in internal types:\n"); 55 | for (final Entry> classFinding : findings.entrySet()) { 56 | description.appendText(classFinding.getKey() + ": " + classFinding.getValue() + "\n"); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/model/Scope.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | import guru.nidi.codeassert.AnalyzerException; 19 | import guru.nidi.codeassert.config.LocationMatcher; 20 | 21 | import java.lang.reflect.Constructor; 22 | import java.util.*; 23 | 24 | public abstract class Scope> implements Iterable { 25 | public static final Packages PACKAGES = new Packages(null); 26 | public static final Classes CLASSES = new Classes(null); 27 | 28 | protected final Model model; 29 | 30 | protected Scope(Model model) { 31 | this.model = model; 32 | } 33 | 34 | public static Scope packages(final Model model) { 35 | return new Packages(model); 36 | } 37 | 38 | public static Scope classes(final Model model) { 39 | return new Classes(model); 40 | } 41 | 42 | public Scope in(Model model) { 43 | try { 44 | final Constructor c = getClass().getDeclaredConstructor(Model.class); 45 | return c.newInstance(model); 46 | } catch (ReflectiveOperationException e) { 47 | throw new AnalyzerException("Could not create new Scope", e); 48 | } 49 | } 50 | 51 | public List matchingElements(LocationMatcher matcher) { 52 | final List res = new ArrayList<>(); 53 | for (final T elem : this) { 54 | if (elem.isMatchedBy(matcher)) { 55 | res.add(elem); 56 | } 57 | } 58 | return res; 59 | } 60 | 61 | public static class Packages extends Scope { 62 | protected Packages(Model model) { 63 | super(model); 64 | } 65 | 66 | @Override 67 | public Iterator iterator() { 68 | return model.packages.values().iterator(); 69 | } 70 | } 71 | 72 | public static class Classes extends Scope { 73 | public Classes(Model model) { 74 | super(model); 75 | } 76 | 77 | @Override 78 | public Iterator iterator() { 79 | return model.classes.values().iterator(); 80 | } 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/model/UsingElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | import guru.nidi.codeassert.config.LocationMatcher; 19 | 20 | import java.util.Collection; 21 | import java.util.List; 22 | 23 | public abstract class UsingElement { 24 | public abstract T self(); 25 | 26 | public abstract String getName(); 27 | 28 | public abstract String getPackageName(); 29 | 30 | public abstract Collection usedVia(UsingElement other); 31 | 32 | public abstract boolean isMatchedBy(LocationMatcher matcher); 33 | 34 | public abstract Collection uses(); 35 | 36 | public boolean uses(T elem) { 37 | return uses().contains(elem); 38 | } 39 | 40 | public int mostSpecificMatch(Collection matchers) { 41 | int s = 0; 42 | for (final LocationMatcher matcher : matchers) { 43 | if (isMatchedBy(matcher) && matcher.specificity() > s) { 44 | s = matcher.specificity(); 45 | } 46 | } 47 | return s; 48 | } 49 | 50 | public boolean matchesAny(List matchers) { 51 | for (final UsingElementMatcher matcher : matchers) { 52 | if (matcher.matches(this)) { 53 | return true; 54 | } 55 | } 56 | return false; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/model/UsingElementMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | public interface UsingElementMatcher { 19 | boolean matches(UsingElement elem); 20 | } 21 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/util/CountSet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.util; 17 | 18 | import java.util.*; 19 | 20 | public class CountSet { 21 | private final Map map = new HashMap<>(); 22 | 23 | public void add(T elem) { 24 | final Integer c = map.get(elem); 25 | map.put(elem, (c == null ? 0 : c) + 1); 26 | } 27 | 28 | public boolean contains(T elem) { 29 | return map.containsKey(elem); 30 | } 31 | 32 | public Map asMap() { 33 | return map; 34 | } 35 | 36 | public Set asSet() { 37 | return map.keySet(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/util/ListUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.util; 17 | 18 | import java.util.*; 19 | 20 | import static java.lang.System.lineSeparator; 21 | import static java.util.Arrays.asList; 22 | 23 | public final class ListUtils { 24 | private ListUtils() { 25 | } 26 | 27 | public static String andJoin(Object first, Object[] os) { 28 | return andJoin(first, asList(os)); 29 | } 30 | 31 | public static String andJoin(Object first, List os) { 32 | return first.toString() + lineSeparator() + join(lineSeparator(), os); 33 | } 34 | 35 | public static String join(String sep, Object[] os) { 36 | return join(sep, asList(os)); 37 | } 38 | 39 | public static String join(String sep, List os) { 40 | final StringBuilder res = new StringBuilder(); 41 | for (final Object o : os) { 42 | res.append(sep).append(o); 43 | } 44 | return os.isEmpty() ? "" : res.substring(sep.length()); 45 | } 46 | 47 | public static List concat(List a, List b) { 48 | final ArrayList res = new ArrayList<>(a); 49 | res.addAll(b); 50 | return res; 51 | } 52 | 53 | public static > EnumSet concat(EnumSet a, EnumSet b) { 54 | final EnumSet res = EnumSet.copyOf(a); 55 | res.addAll(b); 56 | return res; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /code-assert-core/src/main/java/guru/nidi/codeassert/util/ResultMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.util; 17 | 18 | import guru.nidi.codeassert.AnalyzerResult; 19 | import org.hamcrest.TypeSafeMatcher; 20 | 21 | import java.util.List; 22 | 23 | public abstract class ResultMatcher>, S> extends TypeSafeMatcher { 24 | @Override 25 | protected boolean matchesSafely(T item) { 26 | return item.findings().isEmpty(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/CodeCoverage.java: -------------------------------------------------------------------------------- 1 | import guru.nidi.codeassert.config.For; 2 | import guru.nidi.codeassert.jacoco.CoverageCollector; 3 | import guru.nidi.codeassert.jacoco.JacocoAnalyzer; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static guru.nidi.codeassert.jacoco.CoverageType.*; 7 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.hasEnoughCoverage; 8 | import static org.hamcrest.MatcherAssert.assertThat; 9 | 10 | public class CodeCoverage { 11 | @Test 12 | void coverage() { 13 | JacocoAnalyzer analyzer = new JacocoAnalyzer(new CoverageCollector(BRANCH, LINE, METHOD).just( 14 | For.allPackages().setMinima(75, 75, 75), 15 | For.thePackage("*.codeassert").setMinima(75, 75, 65), 16 | For.thePackage("*.config").setMinima(70, 75, 70), 17 | For.thePackage("*.io").setMinima(0, 0, 0), 18 | For.thePackage("*.junit").setMinima(40, 50, 55), 19 | For.thePackage("*.util").setMinima(50, 45, 55) 20 | )); 21 | assertThat(analyzer.analyze(), hasEnoughCoverage()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/a/A1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.a; 17 | 18 | 19 | import guru.nidi.codeassert.dependency.c.C1; 20 | 21 | public class A1 { 22 | C1 c1; 23 | } 24 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/a/A2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.a; 17 | 18 | public class A2 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/a/a/Aa1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.a.a; 17 | 18 | 19 | import guru.nidi.codeassert.dependency.b.a.Ba1; 20 | 21 | public class Aa1 { 22 | Ba1 ba1; 23 | } 24 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/a/b/Ab1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.a.b; 17 | 18 | public class Ab1 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/b/B1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.b; 17 | 18 | 19 | import guru.nidi.codeassert.dependency.a.A1; 20 | import guru.nidi.codeassert.dependency.c.C1; 21 | 22 | public class B1 { 23 | A1 a1; 24 | C1 c1; 25 | } 26 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/b/B2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.b; 17 | 18 | public class B2 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/b/a/Ba1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.b.a; 17 | 18 | 19 | import guru.nidi.codeassert.dependency.a.a.Aa1; 20 | import guru.nidi.codeassert.dependency.a.b.Ab1; 21 | 22 | public class Ba1 { 23 | Aa1 aa1; 24 | Ab1 ab1; 25 | } 26 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/b/a/Ba2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.b.a; 17 | 18 | 19 | import guru.nidi.codeassert.dependency.c.a.Ca1; 20 | import guru.nidi.codeassert.dependency.c.b.Cb1; 21 | 22 | public class Ba2 { 23 | Ca1 ca1; 24 | Cb1 cb1; 25 | } 26 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/b/b/Bb1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.b.b; 17 | 18 | 19 | import guru.nidi.codeassert.dependency.c.a.Ca1; 20 | import guru.nidi.codeassert.dependency.c.b.Cb1; 21 | 22 | public class Bb1 { 23 | Ca1 ca1; 24 | Cb1 cb1; 25 | } 26 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/c/C1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.c; 17 | 18 | import guru.nidi.codeassert.dependency.a.A1; 19 | import guru.nidi.codeassert.dependency.b.B1; 20 | 21 | public class C1 { 22 | A1 a1; 23 | B1 b1; 24 | } 25 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/c/C2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.c; 17 | 18 | 19 | import guru.nidi.codeassert.dependency.b.B1; 20 | 21 | public class C2 { 22 | B1 b1; 23 | } 24 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/c/a/Ca1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.c.a; 17 | 18 | import guru.nidi.codeassert.dependency.a.a.Aa1; 19 | import guru.nidi.codeassert.dependency.b.a.Ba1; 20 | 21 | public class Ca1 { 22 | Aa1 aa1; 23 | Ba1 ba1; 24 | } 25 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/dependency/c/b/Cb1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.dependency.c.b; 17 | 18 | public class Cb1 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/junit/EmptyCodeAssertCoreJunit5Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | class EmptyCodeAssertCoreJunit5Test extends CodeAssertCoreJunit5Test { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/junit/EmptyCodeAssertCoreTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | class EmptyCodeAssertCoreTest extends CodeAssertCoreTest { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/AnalyzerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | import guru.nidi.codeassert.config.AnalyzerConfig; 19 | import org.junit.jupiter.api.Test; 20 | 21 | import static org.hamcrest.CoreMatchers.anyOf; 22 | import static org.hamcrest.CoreMatchers.equalTo; 23 | import static org.hamcrest.MatcherAssert.assertThat; 24 | import static org.junit.jupiter.api.Assertions.assertEquals; 25 | 26 | class AnalyzerTest { 27 | final Model model = Model.from(AnalyzerConfig.maven().mainAndTest("guru/nidi/codeassert/model").getClasses()).read(); 28 | 29 | @Test 30 | void packages() { 31 | assertEquals(41, model.getPackages().size()); 32 | } 33 | 34 | @Test 35 | void classes() { 36 | assertThat(model.getClasses().size(), anyOf(equalTo(139), equalTo(140), equalTo(141), equalTo(142))); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/ExampleAbstractClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | 19 | import java.math.BigDecimal; 20 | 21 | /** 22 | * @author Mike Clark 23 | * @author Clarkware Consulting, Inc. 24 | */ 25 | 26 | public abstract class ExampleAbstractClass implements ExampleInterface, java.io.Serializable { 27 | 28 | private java.lang.reflect.Method method; 29 | 30 | public ExampleAbstractClass() { 31 | } 32 | 33 | public void a() { 34 | java.net.URL url; 35 | } 36 | 37 | public java.util.Vector b(String[] s, java.text.NumberFormat nf) { 38 | return null; 39 | } 40 | 41 | public void c(BigDecimal bd, byte[] b) throws java.rmi.RemoteException { 42 | 43 | } 44 | 45 | public abstract java.io.File[] d() throws java.io.IOException; 46 | } 47 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/ExampleAnnotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | 19 | import guru.nidi.codeassert.model.p1.ExampleInnerAnnotation; 20 | import guru.nidi.codeassert.model.p2.ExampleEnum; 21 | import guru.nidi.codeassert.model.p3.ExampleSecondEnum; 22 | 23 | import java.lang.annotation.Retention; 24 | 25 | import static guru.nidi.codeassert.model.p3.ExampleSecondEnum.NO_DEPENDENCIES_ON_ME; 26 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 27 | 28 | @Retention(RUNTIME) 29 | public @interface ExampleAnnotation { 30 | 31 | Class c1() default Object.class; 32 | 33 | Class c2() default Object.class; 34 | 35 | ExampleInnerAnnotation c3(); 36 | 37 | ExampleEnum c4(); 38 | 39 | ExampleSecondEnum c5() default NO_DEPENDENCIES_ON_ME; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/ExampleConcreteClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | 19 | import guru.nidi.codeassert.model.p1.ExampleInnerAnnotation; 20 | import guru.nidi.codeassert.model.p2.ExampleEnum; 21 | import org.junit.jupiter.api.*; 22 | 23 | import java.math.BigDecimal; 24 | 25 | /** 26 | * @author Mike Clark 27 | * @author Clarkware Consulting, Inc. 28 | */ 29 | @Tag("hula") 30 | public class ExampleConcreteClass extends ExampleAbstractClass { 31 | 32 | private java.sql.Statement[] statements; 33 | 34 | public ExampleConcreteClass() { 35 | } 36 | 37 | public void a() { 38 | try { 39 | java.net.URL url = new java.net.URL("http://www.clarkware.com"); 40 | } catch (Exception e) { 41 | //ignore 42 | } 43 | } 44 | 45 | public java.util.Vector b(String[] s, java.text.NumberFormat nf) { 46 | return null; 47 | } 48 | 49 | public void c(BigDecimal bd, byte[] bytes) throws java.rmi.RemoteException { 50 | int[] a = {1, 2, 3}; 51 | int[][] b = {{1, 2}, {3, 4}, {5, 6}}; 52 | } 53 | 54 | public java.io.File[] d() throws java.io.IOException { 55 | java.util.jar.JarFile[] files = new java.util.jar.JarFile[1]; 56 | return new java.io.File[10]; 57 | } 58 | 59 | public java.lang.String[] e() { 60 | return new String[1]; 61 | } 62 | 63 | @Test 64 | @ExampleAnnotation( 65 | c1 = java.awt.geom.AffineTransform.class, 66 | c2 = java.awt.image.renderable.ContextualRenderedImageFactory.class, 67 | c3 = @ExampleInnerAnnotation({ 68 | java.awt.im.InputContext.class, 69 | java.awt.Checkbox.class}), 70 | c4 = ExampleEnum.E1) 71 | @Disabled 72 | public void f() { 73 | } 74 | 75 | public class ExampleInnerClass { 76 | } 77 | } 78 | 79 | class ExamplePackageClass { 80 | } 81 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/ExampleInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | import java.math.BigDecimal; 19 | 20 | /** 21 | * @author Mike Clark 22 | * @author Clarkware Consulting, Inc. 23 | */ 24 | 25 | public interface ExampleInterface { 26 | 27 | void a(); 28 | 29 | java.util.Vector b(String[] s, java.text.NumberFormat nf); 30 | 31 | void c(BigDecimal bd, byte[] b) throws java.rmi.RemoteException; 32 | 33 | java.io.File[] d() throws java.io.IOException; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/Path.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model; 17 | 18 | import java.io.File; 19 | 20 | public final class Path { 21 | private static final String PACKAGE = "guru/nidi/codeassert/model/"; 22 | private static final String TEST_RESOURCES = "src/test/resources/"; 23 | private static final String TEST_JAVAS = "src/test/java/" + PACKAGE; 24 | public static final String TEST_CLASSES = "target/test-classes/" + PACKAGE; 25 | public static final String CLASSES = "target/classes/" + PACKAGE; 26 | 27 | private Path() { 28 | } 29 | 30 | public static File testJava(String name) { 31 | return new File(TEST_JAVAS + name + ".java"); 32 | } 33 | 34 | public static File testClass(String name) { 35 | return new File(TEST_CLASSES + name + ".class"); 36 | } 37 | 38 | public static File testResource(String name) { 39 | return new File(TEST_RESOURCES + name); 40 | } 41 | 42 | public static File clazz(String name) { 43 | return new File(CLASSES + name + ".class"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p1/ExampleInnerAnnotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p1; 17 | 18 | public @interface ExampleInnerAnnotation { 19 | Class[] value() default {}; 20 | } 21 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p2/ExampleEnum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p2; 17 | 18 | public enum ExampleEnum { 19 | E1, 20 | E2 21 | } 22 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p3/ExampleSecondEnum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p3; 17 | 18 | public enum ExampleSecondEnum { 19 | NO_DEPENDENCIES_ON_ME 20 | } 21 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/GenericParameters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4; 17 | 18 | import guru.nidi.codeassert.model.p4.p1.Type1; 19 | import guru.nidi.codeassert.model.p4.p10.Type10; 20 | import guru.nidi.codeassert.model.p4.p2.Type2; 21 | import guru.nidi.codeassert.model.p4.p3.Type3; 22 | import guru.nidi.codeassert.model.p4.p4.Type4; 23 | import guru.nidi.codeassert.model.p4.p5.Type5; 24 | import guru.nidi.codeassert.model.p4.p6.Type6; 25 | import guru.nidi.codeassert.model.p4.p7.Type7; 26 | import guru.nidi.codeassert.model.p4.p8.Type8; 27 | import guru.nidi.codeassert.model.p4.p9.Type9; 28 | 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | 32 | public class GenericParameters { 33 | private List list; 34 | private List l2 = new ArrayList(); 35 | 36 | private List method4(Type7 t7, X x) { 37 | return null; 38 | } 39 | 40 | private List method42() throws Type8 { 41 | return null; 42 | } 43 | 44 | private void method5(List param, List> sub) throws E { 45 | } 46 | 47 | private List method6(List a) { 48 | return null; 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/SubGenericParameters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4; 17 | 18 | import guru.nidi.codeassert.model.p4.p1.Type1; 19 | 20 | class SubGenericParameters extends GenericParameters { 21 | } 22 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p1/Type1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p1; 17 | 18 | public class Type1 { 19 | public class Type1Sub { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p10/Type10.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p10; 17 | 18 | public class Type10 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p2/Type2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p2; 17 | 18 | public class Type2 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p3/Type3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p3; 17 | 18 | public class Type3 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p4/Type4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p4; 17 | 18 | public class Type4 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p5/Type5.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p5; 17 | 18 | public class Type5 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p6/Type6.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p6; 17 | 18 | public interface Type6 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p7/Type7.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p7; 17 | 18 | public interface Type7 { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p8/Type8.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p8; 17 | 18 | public class Type8 extends Exception { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p4/p9/Type9.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p4.p9; 17 | 18 | public class Type9 extends Exception { 19 | } 20 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p5/Annotations.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p5; 17 | 18 | import guru.nidi.codeassert.model.ExampleAnnotation; 19 | import guru.nidi.codeassert.model.p1.ExampleInnerAnnotation; 20 | import guru.nidi.codeassert.model.p2.ExampleEnum; 21 | 22 | @ExampleAnnotation(c3 = @ExampleInnerAnnotation, c4 = ExampleEnum.E1) 23 | @ClassRetentionAnnotation 24 | @SourceRetentionAnnotation 25 | public class Annotations { 26 | } 27 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p5/ClassRetentionAnnotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p5; 17 | 18 | 19 | import java.lang.annotation.Retention; 20 | 21 | import static java.lang.annotation.RetentionPolicy.CLASS; 22 | 23 | @Retention(CLASS) 24 | public @interface ClassRetentionAnnotation { 25 | } 26 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p5/SourceRetentionAnnotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.model.p5; 17 | 18 | 19 | import java.lang.annotation.Retention; 20 | 21 | import static java.lang.annotation.RetentionPolicy.SOURCE; 22 | 23 | @Retention(SOURCE) 24 | public @interface SourceRetentionAnnotation { 25 | } 26 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/model/p5/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 | @SourceRetentionAnnotation 17 | @ClassRetentionAnnotation 18 | @ExampleAnnotation(c3 = @ExampleInnerAnnotation, c4 = ExampleEnum.E1) 19 | package guru.nidi.codeassert.model.p5; 20 | 21 | import guru.nidi.codeassert.model.ExampleAnnotation; 22 | import guru.nidi.codeassert.model.p1.ExampleInnerAnnotation; 23 | import guru.nidi.codeassert.model.p2.ExampleEnum; 24 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/snippets/CodeCoverage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import guru.nidi.codeassert.config.For; 19 | import guru.nidi.codeassert.jacoco.CoverageCollector; 20 | import guru.nidi.codeassert.jacoco.JacocoAnalyzer; 21 | import org.junit.jupiter.api.Disabled; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import static guru.nidi.codeassert.jacoco.CoverageType.*; 25 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.hasEnoughCoverage; 26 | import static org.hamcrest.MatcherAssert.assertThat; 27 | 28 | @Disabled 29 | //## codeCoverage 30 | public class CodeCoverage { 31 | @Test 32 | public void coverage() { 33 | // Coverage of branches must be at least 70%, lines 80% and methods 90% 34 | // This is checked globally and for all packages except for entities. 35 | JacocoAnalyzer analyzer = new JacocoAnalyzer(new CoverageCollector(BRANCH, LINE, METHOD) 36 | .just(For.global().setMinima(70, 80, 90)) 37 | .just(For.allPackages().setMinima(70, 80, 90)) 38 | .just(For.thePackage("org.proj.entity.*").setNoMinima())); 39 | assertThat(analyzer.analyze(), hasEnoughCoverage()); 40 | } 41 | } 42 | //## 43 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/snippets/CodeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import guru.nidi.codeassert.config.AnalyzerConfig; 19 | import guru.nidi.codeassert.dependency.*; 20 | import guru.nidi.codeassert.junit.CodeAssertCoreJunit5Test; 21 | import org.junit.jupiter.api.Disabled; 22 | 23 | import static guru.nidi.codeassert.dependency.DependencyRules.denyAll; 24 | 25 | @Disabled 26 | //## codeTest 27 | //extend CodeAssertTest if you still use JUnit 4 28 | public class CodeTest extends CodeAssertCoreJunit5Test { 29 | 30 | private static final AnalyzerConfig CONFIG = AnalyzerConfig.maven().main(); 31 | 32 | @Override 33 | protected DependencyResult analyzeDependencies() { 34 | class MyProject extends DependencyRuler { 35 | DependencyRule packages; 36 | 37 | @Override 38 | public void defineRules() { 39 | //TODO 40 | } 41 | } 42 | 43 | final DependencyRules rules = denyAll().withExternals("java.*").withRelativeRules(new MyProject()); 44 | return new DependencyAnalyzer(CONFIG).rules(rules).analyze(); 45 | } 46 | } 47 | //## 48 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/snippets/DependencyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import guru.nidi.codeassert.config.AnalyzerConfig; 19 | import guru.nidi.codeassert.dependency.*; 20 | import org.junit.jupiter.api.Disabled; 21 | import org.junit.jupiter.api.Test; 22 | 23 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.hasNoCycles; 24 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.matchesRulesExactly; 25 | import static org.hamcrest.MatcherAssert.assertThat; 26 | 27 | @Disabled 28 | //## dependency 29 | public class DependencyTest { 30 | 31 | // Analyze all sources in src/main/java 32 | private final AnalyzerConfig config = AnalyzerConfig.maven().main(); 33 | 34 | @Test 35 | public void noCycles() { 36 | assertThat(new DependencyAnalyzer(config).analyze(), hasNoCycles()); 37 | } 38 | 39 | @Test 40 | public void dependency() { 41 | // Defines the dependency rules for package org.proj 42 | class OrgProj extends DependencyRuler { 43 | // Rules for org.proj.dep, org.proj.model, org.proj.util 44 | DependencyRule dep, model, util; 45 | 46 | @Override 47 | public void defineRules() { 48 | base().mayUse(util, dep.allSubOf()); //org.proj may use org.proj.util and all subpackages of org.proj.dep 49 | dep.andAllSub().mustUse(model); //org.proj.dep and all subpackages thereof must use org.proj.model 50 | model.mayUse(util).mustNotUse(base()); //org.proj.model may use org.proj.util but not org.proj 51 | } 52 | } 53 | 54 | // All dependencies are forbidden, except the ones defined in OrgProj 55 | // java, org, net packages may be used freely 56 | DependencyRules rules = DependencyRules.denyAll() 57 | .withRelativeRules(new OrgProj()) 58 | .withExternals("java.*", "org.*", "net.*"); 59 | 60 | DependencyResult result = new DependencyAnalyzer(config).rules(rules).analyze(); 61 | assertThat(result, matchesRulesExactly()); 62 | } 63 | } 64 | //## 65 | -------------------------------------------------------------------------------- /code-assert-core/src/test/java/guru/nidi/codeassert/util/ListUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.util; 17 | 18 | import org.junit.jupiter.api.Test; 19 | 20 | import static guru.nidi.codeassert.util.ListUtils.andJoin; 21 | import static guru.nidi.codeassert.util.ListUtils.join; 22 | import static java.util.Arrays.asList; 23 | import static org.junit.jupiter.api.Assertions.assertEquals; 24 | 25 | class ListUtilsTest { 26 | 27 | @Test 28 | void emptyArray() { 29 | assertEquals("", join(",", new String[]{})); 30 | } 31 | 32 | @Test 33 | void oneElementArray() { 34 | assertEquals("a", join(",", new String[]{"a"})); 35 | } 36 | 37 | @Test 38 | void multiElementArray() { 39 | assertEquals("a,b", join(",", new String[]{"a", "b"})); 40 | } 41 | 42 | @Test 43 | void notStringArray() { 44 | assertEquals("1,2", join(",", new Integer[]{1, 2})); 45 | } 46 | 47 | @Test 48 | void emptyList() { 49 | assertEquals("", join(",", asList())); 50 | } 51 | 52 | @Test 53 | void oneElementList() { 54 | assertEquals("a", join(",", asList("a"))); 55 | } 56 | 57 | @Test 58 | void multiElementList() { 59 | assertEquals("a,b", join(",", asList("a", "b"))); 60 | } 61 | 62 | @Test 63 | void notStringList() { 64 | assertEquals("1,2", join(",", asList(1, 2))); 65 | } 66 | 67 | @Test 68 | void andJoinArray() { 69 | assertEquals(String.format("start%n1%n2"), andJoin("start", new Integer[]{1, 2})); 70 | } 71 | 72 | @Test 73 | void andJoinList() { 74 | assertEquals(String.format("start%n1%n2"), andJoin("start", asList(1, 2))); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /code-assert-core/src/test/resources/example_class1.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nidi3/code-assert/a722c20b1541cabc42f08eb4d33fd46e9745da84/code-assert-core/src/test/resources/example_class1.bin -------------------------------------------------------------------------------- /code-assert-core/src/test/resources/example_class2.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nidi3/code-assert/a722c20b1541cabc42f08eb4d33fd46e9745da84/code-assert-core/src/test/resources/example_class2.bin -------------------------------------------------------------------------------- /code-assert-core/src/test/resources/test.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nidi3/code-assert/a722c20b1541cabc42f08eb4d33fd46e9745da84/code-assert-core/src/test/resources/test.jar -------------------------------------------------------------------------------- /code-assert-core/src/test/resources/test.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nidi3/code-assert/a722c20b1541cabc42f08eb4d33fd46e9745da84/code-assert-core/src/test/resources/test.zip -------------------------------------------------------------------------------- /code-assert-gui/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | code-assert-parent 6 | guru.nidi 7 | 0.9.16-SNAPSHOT 8 | 9 | 10 | code-assert-gui 11 | ${project.artifactId} 12 | 13 | 14 | guru.nidi.codeassert.gui 15 | 16 | 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-dependencies 22 | 2.4.2 23 | pom 24 | import 25 | 26 | 27 | 28 | 29 | 30 | 31 | guru.nidi 32 | code-assert 33 | ${project.version} 34 | compile 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-web 39 | 40 | 41 | com.fasterxml.jackson.core 42 | jackson-databind 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /code-assert-gui/src/main/java/guru/nidi/codeassert/gui/AppController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.gui; 17 | 18 | import guru.nidi.codeassert.model.Model; 19 | import org.springframework.web.bind.annotation.*; 20 | 21 | import java.io.File; 22 | 23 | @RestController 24 | public class AppController { 25 | 26 | @GetMapping("model") 27 | public Model model(@RequestParam String jarfile) { 28 | return Model.from(new File(jarfile)).read(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /code-assert-gui/src/main/java/guru/nidi/codeassert/gui/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.gui; 17 | 18 | import com.fasterxml.jackson.databind.Module; 19 | import com.fasterxml.jackson.databind.module.SimpleModule; 20 | import guru.nidi.codeassert.model.CodeClass; 21 | import guru.nidi.codeassert.model.CodePackage; 22 | import org.springframework.boot.SpringApplication; 23 | import org.springframework.boot.autoconfigure.SpringBootApplication; 24 | import org.springframework.context.annotation.Bean; 25 | 26 | @SpringBootApplication 27 | public class Application { 28 | public static void main(String[] args) { 29 | SpringApplication.run(Application.class, args); 30 | } 31 | 32 | @Bean 33 | Module jacksonModule() { 34 | return new SimpleModule() 35 | .addSerializer(new CodePackageSerializer(CodePackage.class)) 36 | .addSerializer(new CodeClassSerializer(CodeClass.class)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /code-assert-gui/src/main/java/guru/nidi/codeassert/gui/CodeClassSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.gui; 17 | 18 | import com.fasterxml.jackson.core.JsonGenerator; 19 | import com.fasterxml.jackson.databind.SerializerProvider; 20 | import com.fasterxml.jackson.databind.ser.std.StdSerializer; 21 | import guru.nidi.codeassert.model.CodeClass; 22 | import guru.nidi.codeassert.model.CodePackage; 23 | 24 | import java.io.IOException; 25 | import java.util.Map; 26 | 27 | public class CodeClassSerializer extends StdSerializer { 28 | protected CodeClassSerializer(Class t) { 29 | super(t); 30 | } 31 | 32 | @Override 33 | public void serialize(CodeClass value, JsonGenerator gen, SerializerProvider serializers) throws IOException { 34 | gen.writeStartObject(); 35 | gen.writeStringField("name", value.getName()); 36 | gen.writeStringField("package", value.getPackage().getName()); 37 | gen.writeNumberField("size", value.getTotalSize()); 38 | gen.writeArrayFieldStart("usePackages"); 39 | for (final CodePackage pack : value.usedPackages()) { 40 | gen.writeString(pack.getName()); 41 | } 42 | gen.writeEndArray(); 43 | gen.writeObjectFieldStart("useClasses"); 44 | for (final Map.Entry entry : value.usedClassCounts().entrySet()) { 45 | gen.writeNumberField(entry.getKey().getName(), entry.getValue()); 46 | } 47 | gen.writeEndObject(); 48 | gen.writeEndObject(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /code-assert-gui/src/main/java/guru/nidi/codeassert/gui/CodePackageSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.gui; 17 | 18 | import com.fasterxml.jackson.core.JsonGenerator; 19 | import com.fasterxml.jackson.databind.SerializerProvider; 20 | import com.fasterxml.jackson.databind.ser.std.StdSerializer; 21 | import guru.nidi.codeassert.model.CodeClass; 22 | import guru.nidi.codeassert.model.CodePackage; 23 | 24 | import java.io.IOException; 25 | 26 | public class CodePackageSerializer extends StdSerializer { 27 | protected CodePackageSerializer(Class t) { 28 | super(t); 29 | } 30 | 31 | @Override 32 | public void serialize(CodePackage value, JsonGenerator gen, SerializerProvider serializers) throws IOException { 33 | gen.writeStartObject(); 34 | gen.writeStringField("name", value.getName()); 35 | gen.writeArrayFieldStart("classes"); 36 | for (final CodeClass clazz : value.getClasses()) { 37 | gen.writeString(clazz.getName()); 38 | } 39 | gen.writeEndArray(); 40 | gen.writeArrayFieldStart("uses"); 41 | for (final CodePackage pack : value.uses()) { 42 | gen.writeString(pack.getName()); 43 | } 44 | gen.writeEndArray(); 45 | gen.writeEndObject(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /code-assert-gui/src/main/resources/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Dependencies 6 | 7 | 8 | 9 | 10 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /code-assert-gui/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /code-assert-maven-plugin/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | guru.nidi 7 | code-assert-parent 8 | 0.9.16-SNAPSHOT 9 | 10 | 11 | code-assert-maven-plugin 12 | ${project.artifactId} 13 | maven-plugin 14 | 15 | 16 | guru.nidi.codeassert.mavenplugin 17 | 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-plugin-plugin 24 | 3.4 25 | 26 | code-assert 27 | 28 | 29 | 30 | default-descriptor 31 | 32 | descriptor 33 | 34 | process-classes 35 | 36 | 37 | help-descriptor 38 | 39 | helpmojo 40 | 41 | process-classes 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.apache.maven 51 | maven-core 52 | 3.2.5 53 | 54 | 55 | com.google.guava 56 | guava 57 | 58 | 59 | 60 | 61 | org.apache.maven.plugin-tools 62 | maven-plugin-annotations 63 | 3.3 64 | provided 65 | 66 | 67 | 68 | org.twdata.maven 69 | mojo-executor 70 | 2.2.0 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/checkstyle/CheckstyleConfigs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.checkstyle; 17 | 18 | import guru.nidi.codeassert.config.*; 19 | 20 | import static com.puppycrawl.tools.checkstyle.api.TokenTypes.*; 21 | 22 | public final class CheckstyleConfigs { 23 | private static final String VARIABLE_PATTERN = "^[a-z][a-zA-Z0-9]*$"; 24 | 25 | private CheckstyleConfigs() { 26 | } 27 | 28 | public static StyleChecks.Google adjustedGoogleStyleChecks() { 29 | return StyleChecks.google() 30 | .maxLineLen(120).indentBasic(4).indentCase(4) 31 | .paramName(VARIABLE_PATTERN) 32 | .catchParamName(VARIABLE_PATTERN) 33 | .localVarName(VARIABLE_PATTERN) 34 | .memberName(VARIABLE_PATTERN) 35 | .methodName(VARIABLE_PATTERN) 36 | .emptyLineSeparatorTokens(IMPORT, CLASS_DEF, INTERFACE_DEF, ENUM_DEF, 37 | STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF); 38 | } 39 | 40 | public static CollectorTemplate minimalCheckstyleIgnore() { 41 | return CollectorTemplate.of(Ignore.class) 42 | .because("I don't agree", In.everywhere() 43 | .ignore("import.avoidStar", "custom.import.order.nonGroup.expected", "custom.import.order.lex", 44 | "javadoc.packageInfo", "javadoc.missing", 45 | "multiple.variable.declarations.comma", "final.parameter", 46 | "design.forExtension", "hidden.field", "inline.conditional.avoid", "magic.number")); 47 | } 48 | 49 | public static StyleChecks.Sun adjustedSunStyleChecks() { 50 | return StyleChecks.sun().maxLineLen(120).allowDefaultAccessMembers(true); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/checkstyle/CheckstyleMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.checkstyle; 17 | 18 | import com.puppycrawl.tools.checkstyle.api.AuditEvent; 19 | import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; 20 | import guru.nidi.codeassert.util.ResultMatcher; 21 | import org.hamcrest.Description; 22 | 23 | import static java.lang.System.lineSeparator; 24 | 25 | public class CheckstyleMatcher extends ResultMatcher { 26 | public void describeTo(Description description) { 27 | description.appendText("Has no Checkstyle issues"); 28 | } 29 | 30 | @Override 31 | protected void describeMismatchSafely(CheckstyleResult item, Description description) { 32 | for (final AuditEvent event : item.findings()) { 33 | description.appendText(lineSeparator()).appendText(printEvent(event)); 34 | } 35 | } 36 | 37 | private String printEvent(AuditEvent event) { 38 | final LocalizedMessage msg = event.getLocalizedMessage(); 39 | return String.format("%-8s %-40s %s:%d %s", 40 | msg.getSeverityLevel(), msg.getKey(), event.getFileName(), event.getLine(), event.getMessage()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/checkstyle/CheckstyleResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.checkstyle; 17 | 18 | import com.puppycrawl.tools.checkstyle.api.AuditEvent; 19 | import guru.nidi.codeassert.Analyzer; 20 | import guru.nidi.codeassert.AnalyzerResult; 21 | 22 | import java.util.List; 23 | 24 | public class CheckstyleResult extends AnalyzerResult> { 25 | CheckstyleResult(Analyzer> analyzer, 26 | List findings, List unusedActions) { 27 | super(analyzer, findings, unusedActions); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/detekt/DetektCollector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.detekt; 17 | 18 | import guru.nidi.codeassert.config.*; 19 | 20 | import java.io.File; 21 | import java.util.List; 22 | 23 | import static guru.nidi.codeassert.config.Language.KOTLIN; 24 | import static guru.nidi.codeassert.util.ListUtils.andJoin; 25 | 26 | public class DetektCollector extends BaseCollector { 27 | @Override 28 | public DetektCollector config(final CollectorConfig... configs) { 29 | return new DetektCollector() { 30 | @Override 31 | public ActionResult accept(TypedDetektFinding issue) { 32 | return accept(issue, DetektCollector.this, configs); 33 | } 34 | 35 | public List unused(UsageCounter counter) { 36 | return unused(counter, DetektCollector.this, configs); 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return andJoin(DetektCollector.this, configs); 42 | } 43 | }; 44 | } 45 | 46 | @Override 47 | public ActionResult accept(TypedDetektFinding issue) { 48 | return ActionResult.accept(null, 1); 49 | } 50 | 51 | @Override 52 | protected ActionResult doAccept(TypedDetektFinding issue, Ignore action) { 53 | final File file = new File(issue.basedir, issue.entity.getLocation().getFile()); 54 | final String className = guessClassFromFile(file.getAbsolutePath(), KOTLIN); 55 | return action.accept(new NamedLocation(issue.name, KOTLIN, className, null, true)); 56 | } 57 | 58 | @Override 59 | public List unused(UsageCounter counter) { 60 | return unusedNullAction(counter, false); 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return ""; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/detekt/DetektMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.detekt; 17 | 18 | import guru.nidi.codeassert.util.ResultMatcher; 19 | import io.gitlab.arturbosch.detekt.api.Location; 20 | import org.hamcrest.Description; 21 | 22 | import java.io.File; 23 | 24 | import static java.lang.System.lineSeparator; 25 | 26 | public class DetektMatcher extends ResultMatcher { 27 | public void describeTo(Description description) { 28 | description.appendText("Has no detekt issues"); 29 | } 30 | 31 | @Override 32 | protected void describeMismatchSafely(DetektResult item, Description description) { 33 | for (final TypedDetektFinding error : item.findings()) { 34 | description.appendText(lineSeparator()).appendText(printError(error)); 35 | } 36 | } 37 | 38 | private String printError(TypedDetektFinding finding) { 39 | final Location location = finding.entity.getLocation(); 40 | final String path = new File(location.getFile()).getAbsolutePath(); 41 | return String.format("%-15s %-15s %-30s %s:%d %s", finding.severity, finding.type, finding.name, 42 | path, location.getSource().getLine(), finding.description); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/detekt/DetektResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.detekt; 17 | 18 | import guru.nidi.codeassert.Analyzer; 19 | import guru.nidi.codeassert.AnalyzerResult; 20 | 21 | import java.util.List; 22 | 23 | public class DetektResult extends AnalyzerResult> { 24 | DetektResult(Analyzer> analyzer, 25 | List findings, List unusedActions) { 26 | super(analyzer, findings, unusedActions); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/detekt/TypedDetektFinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.detekt; 17 | 18 | import io.gitlab.arturbosch.detekt.api.*; 19 | 20 | import java.io.File; 21 | 22 | public class TypedDetektFinding { 23 | final File basedir; 24 | final Entity entity; 25 | final String type; 26 | final String name; 27 | final Severity severity; 28 | final String description; 29 | 30 | TypedDetektFinding(File basedir, Entity entity, String type, String name, Severity severity, String description) { 31 | this.basedir = basedir; 32 | this.entity = entity; 33 | this.type = type; 34 | this.name = name; 35 | this.severity = severity; 36 | this.description = description; 37 | } 38 | 39 | TypedDetektFinding(File basedir, String type, Finding finding) { 40 | this(basedir, finding.getEntity(), type, finding.getId(), 41 | finding.getIssue().getSeverity(), finding.getIssue().getDescription()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/findbugs/FindBugsConfigs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.findbugs; 17 | 18 | import guru.nidi.codeassert.config.*; 19 | 20 | import static guru.nidi.codeassert.config.Language.KOTLIN; 21 | 22 | public final class FindBugsConfigs { 23 | private FindBugsConfigs() { 24 | } 25 | 26 | public static CollectorTemplate minimalFindBugsIgnore() { 27 | return CollectorTemplate.forA(BugCollector.class) 28 | .because("modern compilers are clever", In.everywhere().ignore( 29 | "SBSC_USE_STRINGBUFFER_CONCATENATION")) 30 | .because("it's compiler generated code", In.languages(KOTLIN).ignore( 31 | "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", "BC_BAD_CAST_TO_ABSTRACT_COLLECTION")) 32 | .because("it's compiler generated code, but why?", In.languages(KOTLIN).ignore( 33 | "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")) 34 | .because("findbugs seems to be cleverer than kotlin compiler", In.languages(KOTLIN).ignore( 35 | "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")) 36 | .because("inline methods seem to cause this", In.languages(KOTLIN).ignore( 37 | "UPM_UNCALLED_PRIVATE_METHOD")); 38 | } 39 | 40 | public static CollectorTemplate dependencyTestIgnore(Class dependencyTest) { 41 | return CollectorTemplate.of(Ignore.class).just(In.clazz(dependencyTest) 42 | .ignore("NP_UNWRITTEN_FIELD", "UWF_UNWRITTEN_FIELD", "UUF_UNUSED_FIELD")); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/findbugs/FindBugsResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.findbugs; 17 | 18 | import edu.umd.cs.findbugs.BugInstance; 19 | import guru.nidi.codeassert.Analyzer; 20 | import guru.nidi.codeassert.AnalyzerResult; 21 | 22 | import java.util.List; 23 | 24 | public class FindBugsResult extends AnalyzerResult> { 25 | FindBugsResult(Analyzer> analyzer, 26 | List findings, List unusedActions) { 27 | super(analyzer, findings, unusedActions); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/junit/CodeAssertTestBase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit; 17 | 18 | import guru.nidi.codeassert.checkstyle.CheckstyleResult; 19 | import guru.nidi.codeassert.findbugs.FindBugsResult; 20 | import guru.nidi.codeassert.pmd.CpdResult; 21 | import guru.nidi.codeassert.pmd.PmdResult; 22 | 23 | public class CodeAssertTestBase extends CodeAssertCoreTestBase { 24 | private FindBugsResult findBugsResult; 25 | private PmdResult pmdResult; 26 | private CpdResult cpdResult; 27 | private CheckstyleResult checkstyleResult; 28 | 29 | protected FindBugsResult analyzeFindBugs() { 30 | return null; 31 | } 32 | 33 | protected PmdResult analyzePmd() { 34 | return null; 35 | } 36 | 37 | protected CpdResult analyzeCpd() { 38 | return null; 39 | } 40 | 41 | protected CheckstyleResult analyzeCheckstyle() { 42 | return null; 43 | } 44 | 45 | protected synchronized FindBugsResult findBugsResult() { 46 | if (findBugsResult == null) { 47 | findBugsResult = analyzeFindBugs(); 48 | } 49 | return findBugsResult; 50 | } 51 | 52 | protected synchronized PmdResult pmdResult() { 53 | if (pmdResult == null) { 54 | pmdResult = analyzePmd(); 55 | } 56 | return pmdResult; 57 | } 58 | 59 | protected synchronized CpdResult cpdResult() { 60 | if (cpdResult == null) { 61 | cpdResult = analyzeCpd(); 62 | } 63 | return cpdResult; 64 | } 65 | 66 | protected synchronized CheckstyleResult checkstyleResult() { 67 | if (checkstyleResult == null) { 68 | checkstyleResult = analyzeCheckstyle(); 69 | } 70 | return checkstyleResult; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/junit/kotlin/KotlinCodeAssertMatchers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.junit.kotlin; 17 | 18 | import guru.nidi.codeassert.detekt.DetektMatcher; 19 | import guru.nidi.codeassert.detekt.DetektResult; 20 | import guru.nidi.codeassert.ktlint.KtlintMatcher; 21 | import guru.nidi.codeassert.ktlint.KtlintResult; 22 | import org.hamcrest.Matcher; 23 | 24 | public final class KotlinCodeAssertMatchers { 25 | private KotlinCodeAssertMatchers() { 26 | } 27 | 28 | public static Matcher hasNoKtlintIssues() { 29 | return new KtlintMatcher(); 30 | } 31 | 32 | public static Matcher hasNoDetektIssues() { 33 | return new DetektMatcher(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/ktlint/KtlintCollector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.ktlint; 17 | 18 | import guru.nidi.codeassert.config.*; 19 | 20 | import java.util.List; 21 | 22 | import static guru.nidi.codeassert.util.ListUtils.andJoin; 23 | 24 | public class KtlintCollector extends BaseCollector { 25 | @Override 26 | public KtlintCollector config(final CollectorConfig... configs) { 27 | return new KtlintCollector() { 28 | @Override 29 | public ActionResult accept(LocatedLintError issue) { 30 | return accept(issue, KtlintCollector.this, configs); 31 | } 32 | 33 | public List unused(UsageCounter counter) { 34 | return unused(counter, KtlintCollector.this, configs); 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return andJoin(KtlintCollector.this, configs); 40 | } 41 | }; 42 | } 43 | 44 | @Override 45 | public ActionResult accept(LocatedLintError issue) { 46 | return ActionResult.accept(null, 1); 47 | } 48 | 49 | @Override 50 | protected ActionResult doAccept(LocatedLintError issue, Ignore action) { 51 | final String className = guessClassFromFile(issue.file.getAbsolutePath(), Language.KOTLIN); 52 | final Language language = Language.byFilename(issue.file.getName()); 53 | return action.accept(new NamedLocation(issue.ruleId, language, className, null, true)); 54 | } 55 | 56 | @Override 57 | public List unused(UsageCounter counter) { 58 | return unusedNullAction(counter, false); 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return ""; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/ktlint/KtlintMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.ktlint; 17 | 18 | import guru.nidi.codeassert.util.ResultMatcher; 19 | import org.hamcrest.Description; 20 | 21 | import static java.lang.System.lineSeparator; 22 | 23 | public class KtlintMatcher extends ResultMatcher { 24 | public void describeTo(Description description) { 25 | description.appendText("Has no ktlint issues"); 26 | } 27 | 28 | @Override 29 | protected void describeMismatchSafely(KtlintResult item, Description description) { 30 | for (final LocatedLintError error : item.findings()) { 31 | description.appendText(lineSeparator()).appendText(printError(error)); 32 | } 33 | } 34 | 35 | private String printError(LocatedLintError error) { 36 | return String.format("%-35s %s:%d %s", 37 | error.ruleId, error.file.getAbsolutePath(), error.line, error.detail); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/ktlint/KtlintResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.ktlint; 17 | 18 | import guru.nidi.codeassert.Analyzer; 19 | import guru.nidi.codeassert.AnalyzerResult; 20 | 21 | import java.util.List; 22 | 23 | public class KtlintResult extends AnalyzerResult> { 24 | KtlintResult(Analyzer> analyzer, 25 | List findings, List unusedActions) { 26 | super(analyzer, findings, unusedActions); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/ktlint/LocatedLintError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.ktlint; 17 | 18 | import java.io.File; 19 | 20 | public class LocatedLintError { 21 | final File file; 22 | final int line; 23 | final String ruleId; 24 | final String detail; 25 | 26 | LocatedLintError(File file, int line, String ruleId, String detail) { 27 | this.file = file; 28 | this.line = line; 29 | this.ruleId = ruleId; 30 | this.detail = detail; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/CpdMatchCollector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import guru.nidi.codeassert.config.*; 19 | import net.sourceforge.pmd.cpd.Mark; 20 | import net.sourceforge.pmd.cpd.Match; 21 | 22 | import java.util.List; 23 | 24 | import static guru.nidi.codeassert.util.ListUtils.andJoin; 25 | import static java.util.Collections.emptyList; 26 | 27 | public class CpdMatchCollector extends BaseCollector { 28 | @Override 29 | public CpdMatchCollector config(final CollectorConfig... configs) { 30 | return new CpdMatchCollector() { 31 | @Override 32 | public ActionResult accept(Match issue) { 33 | return accept(issue, CpdMatchCollector.this, configs); 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return andJoin(CpdMatchCollector.this, configs); 39 | } 40 | }; 41 | } 42 | 43 | @Override 44 | protected ActionResult doAccept(Match issue, Ignore action) { 45 | ActionResult res = ActionResult.undecided(null); 46 | for (final Mark mark : issue) { 47 | final Language language = Language.byFilename(mark.getFilename()); 48 | res = res.orMoreQuality(action.accept( 49 | new NamedLocation(mark.getSourceCodeSlice(), language, PmdUtils.className(mark), "", false))); 50 | } 51 | return res; 52 | } 53 | 54 | @Override 55 | public ActionResult accept(Match issue) { 56 | return ActionResult.accept(null, 1); 57 | } 58 | 59 | @Override 60 | public List unused(UsageCounter counter) { 61 | return emptyList(); 62 | } 63 | 64 | @Override 65 | public String toString() { 66 | return ""; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/CpdMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import guru.nidi.codeassert.util.ResultMatcher; 19 | import net.sourceforge.pmd.cpd.Mark; 20 | import net.sourceforge.pmd.cpd.Match; 21 | import org.hamcrest.Description; 22 | 23 | import java.util.*; 24 | 25 | import static java.lang.System.lineSeparator; 26 | 27 | public class CpdMatcher extends ResultMatcher { 28 | private static final Comparator MARK_COMPARATOR = Comparator.comparing(Mark::getFilename); 29 | 30 | public void describeTo(Description description) { 31 | description.appendText("Has no code duplications"); 32 | } 33 | 34 | @Override 35 | protected void describeMismatchSafely(CpdResult item, Description description) { 36 | for (final Match match : item.findings()) { 37 | description.appendText(lineSeparator()).appendText(printMatch(match)); 38 | } 39 | } 40 | 41 | private String printMatch(Match match) { 42 | final StringBuilder s = new StringBuilder(); 43 | boolean first = true; 44 | for (final Mark mark : getMarks(match)) { 45 | s.append(first ? String.format("%-4d ", match.getTokenCount()) : " "); 46 | first = false; 47 | s.append(String.format("%s:%d-%d%n", mark.getFilename(), mark.getBeginLine(), mark.getEndLine())); 48 | } 49 | return s.substring(0, s.length() - lineSeparator().length()); 50 | } 51 | 52 | private List getMarks(Match match) { 53 | final List marks = new ArrayList<>(); 54 | for (final Mark mark : match) { 55 | marks.add(mark); 56 | } 57 | marks.sort(MARK_COMPARATOR); 58 | return marks; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/CpdResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import guru.nidi.codeassert.Analyzer; 19 | import guru.nidi.codeassert.AnalyzerResult; 20 | import net.sourceforge.pmd.cpd.Match; 21 | 22 | import java.util.List; 23 | 24 | public class CpdResult extends AnalyzerResult> { 25 | CpdResult(Analyzer> analyzer, List findings, List unusedActions) { 26 | super(analyzer, findings, unusedActions); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/JavaUtilLoggerConfigurer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import org.slf4j.bridge.SLF4JBridgeHandler; 19 | 20 | final class JavaUtilLoggerConfigurer { 21 | static { 22 | SLF4JBridgeHandler.removeHandlersForRootLogger(); 23 | SLF4JBridgeHandler.install(); 24 | } 25 | 26 | private JavaUtilLoggerConfigurer() { 27 | } 28 | 29 | static void init() { 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/PmdMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import guru.nidi.codeassert.util.ResultMatcher; 19 | import net.sourceforge.pmd.Rule; 20 | import net.sourceforge.pmd.RuleViolation; 21 | import org.hamcrest.Description; 22 | 23 | import static java.lang.System.lineSeparator; 24 | 25 | public class PmdMatcher extends ResultMatcher { 26 | public void describeTo(Description description) { 27 | description.appendText("Has no PMD issues"); 28 | } 29 | 30 | @Override 31 | protected void describeMismatchSafely(PmdResult item, Description description) { 32 | for (final RuleViolation violation : item.findings()) { 33 | description.appendText(lineSeparator()).appendText(printViolation(violation)); 34 | } 35 | } 36 | 37 | private String printViolation(RuleViolation viol) { 38 | final Rule rule = viol.getRule(); 39 | return String.format("%-11s %-45s %s:%d %s", 40 | rule.getPriority(), rule.getName(), viol.getFilename(), viol.getBeginLine(), viol.getDescription()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/PmdResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import guru.nidi.codeassert.Analyzer; 19 | import guru.nidi.codeassert.AnalyzerResult; 20 | import net.sourceforge.pmd.RuleViolation; 21 | 22 | import java.util.List; 23 | 24 | public class PmdResult extends AnalyzerResult> { 25 | PmdResult(Analyzer> analyzer, List findings, List unusedActions) { 26 | super(analyzer, findings, unusedActions); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/PmdRuleset.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import guru.nidi.codeassert.AnalyzerException; 19 | import net.sourceforge.pmd.*; 20 | 21 | import java.lang.reflect.Field; 22 | 23 | public class PmdRuleset { 24 | final String name; 25 | 26 | public PmdRuleset(String name) { 27 | this.name = name; 28 | } 29 | 30 | public void apply(RuleSets ruleSets) { 31 | for (final Field descField : getClass().getDeclaredFields()) { 32 | final PropertyField propertyField = descField.getAnnotation(PropertyField.class); 33 | if (propertyField != null) { 34 | try { 35 | descField.setAccessible(true); 36 | final Object value = descField.get(this); 37 | if (value != null) { 38 | setProperty(ruleSets, propertyField.rule(), propertyField.property(), value); 39 | } 40 | } catch (IllegalAccessException e) { 41 | throw new AnalyzerException("Could not read property " + descField.getName() 42 | + " from class " + getClass(), e); 43 | } 44 | } 45 | } 46 | } 47 | 48 | private void setProperty(RuleSets ruleSets, String rule, String property, Object value) { 49 | final Rule r = ruleSets.getRuleByName(rule); 50 | if (r == null) { 51 | throw new AnalyzerException("Rule '" + rule + "' not existing."); 52 | } 53 | final PropertyDescriptor descriptor = (PropertyDescriptor) r.getPropertyDescriptor(property); 54 | if (descriptor == null) { 55 | throw new AnalyzerException("Property '" + property + "' for rule '" + rule + "' not existing."); 56 | } 57 | r.setProperty(descriptor, value); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/PmdUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import net.sourceforge.pmd.RuleViolation; 19 | import net.sourceforge.pmd.cpd.Mark; 20 | 21 | final class PmdUtils { 22 | private PmdUtils() { 23 | } 24 | 25 | public static String className(Mark mark) { 26 | return className(null, mark.getFilename()); 27 | } 28 | 29 | public static String className(RuleViolation violation) { 30 | if (violation.getClassName().length() > 0) { 31 | return (violation.getPackageName().length() > 0 ? (violation.getPackageName() + ".") : "") 32 | + violation.getClassName(); 33 | } 34 | return className(violation.getPackageName(), violation.getFilename()); 35 | } 36 | 37 | private static String className(String packageName, String rawFilename) { 38 | if (rawFilename.length() > 0) { 39 | final String filename = rawFilename.replace('\\', '/'); 40 | final int last = filename.lastIndexOf('/'); 41 | String prefix = ""; 42 | if (packageName != null && packageName.length() > 0) { 43 | prefix = packageName + "."; 44 | } else { 45 | //TODO can this heuristic be improved? 46 | //TODO problem with "/graphviz-java/src/main/java/..." matches first "java/" 47 | final int src = filename.indexOf("src/") + 3; 48 | final int java = filename.indexOf("java/") + 4; 49 | final int later = Math.max(src, java); 50 | if (later >= 4) { 51 | prefix = filename.substring(later + 1, last + 1).replace('/', '.'); 52 | } 53 | } 54 | return prefix + filename.substring(last + 1, filename.length() - 5); 55 | } 56 | return "?"; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /code-assert/src/main/java/guru/nidi/codeassert/pmd/PropertyField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.pmd; 17 | 18 | import java.lang.annotation.*; 19 | 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Target(ElementType.FIELD) 22 | public @interface PropertyField { 23 | String rule(); 24 | 25 | String property(); 26 | } 27 | -------------------------------------------------------------------------------- /code-assert/src/test/java/CodeCoverage.java: -------------------------------------------------------------------------------- 1 | import guru.nidi.codeassert.config.For; 2 | import guru.nidi.codeassert.jacoco.CoverageCollector; 3 | import guru.nidi.codeassert.jacoco.JacocoAnalyzer; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import static guru.nidi.codeassert.jacoco.CoverageType.*; 7 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.hasEnoughCoverage; 8 | import static org.hamcrest.MatcherAssert.assertThat; 9 | 10 | public class CodeCoverage { 11 | @Test 12 | void coverage() { 13 | JacocoAnalyzer analyzer = new JacocoAnalyzer(new CoverageCollector(BRANCH, LINE, METHOD).just( 14 | For.allPackages().setMinima(75, 80, 80), 15 | For.thePackage("*.detekt").setMinima(35, 80, 65), 16 | For.thePackage("*.findbugs").setMinima(70, 80, 80), 17 | For.thePackage("*.junit").setMinima(35, 55, 50) 18 | )); 19 | assertThat(analyzer.analyze(), hasEnoughCoverage()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/Bugs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert; 17 | 18 | public class Bugs { 19 | public void bugs() { 20 | Object a = new Integer(5); 21 | } 22 | 23 | public void more() { 24 | Object a = new Integer(5); 25 | } 26 | 27 | public static class InnerBugs { 28 | private InnerBugs() { 29 | //not seen by PMD? 30 | } 31 | 32 | public void bugs() { 33 | Object a = new Integer(5); 34 | } 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/Bugs2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert; 17 | 18 | public class Bugs2 { 19 | private Bugs2() { 20 | Integer a = new Integer(44); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/detekt/DetektAnalyzerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.detekt; 17 | 18 | import guru.nidi.codeassert.AnalyzerResult; 19 | import guru.nidi.codeassert.config.AnalyzerConfig; 20 | import guru.nidi.codeassert.config.In; 21 | import io.gitlab.arturbosch.detekt.api.Severity; 22 | import org.hamcrest.Matcher; 23 | import org.hamcrest.StringDescription; 24 | import org.junit.jupiter.api.Test; 25 | 26 | import java.io.File; 27 | 28 | import static guru.nidi.codeassert.config.Language.KOTLIN; 29 | import static guru.nidi.codeassert.junit.kotlin.KotlinCodeAssertMatchers.hasNoDetektIssues; 30 | import static guru.nidi.codeassert.pmd.RegexMatcher.matchesFormat; 31 | import static io.gitlab.arturbosch.detekt.api.Severity.Style; 32 | import static org.hamcrest.MatcherAssert.assertThat; 33 | import static org.junit.jupiter.api.Assertions.assertFalse; 34 | 35 | class DetektAnalyzerTest { 36 | @Test 37 | void analyze() { 38 | final DetektResult result = new DetektAnalyzer(AnalyzerConfig.maven(KOTLIN).mainAndTest(), new DetektCollector() 39 | .just(In.classes("Linker").ignore("MaxLineLength"))) 40 | .analyze(); 41 | assertMatcher("" 42 | + line(Style, "style", "NewLineAtEndOfFile", "Linker", 59, "Checks whether files end with a line separator."), 43 | result, hasNoDetektIssues()); 44 | } 45 | 46 | private String line(Severity severity, String type, String name, String file, int line, String desc) { 47 | return String.format("%n%-15s %-15s %-30s %s:%d %s", 48 | severity, type, name, new File("src/test/kotlin/guru/nidi/codeassert/" + file + ".kt").getAbsolutePath(), line, desc); 49 | } 50 | 51 | private > void assertMatcher(String message, T result, Matcher matcher) { 52 | assertFalse(matcher.matches(result)); 53 | final StringDescription sd = new StringDescription(); 54 | matcher.describeMismatch(result, sd); 55 | assertThat(sd.toString(), matchesFormat(message)); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/ktlint/KtlintAnalyzerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.ktlint; 17 | 18 | import guru.nidi.codeassert.AnalyzerResult; 19 | import guru.nidi.codeassert.config.AnalyzerConfig; 20 | import guru.nidi.codeassert.config.In; 21 | import org.hamcrest.Matcher; 22 | import org.hamcrest.StringDescription; 23 | import org.junit.jupiter.api.Test; 24 | 25 | import java.io.File; 26 | 27 | import static guru.nidi.codeassert.config.Language.KOTLIN; 28 | import static guru.nidi.codeassert.junit.kotlin.KotlinCodeAssertMatchers.hasNoKtlintIssues; 29 | import static guru.nidi.codeassert.pmd.RegexMatcher.matchesFormat; 30 | import static org.hamcrest.MatcherAssert.assertThat; 31 | import static org.junit.jupiter.api.Assertions.assertFalse; 32 | 33 | class KtlintAnalyzerTest { 34 | @Test 35 | void analyze() { 36 | final KtlintResult result = new KtlintAnalyzer(AnalyzerConfig.maven(KOTLIN).test(), new KtlintCollector() 37 | .just(In.classes("Linker").ignore("no-semi"))).analyze(); 38 | assertMatcher("" 39 | + line("import-ordering", "Linker", 18, "Imports must be ordered in lexicographic order without any empty lines in-between") 40 | + line("no-consecutive-blank-lines", "Linker", 34, "Needless blank line(s)") 41 | + line("no-unused-imports", "Linker", 18, "Unused import") 42 | + line("no-wildcard-imports", "Linker", 19, "Wildcard import"), 43 | result, hasNoKtlintIssues()); 44 | } 45 | 46 | private String line(String id, String file, int line, String desc) { 47 | return String.format("%n%-35s %s:%d %s", 48 | id, new File("src/test/kotlin/guru/nidi/codeassert/" + file + ".kt").getAbsolutePath(), line, desc); 49 | } 50 | 51 | private > void assertMatcher(String message, T result, Matcher matcher) { 52 | assertFalse(matcher.matches(result)); 53 | final StringDescription sd = new StringDescription(); 54 | matcher.describeMismatch(result, sd); 55 | assertThat(sd.toString(), matchesFormat(message)); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/snippets/CheckstyleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 19 | import guru.nidi.codeassert.checkstyle.*; 20 | import guru.nidi.codeassert.config.AnalyzerConfig; 21 | import guru.nidi.codeassert.config.In; 22 | import org.junit.jupiter.api.Disabled; 23 | import org.junit.jupiter.api.Test; 24 | 25 | import static guru.nidi.codeassert.junit.CodeAssertMatchers.hasNoCheckstyleIssues; 26 | import static org.hamcrest.MatcherAssert.assertThat; 27 | 28 | @Disabled 29 | //## checkstyle 30 | public class CheckstyleTest { 31 | @Test 32 | public void checkstyle() { 33 | // Analyze all sources in src/main/java 34 | AnalyzerConfig config = AnalyzerConfig.maven().main(); 35 | 36 | // Only treat issues with severity WARNING or higher 37 | StyleEventCollector collector = new StyleEventCollector().severity(SeverityLevel.WARNING) 38 | .just(In.everywhere().ignore("import.avoidStar", "javadoc.missing")) 39 | .because("in tests, long lines are ok", In.classes("*Test").ignore("maxLineLen")); 40 | 41 | //use google checks, but adjust max line length 42 | final StyleChecks checks = StyleChecks.google().maxLineLen(120); 43 | 44 | CheckstyleResult result = new CheckstyleAnalyzer(config, checks, collector).analyze(); 45 | assertThat(result, hasNoCheckstyleIssues()); 46 | } 47 | } 48 | //## 49 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/snippets/CodeCoverage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import guru.nidi.codeassert.config.For; 19 | import guru.nidi.codeassert.jacoco.CoverageCollector; 20 | import guru.nidi.codeassert.jacoco.JacocoAnalyzer; 21 | import org.junit.jupiter.api.Disabled; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import static guru.nidi.codeassert.jacoco.CoverageType.*; 25 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.hasEnoughCoverage; 26 | import static org.hamcrest.MatcherAssert.assertThat; 27 | 28 | @Disabled 29 | //## codeCoverage 30 | public class CodeCoverage { 31 | @Test 32 | public void coverage() { 33 | // Coverage of branches must be at least 70%, lines 80% and methods 90% 34 | // This is checked globally and for all packages except for entities. 35 | JacocoAnalyzer analyzer = new JacocoAnalyzer(new CoverageCollector(BRANCH, LINE, METHOD) 36 | .just(For.global().setMinima(70, 80, 90)) 37 | .just(For.allPackages().setMinima(70, 80, 90)) 38 | .just(For.thePackage("org.proj.entity.*").setNoMinima())); 39 | assertThat(analyzer.analyze(), hasEnoughCoverage()); 40 | } 41 | } 42 | //## 43 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/snippets/DependencyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import guru.nidi.codeassert.config.AnalyzerConfig; 19 | import guru.nidi.codeassert.dependency.*; 20 | import org.junit.jupiter.api.Disabled; 21 | import org.junit.jupiter.api.Test; 22 | 23 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.hasNoCycles; 24 | import static guru.nidi.codeassert.junit.CodeAssertCoreMatchers.matchesRulesExactly; 25 | import static org.hamcrest.MatcherAssert.assertThat; 26 | 27 | @Disabled 28 | //## dependency 29 | public class DependencyTest { 30 | 31 | // Analyze all sources in src/main/java 32 | private final AnalyzerConfig config = AnalyzerConfig.maven().main(); 33 | 34 | @Test 35 | public void noCycles() { 36 | assertThat(new DependencyAnalyzer(config).analyze(), hasNoCycles()); 37 | } 38 | 39 | @Test 40 | public void dependency() { 41 | // Defines the dependency rules for package org.proj 42 | class OrgProj extends DependencyRuler { 43 | // Rules for org.proj.dep, org.proj.model, org.proj.util 44 | DependencyRule dep, model, util; 45 | 46 | @Override 47 | public void defineRules() { 48 | base().mayUse(util, dep.allSubOf()); //org.proj may use org.proj.util and all subpackages of org.proj.dep 49 | dep.andAllSub().mustUse(model); //org.proj.dep and all subpackages thereof must use org.proj.model 50 | model.mayUse(util).mustNotUse(base()); //org.proj.model may use org.proj.util but not org.proj 51 | } 52 | } 53 | 54 | // All dependencies are forbidden, except the ones defined in OrgProj 55 | // java, org, net packages may be used freely 56 | DependencyRules rules = DependencyRules.denyAll() 57 | .withRelativeRules(new OrgProj()) 58 | .withExternals("java.*", "org.*", "net.*"); 59 | 60 | DependencyResult result = new DependencyAnalyzer(config).rules(rules).analyze(); 61 | assertThat(result, matchesRulesExactly()); 62 | } 63 | } 64 | //## 65 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/snippets/DetektTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import guru.nidi.codeassert.config.AnalyzerConfig; 19 | import guru.nidi.codeassert.config.In; 20 | import guru.nidi.codeassert.detekt.*; 21 | import org.junit.jupiter.api.Disabled; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import static guru.nidi.codeassert.config.Language.KOTLIN; 25 | import static guru.nidi.codeassert.junit.kotlin.KotlinCodeAssertMatchers.hasNoDetektIssues; 26 | import static org.hamcrest.MatcherAssert.assertThat; 27 | 28 | @Disabled 29 | //## detekt 30 | public class DetektTest { 31 | @Test 32 | public void analyze() { 33 | // Analyze all sources in src/main/kotlin 34 | AnalyzerConfig config = AnalyzerConfig.maven(KOTLIN).main(); 35 | 36 | DetektCollector collector = new DetektCollector() 37 | .just(In.classes("Linker").ignore("MaxLineLength")); 38 | 39 | DetektResult result = new DetektAnalyzer(config, collector).analyze(); 40 | 41 | assertThat(result, hasNoDetektIssues()); 42 | } 43 | } 44 | //## 45 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/snippets/FindBugsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import edu.umd.cs.findbugs.Priorities; 19 | import guru.nidi.codeassert.config.AnalyzerConfig; 20 | import guru.nidi.codeassert.config.In; 21 | import guru.nidi.codeassert.dependency.DependencyRules; 22 | import guru.nidi.codeassert.findbugs.*; 23 | import guru.nidi.codeassert.pmd.PmdRuleset; 24 | import org.junit.jupiter.api.Disabled; 25 | import org.junit.jupiter.api.Test; 26 | 27 | import static guru.nidi.codeassert.junit.CodeAssertMatchers.hasNoBugs; 28 | import static org.hamcrest.MatcherAssert.assertThat; 29 | 30 | @Disabled 31 | //## findBugs 32 | public class FindBugsTest { 33 | @Test 34 | public void findBugs() { 35 | // Analyze all sources in src/main/java 36 | AnalyzerConfig config = AnalyzerConfig.maven().main(); 37 | 38 | // Only treat bugs with rank < 17 and with NORMAL_PRIORITY or higher 39 | // Ignore the given bug types in the given classes / methods. 40 | BugCollector collector = new BugCollector().maxRank(17).minPriority(Priorities.NORMAL_PRIORITY) 41 | .just(In.everywhere().ignore("UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")) 42 | .because("It's checked and OK like this", 43 | In.classes(DependencyRules.class, PmdRuleset.class).ignore("DP_DO_INSIDE_DO_PRIVILEGED"), 44 | In.classes("*Test", "Rulesets") 45 | .and(In.classes("ClassFileParser").withMethods("doParse")) 46 | .ignore("URF_UNREAD_FIELD")); 47 | 48 | FindBugsResult result = new FindBugsAnalyzer(config, collector).analyze(); 49 | assertThat(result, hasNoBugs()); 50 | } 51 | } 52 | //## 53 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/snippets/KtlintTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import guru.nidi.codeassert.config.AnalyzerConfig; 19 | import guru.nidi.codeassert.config.In; 20 | import guru.nidi.codeassert.ktlint.*; 21 | import org.junit.jupiter.api.Disabled; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import static guru.nidi.codeassert.config.Language.KOTLIN; 25 | import static guru.nidi.codeassert.junit.kotlin.KotlinCodeAssertMatchers.hasNoKtlintIssues; 26 | import static org.hamcrest.MatcherAssert.assertThat; 27 | 28 | @Disabled 29 | //## ktlint 30 | public class KtlintTest { 31 | @Test 32 | public void analyze() { 33 | // Analyze all sources in src/main/kotlin 34 | AnalyzerConfig config = AnalyzerConfig.maven(KOTLIN).main(); 35 | 36 | KtlintCollector collector = new KtlintCollector() 37 | .just(In.classes("Linker").ignore("no-semi")); 38 | 39 | KtlintResult result = new KtlintAnalyzer(config, collector).analyze(); 40 | 41 | assertThat(result, hasNoKtlintIssues()); 42 | } 43 | } 44 | //## 45 | -------------------------------------------------------------------------------- /code-assert/src/test/java/guru/nidi/codeassert/snippets/ReuseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert.snippets; 17 | 18 | import guru.nidi.codeassert.config.*; 19 | import guru.nidi.codeassert.pmd.*; 20 | import net.sourceforge.pmd.RulePriority; 21 | import org.junit.jupiter.api.Disabled; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import static guru.nidi.codeassert.config.Language.KOTLIN; 25 | import static guru.nidi.codeassert.junit.CodeAssertMatchers.hasNoPmdViolations; 26 | import static guru.nidi.codeassert.pmd.PmdRulesets.*; 27 | import static org.hamcrest.MatcherAssert.assertThat; 28 | 29 | @Disabled 30 | public class ReuseTest { 31 | private final AnalyzerConfig config = AnalyzerConfig.maven().main(); 32 | private final PmdRuleset[] rules = new PmdRuleset[]{basic(), braces(), design(), empty(), optimizations()}; 33 | 34 | //## reuse 35 | private final CollectorTemplate pmdTestCollector = CollectorTemplate.forA(PmdViolationCollector.class) 36 | .because("It's a test", In.classes("*Test") 37 | .ignore("JUnitSpelling", "AvoidDuplicateLiterals", "SignatureDeclareThrowsException")) 38 | .because("It's compiler generated code", In.languages(KOTLIN) 39 | .ignore("BC_BAD_CAST_TO_ABSTRACT_COLLECTION")); 40 | 41 | @Test 42 | public void pmd() { 43 | PmdViolationCollector collector = new PmdViolationCollector().minPriority(RulePriority.MEDIUM) 44 | .apply(pmdTestCollector) 45 | .because("It's not severe and occurs often", In.everywhere().ignore("MethodArgumentCouldBeFinal")); 46 | 47 | PmdAnalyzer analyzer = new PmdAnalyzer(config, collector).withRulesets(rules); 48 | assertThat(analyzer.analyze(), hasNoPmdViolations()); 49 | } 50 | //## 51 | } 52 | -------------------------------------------------------------------------------- /code-assert/src/test/kotlin/guru/nidi/codeassert/Linker.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch) 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 guru.nidi.codeassert 17 | 18 | import java.util.List 19 | import java.util.* 20 | 21 | object Linker { 22 | private val urlRegex = Regex("(https?://|www\\.)[^\\s\\p{Z}…|”“»<>]+") 23 | private val hashRegex = Regex("#([^\\s\\p{Z}-:;,+!?()…@#*\"'/|\\[\\]{}`<>\$%^&=”“»~’\u2013\u2014.]+)") 24 | private val userRegex = Regex("@([^\\s\\p{Z}-:;,+!?()…@#*\"'/|\\[\\]{}`<>\$%^&=”“»~’\u2013\u2014]+)") 25 | 26 | fun twitter(content: String) = user(hash(url(newlines(content)), "https://twitter.com/hashtag/"), "https://twitter.com/") 27 | 28 | fun facebook(content: String) = hash(url(newlines(content)), "https://facebook.com/hashtag/") 29 | 30 | fun instagram(content: String) = user(hash(url(newlines(content)), "https://instagram.com/explore/tags/"), "https://instagram.com/") 31 | 32 | private fun newlines(s: String) = s.replace("\n", "
"); 33 | 34 | 35 | fun url(s: String) = 36 | urlRegex.replace(s) { res -> 37 | val value = res.value 38 | val trim = value.trimEnd('.', ')', ',', '!', '?') 39 | val rest = value.substring(trim.length) 40 | """$trim$rest""" 41 | } 42 | 43 | fun hash(s: String, base: String) = try { 44 | hashRegex.replace(s, """#$1""") 45 | } catch (e: Exception) { 46 | "" 47 | } 48 | 49 | fun user(s: String, base: String) = 50 | userRegex.replace(s) { res -> 51 | val value = res.groups[1]!!.value 52 | if (".." in value) res.value 53 | else { 54 | val trim = value.trimEnd('.') 55 | val rest = value.substring(trim.length) 56 | """@$trim$rest""" 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /owasp-suppression.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | CVE-2019-9658 5 | CVE-2019-10782 6 | cpe:2.3:a:checkstyle:checkstyle:6.19 7 | 8 | 9 | CVE-2019-10086 10 | cpe:2.3:a:apache:commons_beanutils:1.9.2 11 | 12 | 13 | CVE-2018-10237 14 | CVE-2020-8908 15 | cpe:2.3:a:google:guava:19.0 16 | 17 | 18 | Directory traversal in org.codehaus.plexus.util.Expand 19 | Possible XML Injection 20 | cpe:2.3:a:plexus-utils_project:plexus-utils:3.0.20 21 | 22 | 23 | CVE-2019-7722 24 | cpe:2.3:a:pmd_project:pmd:5.8.1 25 | 26 | 27 | CVE-2020-10683 28 | cpe:/a:dom4j_project:dom4j 29 | 30 | 31 | CVE-2020-15250 32 | cpe:2.3:a:junit:junit:4.13.1 33 | 34 | 35 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | 7 | guru.nidi 8 | guru-nidi-parent-pom 9 | 1.1.35 10 | 11 | 12 | 13 | code-assert-parent 14 | 0.9.16-SNAPSHOT 15 | pom 16 | 17 | ${project.artifactId} 18 | Assert that the java code of a project satisfies certain checks. 19 | https://github.com/nidi3/code-assert 20 | 2015 21 | 22 | 23 | 1.8 24 | owasp-suppression.xml 25 | 26 | 27 | 28 | code-assert-core 29 | code-assert 30 | code-assert-maven-plugin 31 | code-assert-gui 32 | 33 | 34 | 35 | scm:git:https://github.com/nidi3/code-assert 36 | scm:git:https://github.com/nidi3/code-assert 37 | https://github.com/nidi3/code-assert 38 | HEAD 39 | 40 | 41 | 42 | 43 | 44 | 45 | guru.nidi.maven.plugins 46 | snippets-maven-plugin 47 | 1.0.0 48 | 49 | 50 | 51 | 52 | 53 | 54 | --------------------------------------------------------------------------------