Skip to content

Commit a2067cc

Browse files
authored
feat: optimize eval and error logs (#360)
1 parent 3d22ec5 commit a2067cc

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@
185185
<version>4.13.2</version>
186186
<scope>test</scope>
187187
</dependency>
188+
<dependency>
189+
<groupId>org.mockito</groupId>
190+
<artifactId>mockito-inline</artifactId>
191+
<version>4.11.0</version>
192+
<scope>test</scope>
193+
</dependency>
188194
<dependency>
189195
<groupId>com.googlecode.aviator</groupId>
190196
<artifactId>aviator</artifactId>

src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ public static boolean eval(String eval, Map<String, Object> env, AviatorEvaluato
410410
try {
411411
res = (boolean) aviatorEval.execute(eval, env);
412412
} catch (Exception e) {
413+
Util.logPrintfWarn("Execute 'eval' function error, nested exception is: {}", e.getMessage());
413414
res = false;
414415
}
415416
} else {

src/main/java/org/casbin/jcasbin/util/Util.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public static void logPrintfError(String format, Object... v) {
8787
}
8888
}
8989

90+
/**
91+
* logPrintf prints the log with the format as an error.
92+
*
93+
* @param message the message accompanying the exception
94+
* @param t the exception (throwable) to log
95+
*/
96+
public static void logPrintfError(String message, Throwable t) {
97+
if (enableLog) {
98+
LOGGER.error(message, t);
99+
}
100+
}
101+
90102
/**
91103
* logEnforce prints the log of Enforce.
92104
*
@@ -264,8 +276,7 @@ public static String[] splitCommaDelimited(String s) {
264276
records[i] = csvRecords.get(0).get(i).trim();
265277
}
266278
} catch (IOException e) {
267-
e.printStackTrace();
268-
Util.logPrintfError("CSV parser failed to parse this line:", s);
279+
Util.logPrintfError("CSV parser failed to parse this line: " + s, e);
269280
}
270281
}
271282
return records;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616

1717
import com.googlecode.aviator.AviatorEvaluator;
1818
import com.googlecode.aviator.AviatorEvaluatorInstance;
19+
import org.casbin.jcasbin.util.BuiltInFunctions;
20+
import org.casbin.jcasbin.util.Util;
1921
import org.junit.Test;
22+
import org.mockito.BDDMockito;
23+
import org.mockito.MockedStatic;
2024

2125
import java.util.HashMap;
2226
import java.util.Map;
2327

2428
import static org.casbin.jcasbin.main.TestUtil.*;
29+
import static org.mockito.ArgumentMatchers.*;
2530

2631
public class BuiltInFunctionsUnitTest {
2732

@@ -260,4 +265,20 @@ public void testGlobMatchFunc() {
260265
testGlobMatch("/prefix/subprefix/foobar", "*/foo*", false);
261266
testGlobMatch("/prefix/subprefix/foobar", "*/foo/*", false);
262267
}
268+
269+
@Test
270+
public void should_logged_when_eval_given_errorExpression() {
271+
// given
272+
AviatorEvaluatorInstance instance = AviatorEvaluator.getInstance();
273+
Map<String, Object> env = new HashMap<>();
274+
275+
try (MockedStatic<Util> utilMocked = BDDMockito.mockStatic(Util.class)) {
276+
utilMocked.when(() -> Util.logPrintfWarn(anyString(), anyString())).thenCallRealMethod();
277+
// when
278+
BuiltInFunctions.eval("error", env, instance);
279+
// then
280+
utilMocked.verify(() -> Util.logPrintfWarn(eq("Execute 'eval' function error, nested exception is: {}"), any()));
281+
}
282+
}
283+
263284
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@
1414

1515
package org.casbin.jcasbin.main;
1616

17+
import org.apache.commons.csv.CSVFormat;
18+
import org.apache.commons.csv.CSVParser;
1719
import org.casbin.jcasbin.util.SyncedLRUCache;
1820
import org.casbin.jcasbin.util.Util;
1921
import org.junit.Test;
22+
import org.mockito.ArgumentMatchers;
23+
import org.mockito.BDDMockito;
24+
import org.mockito.MockedConstruction;
25+
import org.mockito.MockedStatic;
26+
27+
import java.io.IOException;
28+
import java.io.StringReader;
2029

2130
import static org.junit.Assert.*;
31+
import static org.mockito.ArgumentMatchers.*;
2232

2333
public class UtilTest {
2434

@@ -92,4 +102,24 @@ public void testLruCache() {
92102
TestUtil.testCacheGet(cache, "three", 3, true);
93103
TestUtil.testCacheGet(cache, "four", 4, true);
94104
}
105+
106+
@Test
107+
public void should_logged_when_splitCommaDelimited_given_ioException() {
108+
IOException ioEx = new IOException("Mock IOException");
109+
try (
110+
MockedStatic<Util> utilMocked = BDDMockito.mockStatic(Util.class);
111+
MockedConstruction<CSVFormat> stringReaderMocked = BDDMockito.mockConstruction(CSVFormat.class, (mock, context) -> {
112+
BDDMockito.given(mock.parse(any(StringReader.class))).willThrow(ioEx);
113+
})) {
114+
// given
115+
utilMocked.when(() -> Util.splitCommaDelimited(anyString())).thenCallRealMethod();
116+
String csv = "\n";
117+
118+
// when
119+
Util.splitCommaDelimited(csv);
120+
121+
// then
122+
utilMocked.verify(() -> Util.logPrintfError(eq("CSV parser failed to parse this line: " + csv), eq(ioEx)));
123+
}
124+
}
95125
}

0 commit comments

Comments
 (0)