/
PropertyFromMethodTest.java
106 lines (92 loc) · 3.4 KB
/
PropertyFromMethodTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.jboss.weld.extensions.test.properties;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method;
import java.net.URL;
import org.jboss.weld.extensions.properties.Properties;
import org.jboss.weld.extensions.properties.Property;
import org.junit.Test;
/**
* Verify that only valid properties are permitted, as per the JavaBean specification.
*
* @author Dan Allen
* @see http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html
*/
public class PropertyFromMethodTest
{
@Test
public void testValidPropertyGetterMethod() throws Exception
{
Method getter = ClassToIntrospect.class.getMethod("getName");
Property<String> p = Properties.createProperty(getter);
assertNotNull(p);
assertEquals("name", p.getName());
assertEquals(getter, p.getMember());
}
@Test
public void testValidPropertySetterMethod() throws Exception
{
Property<String> p = Properties.createProperty(ClassToIntrospect.class.getMethod("setName", String.class));
assertNotNull(p);
assertEquals("name", p.getName());
}
@Test
public void testReadOnlyProperty() throws Exception
{
Property<String> p = Properties.createProperty(ClassToIntrospect.class.getMethod("getTitle"));
assertNotNull(p);
assertEquals("title", p.getName());
assertTrue(p.isReadOnly());
}
@Test(expected = IllegalArgumentException.class)
public void testEmptyPropertyGetterMethod() throws Exception
{
Properties.createProperty(ClassToIntrospect.class.getMethod("get"));
}
@Test(expected = IllegalArgumentException.class)
public void testEmptyBooleanPropertyGetterMethod() throws Exception
{
Properties.createProperty(ClassToIntrospect.class.getMethod("is"));
}
@Test(expected = IllegalArgumentException.class)
public void testNonPrimitiveBooleanPropertyIsMethod() throws Exception
{
Properties.createProperty(ClassToIntrospect.class.getMethod("isValid"));
}
@Test
public void testSingleCharPropertyGetterMethod() throws Exception
{
Method getter = ClassToIntrospect.class.getMethod("getP");
Property<String> p = Properties.createProperty(getter);
assertNotNull(p);
assertEquals("p", p.getName());
assertEquals(getter, p.getMember());
}
@Test
public void testSingleCharPropertySetterMethod() throws Exception
{
Property<String> p = Properties.createProperty(ClassToIntrospect.class.getMethod("setP", String.class));
assertNotNull(p);
assertEquals("p", p.getName());
}
@Test(expected = IllegalArgumentException.class)
public void testGetterMethodWithVoidReturnType() throws Exception
{
Properties.createProperty(ClassToIntrospect.class.getMethod("getFooBar"));
}
@Test(expected = IllegalArgumentException.class)
public void testSetterMethodWithMultipleParameters() throws Exception
{
Properties.createProperty(ClassToIntrospect.class.getMethod("setSalary", Double.class, Double.class));
}
@Test
public void testAcronymProperty() throws Exception
{
Method getter = ClassToIntrospect.class.getMethod("getURL");
Property<URL> p = Properties.createProperty(getter);
assertNotNull(p);
assertEquals("URL", p.getName());
assertEquals(getter, p.getMember());
}
}