I finally completed the second release of PODAM, which is now in version 2.0.0.RELEASE.
In this post I want to show you how to use PODAM to help your daily productivity. PODAM is set to cover the majority of developers' needs when it comes to automatically populate POJOs and JavaBeans with data. The tool allows also users to provide their own strategy to fill their objects.
The most basic use of PODAM is the following:
//This will use the default Random Data Provider Strategy
PodamFactory factory = new PodamFactoryImpl();
Pojo myPojo = factory.manufacturePojo(Pojo.class);
If you want to provide your own Data Provider Strategy, you can use the following syntax:
DataProviderStrategy strategy = new MyDataProviderStrategy();
PodamFactory factory = new PodamFactoryImpl(strategy);
Pojo myPojo = factory.manufacturePojo(Pojo.class);
PODAM can also be easily used with the Spring framework:
# Define your Spring app context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- The definition of the default strategy -->
<bean id="randomDataProviderStrategy"
factory-method="getInstance" />
<!-- The definition of PODAM factory -->
<bean id="podamFactory">
<constructor-arg index="0" ref="randomDataProviderStrategy" />
</bean>
</beans>
# Use Podam Factory in your code
/**
*
*/
package uk.co.jemos.podam.test.unit.integration;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.test.dto.SimplePojoToTestSetters;
/**
* @author mtedone
*
*/
@ContextConfiguration(locations = { "classpath:podam-test-appContext.xml" })
public class PodamFactoryInjectionIntegrationTest extends
AbstractJUnit4SpringContextTests {
/** The Podam Factory */
@Autowired
private PodamFactory factory;
@Before
public void init() {
Assert.assertNotNull("The PODAM factory cannot be null!", factory);
Assert.assertNotNull("The factory strategy cannot be null!",
factory.getStrategy());
}
@Test
public void testSimplePojo() {
SimplePojoToTestSetters pojo = factory
.manufacturePojo(SimplePojoToTestSetters.class);
Assert.assertNotNull("The pojo cannot be null!", pojo);
...etc
}
}
PODAM uses introspection to figure out which data to fill in a POJO / JavaBean. It supports the following configurations:
- Simple, one dimensional POJOs / JavaBeans
- Complex graphs of POJOs / JavaBeans
- Immutable-like POJOs / JavaBeans (e.g. classes without setters and all final instance attributes)
- Singleton-like POJOs / JavaBeans (e.g. with a private constructor and a public static factory method, with or without arguments)
PODAM offers also custom annotations to customise numeric types and String values. Currently there is an annotation to customise each of the following types:
- byte / Byte
- char / Character
- short / Short
- int / Integer
- long / Long
- float / Float
- double / Double
- String
- Container-type attributes (such as Collections, Maps and Arrays)
Additionally PODAM offers an annotation (@PodamExclude) to exclude a particular attribute from being set and another one (@PodamConstructor) to indicate which constructor to use in an Immutable-like POJO scenario.
As for types, PODAM supports the following Java types:
- Primitive types
- Wrapper types
- Collections
- Maps
- Arrays
- Custom POJOs / JavaBeans
- Classes in the java / javax namespace which provide either a no-argument constructor or a static factory method, with or without arguments
PODAM supports also the following corner cases:
- Inheritance (POJOs / JavaBeans extending other classes, either abstract or not)
- Recursion. A POJO can declare an instance attribute of its type. Currently the nesting level is set to one, but one can change the constant and increase it
PODAM is open source and runs under the MIT license. The source code is available on GitHub and the full documentation is available on Jemos PODAM home page
I hope you will enjoy PODAM as much as I do. I know for sure that I will start using it at work from tomorrow, which is ultimately the reason why I wrote it.
Happy technology
Exactly what I was looking for. Nice work!
Thanks for sharing it :-)
Posted by: Marcus K. | Monday, 22 August 2011 at 13:57
link to full documentation is dead (http://www.jemos.eu/projects/podam/)
Posted by: xiorcal | Tuesday, 19 January 2016 at 15:37