Dienstag, 17. Juni 2014

Generating JAXB Beans from XSD with Maven

The following example shows a simple maven plugin configuration to generate Java beans from a XSD. I use the “maven-jaxb22-plugin” because it allows a simple integration of plugins to adjust the Bean generation. Leave the following arguments if you don’t need the feature:
  • Xsetters - Generates setters to beans
  • XtoString - Generates common to string method to beans  
  • Xequals - Generates a equals method to beans  
  • XhashCode - Generates a hashcode method to beans 
  • Xfluent-api - Generates a fluent API to beans

1:  <plugin>  
2:   <groupId>org.jvnet.jaxb2.maven2</groupId>  
3:   <artifactId>maven-jaxb22-plugin</artifactId>  
4:   <executions>  
5:    <execution>  
6:     <id>Generating Asset Admin Api Beans</id>  
7:     <goals>  
8:      <goal>generate</goal>  
9:     </goals>  
10:     <configuration>  
11:      <encoding>UTF-8</encoding>  
12:      <forceRegenerate>true</forceRegenerate>  
13:      <schemaDirectory>${basedir}/src/main/resources/xsd/</schemaDirectory>  
14:      <generatePackage>de.lehavre.demo</generatePackage>  
15:      <schemaIncludes>  
16:       <include>model1.xsd</include>  
17:       <include>model2.xsd</include>  
18:      </schemaIncludes>  
19:      <extension>true</extension>  
20:      <args>  
21:       <arg>-Xsetters</arg>  
22:       <arg>-XtoString</arg>  
23:       <arg>-Xequals</arg>  
24:       <arg>-XhashCode</arg>  
25:       <arg>-Xfluent-api</arg>  
26:      </args>  
27:      <plugins>  
28:       <plugin>  
29:        <groupId>org.jvnet.jaxb2_commons</groupId>  
30:        <artifactId>jaxb2-basics</artifactId>  
31:       </plugin>  
32:       <plugin>  
33:        <groupId>net.java.dev.jaxb2-commons</groupId>  
34:        <artifactId>jaxb-fluent-api</artifactId>  
35:        <version>2.1.8</version>  
36:       </plugin>  
37:      </plugins>  
38:     </configuration>  
39:    </execution>  
40:   </executions>  
41:  </plugin>  

Generating Fluent Builder with IntelliJ

The JavaBean getter and setter usage is often a bit laborious. I like to use a fluent API, but I don't like to implement these methods direly in the bean class. There is a nice IntelliJ Plugin named "Builder-Genarer", which generates a Builder from POJO.

Just open class and press ALT-SHIFT-B

Plugin: http://plugins.jetbrains.com/plugin/6585

Mittwoch, 4. Juni 2014

Fast creation of test data

Do you know this problem? You spend too much time into developing unit tests, because you have to fill complex beans with test data? The PodamFactory offers a very easy way to create test data Pojos with reflection:

import de.havre.CpeBo;
import org.junit.Test;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

public class PojoTest

  CommitmentConfigStateHandler sut;

  PodamFactory factory = new PodamFactoryImpl();

  public void testProcessConfigState_success() throws Exception
    ComplexBean pojo = factory.manufacturePojo(ComplexBean.class);

Maven dependency: