View Javadoc
1   package org.pojomatic.internal;
2   
3   import static org.testng.Assert.*;
4   
5   import org.testng.annotations.Test;
6   
7   import java.lang.annotation.Retention;
8   import java.lang.annotation.RetentionPolicy;
9   import java.lang.reflect.Field;
10  import java.lang.reflect.Method;
11  
12  public class PropertyElementTest {
13  
14    @Retention(RetentionPolicy.RUNTIME) @interface Expected {
15      String value();
16    }
17  
18    @Test
19    public void testGetDeclaringClass() throws Exception {
20      assertEquals(new PropertyAccessor(getTestMethod(), "").getDeclaringClass(), PropertyElementTest.class);
21      assertEquals(new PropertyField(getTestField(), "").getDeclaringClass(), PropertyElementTest.class);
22    }
23  
24    @Test
25    public void testEquals() throws Exception {
26      PropertyAccessor testMethodProperty = new PropertyAccessor(getTestMethod(), "");
27      assertEquals(testMethodProperty, testMethodProperty);
28      assertFalse(testMethodProperty.equals(null));
29      assertFalse(testMethodProperty.equals("someOtherClass"));
30      assertEquals(new PropertyAccessor(getTestMethod(), ""), testMethodProperty);
31      final PropertyField testFieldProperty = new PropertyField(getTestField(), "");
32      assertFalse(testMethodProperty.equals(testFieldProperty));
33      assertFalse(testFieldProperty.equals(testMethodProperty));
34    }
35  
36    @Test
37    public void testToString() throws Exception {
38      assertEquals(new PropertyAccessor(getTestMethod(), "").toString(), getTestMethod().toString());
39    }
40  
41    @Test
42    public void testMethodHashCode() throws Exception {
43      assertEquals(new PropertyAccessor(getTestMethod(), "salt").hashCode(), getTestMethod().hashCode());
44    }
45  
46    @Test
47    public void testFieldHashCode() throws Exception {
48      assertEquals(new PropertyField(getTestField(), "salt").hashCode(), getTestField().hashCode());
49    }
50  
51    @Test
52    public void testGetNameForField() throws Exception {
53      assertEquals(new PropertyField(getTestField(), "").getName(), "testField");
54      assertEquals(new PropertyField(getTestField(), "foo").getName(), "foo");
55    }
56  
57    @Test
58    public void testOverrideNameForAccessor() throws Exception {
59      assertEquals(new PropertyAccessor(getTestMethod(), "bar").getName(), "bar");
60    }
61  
62    @Test
63    public void testGetNameForAccessor() throws Exception {
64      for (Method method: getClass().getDeclaredMethods()) {
65        Expected expected = method.getAnnotation(Expected.class);
66        if (expected != null) {
67          assertEquals(new PropertyAccessor(method, "").getName(), expected.value(), "name for method " + method.getName());
68        }
69      }
70    }
71  
72    // methods tested in testGetNameForAccessor
73    @Expected("foo") public Object getFoo() { return null; }
74    @Expected("URL") public Object getURL() { return null; }
75    @Expected("get") public Object get() { return null; }
76    @Expected("getter") public Object getter() { return null; }
77    @Expected("isString") public Object isString() { return null; }
78    @Expected("boolean") public Boolean isBoolean() { return null; }
79    @Expected("bool") public boolean isBool() { return false; }
80    @Expected("is") public boolean is() { return false; }
81  
82    private Method getTestMethod() throws Exception {
83      return getMethod("testAccessor");
84    }
85  
86    private Method getMethod(String name) throws Exception {
87      return this.getClass().getDeclaredMethod(name, (Class[])null);
88    }
89  
90    private Field getTestField() throws Exception {
91      return getClass().getDeclaredField("testField");
92    }
93  
94    // test properties
95    @SuppressWarnings("unused")
96    private Object testAccessor() { return "Test string"; }
97    @SuppressWarnings("unused")
98    private final Object testField = new Object();
99  }