Thucyides Test cases Queuing - selenium-webdriver

Implemented A Thucydides(SERENITY) BDD Environment for automated testing of version 0.9.269. I have seen that the runner of test cases picks up the random test stories. Is there any way so that the stories can be queued?
The code for PortalTestSuit is as
public class PortalTestSuite extends ThucydidesJUnitStories {
private static final Logger LOGGER = LoggerFactory.getLogger(PortalTestSuite.class.getName());
/**
* Instantiates a new Portal test suite.
*/
public PortalTestSuite() {
/*Some Code to check the server is working or not*/
/* Do all stories */
findStoriesCalled("*.story");
}}
Here, the findStories will pick up the random stories from the directory and executes relative code... but please let me know the way to queue the Stories. Thanks.

Yes, we can maintain the order of story by overriding storyPaths() method of ThucydidesJUnitStories class.
#Override
public List<String> storyPaths() {
try {
File file = new File(System.getProperty("user.dir").concat("/src/test/resources/StoryContextTest.script"));
try (FileReader reader = new FileReader(file)) {
char[] buffer = new char[(int) file.length()];
reader.read(buffer);
String[] lines = new String(buffer).split("\n");
List<String> storiesList = new ArrayList<>(lines.length);
StoryFinder storyFinder = new StoryFinder();
for (String line : lines) {
if (!line.equals("") && !line.startsWith("#")) {
if (line.endsWith("*")) {
for (URL classpathRootUrl : allClasspathRoots()) {
storiesList.addAll(storyFinder.findPaths(classpathRootUrl, line.concat("*/*.story"), ""));
}
} else {
storiesList.add(line);
}
}
}
return storiesList;
}
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private List<URL> allClasspathRoots() {
try {
return Collections.list(getClassLoader().getResources("."));
} catch (IOException e) {
throw new IllegalArgumentException("Could not load the classpath roots when looking for story files",e);
}
}
The stories are being loaded from StoryContextTest.script as
################# Stories goes here #################
stories/authentication/authentication/authentication.story
stories/authentication/authentication/authentication1.story
(Or)
*/authentication/* (will get stories randomly)
This way you can serialize your stories as in Thucydides.

Related

Winforms recursive file scan blocks UI

I have a program that searches the given directory and adds all the files to a list view. My problem is that the ui thread gets stuck while the search is busy. I have tried using tasks but can’t get it to work in async. The list view must be updated after each file has been found.
I have done a lot of reading about the TPL and how to use it but can’t get it to work in this case. I got it to work where the processing of data is in one method that create a task to process it. Can any one tel me what is wrong in the code below and how to fix it?
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
WalkDirectory(new DirectoryInfo(drive));
});
}
public void testTaskUpdateLabel(string labelTeks)
{
Task taskUpdateLabel = new Task(() =>
{
label4.Text = labelTeks;
});
taskUpdateLabel.Start(uiScheduler);
}
public void testTaskUpdateLabel(string labelTeks)
{
Task taskUpdateLabel = new Task(() =>
{
label4.Text = labelTeks;
});
taskUpdateLabel.Start(uiScheduler);
}
public bool WalkDirectory(DirectoryInfo directory)
{
if (directory == null)
{
throw new ArgumentNullException("directory");
}
return this.WalkDirectories(directory);
}
private bool WalkDirectories(DirectoryInfo directory)
{
bool continueScan = true;
continueScan = WalkFilesInDirectory(directory);
if (continueScan)
{
DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach (DirectoryInfo subDirectory in subDirectories)
{
try
{
if ((subDirectory.Attributes & FileAttributes.ReparsePoint) != 0)
{
continue;
}
if (!(continueScan = WalkDirectory(subDirectory)))
{
break;
}
}
catch (UnauthorizedAccessException)
{
continue;
}
}
}
if (continueScan)
{
testTaskUpdateLabel(directory.FullName);
}
return continueScan;
}
private bool WalkFilesInDirectory(DirectoryInfo directory)
{
bool continueScan = true;
// Break up the search pattern in separate patterns
string[] searchPatterns = _searchPattern.Split(';');
// Try to find files for each search pattern
foreach (string searchPattern in searchPatterns)
{
if (!continueScan)
{
break;
}
// Scan all files in the current path
foreach (FileInfo file in directory.GetFiles(searchPattern))
{
try
{
testTaskUpdate(file.FullName);
}
catch (UnauthorizedAccessException)
{
continue;
}
}
}
return continueScan;
}
If you use a BackgroundWorker class, the UI will work and progress can be updated in the ProgressChanged event handler.
MSDN Reference
Can any one tel me what is wrong in the code below and how to fix it?
The problem is here
public void testTaskUpdateLabel(string labelTeks)
{
Task taskUpdateLabel = new Task(() =>
{
label4.Text = labelTeks;
});
taskUpdateLabel.Start(uiScheduler);
}
You should not use TPL to update the UI. TPL tasks are for doing non UI work and UI should only be updated on the UI thread. You already moved the work on a thread pool thread (via Task.Run), so the only problem you need to solve is how to update the UI from inside the worker. There are many ways to do that - using Control.Invoke/BeginInvoke, SynchronizationContext etc, but the preferred approach for TPL is to pass and use IProgress<T> interface. Don't be fooled by the name - the interface is an abstraction of a callback with some data. There is a standard BCL provided implementation - Progress<T> class with the following behavior, according to the documentation
Any handler provided to the constructor or event handlers registered with the ProgressChanged event are invoked through a SynchronizationContext instance captured when the instance is constructed.
i.e. perfectly fits in UI update scenarios.
With all that being said, here is how you can apply that to your code. We'll use IProgress<string> and will call Report method and pass the full name for each file/directory we find - a direct replacement of your testTaskUpdateLabel calls.
private void button1_Click(object sender, EventArgs e)
{
var progress = new Progress<string>(text => label4.Text = text);
Task.Run(() =>
{
WalkDirectory(new DirectoryInfo(drive), progress);
});
}
public bool WalkDirectory(DirectoryInfo directory, IProgress<string> progress)
{
if (directory == null) throw new ArgumentNullException("directory");
if (progress == null) throw new ArgumentNullException("progress");
return WalkDirectories(directory, progress);
}
bool WalkDirectories(DirectoryInfo directory, IProgress<string> progress)
{
// ...
if (!(continueScan = WalkDirectories(subDirectory, progress)))
// ...
if (continueScan)
progress.Report(directory.FullName);
// ...
}
bool WalkFilesInDirectory(DirectoryInfo directory, IProgress<string> progress)
{
// ...
try
{
progress.Report(file.FullName);
}
// ...
}
I got it to work by making the walkDirectory, walkDirectories and WalkFiles methods async. Thus using the await keyword before I call the testUpdate and testUpdateLabel methods. This way the listview is updated with the search results while the search is running without blocking the UI thread. I.E. the user can cancel the search when the file he was searching for has been found.

Testing this command

I'm quite new to unit tests and I've got some troubles testing this command
internal async Task OnDeleteTreasurerCommandExecute(TesorieraItemResult tesoriera)
{
try
{
if (await MessageService.ShowAsync("Confermare l'operazione?", string.Empty, MessageButton.YesNo, MessageImage.Question) == MessageResult.Yes)
{
await repository.DeleteTesorieraItemAsync(tesoriera.ID_ISTITUTO,tesoriera.ID_DIVISA,tesoriera.PROGRESSIVO);
await MessageService.ShowInformationAsync("Operazione completata");
if (SelectedInstitute != null)
await OnLoadDataCommandExecute();
}
}
catch (Exception ex)
{
ErrorService.HandleError(GetType(), ex);
}
}
I'm using Catel as MVVM framework
how do I simulate the yes/no answers?
Thanks
You need to substitute the MessageService with a class that can return yes or no answer. Here's an example using NSubstitute.
Install-Package NSubstitute
Install-Package NUnit
Let us say you have a class that has a method that
needs Yes, then No:
public class AccountViewModel
{
readonly IMessageService _messageService;
readonly ICustomerRepository _customerRepository;
public AccountViewModel(IMessageService messageService, ICustomerRepository customerRepository)
{
_messageService = messageService;
_customerRepository = customerRepository;
}
public async Task OnDeleteCustomer(Customer customer)
{
if (await MessageService.ShowAsync(
"Confirm?",
string.Empty,
MessageButton.YesNo,
MessageImage.Question) == MessageResult.Yes)
{
_customerRepository.Delete(customer);
await MessageService.ShowInformationAsync("Completed");
}
}
}
Then your test case looks like this:
public class TestAccountViewModel
{
[TestCase]
public class TestDeleteCustomer()
{
// arrange
var messageService = Substitute.For<IMessageService>();
messageService
.ShowAsync(
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<MessageButton>(),
Arg.Any<MessageImage>())
.Returns(Task.FromResult(MessageResult.Yes);
messageService
.ShowInformationAsync(Arg.Any<string>())
.Returns(Task.FromResult<object>(null));
var customerRepository = Substitute.For<ICustomerRepository>();
// act
var sut = new AccountViewModel(messageService, customerRepository);
var customer = new Customer();
sut.OnDeleteCustomer(customer);
// assert
Assert.IsTrue(customerRepository.Received().DeleteCustomer(customer));
}
}
In a past version, Catel provided a test implementation of the IMessageService that allowed you to queue expected results so you could test the different paths inside a command.
I just noticed this class is no longer available, but you can easily implement a test stub yourself (using mocking, etc). Or you could contribute to Catel and revive the test implementation.

Test Automation Framework for Web Application using Java

I am beginning to write a Test Automation Framework in Java (language that I am comfortable with) for my Web Application. Currently, it is entirely tested on UI. No Backend / API testing in near sight.
I plan to use Selenium Web Driver. This framework will support both Functional/Integration and Performance testing.
I am building with Open Source Solutions for the first time (over using tools like LoadRunner) and my needs are this framework will work with Continuous Integration tools like Jenkins/Hudson and an in-house Test Management tool for reporting results.
I searched for this specific scenario but could not find one. I know there will be numerous integrations, plug-ins, etc... that needs to be built. My question is can you provide some pointers (even good reads is OK) towards beginning to build this framework with Open source solutions ?
Selenium will allow you to automate all your web (browsers) actions
automations.
Junit/TestNG as the testing framework,
including their default reports system
Maven for the project
management and lifecycle (including test phase with surefire
plugin)
Jenkins is a good integration tool that will easily
run the setup above
Good luck!
I am giving here framework functions which reduces code very much
public TestBase() throws Exception{
baseProp = new Properties();
baseProp.load(EDCPreRegistration.class.getResourceAsStream("baseproperties.properties"));
// Firefox profile creation
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
profile.setPreference("browser.cache.disk.enable", false);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port",8080);
driver = new FirefoxDriver(profile);
//System.setProperty("webdriver.ie.driver","E:\\Phyweb Webdriver\\IEDriverServer.exe");
//driver = new InternetExplorerDriver();
driver.manage().window().maximize();
}
//To find WebElement by id
public static WebElement FindElement(String id)
{
try
{
webElement= driver.findElement(By.id(id));
}
catch(Exception e)
{
Print(e);
}
return webElement;
}
//To find WebElement by name
public static WebElement FindElementByName(String name)
{
try
{
webElement= driver.findElement(By.name(name));
}
catch(Exception e)
{
Print(e);
}
return webElement;
}
//To find WebElement by Class
public static WebElement FindElementByClass(String classname)
{
try
{
webElement= driver.findElement(By.className(classname));
}
catch(Exception e)
{
Print(e);
}
return webElement;
}
//To get data of a cell
public static String GetCellData(XSSFSheet sheet,int row,int col)
{
String cellData = null;
try
{
cellData=PhyWebUtil.getValueFromExcel(row, col, sheet);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return cellData;
}
//To click a button using id
public static void ClickButton(String id,String label)
{
try
{
WebElement webElement= FindElement(id);
Snooze();
webElement.click();
PrintMessage(label+" is selected");
}
catch(Exception e)
{
Print(e);
}
}
//To click a button using class
public void ClickButtonByClass(String classname,String label)
{
try
{
WebElement webElement= FindElementByClass(classname);
Snooze();
webElement.click();
PrintMessage(label+" is selected");
}
catch(Exception e)
{
Print(e);
}
}
//To enter data into Textbox
public String editTextField(int rownum, int celnum,WebElement element ,XSSFSheet sheet,String Label)
{
XSSFRow row = sheet.getRow(rownum);
XSSFCell Cell = row.getCell(celnum);
String inputValue = Cell.getStringCellValue().trim();
element.clear();//To clear contents if present
try
{
element.sendKeys(inputValue);
String elementVal=element.toString();
if(elementVal.contains("password"))
{
PrintMessage("Password is entered");
}
else
{
PrintMessage("Value entered for "+Label+" is "+inputValue);
}
}
catch(Exception e){
Print(e);
//cv.verifyTrue(false, "<font color= 'red'> Failed due to : </font> "+e.getMessage());
}
return inputValue;
}
//To enter data into Textbox
public String editTextFieldDirect(WebElement element ,String text,String label)
{
element.clear();//To clear contents if present
try
{
element.sendKeys(text);
String elementVal=element.toString();
if(elementVal.contains("password"))
{
PrintMessage("Password is entered");
}
else
{
PrintMessage("Value entered for "+label+" is "+text);
}
}
catch(Exception e){
Print(e);
//cv.verifyTrue(false, "<font color= 'red'> Failed due to : </font> "+e.getMessage());
}
return text;
}
//To select Radio button
public void ClickRadioButton(String id)
{
try
{
WebElement webElement= FindElement(id);
Snooze();
webElement.click();
text=webElement.getText();
PrintMessage(text+" is selected");
}
catch(Exception e)
{
Print(e);
}
}
//To select Link
public void ClickLink(String id,String label)
{
try
{
ClickButton(id,label);
}
catch(Exception e)
{
Print(e);
}
}
//To Click an Image button
public void ClickImage(String xpath)
{
try
{
WebElement webElement= FindElement(id);
Snooze();
webElement.click();
text=GetText(webElement);
PrintMessage(text+" is selected");
}
catch(Exception e)
{
Print(e);
}
}
//Select a checkbox
public void CheckboxSelect(String id,String label)
{
try
{
WebElement webElement= FindElement(id);
Snooze();
webElement.click();
PrintMessage("Checkbox "+label+" is selected");
}
catch(Exception e)
{
Print(e);
}
}
//To select value in Combobox
public void SelectData(String id,String label,String cellval)
{
try
{
WebElement webElement= FindElement(id);
Snooze();
webElement.click();
String elementStr=webElement.toString();
int itemIndex=elementStr.indexOf("value");
if(itemIndex>-1)
{
int endIndex=elementStr.length()-3;
String item=elementStr.substring(itemIndex+7, endIndex);
if(cellval=="0")
{
PrintMessage(item+" is selected for "+label);
}
else
{
PrintMessage(cellval+" "+label+" is selected");
}
}
else
{
PrintMessage(cellval+" is selected for "+label);
}
}
catch(Exception e)
{
Print(e);
}
}
//To check if WebElement with id exists
public static boolean isExists(String id)
{
boolean exists = false;
driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
try
{
exists=driver.findElements( By.id(id) ).size() != 0;
}
catch (Exception e)
{
Print(e);
}
if(exists==true)
return true;
else
return false;
}
//To check if WebElement with name exists
public static boolean isExistsName(String name)
{
boolean exists = false;
driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
try
{
exists=driver.findElements( By.name(name) ).size() != 0;
}
catch (Exception e)
{
if(e.getMessage().contains("InvalidSelectorError"))
{
System.out.println("");
}
else
Print(e);
}
if(exists==true)
return true;
else
return false;
}
//Explicit wait until a element is visible and enabled using id
public void ExplicitlyWait(String id)
{
try
{
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
}
catch(Exception e)
{
Print(e);
}
}
//Explicit wait until a element is visible and enabled using classname
public void ExplicitlyWaitByClass(String classname)
{
try
{
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.className(classname)));
}
catch(Exception e)
{
Print(e);
}
}
//Explicit wait until a element is visible and enabled using id
public void ExplicitlyWaitSpecific(int sec,String id)
{
try
{
WebElement myDynamicElement = (new WebDriverWait(driver, sec))
.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
}
catch(Exception e)
{
Print(e);
}
}
//Snooze for 10 seconds
public static void Snooze()
{
try
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
catch(Exception e)
{
Print(e);
}
}
//Snooze for Secs
public static void SnoozeSpecific(int seconds)
{
try
{
driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
}
catch(Exception e)
{
Print(e);
}
}
//Sleep for milliSeconds
public static void Sleep(int milisec) throws InterruptedException
{
Thread.sleep(milisec);
}
//To get text using text()
public static String GetText(WebElement element)
{
try
{
text=element.getText();
}
catch(Exception e){
Print(e);
}
return text;
}
//To get text using getAttribute("value")
public static String GetTextAttribute(WebElement element)
{
try
{
text=element.getAttribute("value");
}
catch(Exception e){
Print(e);
}
return text;
}
//To Print error messages to both Console and Results file
public static void Print(Exception e)
{
Reporter.log("Exception is :"+e.getMessage());
System.out.println(e);
}
//To Print messages to both Console and Results file
public static void PrintMessage(String str)
{
Reporter.log(str);
System.out.println(str);
}
//To Print Blank row
public static void BlankRow()
{
Reporter.log(" ");
System.out.println(" ");
}
//To Print Sub header
public static void Header(String str)
{
BlankRow();
Reporter.log("***********************"+str+" Verifications***********************");
System.out.println("***********************"+str+" Verifications***********************");
BlankRow();
}
//To Print Sub header
public static void SubHeader(String str)
{
BlankRow();
Reporter.log("-----------------------"+str+" Verifications-----------------------");
System.out.println("-----------------------"+str+" Verifications-----------------------");
BlankRow();
}
So long as you have a command line for kicking off your framework and you report back using the xunit log format then you should be good for integration with any number of Continuous integration frameworks.
Your trade off on running a browser instance under load will be fewer virtual users per host and a very careful examination of your load generator resources under load. Don't forget to include monitoring API in your framework for system metrics under load and an auto evaluation engine related to SLA metrics acceptance to determine pass of fail criteria under load at a given load point.
We are begining to develop something very related to your needs; Java, Webdriver, Jenkins, Maven, etc. We are quite new to automation here, but still have good Java ressources.
We are builing our framework based on Tarun Kumar from www.seleniumtests.com.
He's got a lot of good videos from Youtube (sounds quality is not so good), and he manage to create something very user friendly, using PageObjects Pattern.
If you don't have any clue where to start, I would start from there.
Good luck!
I created a java library on the top of selenium which simplifies test automation of a website. It has an implicit waiting mechanism and is easy to use:
https://github.com/gartenkralle/web-ui-automation
Example:
import org.junit.Test;
import org.openqa.selenium.By;
import common.UserInterface;
import common.TestBase;
public class Google extends TestBase
{
private final static String GOOGLE_URL = "https://www.google.com/";
private final static By SEARCH_FIELD = By.xpath("//input[#id='lst-ib']");
private final static By AUTO_COMPLETION_LIST_BOX = By.xpath("//*[#id='sbtc']/div[2][not(contains(#style,'none'))]");
private final static By SEARCH_BUTTON = By.xpath("//input[#name='btnK']");
#Test
public void weatherSearch()
{
UserInterface.Action.visitUrl(GOOGLE_URL);
UserInterface.Action.fillField(SEARCH_FIELD, "weather");
UserInterface.Verify.appeared(AUTO_COMPLETION_LIST_BOX);
UserInterface.Action.pressEscape();
UserInterface.Action.clickElement(SEARCH_BUTTON);
}
}
Selenium WebDriver is surely a tool for UI automation and we use it extensively to do cross Browser testing on Cloud Solutions like Browser Stack.
Our use case let us build an open source Framework "omelet" built in Java using TestNG as test runner , which takes care of almost everything related to web-testing and leaves us to actually automated application rather than thinking about reports , parallel run and CI integration etc.
Suggestion, Contribution always welcome :)
Documentation over here and
Github link over here
Do remember to checkout 5 min tutorial on website
For Functional Regression test:
Selenium Webdriver - Selenium a Web based automation tool that automates anything and everything available on a Web page. you use Selenium Webdriver with JAVA.
Watij- Web Application Testing in Java
Automates functional testing of web applications through real web browsers.
TestProject - It supports for testing both web and Mobile (Android & iOS).
For Non-functional test:
Gatling- For performance testing and Stress testing
Apache JMeter - For Volume, Performance, Load & Stress testing
CI tool:
Jenkins- Jenkins provides continuous integration services for software development.
For Functional Regression test:
TestProject
Selenium
Cucumber : It's a BDD tool
For Non-functional: Performance and Load testing:
JMeter
Note: TestComplete is a very good commercial tool.

Increasing heap by excessive use oft Java ScriptEngine (Jyhton)

We have a JavaEE application that uses jython to execute some python scripts. By and by the used heapspace gets bigger and bigger until there is no more heapspace left. In a heapdump i can se that there are a lot of Py*-classes.
So i wrote a small test-program:
TestApp
public class TestApp {
private final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
private HashMap<String, ScriptEngine> scriptEngines = new HashMap<String, ScriptEngine>();
private final String scriptContainerPath = "";
public static void main(String[] args) throws InterruptedException {
int counter = 1;
while(true) {
System.out.println("iteration: " + counter);
TestApp testApp = new TestApp();
testApp.execute();
counter++;
Thread.sleep(100);
}
}
void execute() {
File scriptContainer = new File(scriptContainerPath);
File[] scripts = scriptContainer.listFiles();
if (scripts != null && scripts.length > 0) {
Arrays.sort(scripts, new Comparator<File>() {
#Override
public int compare(File file1, File file2) {
return file1.getName().compareTo(file2.getName());
}
});
for (File script : scripts) {
String engineName = ScriptExecutor.getEngineNameByExtension(script.getName());
if(!scriptEngines.containsKey(engineName)) {
scriptEngines.put(engineName, scriptEngineManager.getEngineByName(engineName));
}
ScriptEngine scriptEngine = scriptEngines.get(engineName);
try {
ScriptExecutor scriptExecutor = new ScriptExecutor(scriptEngine, script, null);
Boolean disqualify = scriptExecutor.getBooleanScriptValue("disqualify");
String reason = scriptExecutor.getStringScriptValue("reason");
System.out.println("disqualify: " + disqualify);
System.out.println("reason: " + reason);
} catch (Exception e) {
e.printStackTrace();
}
}
// cleanup
for(Map.Entry<String, ScriptEngine> entry : scriptEngines.entrySet()) {
ScriptEngine engine = entry.getValue();
engine.getContext().setErrorWriter(null);
engine.getContext().setReader(null);
engine.getContext().setWriter(null);
}
}
}
}
ScriptExecutor
public class ScriptExecutor {
private final static String pythonExtension = "py";
private final static String pythonEngine = "python";
private final ScriptEngine scriptEngine;
public ScriptExecutor(ScriptEngine se, File file, Map<String, Object> keyValues) throws FileNotFoundException, ScriptException {
scriptEngine = se;
if (keyValues != null) {
for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
scriptEngine.put(entry.getKey(), entry.getValue());
}
}
// execute script
Reader reader = null;
try {
reader = new FileReader(file);
scriptEngine.eval(reader);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// nothing to do
}
}
}
}
public Boolean getBooleanScriptValue(String key) {
// convert Object to Boolean
}
public String getStringScriptValue(String key) {
// convert Object to String
}
public static String getEngineNameByExtension(String fileName) {
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
if (pythonExtension.equalsIgnoreCase(extension)) {
System.out.println("Found engine " + pythonEngine + " for extension " + extension + ".");
return pythonEngine;
}
throw new RuntimeException("No suitable engine found for extension " + extension);
}
}
In the specified directory are 14 python scripts that all look like this:
disqualify = True
reason = "reason"
I start this program with the following VM-arguments:
-Xrs -Xms16M -Xmx16M -XX:MaxPermSize=32M -XX:NewRatio=3 -Dsun.rmi.dgc.client.gcInterval=300000 -Dsun.rmi.dgc.server.gcInterval=300000 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -server
These are the arguments our AppServer is running with. Only Xms, Xmx and MaxPermSize are smaller in my testcase.
When I run this application I can see that the CMS Old Gen pool increases to its max size. After that the Par Eden Space pool increases. In addition at any time the ParNewGC does not run anymore. The cleanup part improved the situation but didn't resolve the problem. Has anybody an idea why my heap isn't completly cleaned?
I think I have found a solution for my problem: I removed the JSR223 stuff und now use the PythonInterpreter directly.

Easy way to dynamically invoke web services (without JDK or proxy classes)

In Python I can consume a web service so easily:
from suds.client import Client
client = Client('http://www.example.org/MyService/wsdl/myservice.wsdl') #create client
result = client.service.myWSMethod("Bubi", 15) #invoke method
print result #print the result returned by the WS method
I'd like to reach such a simple usage with Java.
With Axis or CXF you have to create a web service client, i.e. a package which reproduces all web service methods so that we can invoke them as if they where normal methods. Let's call it proxy classes; usually they are generated by wsdl2java tool.
Useful and user-friendly. But any time I add/modify a web service method and I want to use it in a client program I need to regenerate proxy classes.
So I found CXF DynamicClientFactory, this technique avoids the use of proxy classes:
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
//...
//create client
DynamicClientFactory dcf = DynamicClientFactory.newInstance();
Client client = dcf.createClient("http://www.example.org/MyService/wsdl/myservice.wsdl");
//invoke method
Object[] res = client.invoke("myWSMethod", "Bubi");
//print the result
System.out.println("Response:\n" + res[0]);
But unfortunately it creates and compiles proxy classes runtime, hence requires JDK on the production machine. I have to avoid this, or at least I can't rely on it.
My question:
Is there another way to dinamically invoke any method of a web service in Java, without having a JDK at runtime and without generating "static" proxy classes? Maybe with a different library? Thanks!
I know this is a really old question but if you are still interested you could use soap-ws github project: https://github.com/reficio/soap-ws
Here you have a sample usage really simple:
Wsdl wsdl = Wsdl.parse("http://www.webservicex.net/CurrencyConvertor.asmx?WSDL");
SoapBuilder builder = wsdl.binding()
.localPart("CurrencyConvertorSoap")
.find();
SoapOperation operation = builder.operation()
.soapAction("http://www.webserviceX.NET/ConversionRate")
.find();
Request request = builder.buildInputMessage(operation)
SoapClient client = SoapClient.builder()
.endpointUrl("http://www.webservicex.net/CurrencyConvertor.asmx")
.build();
String response = client.post(request);
As you can see it is really simple.
With CXF 3.x this could be possible with StaxDataBinding. Follow below steps to get the basics. Of course, this could be enhanced to your needs.
Create StaxDataBinding something like below. Note below code can be enhanced to your sophistication.
class StaxDataBinding extends AbstractInterceptorProvidingDataBinding {
private XMLStreamDataReader xsrReader;
private XMLStreamDataWriter xswWriter;
public StaxDataBinding() {
super();
this.xsrReader = new XMLStreamDataReader();
this.xswWriter = new XMLStreamDataWriter();
inInterceptors.add(new StaxInEndingInterceptor(Phase.POST_INVOKE));
inFaultInterceptors.add(new StaxInEndingInterceptor(Phase.POST_INVOKE));
inInterceptors.add(RemoveStaxInEndingInterceptor.INSTANCE);
inFaultInterceptors.add(RemoveStaxInEndingInterceptor.INSTANCE);
}
static class RemoveStaxInEndingInterceptor
extends AbstractPhaseInterceptor<Message> {
static final RemoveStaxInEndingInterceptor INSTANCE = new RemoveStaxInEndingInterceptor();
public RemoveStaxInEndingInterceptor() {
super(Phase.PRE_INVOKE);
addBefore(StaxInEndingInterceptor.class.getName());
}
public void handleMessage(Message message) throws Fault {
message.getInterceptorChain().remove(StaxInEndingInterceptor.INSTANCE);
}
}
public void initialize(Service service) {
for (ServiceInfo serviceInfo : service.getServiceInfos()) {
SchemaCollection schemaCollection = serviceInfo.getXmlSchemaCollection();
if (schemaCollection.getXmlSchemas().length > 1) {
// Schemas are already populated.
continue;
}
new ServiceModelVisitor(serviceInfo) {
public void begin(MessagePartInfo part) {
if (part.getTypeQName() != null
|| part.getElementQName() != null) {
return;
}
part.setTypeQName(Constants.XSD_ANYTYPE);
}
}.walk();
}
}
#SuppressWarnings("unchecked")
public <T> DataReader<T> createReader(Class<T> cls) {
if (cls == XMLStreamReader.class) {
return (DataReader<T>) xsrReader;
}
else {
throw new UnsupportedOperationException(
"The type " + cls.getName() + " is not supported.");
}
}
public Class<?>[] getSupportedReaderFormats() {
return new Class[] { XMLStreamReader.class };
}
#SuppressWarnings("unchecked")
public <T> DataWriter<T> createWriter(Class<T> cls) {
if (cls == XMLStreamWriter.class) {
return (DataWriter<T>) xswWriter;
}
else {
throw new UnsupportedOperationException(
"The type " + cls.getName() + " is not supported.");
}
}
public Class<?>[] getSupportedWriterFormats() {
return new Class[] { XMLStreamWriter.class, Node.class };
}
public static class XMLStreamDataReader implements DataReader<XMLStreamReader> {
public Object read(MessagePartInfo part, XMLStreamReader input) {
return read(null, input, part.getTypeClass());
}
public Object read(QName name, XMLStreamReader input, Class<?> type) {
return input;
}
public Object read(XMLStreamReader reader) {
return reader;
}
public void setSchema(Schema s) {
}
public void setAttachments(Collection<Attachment> attachments) {
}
public void setProperty(String prop, Object value) {
}
}
public static class XMLStreamDataWriter implements DataWriter<XMLStreamWriter> {
private static final Logger LOG = LogUtils
.getL7dLogger(XMLStreamDataWriter.class);
public void write(Object obj, MessagePartInfo part, XMLStreamWriter writer) {
try {
if (!doWrite(obj, writer)) {
// WRITE YOUR LOGIC HOW you WANT TO HANDLE THE INPUT DATA
//BELOW CODE JUST CALLS toString() METHOD
if (part.isElement()) {
QName element = part.getElementQName();
writer.writeStartElement(element.getNamespaceURI(),
element.getLocalPart());
if (obj != null) {
writer.writeCharacters(obj.toString());
}
writer.writeEndElement();
}
}
}
catch (XMLStreamException e) {
throw new Fault("COULD_NOT_READ_XML_STREAM", LOG, e);
}
}
public void write(Object obj, XMLStreamWriter writer) {
try {
if (!doWrite(obj, writer)) {
throw new UnsupportedOperationException("Data types of "
+ obj.getClass() + " are not supported.");
}
}
catch (XMLStreamException e) {
throw new Fault("COULD_NOT_READ_XML_STREAM", LOG, e);
}
}
private boolean doWrite(Object obj, XMLStreamWriter writer)
throws XMLStreamException {
if (obj instanceof XMLStreamReader) {
XMLStreamReader xmlStreamReader = (XMLStreamReader) obj;
StaxUtils.copy(xmlStreamReader, writer);
xmlStreamReader.close();
return true;
}
else if (obj instanceof XMLStreamWriterCallback) {
((XMLStreamWriterCallback) obj).write(writer);
return true;
}
return false;
}
public void setSchema(Schema s) {
}
public void setAttachments(Collection<Attachment> attachments) {
}
public void setProperty(String key, Object value) {
}
}
}
Prepare your input to match the expected input, something like below
private Object[] prepareInput(BindingOperationInfo operInfo, String[] paramNames,
String[] paramValues) {
List<Object> inputs = new ArrayList<Object>();
List<MessagePartInfo> parts = operInfo.getInput().getMessageParts();
if (parts != null && parts.size() > 0) {
for (MessagePartInfo partInfo : parts) {
QName element = partInfo.getElementQName();
String localPart = element.getLocalPart();
// whatever your input data you need to match data value for given element
// below code assumes names are paramNames variable and value in paramValues
for (int i = 0; i < paramNames.length; i++) {
if (paramNames[i].equals(localPart)) {
inputs.add(findParamValue(paramNames, paramValues, localPart));
}
}
}
}
return inputs.toArray();
}
Now set the proper data binding and pass the data
Bus bus = CXFBusFactory.getThreadDefaultBus();
WSDLServiceFactory sf = new WSDLServiceFactory(bus, wsdl);
sf.setAllowElementRefs(false);
Service svc = sf.create();
Client client = new ClientImpl(bus, svc, null,
SimpleEndpointImplFactory.getSingleton());
StaxDataBinding databinding = new StaxDataBinding();
svc.setDataBinding(databinding);
bus.getFeatures().add(new StaxDataBindingFeature());
BindingOperationInfo operInfo = ...//find the operation you need (see below)
Object[] inputs = prepareInput(operInfo, paramNames, paramValues);
client.invoke("operationname", inputs);
If needed you can match operation name something like below
private BindingOperationInfo findBindingOperation(Service service,
String operationName) {
for (ServiceInfo serviceInfo : service.getServiceInfos()) {
Collection<BindingInfo> bindingInfos = serviceInfo.getBindings();
for (BindingInfo bindingInfo : bindingInfos) {
Collection<BindingOperationInfo> operInfos = bindingInfo.getOperations();
for (BindingOperationInfo operInfo : operInfos) {
if (operInfo.getName().getLocalPart().equals(operationName)) {
if (operInfo.isUnwrappedCapable()) {
return operInfo.getUnwrappedOperation();
}
return operInfo;
}
}
}
}
return null;
}

Resources