package org.jboss.jopr.jsfunit.as5.scripts;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.lang.StringUtils;
import org.jboss.jopr.jsfunit.ApplicationTestBaseAS5;
import org.jboss.jopr.jsfunit.DebugUtils;
import org.jboss.jopr.jsfunit.exceptions.EmbJoprTestException;
import org.jboss.jopr.jsfunit.exceptions.HtmlElementNotFoundException;
import org.jboss.jopr.jsfunit.util.ActiveConditionChecker;
import org.jboss.jopr.jsfunit.util.DescribedCondition;
import org.jboss.jopr.jsfunit.util.EmbJoprTestToolkit.ContentTable;
import org.jboss.jopr.jsfunit.util.EmbJoprTestToolkit.ContentTableRow;
/**
*
* @author ondra
*/
public class ScriptsTest extends ApplicationTestBaseAS5 {
//public static final DeployableTypes APP_TYPE = DeployableTypes.EMB_WAR;
/**
* @return the suite of tests being tested
*/
public static Test suite(){
return new TestSuite(ScriptsTest.class);
}
final String EXECUTE_OP_NAME = "Execute Script";
/**
* Tests whether the scripts node exists and that all the scripts are listed.
*/
public void testScriptsListingTest() throws IOException, EmbJoprTestException {
ejtt.navTree.getNodeByLabel(NAV_SCRIPTS).click();
ContentTable scriptsListingTable = ejtt.tabMenu.getTabContentBox().getFirstTable();
// Go through the dir, and check whether each *.sh file is listed in the page.
File binDir = new File(ejtt.getJBossHomeDir());
if( !binDir.isDirectory() )
throw new EmbJoprTestException(binDir.getAbsolutePath()+" is not a directory.");
// Get the list of .sh files in the <JBOSS_HOME>/bin dir.
log.info("Scanning directory "+binDir.getAbsolutePath());
//File scriptFiles[] = binDir.listFiles(new FilenameSuffixFilter("sh"));
File scriptFiles[] = binDir.listFiles((FilenameFilter)new SuffixFileFilter(".sh"));
/*
List<String> notListedScripts = new ArrayList();
for( File file : scriptFiles ) {
// Go to the scripts node.
ejtt.navTree.getNodeByLabel(NAV_SCRIPTS).click();
// Check whether the script is listed (on any page).
ContentTableRow row = ejtt.getTabMenu().getTabContentBox().findLinkRowInDataTableUsingPagination(file.getName());
if( null == row )
notListedScripts.add("");
}
/*/
// Faster method.
List<String> notListedScripts = new ArrayList();
for( File file : scriptFiles ) {
notListedScripts.add(file.getName());
log.info("Script in bin/ : "+file.getName());
}
// Go to the scripts node.
ejtt.navTree.getNodeByLabel(NAV_SCRIPTS).click();
int curPage = 1;
do {
log.info("Scanning scripts listing, page "+curPage);
// Remove all scripts listed on this page from the not-listed list.
ContentTable scriptListingTable = ejtt.getTabMenu().getTabContentBox().getDefaultTable();
for( ContentTableRow row : scriptListingTable.getRows() ) {
String scriptName = row.getCell(0).getTextContent().trim();
notListedScripts.remove(scriptName);
log.info("Listed script: '"+scriptName+"'");
}
// Go to the next page. If there's no next page, quit the loop.
Page originalPage = ejtt.getClient().getContentPage();
boolean wentNext = ejtt.getTabMenu().getTabContentBox().getPagination().goNext();
if( !wentNext )
break;
if( originalPage.equals( ejtt.getClient().getContentPage() ) )
break;
} while( true );
/**/
// If some of the scripts from the <JBOSS_HOME>/bin directory are not listed, throw.
if( 0 < notListedScripts.size() ){
throw new EmbJoprTestException("Following <JBOSS_HOME>/bin/*.sh files are not listed: "+notListedScripts.toArray( new String[0] ) );
}
}
/**
* Adds a test script to the /bin directory and checks that it appears in the Scripts listing.
*/
public void testScriptsAddNewShFileTest() throws IOException, EmbJoprTestException {
final String SCRIPT_FILE_NAME = "embjopr-test.sh";
// Create the script in JBOSS_HOME/bin/
String shFilePath = ejtt.getJBossHomeDir()+"/"+SCRIPT_FILE_NAME;
final File scriptFile = new File(shFilePath);
try {
if( scriptFile.exists() ){
log.warn(shFilePath+" already exists, thus is already visible in EmbJopr.");
}
new FileWriter(shFilePath, false).append("echo 'Test script.'").close();
// Wait for the script to appear.
try {
new ActiveConditionChecker(new DescribedCondition("Script "+SCRIPT_FILE_NAME+" appears in Scripts listing") {
int count = 0;
public boolean isTrue() throws Exception {
ejtt.navTree.getNodeByLabel(NAV_SCRIPTS).click();
return null == ejtt.tabMenu.getTabContentBox().getFirstTable().findFirstRowContainingLink(SCRIPT_FILE_NAME);
// TODO: This will check all pages.
//return null == ejtt.tabMenu.getTabContentBox().findLinkRowInDataTableUsingPagination(SCRIPT_FILE_NAME);
}
}).throwOnTimeout().dumpPageOnTimeout(this).waitWithTimeout(3000, 20);
}
catch( EmbJoprTestException ex ){
throw ex;
}
catch( Exception ex ){
throw new EmbJoprTestException( ex.getMessage(), ex );
}
}
finally {
// Delete the test script file if it exists.
if( scriptFile.exists() )
scriptFile.delete();
}
}
/**
* Runs the classpath.sh script, without parameters.
*/
public void testScriptsRunClasspathshTest() throws IOException, EmbJoprTestException {
final String SCRIPT_FILE_NAME = "classpath.sh";
try {
// Go to the Scripts node.
ejtt.navTree.getNodeByLabel(NAV_SCRIPTS).click();
// Go to the ClassPath script.
ejtt.tabMenu.getTabContentBox().getFirstTable().getFirstRowContainingLink(SCRIPT_FILE_NAME).getLinkByLabel(SCRIPT_FILE_NAME).click();
ejtt.tabMenu.clickControlTab();
// Execute the script.
ejtt.tabMenu.getTabContentBox().getButtonInputByLabel(EXECUTE_OP_NAME).click();
// Command Line Arguments - none
//ejtt.tabMenu.getTabContentBox().getSubmitButtonByLabel("OK").click();
// No tabs here.
// (22:50:51) ozizka: ccrouch, ips: After clicking "Execute Script", the Control Operation page (the one where I set the arguments) doesn't have the tabs. All other pages have tabs. Is this intentional?
// (22:52:49) ips: ozizka: yeah probably intentional to show you're in the middle of a workflow
// (22:52:55) ips: ideally we should switch it over to using a modal on the original page for prompting for the params
ejtt.sleep(2000);
// Run.
/*String label = "OK";
HtmlInput okButton = ((HtmlDivision) client.getElement("content")).getFirstByXPath(".//input[normalize-space(@value)='" + label + "']");
okButton.click();*/
((HtmlSubmitInput) client.getElement("parametersForm:okButton")).click();
// Wait for the operation to succesfuly finish.
ejtt.operations.waitActivelyForOperationToFinish(EXECUTE_OP_NAME, 5000, 5);
assertTrue(EXECUTE_OP_NAME+" operation wasn't successful.", ejtt.tabMenu.getTabContentBox().getOperationsHistoryTable().wasLastOperationSuccesful() );
ejtt.sleep(2000);
// Get the results table.
//client.getElement("historyDetailsPanel_body");
HtmlDivision resultsDiv = (HtmlDivision) client.getElement("operationResults");
if( null == resultsDiv )
throw new HtmlElementNotFoundException("#operationResults - a div around a results table.");
HtmlTable resultsTableElm = resultsDiv.getFirstByXPath(".//table[@class='properties-table']");
if( null == resultsTableElm )
throw new HtmlElementNotFoundException(".//table[@class='properties-table'] - a results table.");
ContentTable resultsTable = ejtt.getTabMenu().getTabContentBox().getTable(resultsTableElm);
// Process the results table.
// Exit Code
HtmlTextInput exitCodeInput = resultsTable.getFirstRowContainingText("Exit Code").getCellByColumnName("Value").getFirstByXPath(".//input");
assertEquals("Exit Code should be 0", "0", exitCodeInput.getValueAttribute().trim() );
String expectedOutputPart = "usage";
HtmlTextArea outputTA = resultsTable.getFirstRowContainingText("Output").getCellByColumnName("Value").getFirstByXPath(".//textarea");
String taText = outputTA.getText();
DebugUtils.writeFile("target/scriptsWithoutParam-output.html", taText);
assertTrue("classpath.sh parameter-less run output should contain '"+expectedOutputPart+"'; is:\n"+taText, taText.toLowerCase().contains(expectedOutputPart));
}
finally {
}
}
/*
usage: classpath.sh [options] <classpath>
options:
-h, --help Print this help message.
-- Stop processing options.
-r, --relative Use relative paths.
classpath:
-c, --client Client classpath (client/*).
-s, --server Server classpath (lib/*).
-b, --both Both the client and server classpaths.
*/
/**
* Runs `classpath.sh -c`, which prints the classpath.
*
* DISABLED, because from JSFUnit, setting the op param doesn't work.
*/
public void DISABLEDtestScriptsRunClasspathshWithParamTest() throws IOException, EmbJoprTestException {
final String SCRIPT_FILE_NAME = "classpath.sh";
try {
// Go to the Scripts node.
ejtt.navTree.getNodeByLabel(NAV_SCRIPTS).click();
// Go to the ClassPath script.
ejtt.tabMenu.getTabContentBox().getFirstTable().getFirstRowContainingLink(SCRIPT_FILE_NAME).getLinkByLabel(SCRIPT_FILE_NAME).click();
ejtt.tabMenu.clickControlTab();
// Execute the script.
ejtt.tabMenu.getTabContentBox().getButtonInputByLabel(EXECUTE_OP_NAME).click();
// Command Line Arguments: "-c"
// No tab content box in this block.
{
HtmlTable resultsTableElm = ((HtmlForm)client.getElement("parametersForm")).getFirstByXPath(".//table[@class='properties-table']");
ContentTable resultsTable = ejtt.getTable(resultsTableElm);
ContentTableRow row = resultsTable.getFirstRowContainingText("Command Line Arguments");
assertNotNull("'Command Line Arguments' row not found.", row);
HtmlCheckBoxInput unsetCheckbox = row./*getCellByColumnName("Unset").*/ getElement().getFirstByXPath(".//input[@type='checkbox']");
unsetCheckbox.setChecked(false);
HtmlTextArea argsTA = row./*getCellByColumnName("Value").*/ getElement().getFirstByXPath(".//textarea"); // [@ondblclick='commandLineArguments']
argsTA.setText("-c");
DebugUtils.writeFile("target/scriptsWithParam-settingParams.html", client.getPageAsText());
ejtt.sleep(2000);
// Run.
((HtmlSubmitInput) client.getElement("parametersForm:okButton")).click();
}
// Wait for the operation to succesfuly finish.
ejtt.operations.waitActivelyForOperationToFinish(EXECUTE_OP_NAME, 5000, 5);
assertTrue(EXECUTE_OP_NAME+" operation wasn't successful.", ejtt.tabMenu.getTabContentBox().getOperationsHistoryTable().wasLastOperationSuccesful() );
ejtt.sleep(2000);
// Get the results table.
//client.getElement("historyDetailsPanel_body");
HtmlDivision resultsDiv = (HtmlDivision) client.getElement("operationResults");
if( null == resultsDiv )
throw new HtmlElementNotFoundException("#operationResults - a div around a results table.");
HtmlTable resultsTableElm = resultsDiv.getFirstByXPath(".//table[@class='properties-table']");
if( null == resultsTableElm )
throw new HtmlElementNotFoundException(".//table[@class='properties-table'] - a results table.");
ContentTable resultsTable = ejtt.getTabMenu().getTabContentBox().getTable(resultsTableElm);
// Process the results table.
// Exit Code
HtmlTextInput exitCodeInput = resultsTable.getFirstRowContainingText("Exit Code").getCellByColumnName("Value").getFirstByXPath(".//input");
assertEquals("Exit Code should be 0", "0", exitCodeInput.getValueAttribute().trim() );
String expectedOutputPart = "/client";
HtmlTextArea outputTA = resultsTable.getFirstRowContainingText("Output").getCellByColumnName("Value").getFirstByXPath(".//textarea");
String taText = outputTA.getText().trim();
DebugUtils.writeFile("target/scriptsWithParam-output.html", client.getPageAsText());
assertTrue("`classpath.sh -c` run output should contain '"+expectedOutputPart+"'; is:\n"+taText, taText.toLowerCase().contains(expectedOutputPart));
}
finally {
}
}
}// class