Skip to content

Commit c37080d

Browse files
author
Vasil Hristov
authored
Merge pull request #1570 from NativeScript/vmutafov/metadata-filtering-improvements
Metadata filtering empty line bug fix
2 parents 722c9c5 + c3e649b commit c37080d

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

test-app/build-tools/android-metadata-generator/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ dependencies {
5151

5252
testCompile 'junit:junit:4.13'
5353
testCompile 'org.mockito:mockito-core:3.0.0'
54-
implementation 'junit:junit:4.12'
54+
testImplementation 'junit:junit:4.12'
5555
}
5656

5757
task copyNecessaryFiles {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.telerik.metadata.security.filtering.input.user
22

3-
class UserLineCommentFilter {
3+
class UserLineFilter {
44
fun isCommentLine(line: String) = line.startsWith("//") || line.startsWith("#")
5+
fun isEmptyLine(line: String) = line.isBlank()
56
}

test-app/build-tools/android-metadata-generator/src/src/com/telerik/metadata/security/filtering/input/user/UserPatternsCollection.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ object UserPatternsCollection : PatternsCollection {
4040
private fun parseFile(path: Path): Collection<PatternEntry> {
4141
val fileLines = Files.readAllLines(path)
4242
val lineSplitter = UserLineSplitter()
43-
val commentFilter = UserLineCommentFilter()
43+
val lineFilter = UserLineFilter()
4444

4545
return fileLines
46-
.filter { !commentFilter.isCommentLine(it) }
46+
.asSequence()
47+
.filter { !lineFilter.isCommentLine(it) }
48+
.filter { !lineFilter.isEmptyLine(it) }
4749
.map { lineSplitter.splitLine(it) }
50+
.toList()
4851
}
4952
}

test-app/build-tools/android-metadata-generator/src/src/com/telerik/metadata/security/filtering/matching/impl/PatternMatcherImpl.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ class PatternMatcherImpl : PatternMatcher {
99
Algorithm is borrowed from: https://www.geeksforgeeks.org/wildcard-character-matching/
1010
*/
1111

12+
13+
// Short circuit if pattern is '*'
14+
if (pattern == "*" && input.isNotEmpty()) {
15+
return true
16+
}
17+
1218
// If we reach at the end of both strings,
1319
// we are done
1420
if (pattern.isEmpty() && input.isEmpty())
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import org.junit.Test
44

55
import org.junit.Assert.*
66

7-
class UserLineCommentFilterTest {
7+
class UserLineFilterTest {
88

99
companion object {
1010
private const val UNEXPECTED_LINE_FILTERING_RESULT_MESSAGE = "Unexpected line filtering result"
1111
}
1212

13-
private val filter = UserLineCommentFilter()
13+
private val filter = UserLineFilter()
1414

1515
@Test
1616
fun `Is regular commented line`() {
@@ -38,4 +38,22 @@ class UserLineCommentFilterTest {
3838

3939
assertFalse(UNEXPECTED_LINE_FILTERING_RESULT_MESSAGE, isComment)
4040
}
41+
42+
@Test
43+
fun `Is empty line`(){
44+
val line = System.lineSeparator()
45+
46+
val isEmptyLine = filter.isEmptyLine(line)
47+
48+
assertTrue(UNEXPECTED_LINE_FILTERING_RESULT_MESSAGE, isEmptyLine)
49+
}
50+
51+
@Test
52+
fun `Is whitespace line`(){
53+
val line = " "
54+
55+
val isEmptyLine = filter.isEmptyLine(line)
56+
57+
assertTrue(UNEXPECTED_LINE_FILTERING_RESULT_MESSAGE, isEmptyLine)
58+
}
4159
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.telerik.metadata.security.filtering.matching.impl
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.Parameterized
7+
8+
9+
@RunWith(Parameterized::class)
10+
class PatternMatcherImplTest(private var pattern: String, private var input: String, private var shouldMatch: Boolean) {
11+
12+
private val matcher = PatternMatcherImpl()
13+
14+
companion object {
15+
@JvmStatic
16+
@Parameterized.Parameters(name = "Case {index}: matcher.match({0}, {1}) should be {2}")
17+
fun data(): Collection<Array<Any>> {
18+
return listOf(
19+
arrayOf("g*ks", "geeks", true),
20+
arrayOf("ge?ks*", "geeksforgeeks", true),
21+
arrayOf("g*k", "gee", false),
22+
arrayOf("*pqrs", "pqrst", false),
23+
arrayOf("abc*bcd", "abcdhghgbcd", true),
24+
arrayOf("abc*c?d", "abcd", false),
25+
arrayOf("*c*d", "abcd", true),
26+
arrayOf("*?c*d", "abcd", true)
27+
)
28+
}
29+
}
30+
31+
@Test
32+
fun `Test match`() {
33+
assertEquals("Wrong pattern matcher result!", shouldMatch, matcher.match(pattern, input))
34+
}
35+
}

0 commit comments

Comments
 (0)