1 package org.pojomatic.internal.factory;
2
3 import static org.testng.Assert.*;
4
5 import org.testng.annotations.Test;
6
7 import java.lang.reflect.Field;
8
9 import org.pojomatic.annotations.Property;
10
11 public class PojoFactoryTest {
12 @Test
13 public void testFactory() throws Exception {
14 PojoFactory pojoFactory = new PojoFactory(new PojoDescriptor(new PropertyDescriptor(int.class)));
15 Object pojo = pojoFactory.create().with("x", 4).pojo();
16 Field field = pojo.getClass().getDeclaredField("x");
17 field.setAccessible(true);
18 assertEquals(field.get(pojo), (Object) 4);
19 assertTrue(field.isAnnotationPresent(Property.class));
20
21 assertEquals(pojoFactory.pojomator().doHashCode(pojo), 31 + 4);
22 }
23
24 @Test
25 public void testParent() throws Exception {
26 PojoDescriptor parent = new PojoDescriptor(new PropertyDescriptor(String.class));
27 PojoClassFactory pojoClassFactory = new PojoClassFactory();
28 pojoClassFactory.generateClass(parent);
29 PojoFactory pojoFactory = new PojoFactory(
30 pojoClassFactory,
31 new PojoDescriptor(
32 "foo",
33 "bar",
34 Access.PUBLIC,
35 parent,
36 new PropertyDescriptor(int.class)));
37 Object pojo = pojoFactory.create().with("x", 4).withParent("x", "foo").pojo();
38 assertEquals(pojoFactory.pojomator().doToString(pojo), "bar{x: {foo}, x: {4}}");
39 }
40 }