Rechercher dans ce blog

Affichage des articles dont le libellé est maven. Afficher tous les articles
Affichage des articles dont le libellé est maven. Afficher tous les articles

dimanche 29 avril 2012

9. Unit-test and EasyMock

Now that the first functions have been implemented. It is key to verify they are working properly.

In this post, I will describe one unit test and explain how to use EasyMock to make sure that the functions are called the appropriate number of times and that the arguments are the one which are expected.

Maven

Let's start to include EasyMock and Junit in the maven configuration. In the project pom.xml, we have to add the following lines:
  
37       <dependencies> 
38           <dependency> 
39               <groupId>junit</groupId> 
40               <artifactId>junit</artifactId> 
41               <version>4.8.2</version> 
42           </dependency> 
43           <dependency> 
44               <groupId>org.easymock</groupId> 
45               <artifactId>easymock</artifactId> 
46               <version>2.0</version> 
47           </dependency> 
48       </dependencies>

Now that it is done, we can use junit and easy mock in our modules.

Unit test sample

Here is a simple example showing how to verify that the YoctoHub object successfully refresh its content.
 
39    
40       private YoctoTemplate yoctoTemplate; 
41    
42       @Before 
43       public void setUp() throws IOException { 
44           yoctoTemplate = EasyMock.createMock(YoctoTemplate.class); 
45    
46    
47           YoctoMap content = RestYoctoMock.getMap(RestYoctoMock.MAIN_JSON); 
48           EasyMock.expect(yoctoTemplate.query("/api.json")).andReturn( 
49                   content).once(); 
50    
51           content = RestYoctoMock.getMap(RestYoctoMock.COLOR_JSON); 
52           EasyMock.expect(yoctoTemplate.query("/bySerial/YRGBLED1-01934/api.json")).andReturn( 
53                   content).anyTimes(); 
54    
55    
56       } 
57    
58    
59       @Test 
60       public void testRefresh() throws Exception { 
61    
62           EasyMock.replay(yoctoTemplate); 
63           YoctoHub hub = new YoctoHub(yoctoTemplate); 
64           hub.refresh(); 
65           hub.findAll(); 
66    
67           Collection<YoctoObject> list = hub.findAll(YoctoProduct.YOCTO_METEO); 
68           for (YoctoObject object : list) 
69               System.out.println(object); 
70    
71           EasyMock.verify(yoctoTemplate); 
72       }

First, let me describe the overall structure of the junit tests:

  • At line 42, @Before means that this function must be called before each test to make sure tho initialize the objects. There is usually a misconception that it is called only once.
  • At line 59, @Test informs the junit framework that this function must be tested. Please note that we can add parameters to the function like expectation of exception.


In this example, we introduce a few items:
  • At line 44, we initialize a mock object: that means it is an object that will share the same API but that will return the results we expect
  • At line 48: we declare that the function query with parameter "/api.json" will be called exactly once and that the return of this function will be a constant.
  • At line 62, we inform the mock object that it must be ready to be called
  • At line 71, we verify that all the objects have been called the expected number of times
What are the advantages of EasyMock against a built-in mockup object. Well, they are numerous:
  • Only the expected function are mocked up, you do not end up with thousands of empty functions that are not needed for the test
  • You can specify the exact number of times a function will be called
  • You can reuse the mockup at many place and only need to called replay to reset the object.
  • The errors are pretty well described and it is easy to understand the issue. 
As we have already configured jenkins, we should already be able to see the results of the tests in an automated way:


samedi 28 avril 2012

6. Code coverage

Maven post was not containing the full configuration. For simplicity sake, I deliberately removed the code coverage section.

Now that jenkins and maven are configured, we can now take some time to configure the code coverage. For this part, I use Cobertura because it runs under Jenkins and it is easy to use...

To configure it, there are two things to do:


  1. Configure the Maven configuration 
  2. Configure Jenkins to support Cobertura

Configure Maven

To configure maven, we have to edit the project pom.xml to set that cobertura must be run. We will run cobertura during the package goal:

 
49    
50    
51       <profiles> 
52           <!-- Jenkins by default defines a property BUILD_NUMBER which is used to enable the profile. --> 
53           <profile> 
54               <id>jenkins</id> 
55               <activation> 
56                   <property> 
57                       <name>env.BUILD_NUMBER</name> 
58                   </property> 
59               </activation> 
60               <build> 
61                   <pluginManagement> 
62                       <plugins> 
63                           <plugin> 
64                               <groupId>org.codehaus.mojo</groupId> 
65                               <artifactId>cobertura-maven-plugin</artifactId> 
66                               <version>2.5.1</version> 
67                               <configuration> 
68                                   <formats> 
69                                       <format>xml</format> 
70                                   </formats> 
71                               </configuration> 
72                               <executions> 
73                                   <execution> 
74                                       <phase>package</phase> 
75                                       <goals> 
76                                           <goal>cobertura</goal> 
77                                       </goals> 
78                                   </execution> 
79                               </executions> 
80    
81                           </plugin> 
82                       </plugins> 
83                   </pluginManagement> 
84    
85                   <plugins> 
86                       <plugin> 
87                           <groupId>org.codehaus.mojo</groupId> 
88                           <artifactId>findbugs-maven-plugin</artifactId> 
89                           <version>2.4.0</version> 
90                           <executions> 
91                               <execution> 
92                                   <phase>package</phase> 
93                                   <goals> 
94                                       <goal>findbugs</goal> 
95                                   </goals> 
96                               </execution> 
97                           </executions> 
98                       </plugin> 
99                   </plugins> 
100              </build> 
101          </profile> 
102      </profiles>

A few explanations are needed:

  • We set here a profile: that means this goal is run only when a particular "user" is running the target. It is good in this case because cobertura will not be run in developer's environment but only by jenkins
  • The plugin is specified to run during the package phase

Configure Jenkins

Now that maven is configured, we can configure Jenkins to use cobertura and this is quite easy:

  1. Install the Cobertura plugin: Jenkins Cobertura plugin. It will be installed with a couple of dependencies.
  2. Go in the project settings and set that you want to run the following goals:
    • package: to run cobertura
    • cobertura:cobertura: to build the result
  3. Go in the project settings and set that you want to display the coverage report. You are asked for a report pattern: set: **/target/site/cobertura/coverage.xml. This will inform cobertura where the pattern are stored and that they are in xml (exactly what we set in the pom.xml.
Now you can run a build and you will see the code coverage of the project.

lundi 23 avril 2012

3. Maven

"Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information."
http://maven.apache.org/

The purpose of maven in this project will be to create builds, unit tests and be able to manage through a few XML scripts. In this project, we will have three configuration file:

  • yocto-api: managing the different modules and the whole build
  • yocto-common: will provide all the objects to manage yoctopuce devices. It does not contain the communication piece (as we will create one module as standalone jar and the other as a GWT module).
  • yocto-standalone: will contain the communication layer in case we run in standard JRE.

Yocto-api

The first step is to create the root configuration. This configuration will contain the description of the project and the different modules and dependencies which are needed.
To create a project, the command mvn archetype:generate and we have to specify a few items: groupId,artifactId,version, etc. But for simplicity, I decided that Intellij Idea would create it for me. So I create a new project with Intellij Idea and selected that I wanted a Maven project. And that's all! My project was created,I started to make a first few changes and I obtained such pom.xml file:

1    <?xml version="1.0" encoding="UTF-8"?> 
2     
3    <project xmlns="http://maven.apache.org/POM/4.0.0" 
4             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
6             http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
7        <modelVersion>4.0.0</modelVersion> 
8     
9        <properties> 
10           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
11       </properties> 
12    
13       <groupId>yocto-api</groupId> 
14       <artifactId>yocto-api</artifactId> 
15       <packaging>pom</packaging> 
16       <version>1.0</version> 
17       <modules> 
18           <module>yocto-standalone</module> 
19           <module>yocto-common</module> 
20       </modules> 
21    
22       <dependencies> 
23           <dependency> 
24               <groupId>junit</groupId> 
25               <artifactId>junit</artifactId> 
26               <version>4.8.2</version> 
27           </dependency> 
28           <dependency> 
29               <groupId>org.easymock</groupId> 
30               <artifactId>easymock</artifactId> 
31               <version>2.0</version> 
32           </dependency> 
33       </dependencies>
Let me describe a bit some items here:
  • The project has been called: yocto-api:yocto-api. The name is silly but I am not very imaginative. The version is 1.0.
  • The project has two modules: yocto-standalone and yocto-common. I will explain them later. But
  • I have added a few dependencies to the project: these are libraries that are used by the source code. Junit and easyMock are used for unit tests. This will be described in a future post. The good point is that only setting this is needed to use them. the Jars will be downloaded automatically by maven on http://repo.maven.apache.org/. You can get the list here as well: http://mvnrepository.com/
Now, let's describe one module: yocto-standalone. Yocto-common is straightforward...

Yocto-standalone

As explained before, yocto-standalone will only provide the communication layer to communicate with the yoctopuce devices.

1    <?xml version="1.0" encoding="UTF-8"?> 
2    <project xmlns="http://maven.apache.org/POM/4.0.0" 
3             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
4             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
5        <parent> 
6            <artifactId>yocto-api</artifactId> 
7            <groupId>yocto-api</groupId> 
8            <version>1.0</version> 
9        </parent> 
10       <modelVersion>4.0.0</modelVersion> 
11    
12       <artifactId>yocto-standalone</artifactId> 
13    
14       <dependencies> 
15           <dependency> 
16               <groupId>yocto-api</groupId> 
17               <artifactId>yocto-common</artifactId> 
18               <version>1.0</version> 
19           </dependency> 
20           <dependency> 
21               <groupId>org.codehaus.jackson</groupId> 
22               <artifactId>jackson-mapper-asl</artifactId> 
23               <version>1.8.5</version> 
24           </dependency> 
25    
26       </dependencies>

We can see that the code is very similar to the main pom.xml file except the dependencies because this module requires:
  • yocto-common as yocto-common contains all the classes needed for the standalone version
  • jackson to manage the json objects returned by the yoctopuce devices.
This is only what needed to be done. we can now run a few goals using the maven command: compile,clean,test,etc.

jeudi 12 avril 2012

1. Introduction

When I look for projects, I usually found some code samples on a particular technology but it is quite rare to find:
  • How to install the test framework
  • How to put the project in production
  • How to use the VCS
The goal of this blog is to introduce a small project and explain the different steps of development. I will not go in details and will only provide a small overview of the project.

Overview

The goal of the project is to provide a map where you can see all the yoctopuce devices from the people who have come on the page. Yoctopuce provide a meteo sensor device that can be plugged through USB. In this project, we would like to have the following flow:


  1. An user goes on the web page with his browser
  2. The web page is loaded and tries to contact known yoctopuce device URL: http://127.0.0.1:4444
  3. If it is successful
    1. The Web page analyzes which yoctopuce devices are plugged
    2. The Web page stores the data in the database with the current location
  4. The webpage loads and displays the world map with all the yoctopuce devices stored in the database




In this blog, I will describe:
  1. Prepare GIT to store the source code
  2. Configure the maven project
  3. Configure and install a virtual appliance with Jenkins
  4. Run a code coverage with Cobertura in Jenkins
  5. Use yoctopuce devices
  6. Basic introduction of java API for yoctopuce
  7. GWT introduction
  8. Put in production with google app engine