ScriptEngineUtils { public static boolean canFactoryProcessViews(ScriptEngineFactory scriptFactory, Dictionary<String, String> headers) { if (scriptFactory == null) { throw new IllegalArgumentException("ScriptEngineFactory is null"); } if (headers != null) { final String hasViews = headers.get(BundleScriptingConfigurationConstants.JAHIA_MODULE_HAS_VIEWS); if ("no".equalsIgnoreCase(StringUtils.trim(hasViews))) { return false; } else { final String commaSeparatedScriptNames = headers.get(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS); final String[] split = StringUtils.split(commaSeparatedScriptNames, ','); if (split != null) { final List<String> extensions = scriptFactory.getExtensions(); List<String> scriptNames = new ArrayList<>(split.length); for (String scriptName : split) { String script = scriptName.trim().toLowerCase(); scriptNames.add(script); if (extensions.contains(script)) { return true; } } return scriptNames.contains(scriptFactory.getEngineName().trim().toLowerCase()) || scriptNames.contains(scriptFactory.getLanguageName().trim().toLowerCase()); } } } return false; } private  ScriptEngineUtils(); static boolean canFactoryForExtensionProcessViews(String extension, Dictionary<String, String> headers); static boolean canFactoryProcessViews(ScriptEngineFactory scriptFactory, Dictionary<String, String> headers); static ScriptEngineUtils getInstance(); ScriptEngine getEngineByName(String name); ScriptEngine scriptEngine(String extension);  }
@Test public void canFactoryProcessViewsShouldThrowIllegalArgExceptionIfFactoryIsNull() { try { ScriptEngineUtils.canFactoryProcessViews(null, null); fail(); } catch (IllegalArgumentException e) { } }
@Test public void canFactoryProcessViewsShouldReturnFalseIfHeadersAreNullOrEmpty() { assertFalse(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), null)); assertFalse(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), new Hashtable<String, String>())); }
@Test public void canFactoryProcessViewsShouldBeRobustWithWeirdInput() { Dictionary<String, String> headers = new Hashtable<>(2); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS, ",,,"); assertFalse(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), headers)); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS, ", foo \t\n,,"); assertTrue(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), headers)); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS, ", js \t\n,, bar,"); assertTrue(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), headers)); }
@Test public void canFactoryProcessViewsShouldReturnFalseIfNoSupportedScriptLanguage() { Dictionary<String, String> headers = new Hashtable<>(2); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS, "js,groovy"); assertFalse(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), headers)); }
@Test public void canFactoryProcessViewsShouldReturnFalseIfBundleIndicatedNoViewsEvenIfItCouldProcessViews() { Dictionary<String, String> headers = new Hashtable<>(2); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS, "foo,js,groovy"); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_HAS_VIEWS, "no"); assertFalse(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), headers)); }
@Test public void canFactoryProcessViewsShouldWorkWithExtensions() { Dictionary<String, String> headers = new Hashtable<>(2); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS, "foo,js,groovy"); headers.put("color", "black"); assertTrue(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), headers)); }
@Test public void canFactoryProcessViewsShouldWorkWithLanguageName() { Dictionary<String, String> headers = new Hashtable<>(2); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS, "groovy,js,lang"); headers.put("color", "black"); assertTrue(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), headers)); }
@Test public void canFactoryProcessViewsShouldWorkWithEngineName() { Dictionary<String, String> headers = new Hashtable<>(2); headers.put(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS, "js,test,groovy"); headers.put("color", "black"); assertTrue(ScriptEngineUtils.canFactoryProcessViews(new TestScriptEngineFactory(), headers)); }