Skip to content

Commit 114ce5e

Browse files
committed
Switch to JUnit and add tests
JUnit is the only supported test framework for JOSM plugins. https://josm.openstreetmap.de/ticket/23219
1 parent 011da81 commit 114ce5e

File tree

12 files changed

+355
-147
lines changed

12 files changed

+355
-147
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.1.1] - 2023-10-XX
8+
### Added
9+
- More tests, including some regression tests
10+
### Changed
11+
- Update Gradle to 8.4
12+
- Switch the test framework from TestNG to JUnit because of upstream dependency
13+
- Fixed a typo in a warning message
14+
- Fixed duplicate warning for the same autofixable issue (bad separator and beatutifiable)
15+
716
## [1.1.0] - 2023-10-05
817
### Added
918
- New validator message in case no (auto-fixable) formatting issue found

build.gradle.kts

+12-8
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,31 @@ fun getGitHash(): String {
7474
}
7575

7676
dependencies {
77+
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
7778
packIntoJar(kotlin("stdlib"))
7879

7980
implementation("org.openstreetmap.josm.plugins:libphonenumber:8.+") { isChanging = true }
8081

81-
testImplementation("org.testng:testng") {
82-
version {
83-
strictly("7.5")
84-
because("TestNG 7.6 and up requires Java 11 or newer")
85-
}
86-
}
82+
// fix test runtime issue
83+
// https://mvnrepository.com/artifact/org.wiremock/wiremock-standalone
84+
testImplementation("org.wiremock:wiremock-standalone:3.2.0")
85+
testImplementation(kotlin("reflect"))
86+
87+
val junit = "5.9.3"
88+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junit}")
89+
testImplementation("org.junit.jupiter:junit-jupiter-api:${junit}")
90+
testImplementation("org.openstreetmap.josm:josm-unittest:SNAPSHOT") { isChanging = true }
8791
}
8892

8993
tasks.test {
90-
useTestNG()
94+
useJUnitPlatform()
9195
finalizedBy(tasks.jacocoTestReport) // report is always generated after tests run
9296
}
9397

9498
tasks.jacocoTestReport {
9599
dependsOn(tasks.test) // tests are required to run before generating the report
96100
reports {
97-
xml.required.set(true)
101+
xml.required = true
98102
}
99103
}
100104

src/main/kotlin/com/github/gabortim/phonenumber/action/PhoneNumberDownloadAction.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.gabortim.phonenumber.action
22

3+
import com.github.gabortim.phonenumber.task.PhoneNumberDownloadTask
34
import org.openstreetmap.josm.actions.JosmAction
45
import org.openstreetmap.josm.data.Bounds
56
import org.openstreetmap.josm.gui.MainApplication

src/main/kotlin/com/github/gabortim/phonenumber/action/PhoneNumberDownloadTask.kt renamed to src/main/kotlin/com/github/gabortim/phonenumber/task/PhoneNumberDownloadTask.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.gabortim.phonenumber.action
1+
package com.github.gabortim.phonenumber.task
22

33
import com.github.gabortim.phonenumber.test.PhoneNumberValidator
44
import org.openstreetmap.josm.data.Bounds

src/main/kotlin/com/github/gabortim/phonenumber/test/PhoneNumber.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class PhoneNumber(
159159
if (number != formatted.first)
160160
isFormatted = true
161161
}
162+
162163
FailReason.UNUSUAL_CHARS -> unusualChars.add(number)
163164
FailReason.INVALID -> invalid.add(number)
164165
FailReason.TOO_SHORT -> tooShort.add(number)
@@ -240,12 +241,15 @@ class PhoneNumber(
240241
}
241242

242243
/**
243-
* Replies true if any of the phone numbers has separator issue.
244+
* Returns true if any of the phone numbers has separator issue.
244245
*/
245246
private fun hasWrongSeparator(): Boolean {
246247
return badSeparator.isNotEmpty()
247248
}
248249

250+
/**
251+
* Returns true if the primitive is fixable.
252+
*/
249253
fun isFixable(): Boolean {
250254
return hasDuplicates() || hasPremiumNumber() || hasWrongSeparator() || isFormatted || isBeautifyable || hasSwitchedClass || hasSchemaChange
251255
}
@@ -267,7 +271,7 @@ class PhoneNumber(
267271
if (isFormatted)
268272
description.add(tr("not in E.123 format"))
269273
else if (isBeautifyable)
270-
description.add(tr("beatuifyable value"))
274+
description.add(tr("beautifiable value"))
271275
if (hasSwitchedClass)
272276
description.add(tr("inappropriate key"))
273277
if (hasSchemaChange)

src/main/kotlin/com/github/gabortim/phonenumber/test/PhoneNumberValidator.kt

+13-12
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,6 @@ class PhoneNumberValidator : TagTest(
101101
parsedNumbers = PhoneNumber(primitive, getIso3166Alpha2Code(primitive), forceContactSchemeProperty.get())
102102

103103
/// non autofixable errors
104-
for (key in parsedNumbers.badSeparator) {
105-
errors.add(
106-
TestError.builder(this, Severity.ERROR, BAD_SEPARATOR)
107-
.message(
108-
tr("Phone number invalid"),
109-
tr("wrong separator used in {0} key", key)
110-
)
111-
.primitives(primitive)
112-
.build()
113-
)
114-
}
115104
for (value in parsedNumbers.invalid) {
116105
errors.add(
117106
TestError.builder(this, Severity.ERROR, PARSE_ERROR)
@@ -169,7 +158,19 @@ class PhoneNumberValidator : TagTest(
169158
}
170159

171160
/// autofixable warnings
172-
if (parsedNumbers.isFixable()) {
161+
for (key in parsedNumbers.badSeparator) {
162+
errors.add(
163+
TestError.builder(this, Severity.ERROR, BAD_SEPARATOR)
164+
.message(
165+
tr("Phone number invalid"),
166+
tr("wrong separator used in {0} key", key)
167+
)
168+
.primitives(primitive)
169+
.build()
170+
)
171+
}
172+
// avoid duplicate warning
173+
if (parsedNumbers.isFixable() && parsedNumbers.badSeparator.isEmpty()) {
173174
errors.add(
174175
TestError.builder(this, Severity.WARNING, MULTI)
175176
.message(

src/main/kotlin/com/github/gabortim/phonenumber/tool/NumberTools.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal object NumberTools {
5757
* @return Split string in an array
5858
*/
5959
fun splitAndStrip(value: String): Collection<String> {
60-
return value.split(Regex("[,;]"))
60+
return value.split(PatternUtils.compile("[,;]"))
6161
.filter(String::isNotBlank)
6262
.map(String::trim)
6363
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.gabortim.phonenumber.test
2+
3+
import org.junit.jupiter.api.Assertions.assertFalse
4+
import org.junit.jupiter.api.Assertions.assertTrue
5+
import org.junit.jupiter.api.Test
6+
import org.openstreetmap.josm.data.coor.LatLon
7+
import org.openstreetmap.josm.data.osm.Node
8+
import org.openstreetmap.josm.data.osm.TagMap
9+
import org.openstreetmap.josm.testutils.annotations.BasicPreferences
10+
11+
@BasicPreferences
12+
/**
13+
* Test for PhoneNumber.
14+
*/
15+
class PhoneNumberTest {
16+
@Test
17+
fun isFixable() {
18+
val node = Node(LatLon.ZERO)
19+
val tags = TagMap("phone", "+36 66 390 686")
20+
node.setKeys(tags)
21+
22+
assertFalse(PhoneNumber(node, "HU", false).isFixable())
23+
assertTrue(PhoneNumber(node, "HU", true).isFixable())
24+
}
25+
26+
@Test
27+
fun testDuplicateDetection() {
28+
val node = Node(LatLon.ZERO)
29+
val tags = TagMap("contact:mobile", "+36 30 000 0000;+36 30 000 0000")
30+
node.setKeys(tags)
31+
32+
assertTrue(PhoneNumber(node, "HU", true).isFixable())
33+
}
34+
}

0 commit comments

Comments
 (0)