Wednesday, June 12, 2013

SWT JFACE

Dependencys:

</dependency> 
      <dependency> 
      <groupId>org.eclipse.swt</groupId> 
      <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId> 
      <version>3.7.2</version>       
</dependency> 

    <dependency> 
         <groupId>org.eclipse</groupId> 
         <artifactId>jface</artifactId> 
         <version>3.3.0‐I20070606‐0010</version> 
       </dependency> 
  

Define repository:

<repositories> 
    <repository> 
      <id>nexusrepo</id> 
      <url>https://swt‐repo.googlecode.com/svn/repo/</url> 
    </repository>   
</repositories> 

SWT demo app:

package com.example.swt.widgets;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FirstSWTApplication {

    public static void main(String[] args) {
        Display display = new Display();

        Shell shell = new Shell(display);
        
        // Layout manager handle the layout
        // of the widgets in the container
        shell.setLayout(new FillLayout());
        
        //TODO add some widgets to the Shell
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
} 

OSGI

Service implementation activator:

public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
SongHibernateDAO df = new SongHibernateDAO();
//NewImplementation df = new NewImplementation();
sr=(ServiceRegistration<SongDAO>) context.registerService(SongDAO.class.getName(), (Object)df, null);
}

Service interface activator:

public class Activator implements BundleActivator {

    public void start(BundleContext context) {
        System.out.println("Starting the bundle");
    }

    public void stop(BundleContext context) {
        System.out.println("Stopping the bundle");
    }

Service consumer activator:

 public void start(BundleContext bundleContext)
    {

    Activator.context = bundleContext;
ServiceReference reference = bundleContext.getServiceReference(SongDAO.class.getName());
//ServiceReference reference = bundleContext.getServiceReference(RandomQuoteService.class.getName());
if (reference == null) {
System.out.println("NULL");
} else {
// System.out.println("service found");
};

try {
service =  (SongDAO)context.getService(reference); //??
}catch(Exception ex) {
ex.printStackTrace();
}
MyServiceTrackerCustomizer customTracker = new MyServiceTrackerCustomizer(bundleContext);
serviceTracker = new ServiceTracker(bundleContext, SongDAO.class.getName(), customTracker);
serviceTracker.open();
    }

POM.XLS extra:

o  <Embed‐Transitive>True</Embed‐Transitive>
o  <Embed‐Dependency>*<Embed‐Dependency> 
o  <Import‐Package> org.osgi.framework,  *;resolution:=optional </Import‐Package> 

All about hibernate



import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class Util {
private static SessionFactory sessionFactory = null;
static {
try {
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable e) {
System.out.println("Error in DAO.Hibernate.HibernateUtil: " + e.getMessage());
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

Insert data
public void insertObject(ArrayList objects) throws Exception
{
dbSession = HibernateUtil.getSessionFactory().openSession();
try
{
dbTransaction = dbSession.beginTransaction();
for (int i = 0; i < objects.size(); i++)
{
dbSession.save(objects.get(i));
dbTransaction.commit();
}
}
catch(Exception e)
{
dbTransaction.rollback();
System.out.println("Error in DAO.Hibernate.PersonHibernateDAO.insertPerson: "+e.getMessage());
}
finally
{
dbSession.flush();
dbSession.close();
}
}


Maven dependencys:
 <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.10.Final</version>
        </dependency>
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.24</version>
        </dependency>

//Config file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
        <property name="hibernate.connection.username">root</property>
         <property name="connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.pool_size">1</property>
          <mapping resource="Song.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Sunday, June 2, 2013

Cucumber automated acceptance tests

Cucumber automated acceptance tests
Cucumber is a tool for running automated acceptance tests written in a behaviour-driven development (BDD) style. Cucumber was originally written in the Ruby programming language, but since the original development and its success it has been implemented in other languages as well.
  1. How it works

It reads in your specification which is written in feature files that have a specific format named Gherkin.Examines the scenarios and runs them on the SUT.The scenarios are a list of steps for cucumber to work through .Besides features you give cucumber a set of step definitions which are in turn mapped to the scenarios written in plain language using the Gherkin.  


The following image describes this relationship really well.
As you can see the features of the application are written in English almost as plain text ,and the step definitions are written in Ruby (or in the language that has been used to create the SUT ).
  1. Gherkin

As mentioned previously gherkin is a cucumber specific format, that creates a set of rules for plain text that makes it easy to define step definitions .The main advantage for using Gherkin that it can be localized to be used in any language and because it uses plain text, even non-technical stakeholders can use it to write test specifications if the follow the following rules.
  1. Every Gherkin file begins with the Feature keyword
  2. The text following is the name of the feature and rest of text is description used for some summary or documentation
  3. The Scenario is a single concrete example of how the system should behave in a concrete situation
    1. Each scenario must make sense and has to be self-contained so there are no dependency issues between tests
    2. To express a desired behavior several scenarios are used
  4. The Given keyword is used to set up context for scenarios
  5. When keyword is used to describe an interaction with the system
  6. Then keyword check the result of an interaction
  7. Each line in a scenario is a step the But, And , * are used to link several steps.
An example feature file in gherkin format would look like this (the example is from the cucumber book):


  1. The workflow of writing typical cucumber acceptance test



Every scenario should follow this basic workflow
  1. Get the system in the desired state
  2. Poke it (Make any action that triggers something)
  3. Examine the new state
  1. Step definitions



Step definitions are functions and code snippets that “execute” the written features. They translate the Gherkin based plain language specification into concrete action on the SUT.
These step definition could originally only be written in Ruby in the following matter, but since it has been ported to other platforms they can be run also as JUnit and NUnit tests.
An example of an original Ruby step definition:
  1. The “Cucumber Algorithm”

You can see below the algorithm cucumber follows when executing automated test.
  1. Platforms

Cucumber is implemented for many different platforms (programming languages/frameworks).
The table below should help you choose the right platform.
Platform
Install
API Docs
Java, Groovy, Clojure,Jython, JRuby, Scala,Rhino JavaScript, Ioke
Cucumber-JVM
Cucumber-JVM
Ruby, JRuby
Cucumber-Ruby
Cucumber-Ruby
Ruby on Rails
Cucumber-Rails
Cucumber-Rails
Node.js, Browsers
Cucumber-JavaScript

Lua


C++

Cucumber-Cpp
C#, F#
SpecFlow
SpecFlow


  1. Cucumber-JVM

Cucumber-jvm is the java virtual machine based cucumber support most of the widespread languages running on java virtual machine. It was created to aid those who did not want to learn Ruby ,or help maintain all of the code (the application , and test) in the same language.
It consist of several modules (jar) that can be easily obtained from the cucumber site.
You will always need the cucumber-core module, which contains the main logic for parsing and executing your Gherkin feature files.


    1. Installation

Installation can be done in several ways:
  1. Downloading and adding jars
  2. Using ANT
  3. Using Maven
(Further on i will show only Maven as it is more commonly used and simple to add to existing maven projects)


You only need to add the following main dependencies
<dependencies>
   <dependency>
       <groupId>info.cukes</groupId>
       <artifactId>cucumber-picocontainer</artifactId>
       <version>1.1.3</version>
       <scope>test</scope>
   </dependency>
   <dependency>
       <groupId>info.cukes</groupId>
       <artifactId>cucumber-junit</artifactId>
       <version>1.1.3</version>
       <scope>test</scope>
   </dependency>
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.11</version>
       <scope>test</scope>
   </dependency>
</dependencies>



    1. The cucumber maven project structure

Be sure to follow the following structure because cucumber-jvm relies on this structure for accessing the feature and step definition files, and otherwise error can easily occur.







    1. Start cucumber class

This class does not have to contain anything else, it only serves as an initializer and should not be used as a hook for the test cases.


package com.mycompany.lehel;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
public class StartCucumber {                                   }


    1. Feature files

Feature files are the same as previously presented at the Gherkin chapter.
Java uses them the same way as they were parsed in the original Ruby version.
Eg.


Scenario: Some cukes
 Given I have 48 cukes in my belly


    1. Step definitions

Step definitions are written in Java and JUnit is used to run the steps. A simple example for the previously shown feature file.


@Given("I have (\\d+) cukes in my belly")public void I_have_cukes_in_my_belly(int cukes) {
 
  // Do something with the cukes
}



  1. References

1. The Cucumber Book: Behaviour-Driven Development for Testers and Developers by Matt Wynne and Aslak Hellesøy
2. Cucumber Recipes: Automate Anything with BDD Tools and Techniques by Ian Dees, Matt Wynne, Aslak Hellesoy
3. Aslakhellesoy.com. Retrieved 2013-05-24.