Skip to content

Commit 34a9c44

Browse files
authored
Merge 62eb02f into 1967342
2 parents 1967342 + 62eb02f commit 34a9c44

File tree

3 files changed

+88
-24
lines changed

3 files changed

+88
-24
lines changed

main/src/main/java/org/mobilitydata/gtfsvalidator/table/GtfsStopSchema.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public interface GtfsStopSchema extends GtfsEntity {
3535

3636
String ttsStopName();
3737

38-
@MixedCase
3938
String stopDesc();
4039

4140
@FieldType(FieldTypeEnum.LATITUDE)

processor/src/main/java/org/mobilitydata/gtfsvalidator/processor/MixedCaseValidatorGenerator.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,45 @@ private static TypeSpec generateValidator(GtfsFileDescriptor fileDescriptor) {
7474
.beginControlFlow(
7575
"if (entity.$L())", FieldNameConverter.hasMethodName(mixedCaseField.name()))
7676
.addStatement("$T value = entity.$L()", String.class, mixedCaseField.name())
77-
.beginControlFlow("if (!(value.matches(\".*[a-z].*\") && value.matches(\".*[A-Z].*\")))")
77+
.addStatement("$T[] tokens = value.split(\"[^\\\\p{L}]+\")", String.class)
78+
.addComment(
79+
"If there is only one token, and no numbers, check that it is not all lowercase")
80+
.beginControlFlow("if (tokens.length == 1)")
81+
.beginControlFlow(
82+
"if (tokens[0].length() > 1 && !tokens[0].matches(\".*\\\\d+.*\") && tokens[0].matches(\"^\\\\p{Ll}+$$\"))")
83+
.addStatement(
84+
"noticeContainer.addValidationNotice(new $T(\"$L\", \"$L\", value, entity.csvRowNumber()))",
85+
MixedCaseRecommendedFieldNotice.class,
86+
fileDescriptor.filename(),
87+
FieldNameConverter.gtfsColumnName(mixedCaseField.name()))
88+
.endControlFlow()
89+
.endControlFlow()
90+
.beginControlFlow("else")
91+
.addComment(
92+
"If there are multiple tokens, find all without numbers and check that at least one is mixed case")
93+
.addStatement("boolean hasMixedCaseToken = false")
94+
.addStatement("int noNumberTokensCount = 0")
95+
.beginControlFlow("for (String token : tokens)")
96+
.beginControlFlow("if (token.length() == 1 || token.matches(\".*\\\\d+.*\"))")
97+
.addStatement("continue")
98+
.endControlFlow()
99+
.beginControlFlow("else")
100+
.addStatement("noNumberTokensCount++")
101+
.beginControlFlow("if (token.matches(\"^(?=.*\\\\p{Lu})(?=.*\\\\p{Ll}).*$$\"))")
102+
.addStatement("hasMixedCaseToken = true")
103+
.endControlFlow()
104+
.endControlFlow()
105+
.endControlFlow()
106+
.beginControlFlow("if (noNumberTokensCount >= 2 && !hasMixedCaseToken)")
78107
.addStatement(
79108
"noticeContainer.addValidationNotice(new $T(\"$L\", \"$L\", value, entity.csvRowNumber()))",
80109
MixedCaseRecommendedFieldNotice.class,
81110
fileDescriptor.filename(),
82111
FieldNameConverter.gtfsColumnName(mixedCaseField.name()))
83112
.endControlFlow()
113+
.endControlFlow()
84114
.endControlFlow();
115+
85116
}
86117

87118
typeSpec.addMethod(validateMethod.build());

processor/tests/src/test/java/org/mobilitydata/gtfsvalidator/processor/tests/MixedCaseSchemaTest.java

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import static com.google.common.truth.Truth.assertThat;
1919

2020
import com.google.common.collect.ImmutableList;
21+
import java.util.Arrays;
22+
import java.util.Collection;
2123
import org.junit.Before;
2224
import org.junit.Test;
2325
import org.junit.runner.RunWith;
24-
import org.junit.runners.JUnit4;
26+
import org.junit.runners.Parameterized;
2527
import org.mobilitydata.gtfsvalidator.notice.MixedCaseRecommendedFieldNotice;
2628
import org.mobilitydata.gtfsvalidator.table.MixedCaseTest;
2729
import org.mobilitydata.gtfsvalidator.table.MixedCaseTestTableDescriptor;
@@ -30,8 +32,15 @@
3032
import org.mobilitydata.gtfsvalidator.validator.ValidatorLoader;
3133
import org.mobilitydata.gtfsvalidator.validator.ValidatorLoaderException;
3234

33-
@RunWith(JUnit4.class)
35+
@RunWith(Parameterized.class)
3436
public class MixedCaseSchemaTest {
37+
private final String value;
38+
private final boolean isValid;
39+
40+
public MixedCaseSchemaTest(String value, boolean isValid) {
41+
this.value = value;
42+
this.isValid = isValid;
43+
}
3544

3645
private MixedCaseTestTableDescriptor tableDescriptor;
3746
private LoadingHelper helper;
@@ -44,29 +53,54 @@ public void setup() throws ValidatorLoaderException {
4453
ValidatorLoader.createForClasses(ImmutableList.of(MixedCaseTestMixedCaseValidator.class)));
4554
}
4655

47-
@Test
48-
public void testValidMixedCase() throws ValidatorLoaderException {
49-
50-
String[] validValues = {
51-
"MixedCase", "Mixed-Case", "Mixed_Case", "Mixed Case", "Another good value"
52-
};
53-
54-
for (String value : validValues) {
55-
helper.load(tableDescriptor, MixedCaseTest.SOME_FIELD_FIELD_NAME, value);
56-
57-
assertThat(helper.getValidationNotices()).isEmpty();
58-
}
56+
@Parameterized.Parameters
57+
public static Collection<Object[]> data() {
58+
return Arrays.asList(
59+
new Object[][] {
60+
// valid values
61+
{"Mixed-Case", true},
62+
{"Mixed_Case", true},
63+
{"Mixed Case", true},
64+
{"22222", true},
65+
{"A1", true},
66+
{"ZA112", true},
67+
{"301", true},
68+
{"RTE 30", true},
69+
{"급 행 12", true},
70+
{"급행12", true},
71+
{"東西線", true},
72+
{"101B", true},
73+
{"A14C", true},
74+
{"A14c", true},
75+
{"A14-C", true},
76+
{"A14_C", true},
77+
{"A14 C", true},
78+
{"Route 1", true},
79+
{"Route 1 Boulevard", true},
80+
{"ZA12", true},
81+
{"Avenue des Champs-Élysées", true},
82+
{"UPPERCASE", true},
83+
{"ROUTE 22", true},
84+
{"ROUTE 1", true},
85+
{"route 1 Boulevard", true},
86+
{"Sentences are ok with one mixed case word", true},
87+
{"MixedCaseButSingleWord", true},
88+
// invalid values
89+
{"lowercase", false},
90+
{"snake_case", false},
91+
{"kebab-case", false},
92+
{"UPPER-CASE", false},
93+
{"lower case space", false},
94+
{"34broadst", false},
95+
});
5996
}
6097

6198
@Test
62-
public void testInvalidMixedCases() throws ValidatorLoaderException {
63-
String[] invalidValues = {
64-
"lowercase", "UPPERCASE", "snake_case", "kebab-case", "UPPER-CASE", "lower case space"
65-
};
66-
67-
for (String value : invalidValues) {
68-
helper.load(tableDescriptor, MixedCaseTest.SOME_FIELD_FIELD_NAME, value);
69-
99+
public void testMixedCase() throws ValidatorLoaderException {
100+
helper.load(tableDescriptor, MixedCaseTest.SOME_FIELD_FIELD_NAME, value);
101+
if (isValid) {
102+
assertThat(helper.getValidationNotices()).isEmpty();
103+
} else {
70104
assertThat(helper.getValidationNotices())
71105
.containsExactly(
72106
new MixedCaseRecommendedFieldNotice(

0 commit comments

Comments
 (0)