Skip to content

Commit

Permalink
Check methods of SecurityManager are overridden
Browse files Browse the repository at this point in the history
NoExitSecurityManager is supposed to override all public methods of
SecurityManager. Currently for each new Java version I have to check
manually that this condition is satisfied. The new tests allows me to
check it automatically by running the tests with the respective JDK.
  • Loading branch information
stefanbirkner committed Jun 15, 2018
1 parent fa1397a commit 0d44f84
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import static org.mockito.Mockito.when;

import java.io.FileDescriptor;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

import com.github.stefanbirkner.fishbowl.Statement;
Expand All @@ -19,6 +21,7 @@
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Enclosed.class)
Expand Down Expand Up @@ -98,6 +101,31 @@ public void evaluate() {
}
}

@RunWith(Parameterized.class)
public static class public_methods_override {

@Parameters(name = "{0}")
public static List<Object[]> data() {
List<Object[]> methods = new ArrayList<Object[]>();
for (Method method : NoExitSecurityManager.class.getMethods())
if (notDeclaredByObjectClass(method))
methods.add(new Object[] { testName(method), method });
return methods;
}

@Parameter(0)
public String methodName;

@Parameter(1)
public Method method;

@Test
public void is_implemented_by_NoExitSecurityManager() {
assertThat(method.getDeclaringClass())
.isEqualTo(NoExitSecurityManager.class);
}
}

public static class with_original_SecurityManager {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
Expand Down Expand Up @@ -516,4 +544,23 @@ public void getThreadGroup_may_be_called() {
managerWithoutOriginal.getThreadGroup();
}
}

private static boolean notDeclaredByObjectClass(Method method) {
return !method.getDeclaringClass().equals(Object.class);
}

private static String testName(Method method) {
return method.getName()
+ "(" + join(method.getParameterTypes()) + ")";
}

private static String join(Class<?>[] types) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < types.length; i++) {
if (i != 0)
sb.append(",");
sb.append(types[i].getSimpleName());
}
return sb.toString();
}
}

0 comments on commit 0d44f84

Please sign in to comment.