### Question:
SolarisScriptOnConnector { @SuppressWarnings("fallthrough") public Object runScriptOnResource(ScriptContext request, OperationOptions options) { final String scriptLanguage = request.getScriptLanguage(); final String scriptText = request.getScriptText(); if (StringUtil.isBlank(scriptLanguage)) { throw new IllegalArgumentException("Script language is missing."); } if (StringUtil.isBlank(scriptText)) { throw new IllegalArgumentException("scriptText is missing"); } Shell shell = null; for (Shell s : Shell.values()) { if (s.name().equalsIgnoreCase(scriptLanguage)) { shell = s; break; } } if (null == shell) { throw new IllegalArgumentException("ScriptLanguage is '" + scriptLanguage + "'. The only accepted script languages are: sh, csh, ksh, bash, tcsh, zsh."); } StringBuilder sb = shell.getBuilder(); for (Map.Entry<String, Object> entry : request.getScriptArguments().entrySet()) { if (VARIABLE_NAME_PATTERN.matcher(entry.getKey()).matches()) { String value = quoteForDCLWhenNeeded(null != entry.getKey() ? entry.getValue().toString() : ""); switch (shell) { case bash: case zsh: case ksh: case sh: { sb.append(entry.getKey()).append("=").append(value).append('\n'); sb.append("export ").append(entry.getKey()).append('\n'); break; } case tcsh: case csh: { sb.append("setenv ").append(entry.getKey()).append(" ").append(value).append( '\n'); break; } } } else { throw new IllegalArgumentException("Invalid argument name: " + entry.getKey()); } } sb.append(request.getScriptText()); String[] lines = LINE_BREAK.split(sb.toString()); for (String line : lines) { connection.executeCommand(line, CollectionUtil.newSet(Perl5Compiler .quotemeta(connection.getRootShellPrompt())), CollectionUtil.newSet(">")); } return connection.executeCommand("ENDSSH"); } SolarisScriptOnConnector(SolarisConnector solarisConnector); @SuppressWarnings("fallthrough") Object runScriptOnResource(ScriptContext request, OperationOptions options); static String quoteForDCLWhenNeeded(String unquoted); static String quoteAndEscape(String source, char quoteChar, final char[] escapedChars,
            final char[] quotingTriggers, String escapePattern, boolean force); static String escape(String source, final char[] escapedChars, String escapePattern);  }### Answer:
@Test(dataProvider = "shell") public void testRunScriptOnResource(String scriptLanguage) throws Exception { ScriptContextBuilder builder = new ScriptContextBuilder(scriptLanguage, "echo $ARG_1"); builder.addScriptArgument("ARG_1", "Acme"); Object o = getFacade().runScriptOnResource(builder.build(), null); assertEquals(o, "Acme"); builder = new ScriptContextBuilder("sh", "echo $ARG_1"); o = getFacade().runScriptOnResource(builder.build(), null); assertNotEquals(o, "Acme"); }
@Test(expectedExceptions = ConnectorException.class, expectedExceptionsMessageRegExp = "ERROR, buffer content: <Acme>") public void testRunWrongScriptOnResource() throws Exception { ScriptContextBuilder builder = new ScriptContextBuilder("sh", "echo $ARG_1\nENDSSH"); builder.addScriptArgument("ARG_1", "Acme"); getFacade().runScriptOnResource(builder.build(), null); fail("Script must fail"); }
@Test public void testMultilineRunScriptOnResource() throws Exception { ScriptContextBuilder builder = new ScriptContextBuilder("sh", "echo $ARG_1\necho $ARG_2\r\necho $ARG_3\r"); builder.addScriptArgument("ARG_1", "Unix"); builder.addScriptArgument("ARG_2", "Windows"); builder.addScriptArgument("ARG_3", "OSX"); Object o = getFacade().runScriptOnResource(builder.build(), null); assertEquals(o, "Unix\r\nWindows\r\nOSX"); }
@Test public void testComaRunScriptOnResource() throws Exception { ScriptContextBuilder builder = new ScriptContextBuilder("sh", "echo 'Hello, World'"); Object o = getFacade().runScriptOnResource(builder.build(), null); assertEquals(o, "Hello, World"); }