View Javadoc
1   package org.pojomatic.formatter;
2   
3   import org.testng.annotations.Test;
4   import org.testng.annotations.BeforeMethod;
5   import static org.testng.Assert.*;
6   import org.pojomatic.PropertyElement;
7   import org.pojomatic.TestUtils;
8   
9   public class DefaultEnhancedPojoFormatterTest {
10  
11    private DefaultEnhancedPojoFormatter formatter;
12    private StringBuilder builder;
13  
14    private final static class Foo {
15      @SuppressWarnings("unused")
16      private String firstName, lastName, age;
17    }
18  
19    private final static PropertyElement FIRST_NAME_FIELD, LAST_NAME_FIELD, AGE_FIELD;
20    static {
21      try {
22        FIRST_NAME_FIELD = TestUtils.field(Foo.class, "firstName");
23        LAST_NAME_FIELD = TestUtils.field(Foo.class, "lastName");
24        AGE_FIELD = TestUtils.field(Foo.class, "age");
25      }
26      catch (Exception e) {
27        throw new RuntimeException(e);
28      }
29    }
30  
31    @BeforeMethod
32    public void setUp() {
33      formatter = new DefaultEnhancedPojoFormatter();
34      builder = new StringBuilder();
35    }
36  
37    @Test
38    public void testGetPropertyPrefix() {
39      formatter.appendPropertyPrefix(builder, FIRST_NAME_FIELD);
40      builder.append('|');
41      formatter.appendPropertyPrefix(builder, LAST_NAME_FIELD);
42      builder.append('|');
43      formatter.appendPropertyPrefix(builder, AGE_FIELD);
44  
45      assertFormatted("firstName: {|, lastName: {|, age: {");
46    }
47  
48    @Test
49    public void testGetPropertySuffix() {
50      formatter.appendPropertySuffix(builder, FIRST_NAME_FIELD);
51      builder.append('|');
52      formatter.appendPropertySuffix(builder, LAST_NAME_FIELD);
53      builder.append('|');
54      formatter.appendPropertySuffix(builder, AGE_FIELD);
55      assertFormatted("}|}|}");
56    }
57  
58    @Test
59    public void testGetToStringPrefix() {
60      formatter.appendToStringPrefix(builder, Integer.class);
61      assertFormatted("Integer{");
62    }
63  
64    @Test
65    public void testGetToStringSuffix() {
66      formatter.appendToStringSuffix(builder, Integer.class);
67      assertFormatted("}");
68    }
69  
70    private void assertFormatted(String expected) {
71      assertEquals(builder.toString(), expected);
72    }
73  }