Skip to content

Commit 963bca1

Browse files
authored
Mocks for AccessController, AutoCloseable and Throwable (#1618)
1 parent b6c89b8 commit 963bca1

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.utbot.engine.overrides;
2+
3+
import org.utbot.api.annotation.UtClassMock;
4+
5+
import java.security.AccessControlContext;
6+
import java.security.Permission;
7+
import java.security.PrivilegedAction;
8+
import java.security.PrivilegedActionException;
9+
import java.security.PrivilegedExceptionAction;
10+
11+
@UtClassMock(target = java.security.AccessController.class, internalUsage = true)
12+
public class AccessController {
13+
public static <T> T doPrivileged(PrivilegedAction<T> action) {
14+
return action.run();
15+
}
16+
17+
public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) {
18+
return action.run();
19+
}
20+
21+
public static <T> T doPrivileged(PrivilegedAction<T> action,
22+
AccessControlContext ignoredContext) {
23+
return action.run();
24+
}
25+
26+
public static <T> T doPrivileged(PrivilegedAction<T> action,
27+
AccessControlContext ignoredContext, Permission... perms) {
28+
if (perms == null) {
29+
throw new NullPointerException();
30+
}
31+
32+
for (Permission permission : perms) {
33+
if (permission == null) {
34+
throw new NullPointerException();
35+
}
36+
}
37+
38+
return action.run();
39+
}
40+
41+
public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action,
42+
AccessControlContext context, Permission... perms) {
43+
return doPrivileged(action, context, perms);
44+
}
45+
46+
public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
47+
try {
48+
return action.run();
49+
} catch (RuntimeException e) {
50+
// If the action's run method throws an unchecked exception, it will propagate through this method.
51+
throw e;
52+
} catch (Exception e) {
53+
// Exception is wrapped with the PrivilegedActionException ONLY if this exception is CHECKED
54+
throw new PrivilegedActionException(e);
55+
}
56+
}
57+
58+
public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
59+
return doPrivileged(action);
60+
}
61+
62+
public static <T> T doPrivileged(
63+
PrivilegedExceptionAction<T> action,
64+
AccessControlContext ignoredContext
65+
) throws PrivilegedActionException {
66+
return doPrivileged(action);
67+
}
68+
69+
public static <T> T doPrivileged(
70+
PrivilegedExceptionAction<T> action,
71+
AccessControlContext ignoredContext,
72+
Permission... perms
73+
) throws PrivilegedActionException {
74+
if (perms == null) {
75+
throw new NullPointerException();
76+
}
77+
78+
for (Permission permission : perms) {
79+
if (permission == null) {
80+
throw new NullPointerException();
81+
}
82+
}
83+
84+
return doPrivileged(action);
85+
}
86+
87+
public static <T> T doPrivilegedWithCombiner(
88+
PrivilegedExceptionAction<T> action,
89+
AccessControlContext context,
90+
Permission... perms
91+
) throws PrivilegedActionException {
92+
return doPrivileged(action, context, perms);
93+
}
94+
95+
public static void checkPermission(Permission perm) {
96+
if (perm == null) {
97+
throw new NullPointerException();
98+
}
99+
100+
// We cannot check real permission do determines whether we should throw an AccessControlException,
101+
// so do nothing more.
102+
}
103+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.utbot.engine.overrides;
2+
3+
import org.utbot.api.annotation.UtClassMock;
4+
5+
@UtClassMock(target = java.lang.AutoCloseable.class, internalUsage = true)
6+
public interface AutoCloseable {
7+
default void close() throws Exception {
8+
// Do nothing
9+
}
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.utbot.engine.overrides;
2+
3+
import org.utbot.api.annotation.UtClassMock;
4+
import org.utbot.api.mock.UtMock;
5+
6+
@UtClassMock(target = java.lang.Throwable.class, internalUsage = true)
7+
public class Throwable {
8+
public void printStackTrace() {
9+
// Do nothing
10+
}
11+
12+
public synchronized java.lang.Throwable fillInStackTrace() {
13+
return UtMock.makeSymbolic();
14+
}
15+
16+
public StackTraceElement[] getStackTrace() {
17+
return UtMock.makeSymbolic();
18+
}
19+
20+
public void setStackTrace(StackTraceElement[] stackTrace) {
21+
// Do nothing
22+
}
23+
24+
public final synchronized void addSuppressed(java.lang.Throwable exception) {
25+
// Do nothing
26+
}
27+
28+
public final synchronized java.lang.Throwable[] getSuppressed() {
29+
return UtMock.makeSymbolic();
30+
}
31+
}

utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ private val classesToLoad = arrayOf(
151151
org.utbot.engine.overrides.Long::class,
152152
org.utbot.engine.overrides.Short::class,
153153
org.utbot.engine.overrides.System::class,
154+
org.utbot.engine.overrides.Throwable::class,
155+
org.utbot.engine.overrides.AutoCloseable::class,
156+
org.utbot.engine.overrides.AccessController::class,
154157
org.utbot.engine.overrides.collections.UtOptional::class,
155158
org.utbot.engine.overrides.collections.UtOptionalInt::class,
156159
org.utbot.engine.overrides.collections.UtOptionalLong::class,

0 commit comments

Comments
 (0)