Skip to content

Commit 6504a9e

Browse files
committed
feat: replace JUnit with TestNG
1 parent aa90a03 commit 6504a9e

33 files changed

+128
-122
lines changed

pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@
147147
<target>1.8</target>
148148
</configuration>
149149
</plugin>
150+
<plugin>
151+
<groupId>org.apache.maven.plugins</groupId>
152+
<artifactId>maven-surefire-plugin</artifactId>
153+
<version>3.2.5</version>
154+
</plugin>
150155
<plugin>
151156
<groupId>org.eluder.coveralls</groupId>
152157
<artifactId>coveralls-maven-plugin</artifactId>
@@ -180,9 +185,9 @@
180185
<scope>test</scope>
181186
</dependency>
182187
<dependency>
183-
<groupId>junit</groupId>
184-
<artifactId>junit</artifactId>
185-
<version>4.13.2</version>
188+
<groupId>org.testng</groupId>
189+
<artifactId>testng</artifactId>
190+
<version>7.4.0</version>
186191
<scope>test</scope>
187192
</dependency>
188193
<dependency>

src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package org.casbin.jcasbin.main;
1616

1717
import org.casbin.jcasbin.util.Util;
18-
import org.junit.Test;
18+
import org.testng.annotations.Test;
1919
import java.util.Map;
2020
import java.util.HashMap;
2121

src/test/java/org/casbin/jcasbin/main/BuiltInFunctionsUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.googlecode.aviator.AviatorEvaluatorInstance;
1919
import org.casbin.jcasbin.util.BuiltInFunctions;
2020
import org.casbin.jcasbin.util.Util;
21-
import org.junit.Test;
21+
import org.testng.annotations.Test;
2222
import org.mockito.BDDMockito;
2323
import org.mockito.MockedStatic;
2424

src/test/java/org/casbin/jcasbin/main/CachedEnforcerUnitTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import org.casbin.jcasbin.persist.cache.Cache;
1818
import org.casbin.jcasbin.persist.cache.CacheableParam;
1919
import org.casbin.jcasbin.persist.cache.DefaultCache;
20-
import org.junit.Test;
20+
import org.testng.annotations.Test;
2121

2222
import java.time.Duration;
2323

24-
import static org.junit.Assert.*;
24+
import static org.testng.Assert.*;
2525

2626
public class CachedEnforcerUnitTest {
2727
private CachedEnforcer cachedEnforcer;
@@ -30,7 +30,7 @@ public class CachedEnforcerUnitTest {
3030

3131
private void testEnforceCache(String sub, String obj, String act, boolean expectedRes) throws Exception {
3232
Boolean actualRes = cachedEnforcer.enforce(sub, obj, act);
33-
assertEquals(String.format("%s, %s, %s: %s, supposed to be %s", sub, obj, act, actualRes, expectedRes), expectedRes, actualRes);
33+
assertEquals(actualRes.booleanValue(), expectedRes, String.format("%s, %s, %s: %s, supposed to be %s", sub, obj, act, actualRes, expectedRes));
3434
}
3535

3636
@Test
@@ -88,16 +88,16 @@ public void testInvalidateCache() throws Exception {
8888
cachedEnforcer = new CachedEnforcer("examples/basic_model.conf", "examples/basic_policy.csv", false);
8989

9090
Boolean cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
91-
assertNull(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null), cacheKey);
91+
assertNull(cacheKey, String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null));
9292

9393
Boolean actualRes = cachedEnforcer.enforce("alice", "data1", "read");
9494
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
95-
assertTrue(String.format("alice, data1, read: %s, supposed to be %s", actualRes, true), actualRes);
96-
assertTrue(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, true), cacheKey);
95+
assertTrue(actualRes, String.format("alice, data1, read: %s, supposed to be %s", actualRes, true));
96+
assertTrue(cacheKey, String.format("alice, data1, read: %s, supposed to be %s", cacheKey, true));
9797

9898
cachedEnforcer.invalidateCache();
9999
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
100-
assertNull(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null), cacheKey);
100+
assertNull(cacheKey, String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null));
101101

102102
}
103103

@@ -133,28 +133,28 @@ public void testCacheExpiration() throws Exception {
133133
cache.set(getKey("alice", "data1", "read"),true,Duration.ofMillis(10));
134134
cachedEnforcer.setCache(cache);
135135
Boolean cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
136-
assertTrue(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, true), cacheKey);
136+
assertTrue(cacheKey, String.format("alice, data1, read: %s, supposed to be %s", cacheKey, true));
137137

138138
// Wait for the cache to expire
139139
Thread.sleep(15);
140140

141141
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("alice", "data1", "read"));
142-
assertNull(String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null), cacheKey);
142+
assertNull(cacheKey, String.format("alice, data1, read: %s, supposed to be %s", cacheKey, null));
143143

144144
// Replace cache during test run
145145
cache.clear();
146146
cache.set(getKey("bob", "data1", "read"),true,Duration.ofMillis(1000));
147147
cachedEnforcer.setCache(cache);
148148
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("bob", "data1", "read"));
149-
assertTrue(String.format("bob, data1, read: %s, supposed to be %s", cacheKey, true), cacheKey);
149+
assertTrue(cacheKey, String.format("bob, data1, read: %s, supposed to be %s", cacheKey, true));
150150

151151
cache.clear();
152152
cache.set(getKey("jack", "data1", "write"),true,Duration.ofMillis(1000));
153153
cachedEnforcer.setCache(cache);
154154
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("bob", "data1", "read"));
155-
assertNull(String.format("bob, data1, read: %s, supposed to be %s", cacheKey, null), cacheKey);
155+
assertNull(cacheKey, String.format("bob, data1, read: %s, supposed to be %s", cacheKey, null));
156156

157157
cacheKey = cachedEnforcer.getCache().get(cachedEnforcer.getCacheKey("jack", "data1", "write"));
158-
assertTrue(String.format("jack, data1, write: %s, supposed to be %s", cacheKey, true), cacheKey);
158+
assertTrue(cacheKey, String.format("jack, data1, write: %s, supposed to be %s", cacheKey, true));
159159
}
160160
}

src/test/java/org/casbin/jcasbin/main/ConfigTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import org.casbin.jcasbin.config.Config;
1818

19-
import org.junit.Test;
20-
import static org.junit.Assert.*;
19+
import org.testng.annotations.Test;
20+
import static org.testng.Assert.*;
2121

2222
public class ConfigTest {
2323

@@ -31,7 +31,7 @@ public void testGet() {
3131

3232
// redis::key test
3333
String[] redisKeys = config.getStrings("redis::redis.key");
34-
assertArrayEquals(new String[]{"push1", "push2"}, redisKeys);
34+
assertEquals(new String[]{"push1", "push2"}, redisKeys);
3535
assertEquals("127.0.0.1", config.getString("mysql::mysql.dev.host"));
3636
assertEquals("10.0.0.1", config.getString("mysql::mysql.master.host"));
3737
assertEquals("root", config.getString("mysql::mysql.master.user"));

src/test/java/org/casbin/jcasbin/main/DefaultDetectorTest.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import org.casbin.jcasbin.detector.Detector;
1919
import org.casbin.jcasbin.rbac.DefaultRoleManager;
2020
import org.casbin.jcasbin.rbac.RoleManager;
21-
import org.junit.Test;
21+
import org.testng.annotations.Test;
2222

23-
import static org.junit.Assert.*;
23+
import static org.testng.Assert.*;
2424

2525
/**
2626
* Unit tests for DefaultDetector
@@ -40,7 +40,7 @@ public void testNoCycle() {
4040
Detector detector = new DefaultDetector();
4141
String result = detector.check(rm);
4242

43-
assertNull("Expected no cycle to be detected", result);
43+
assertNull(result, "Expected no cycle to be detected");
4444
}
4545

4646
@Test
@@ -54,11 +54,11 @@ public void testSimpleCycle() {
5454
Detector detector = new DefaultDetector();
5555
String result = detector.check(rm);
5656

57-
assertNotNull("Expected a cycle to be detected", result);
58-
assertTrue("Result should contain 'Cycle detected'", result.contains("Cycle detected:"));
59-
assertTrue("Result should contain role A", result.contains("A"));
60-
assertTrue("Result should contain role B", result.contains("B"));
61-
assertTrue("Result should contain role C", result.contains("C"));
57+
assertNotNull(result, "Expected a cycle to be detected");
58+
assertTrue(result.contains("Cycle detected:"), "Result should contain 'Cycle detected'");
59+
assertTrue(result.contains("A"), "Result should contain role A");
60+
assertTrue(result.contains("B"), "Result should contain role B");
61+
assertTrue(result.contains("C"), "Result should contain role C");
6262
}
6363

6464
@Test
@@ -70,9 +70,9 @@ public void testSelfLoop() {
7070
Detector detector = new DefaultDetector();
7171
String result = detector.check(rm);
7272

73-
assertNotNull("Expected a cycle to be detected", result);
74-
assertTrue("Result should contain 'Cycle detected'", result.contains("Cycle detected:"));
75-
assertTrue("Result should contain role A", result.contains("A"));
73+
assertNotNull(result, "Expected a cycle to be detected");
74+
assertTrue(result.contains("Cycle detected:"), "Result should contain 'Cycle detected'");
75+
assertTrue(result.contains("A"), "Result should contain role A");
7676
}
7777

7878
@Test
@@ -85,8 +85,8 @@ public void testTwoNodeCycle() {
8585
Detector detector = new DefaultDetector();
8686
String result = detector.check(rm);
8787

88-
assertNotNull("Expected a cycle to be detected", result);
89-
assertTrue("Result should contain 'Cycle detected'", result.contains("Cycle detected:"));
88+
assertNotNull(result, "Expected a cycle to be detected");
89+
assertTrue(result.contains("Cycle detected:"), "Result should contain 'Cycle detected'");
9090
}
9191

9292
@Test
@@ -103,7 +103,7 @@ public void testMultipleDisconnectedComponents() {
103103
Detector detector = new DefaultDetector();
104104
String result = detector.check(rm);
105105

106-
assertNull("Expected no cycle to be detected", result);
106+
assertNull(result, "Expected no cycle to be detected");
107107
}
108108

109109
@Test
@@ -121,8 +121,8 @@ public void testCycleInOneComponent() {
121121
Detector detector = new DefaultDetector();
122122
String result = detector.check(rm);
123123

124-
assertNotNull("Expected a cycle to be detected", result);
125-
assertTrue("Result should contain 'Cycle detected'", result.contains("Cycle detected:"));
124+
assertNotNull(result, "Expected a cycle to be detected");
125+
assertTrue(result.contains("Cycle detected:"), "Result should contain 'Cycle detected'");
126126
}
127127

128128
@Test
@@ -146,8 +146,8 @@ public void testComplexGraph() {
146146
Detector detector = new DefaultDetector();
147147
String result = detector.check(rm);
148148

149-
assertNotNull("Expected a cycle to be detected", result);
150-
assertTrue("Result should contain 'Cycle detected'", result.contains("Cycle detected:"));
149+
assertNotNull(result, "Expected a cycle to be detected");
150+
assertTrue(result.contains("Cycle detected:"), "Result should contain 'Cycle detected'");
151151
}
152152

153153
@Test
@@ -158,7 +158,7 @@ public void testEmptyRoleManager() {
158158
Detector detector = new DefaultDetector();
159159
String result = detector.check(rm);
160160

161-
assertNull("Expected no cycle in empty graph", result);
161+
assertNull(result, "Expected no cycle in empty graph");
162162
}
163163

164164
@Test
@@ -171,7 +171,7 @@ public void testSingleNode() {
171171
Detector detector = new DefaultDetector();
172172
String result = detector.check(rm);
173173

174-
assertNull("Expected no cycle with single isolated node", result);
174+
assertNull(result, "Expected no cycle with single isolated node");
175175
}
176176

177177
@Test
@@ -192,8 +192,8 @@ public void testLargeGraph() {
192192
long endTime = System.currentTimeMillis();
193193
long duration = endTime - startTime;
194194

195-
assertNull("Expected no cycle in large chain", result);
196-
assertTrue("Detection should complete in reasonable time (< 5 seconds)", duration < 5000);
195+
assertNull(result, "Expected no cycle in large chain");
196+
assertTrue(duration < 5000, "Detection should complete in reasonable time (< 5 seconds)");
197197
}
198198

199199
@Test
@@ -210,8 +210,8 @@ public void testLargeGraphWithCycle() {
210210
Detector detector = new DefaultDetector();
211211
String result = detector.check(rm);
212212

213-
assertNotNull("Expected a cycle to be detected in large graph", result);
214-
assertTrue("Result should contain 'Cycle detected'", result.contains("Cycle detected:"));
213+
assertNotNull(result, "Expected a cycle to be detected in large graph");
214+
assertTrue(result.contains("Cycle detected:"), "Result should contain 'Cycle detected'");
215215
}
216216

217217
@Test
@@ -249,7 +249,7 @@ public void printRoles() {}
249249

250250
Detector detector = new DefaultDetector();
251251
String result = detector.check(rm);
252-
assertNull("Expected no cycle with default getRoleGraph() implementation", result);
252+
assertNull(result, "Expected no cycle with default getRoleGraph() implementation");
253253
}
254254

255255
@Test
@@ -262,11 +262,11 @@ public void testCycleAfterClear() {
262262

263263
Detector detector = new DefaultDetector();
264264
String result = detector.check(rm);
265-
assertNotNull("Expected a cycle before clear", result);
265+
assertNotNull(result, "Expected a cycle before clear");
266266

267267
rm.clear();
268268
result = detector.check(rm);
269-
assertNull("Expected no cycle after clear", result);
269+
assertNull(result, "Expected no cycle after clear");
270270
}
271271

272272
@Test
@@ -279,10 +279,10 @@ public void testCycleDetectionAfterDelete() {
279279

280280
Detector detector = new DefaultDetector();
281281
String result = detector.check(rm);
282-
assertNotNull("Expected a cycle before delete", result);
282+
assertNotNull(result, "Expected a cycle before delete");
283283

284284
rm.deleteLink("C", "A");
285285
result = detector.check(rm);
286-
assertNull("Expected no cycle after breaking the cycle", result);
286+
assertNull(result, "Expected no cycle after breaking the cycle");
287287
}
288288
}

src/test/java/org/casbin/jcasbin/main/EnforcerUnitTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.casbin.jcasbin.util.BuiltInFunctions;
2121
import org.casbin.jcasbin.util.EnforceContext;
2222
import org.casbin.jcasbin.util.Util;
23-
import org.junit.Assert;
24-
import org.junit.Test;
23+
import org.testng.Assert;
24+
import org.testng.annotations.Test;
2525

2626
import java.io.File;
2727
import java.io.FileInputStream;
@@ -34,8 +34,9 @@
3434
import static java.util.Arrays.asList;
3535
import static org.casbin.jcasbin.main.CoreEnforcer.newModel;
3636
import static org.casbin.jcasbin.main.TestUtil.*;
37-
import static org.junit.Assert.assertFalse;
38-
import static org.junit.Assert.assertTrue;
37+
import static org.testng.Assert.assertEquals;
38+
import static org.testng.Assert.assertFalse;
39+
import static org.testng.Assert.assertTrue;
3940

4041
public class EnforcerUnitTest {
4142
@Test
@@ -668,7 +669,7 @@ public void testBatchEnforce() {
668669
List<Boolean> results = asList(true, true, false);
669670
List<Boolean> myResults = e.batchEnforce(asList(asList("alice", "data1", "read"),
670671
asList("bob", "data2", "write"), asList("jack", "data3", "read")));
671-
Assert.assertArrayEquals(myResults.toArray(new Boolean[0]), results.toArray(new Boolean[0]));
672+
assertEquals(myResults, results);
672673
}
673674

674675
@Test
@@ -684,7 +685,7 @@ public void testDomainBatchEnforce() {
684685
)
685686
);
686687

687-
Assert.assertArrayEquals(myResults.toArray(new Boolean[0]), results.toArray(new Boolean[0]));
688+
assertEquals(myResults, results);
688689
}
689690

690691
@Test
@@ -698,7 +699,7 @@ public void testBatchEnforceWithMatcher() {
698699
List<Boolean> myResults = e.batchEnforceWithMatcher(matcher, asList(asList("alice", "data1", "read"),
699700
asList("bob", "data2", "write"), asList("root", "data2", "read"),
700701
asList("root", "data3", "read"), asList("jack", "data3", "read")));
701-
Assert.assertArrayEquals(myResults.toArray(new Boolean[0]), results.toArray(new Boolean[0]));
702+
assertEquals(myResults, results);
702703
}
703704

704705
@Test

src/test/java/org/casbin/jcasbin/main/FilteredAdapterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.casbin.jcasbin.persist.Adapter;
1919
import org.casbin.jcasbin.persist.file_adapter.FilteredAdapter;
2020
import org.casbin.jcasbin.util.Util;
21-
import org.junit.Test;
21+
import org.testng.annotations.Test;
2222

2323
import static java.util.Arrays.asList;
2424
import static org.casbin.jcasbin.main.TestUtil.testHasPolicy;

src/test/java/org/casbin/jcasbin/main/FrontendUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package org.casbin.jcasbin.main;
1616

1717
import com.google.gson.Gson;
18-
import org.junit.Test;
18+
import org.testng.annotations.Test;
1919

2020
import java.io.IOException;
2121
import java.nio.file.Files;
@@ -24,7 +24,7 @@
2424
import java.util.List;
2525
import java.util.regex.Pattern;
2626

27-
import static org.junit.Assert.assertEquals;
27+
import static org.testng.Assert.assertEquals;
2828

2929
public class FrontendUnitTest {
3030

src/test/java/org/casbin/jcasbin/main/FunctionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.googlecode.aviator.runtime.type.AviatorBoolean;
1919
import com.googlecode.aviator.runtime.type.AviatorObject;
2020
import org.casbin.jcasbin.util.function.CustomFunction;
21-
import org.junit.Test;
21+
import org.testng.annotations.Test;
2222

2323
import java.util.Map;
2424
import java.util.regex.Pattern;

0 commit comments

Comments
 (0)