Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from jborgers/kotlin/AvoidSimpleDateFormat
added AvoidSimpleDataFormat for Kotlin
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/test/java/com/jpinpoint/perf/lang/kotlin/ruleset/common/AvoidSimpleDateFormatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.jpinpoint.perf.lang.kotlin.ruleset.common; | ||
|
||
import net.sourceforge.pmd.testframework.PmdRuleTst; | ||
|
||
public class AvoidSimpleDateFormatTest extends PmdRuleTst { | ||
} |
68 changes: 68 additions & 0 deletions
68
...est/resources/com/jpinpoint/perf/lang/kotlin/ruleset/common/xml/AvoidSimpleDateFormat.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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 http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> | ||
<test-code> | ||
<description>violation: Avoid the use of SimpleDateFormat</description> | ||
<expected-problems>1</expected-problems> | ||
<expected-linenumbers>6</expected-linenumbers> | ||
<code><![CDATA[ | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
class Foo { | ||
private fun toKey(rekenDatum: Date): String { | ||
val formatter = SimpleDateFormat("yyyy-MM-dd") //bad | ||
return formatter.format(rekenDatum) | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
<test-code> | ||
<description>no violation: SimpleDateFormat is allowed in Jackon's ObjectMapper</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.dataformat.xml.XmlMapper | ||
import java.text.SimpleDateFormat | ||
object Foo { | ||
private val OBJECT_MAPPER = createObjectMapper() | ||
private val XML_MAPPER: XmlMapper | ||
private val unknown: Unknown = createUnknownObject() | ||
init { | ||
XML_MAPPER = XmlMapper() | ||
val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss") // good, used in XmlMapper | ||
XML_MAPPER.setDateFormat(df) | ||
} | ||
private fun createObjectMapper(): ObjectMapper { | ||
val mapper = ObjectMapper() | ||
mapper.setDateFormat(SimpleDateFormat("yyyy-MM-dd")) // good, used in ObjectMapper | ||
return mapper | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
<test-code> | ||
<description>violation: SimpleDateFormat is not allowed in non XmlMapper or ObjectMapper</description> | ||
<expected-problems>1</expected-problems> | ||
<expected-linenumbers>8</expected-linenumbers> | ||
<code><![CDATA[ | ||
import java.text.SimpleDateFormat | ||
object Foo { | ||
private val unknown: Unknown = createUnknownObject() | ||
private fun createUnknownObject(): Unknown { | ||
val unknown: Unknown = Unknown() | ||
unknown.setDateFormat(SimpleDateFormat("yyyy-MM-dd")) // bad, used in unknown object | ||
return unknown | ||
} | ||
} | ||
]]></code> | ||
</test-code> | ||
</test-data> |