Skip to content

Commit 702495b

Browse files
authored
Merge pull request eugenp#5721 from eugenp/fix-matcher-test
fix regex matcher unit test
2 parents 86184f8 + 3fa720b commit 702495b

1 file changed

Lines changed: 22 additions & 21 deletions

File tree

‎core-java/src/test/java/com/baeldung/regexp/optmization/OptimizedMatcherUnitTest.java‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.regexp.optmization;
22

3-
import org.junit.After;
43
import org.junit.Before;
54
import org.junit.Test;
65

@@ -14,13 +13,15 @@
1413

1514
public class OptimizedMatcherUnitTest {
1615

17-
private long time;
18-
private long mstimePreCompiled;
19-
private long mstimeNotPreCompiled;
20-
2116
private String action;
2217

2318
private List<String> items;
19+
20+
private class TimeWrapper {
21+
private long time;
22+
private long mstimePreCompiled;
23+
private long mstimeNotPreCompiled;
24+
}
2425

2526
@Before
2627
public void setup() {
@@ -48,27 +49,27 @@ public void setup() {
4849

4950
@Test
5051
public void givenANotPreCompiledAndAPreCompiledPatternA_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
51-
52-
testPatterns("A*");
53-
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
52+
TimeWrapper timeObj = new TimeWrapper();
53+
testPatterns("A*", timeObj);
54+
assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled);
5455
}
5556

5657
@Test
5758
public void givenANotPreCompiledAndAPreCompiledPatternABC_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
58-
59-
testPatterns("A*B*C*");
60-
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
59+
TimeWrapper timeObj = new TimeWrapper();
60+
testPatterns("A*B*C*", timeObj);
61+
assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled);
6162
}
6263

6364
@Test
6465
public void givenANotPreCompiledAndAPreCompiledPatternECWF_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
65-
66-
testPatterns("E*C*W*F*");
67-
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
66+
TimeWrapper timeObj = new TimeWrapper();
67+
testPatterns("E*C*W*F*", timeObj);
68+
assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled);
6869
}
6970

70-
private void testPatterns(String regex) {
71-
time = System.nanoTime();
71+
private void testPatterns(String regex, TimeWrapper timeObj) {
72+
timeObj.time = System.nanoTime();
7273
int matched = 0;
7374
int unmatched = 0;
7475

@@ -83,10 +84,10 @@ private void testPatterns(String regex) {
8384

8485
this.action = "uncompiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
8586

86-
this.mstimeNotPreCompiled = (System.nanoTime() - time) / 1000000;
87-
System.out.println(this.action + ": " + mstimeNotPreCompiled + "ms");
87+
timeObj.mstimeNotPreCompiled = (System.nanoTime() - timeObj.time) / 1000000;
88+
System.out.println(this.action + ": " + timeObj.mstimeNotPreCompiled + "ms");
8889

89-
time = System.nanoTime();
90+
timeObj.time = System.nanoTime();
9091

9192
Matcher matcher = Pattern.compile(regex).matcher("");
9293
matched = 0;
@@ -103,7 +104,7 @@ private void testPatterns(String regex) {
103104

104105
this.action = "compiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
105106

106-
this.mstimePreCompiled = (System.nanoTime() - time) / 1000000;
107-
System.out.println(this.action + ": " + mstimePreCompiled + "ms");
107+
timeObj.mstimePreCompiled = (System.nanoTime() - timeObj.time) / 1000000;
108+
System.out.println(this.action + ": " + timeObj.mstimePreCompiled + "ms");
108109
}
109110
}

0 commit comments

Comments
 (0)