Skip to content

Commit 9360214

Browse files
committed
chore: Refactor unit tests to chain assetions and use parametrized tests
1 parent 1fb65c3 commit 9360214

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

src/main/java/com/endava/cats/dsl/impl/MiniDslParser.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.apache.commons.lang3.RandomStringUtils;
99
import org.apache.commons.lang3.StringUtils;
1010

11-
import javax.naming.OperationNotSupportedException;
11+
import java.lang.reflect.InvocationTargetException;
1212
import java.lang.reflect.Method;
1313
import java.lang.reflect.Modifier;
1414
import java.math.BigDecimal;
@@ -80,7 +80,7 @@ public String parse(String expression, Map<String, String> context) {
8080
}
8181
}
8282

83-
private Object eval(String expr, Map<String, String> ctx) throws Exception {
83+
private Object eval(String expr, Map<String, String> ctx) throws InvocationTargetException, IllegalAccessException {
8484
expr = expr == null ? "" : expr.trim();
8585

8686
if (ctx.containsKey(expr)) {
@@ -332,19 +332,19 @@ private int findTopLevelDotBeforeMethodCall(String expr) {
332332
return -1;
333333
}
334334

335-
private Object evalTypeChain(String expr, Map<String, String> ctx) throws Exception {
335+
private Object evalTypeChain(String expr, Map<String, String> ctx) throws InvocationTargetException, IllegalAccessException {
336336
int open = expr.indexOf('(');
337337
int close = findMatchingParen(expr, open);
338338
String fqcn = expr.substring(open + 1, close).trim();
339339

340340
Class<?> type = ALLOWED_TYPES.get(fqcn);
341341
if (type == null) {
342-
throw new OperationNotSupportedException("Type not allowed: " + fqcn);
342+
throw new SecurityException("Type not allowed: " + fqcn);
343343
}
344344

345345
String rest = expr.substring(close + 1).trim();
346346
if (!rest.startsWith(".")) {
347-
throw new IllegalArgumentException("Expected method chain after T(" + fqcn + ")");
347+
throw new SecurityException("Expected method chain after T(" + fqcn + ")");
348348
}
349349

350350
Object current = type;
@@ -374,7 +374,7 @@ private Object evalTypeChain(String expr, Map<String, String> ctx) throws Except
374374
return current;
375375
}
376376

377-
private Object[] parseArgs(String argsInside, Map<String, String> ctx) throws Exception {
377+
private Object[] parseArgs(String argsInside, Map<String, String> ctx) throws InvocationTargetException, IllegalAccessException {
378378
if (argsInside.isEmpty()) {
379379
return new Object[0];
380380
}
@@ -386,20 +386,20 @@ private Object[] parseArgs(String argsInside, Map<String, String> ctx) throws Ex
386386
return out;
387387
}
388388

389-
private Object invokeStaticAllowed(Class<?> cls, String method, Object[] args) throws Exception {
389+
private Object invokeStaticAllowed(Class<?> cls, String method, Object[] args) throws InvocationTargetException, IllegalAccessException {
390390
if (DENY_METHODS.contains(method)) {
391-
throw new OperationNotSupportedException("Method not allowed: " + method);
391+
throw new SecurityException("Method not allowed: " + method);
392392
}
393393
Method m = findBestMethod(cls, method, true, args);
394394
return m.invoke(null, coerceArgs(m.getParameterTypes(), args));
395395
}
396396

397-
private Object invokeAnyAllowed(Object target, String method, Object[] args) throws Exception {
397+
private Object invokeAnyAllowed(Object target, String method, Object[] args) throws InvocationTargetException, IllegalAccessException {
398398
if (target == null) {
399399
return null;
400400
}
401401
if (DENY_METHODS.contains(method)) {
402-
throw new OperationNotSupportedException("Method not allowed: " + method);
402+
throw new SecurityException("Method not allowed: " + method);
403403
}
404404

405405
if ("toString".equals(method) && args.length == 0) {
@@ -411,7 +411,7 @@ private Object invokeAnyAllowed(Object target, String method, Object[] args) thr
411411
return m.invoke(target, coerceArgs(m.getParameterTypes(), args));
412412
}
413413

414-
throw new OperationNotSupportedException("Instance calls not allowed on: " + target.getClass().getName());
414+
throw new SecurityException("Instance calls not allowed on: " + target.getClass().getName());
415415
}
416416

417417
private Method findBestMethod(Class<?> cls, String name, boolean wantStatic, Object[] args) {

src/test/java/com/endava/cats/dsl/impl/MiniDslParserTest.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -752,26 +752,11 @@ void shouldHandleBigIntegerValueOf() {
752752
assertThat(result).isEqualTo("123");
753753
}
754754

755-
@Test
756-
@DisplayName("Should handle LocalDateTime now")
757-
void shouldHandleLocalDateTimeNow() {
758-
String result = parser.parse("T(java.time.LocalDateTime).now()", context);
759-
760-
assertThat(result).isNotEmpty();
761-
}
762-
763-
@Test
764-
@DisplayName("Should handle ZonedDateTime now")
765-
void shouldHandleZonedDateTimeNow() {
766-
String result = parser.parse("T(java.time.ZonedDateTime).now()", context);
767-
768-
assertThat(result).isNotEmpty();
769-
}
770-
771-
@Test
772-
@DisplayName("Should handle Instant now")
773-
void shouldHandleInstantNow() {
774-
String result = parser.parse("T(java.time.Instant).now()", context);
755+
@ParameterizedTest
756+
@CsvSource("T(java.time.LocalDateTime).now(),T(java.time.ZonedDateTime).now(),T(java.time.Instant).now()")
757+
@DisplayName("Should handle dates now")
758+
void shouldHandleLocalDateTimeNow(String expression) {
759+
String result = parser.parse(expression, context);
775760

776761
assertThat(result).isNotEmpty();
777762
}

0 commit comments

Comments
 (0)