Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging rule: MdcOutsideTryStatement #80

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,53 @@
]]>
</example>
</rule>

<rule name="MdcOutsideTryStatement"
since="0.1.0"
language="java"
externalInfoUrl="https://github.com/dgroup/arch4u-pmd/discussions/TBD"
message="TBD"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
TBD
See discussion about implementation tips/tricks on Github: https://github.com/dgroup/arch4u-pmd/discussions/TBD
</description>
<priority>3</priority>
<properties>
<property name="mdcClasses" type="List[String]" delimiter="|"
description="Full name of the MDC classes"
value="org.slf4j.MDC"/>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix
[some $class in $mdcClasses satisfies pmd-java:typeIsExactly($class)]
[
./*[ends-with(@Image,'.put') and not(ancestor::TryStatement)]
or
./*[(ends-with(@Image,'.remove') or ends-with(@Image,'.clear')) and not(ancestor::FinallyStatement)]
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
import org.slf4j.MDC;

public class Foo {
public void bar() {
try {
MDC.put(request, "someValue");
// ... business logic
} finally {
MDC.remove(request);
// or MDC.clear();
}
}
}
]]>
</example>
</rule>
</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* MIT License
*
* Copyright (c) 2019-2022 Yurii Dubinka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.dgroup.arch4u.pmd;

import net.sourceforge.pmd.testframework.SimpleAggregatorTst;

/**
* Test case for {@code MdcOutsideTryStatement} rule.
*
* @since 0.1.0
*/
@SuppressWarnings({"PMD.TestClassWithoutTestCases", "PMD.JUnit4TestShouldUseBeforeAnnotation"})
public final class MdcOutsideTryStatementTest extends SimpleAggregatorTst {

@Override
public void setUp() {
addRule(
"io/github/dgroup/arch4u/pmd/arch4u-ruleset.xml",
"MdcOutsideTryStatement"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ MIT License
~
~ Copyright (c) 2019-2022 Yurii Dubinka
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"),
~ to deal in the Software without restriction, including without limitation
~ the rights to use, copy, modify, merge, publish, distribute, sublicense,
~ and/or sell copies of the Software, and to permit persons to whom
~ the Software is furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included
~ in all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
~ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
~ OR OTHER DEALINGS IN THE SOFTWARE.
-->

<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests https://pmd.sourceforge.io/rule-tests_1_0_0.xsd">

<test-code>
<description>
[GOOD]: MDC is used in `try` block, `clear` method is invoked in `finally` block
</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.slf4j.MDC;

public class Foo {
public void bar() {
try {
MDC.put(request, "someValue");
// ...logic
} catch(Exception exc) {
// ...logic
} finally {
MDC.clear();
}
}
}
]]></code>
</test-code>

<test-code>
<description>
[GOOD]: MDC is used in try-statement, `remove` method is in `finally` block
</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.slf4j.MDC;

public class Foo {
public void bar() {
try {
MDC.put(request, "someValue");
// ...logic
} catch(Exception exc) {
// ...logic
} finally {
MDC.remove(request);
}
}
}
]]></code>
</test-code>

<test-code>
<description>
[GOOD]: MDC is used in try-statement, `remove` and `put` methods are in the `finally` block
</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.slf4j.MDC;

public class Foo {
public void bar() {
try {
// ...logic
} catch(Exception exc) {
// ...logic
} finally {
MDC.put(request, "anotherValue");
// ...logic
MDC.remove(request);
}
}
}
]]></code>
</test-code>

<test-code>
<description>
[GOOD]: MDC is used in try-statement, however there is no `remove` method
(it's prohibited by another rule: MdcIsNotCleared)
</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.slf4j.MDC;

public class Foo {
public void bar() {
try {
MDC.put(request, "someValue");
// ...logic
} catch(Exception exc) {
// ...logic
}
}
}
]]></code>
</test-code>

<test-code>
<description>
[GOOD]: MDC is not used
</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.slf4j.MDC;
public class SomeClass {
public void method(String str, BigInteger num) {
try (Connection conn = new Connection()) {
conn.executeStatement(str);
}
}
}
]]></code>
</test-code>

<test-code>
<description>
[BAD]: MDC is used not in try-statement
</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>5,7</expected-linenumbers>
<code><![CDATA[
import org.slf4j.MDC;

public class Foo {
public void bar() {
MDC.put(request, "someValue");
// ...logic
MDC.remove(request);
}
}
]]></code>
</test-code>

<test-code>
<description>
[BAD]: MDC `remove` invocation is used not in `finally` block
</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>8</expected-linenumbers>
<code><![CDATA[
import org.slf4j.MDC;

public class Foo {
public void bar() {
try {
MDC.put(request, "someValue");
// ...logic
MDC.remove(request); //violation
} catch(Exception exc) {}
}
}
]]></code>
</test-code>

</test-data>