### Question:
RhinoSandboxClassShutter implements ClassShutter { @Override public boolean visibleToScripts(final String fullClassName) { LOGGER.debug("Checking access to class '{}'", fullClassName); if (securityManager != null) { try { securityManager.checkPackageAccess(fullClassName); } catch (SecurityException ex) { LOGGER.error("Access denied by SecurityManager for class '{}'", fullClassName); return false; } } boolean allowed = false; for (final Pattern pattern : whiteList) { if (pattern.matcher(fullClassName).matches()) { allowed = true; break; } } if (!allowed) { LOGGER.warn("Classname failed to match whitelist: '{}'", fullClassName); return false; } for (final Pattern pattern : blackList) { if (pattern.matcher(fullClassName).matches()) { LOGGER.error("Access to class '{}' denied by blacklist pattern: {}", fullClassName, pattern.pattern()); return false; } } LOGGER.debug("Access allowed for class '{}'", fullClassName); return true; } RhinoSandboxClassShutter(final SecurityManager securityManager, final List<Pattern> whiteList,
                                    final List<Pattern> blackList); @Override boolean visibleToScripts(final String fullClassName);  }### Answer:
@Test public void shouldRespectSecurityManager() { final String className = "java.lang.System"; doThrow(new SecurityException()).when(mockSecurityManager).checkPackageAccess(className); final boolean visible = sandboxClassShutter.visibleToScripts(className); assertThat(visible).isFalse(); }
@Test public void shouldAllowClassesThatMatchWhitelist() { final String className = "java.lang.System"; final boolean visible = sandboxClassShutter.visibleToScripts(className); assertThat(visible).isTrue(); }
@Test public void shouldNotAllowClassesThatDoNotMatchWhitelist() { final String className = "java.lang.Other"; final boolean visible = sandboxClassShutter.visibleToScripts(className); assertThat(visible).isFalse(); }
@Test public void shouldNotAllowClassesThatMatchBlackList() { final List<Pattern> whiteList = Collections.singletonList(Pattern.compile("java\\.lang\\..*")); final List<Pattern> blackList = Collections.singletonList(Pattern.compile("java\\.lang\\.System")); sandboxClassShutter = new RhinoSandboxClassShutter(mockSecurityManager, whiteList, blackList); final String className = "java.lang.System"; final boolean visible = sandboxClassShutter.visibleToScripts(className); assertThat(visible).isFalse(); }