Skip to content

Commit 28889cf

Browse files
committed
converted to gradle project, added unit tests
1 parent a159be5 commit 28889cf

21 files changed

+522
-26
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
build
2+
*.iml
3+
*.ipr
4+
*.iws
5+
.gradle
6+
.idea
7+
out

build.gradle

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
group = "net.codebox"
2+
version = "1.0.0"
3+
4+
apply plugin: 'java'
5+
apply plugin: 'maven'
6+
apply plugin: 'signing'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
compile group: 'junit', name: 'junit', version: '4.11'
14+
15+
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
16+
}
17+
18+
signing {
19+
sign configurations.archives
20+
}
21+
22+
task javadocJar(type: Jar) {
23+
classifier = 'javadoc'
24+
from javadoc
25+
}
26+
27+
task sourcesJar(type: Jar) {
28+
classifier = 'sources'
29+
from sourceSets.main.allSource
30+
}
31+
32+
artifacts {
33+
archives javadocJar, sourcesJar
34+
}
35+
36+
uploadArchives {
37+
repositories {
38+
mavenDeployer {
39+
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
40+
41+
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2") {
42+
authentication(userName: ossrhUsername, password: ossrhPassword)
43+
}
44+
45+
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots") {
46+
authentication(userName: ossrhUsername, password: ossrhPassword)
47+
}
48+
49+
pom.project {
50+
name 'JavaBeanTester'
51+
packaging 'jar'
52+
artifactId 'javabean-tester'
53+
description 'Library for automatically unit-testing Java Bean classes using reflection'
54+
url 'https://github.com/codebox/javabean-tester'
55+
56+
scm {
57+
connection 'scm:[email protected]/codebox/javabean-tester.git'
58+
developerConnection 'scm:[email protected]/codebox/javabean-tester.git'
59+
url 'https://github.com/codebox/javabean-tester'
60+
}
61+
62+
licenses {
63+
license {
64+
name 'MIT License'
65+
url 'https://opensource.org/licenses/MIT'
66+
}
67+
}
68+
69+
developers {
70+
developer {
71+
id 'codebox'
72+
name 'Rob Dawson'
73+
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}

JavaBeanTester.java src/main/java/net/codebox/javabeantester/JavaBeanTester.java

+3-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package net.codebox.javabeantester;
2+
13
import java.beans.IntrospectionException;
24
import java.beans.Introspector;
35
import java.beans.PropertyDescriptor;
@@ -32,7 +34,7 @@ public static <T> void test(final Class<T> clazz, final String... skipThese) thr
3234
continue nextProp;
3335
}
3436
}
35-
findBooleanIsMethods(clazz, prop);
37+
3638
final Method getter = prop.getReadMethod();
3739
final Method setter = prop.getWriteMethod();
3840

@@ -128,30 +130,5 @@ private static Object buildValue(Class<?> clazz) throws InstantiationException,
128130
}
129131
}
130132

131-
/**
132-
* Hunt down missing Boolean read method if needed as Introspector cannot find 'is' getters
133-
* for Boolean type properties.
134-
*
135-
* @param clazz
136-
* the type being introspected
137-
* @param descriptor
138-
* the property descriptor found so far
139-
*/
140-
public static <T> void findBooleanIsMethods(Class<T> clazz, PropertyDescriptor descriptor) throws IntrospectionException {
141-
if ( needToFindReadMethod(descriptor) ) {
142-
findTheReadMethod(descriptor, clazz);
143-
}
144-
}
145-
146-
private static boolean needToFindReadMethod(PropertyDescriptor property) {
147-
return property.getReadMethod() == null && property.getPropertyType() == Boolean.class;
148-
}
149-
150-
private static <T> void findTheReadMethod(PropertyDescriptor descriptor, Class<T> clazz) {
151-
try {
152-
PropertyDescriptor pd = new PropertyDescriptor(descriptor.getName(), clazz);
153-
descriptor.setReadMethod(pd.getReadMethod());
154-
} catch (IntrospectionException e) {}
155-
}
156133
}
157134

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package net.codebox.javabeantester;
2+
3+
import net.codebox.javabeantester.beans.*;
4+
import org.junit.Test;
5+
6+
import java.beans.IntrospectionException;
7+
8+
/**
9+
* Created by rob on 13/08/2017.
10+
*/
11+
public class TestJavaBeanTester {
12+
@Test
13+
public void validBeanPasses() throws IntrospectionException {
14+
JavaBeanTester.test(Bean.class);
15+
}
16+
17+
@Test(expected=AssertionError.class)
18+
public void invalidStringAccessorsFail() throws IntrospectionException {
19+
JavaBeanTester.test(BrokenBeanString.class);
20+
}
21+
22+
@Test(expected=AssertionError.class)
23+
public void invalidIntAccessorsFail() throws IntrospectionException {
24+
JavaBeanTester.test(BrokenBeanInt.class);
25+
}
26+
27+
@Test(expected=AssertionError.class)
28+
public void invalidIntWrapperAccessorsFail() throws IntrospectionException {
29+
JavaBeanTester.test(BrokenBeanInt.class);
30+
}
31+
32+
@Test(expected=AssertionError.class)
33+
public void invalidArrayAccessorsFail() throws IntrospectionException {
34+
JavaBeanTester.test(BrokenBeanArray.class);
35+
}
36+
37+
@Test(expected=AssertionError.class)
38+
public void invalidBooleanAccessorsFail() throws IntrospectionException {
39+
JavaBeanTester.test(BrokenBeanBoolean.class);
40+
}
41+
42+
@Test(expected=AssertionError.class)
43+
public void invalidBooleanWrapperAccessorsFail() throws IntrospectionException {
44+
JavaBeanTester.test(BrokenBeanBooleanWrapper.class);
45+
}
46+
47+
@Test(expected=AssertionError.class)
48+
public void invalidLongAccessorsFail() throws IntrospectionException {
49+
JavaBeanTester.test(BrokenBeanLong.class);
50+
}
51+
52+
@Test(expected=AssertionError.class)
53+
public void invalidLongWrapperAccessorsFail() throws IntrospectionException {
54+
JavaBeanTester.test(BrokenBeanLongWrapper.class);
55+
}
56+
57+
@Test(expected=AssertionError.class)
58+
public void invalidDoubleAccessorsFail() throws IntrospectionException {
59+
JavaBeanTester.test(BrokenBeanDouble.class);
60+
}
61+
62+
@Test(expected=AssertionError.class)
63+
public void invalidDoubleWrapperAccessorsFail() throws IntrospectionException {
64+
JavaBeanTester.test(BrokenBeanDoubleWrapper.class);
65+
}
66+
67+
@Test(expected=AssertionError.class)
68+
public void invalidFloatAccessorsFail() throws IntrospectionException {
69+
JavaBeanTester.test(BrokenBeanFloat.class);
70+
}
71+
72+
@Test(expected=AssertionError.class)
73+
public void invalidFloatWrapperAccessorsFail() throws IntrospectionException {
74+
JavaBeanTester.test(BrokenBeanFloatWrapper.class);
75+
}
76+
77+
@Test(expected=AssertionError.class)
78+
public void invalidCharAccessorsFail() throws IntrospectionException {
79+
JavaBeanTester.test(BrokenBeanChar.class);
80+
}
81+
82+
@Test(expected=AssertionError.class)
83+
public void invalidCharWrapperAccessorsFail() throws IntrospectionException {
84+
JavaBeanTester.test(BrokenBeanCharWrapper.class);
85+
}
86+
87+
@Test(expected=AssertionError.class)
88+
public void invalidEnumAccessorsFail() throws IntrospectionException {
89+
JavaBeanTester.test(BrokenBeanEnum.class);
90+
}
91+
92+
@Test(expected=AssertionError.class)
93+
public void invalidNoArgConstructorObjectAccessorsFail() throws IntrospectionException {
94+
JavaBeanTester.test(BrokenBeanNoArgConstructorObject.class);
95+
}
96+
97+
@Test
98+
public void skippingPropertiesWorksCorrectly() throws IntrospectionException {
99+
JavaBeanTester.test(BrokenBeanString.class, "stringValue");
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package net.codebox.javabeantester.beans;
2+
3+
import java.lang.annotation.RetentionPolicy;
4+
import java.util.Date;
5+
6+
/**
7+
* Created by rob on 13/08/2017.
8+
*/
9+
public class Bean {
10+
private String stringValue;
11+
private Object[] arrayValue;
12+
private boolean booleanValue;
13+
private Boolean booleanWrapperValue;
14+
private int intValue;
15+
private Integer intWrapperValue;
16+
private long longValue;
17+
private Long longWrapperValue;
18+
private double doubleValue;
19+
private Double doubleWrapperValue;
20+
private float floatValue;
21+
private Float floatWrapperValue;
22+
private char charValue;
23+
private Character charWrapperValue;
24+
private RetentionPolicy enumValue;
25+
private Date noArgConstructorObjectValue;
26+
27+
public String getStringValue() {
28+
return stringValue;
29+
}
30+
31+
public void setStringValue(String stringValue) {
32+
this.stringValue = stringValue;
33+
}
34+
35+
public Object[] getArrayValue() {
36+
return arrayValue;
37+
}
38+
39+
public void setArrayValue(Object[] arrayValue) {
40+
this.arrayValue = arrayValue;
41+
}
42+
43+
public boolean isBooleanValue() {
44+
return booleanValue;
45+
}
46+
47+
public void setBooleanValue(boolean booleanValue) {
48+
this.booleanValue = booleanValue;
49+
}
50+
51+
public Boolean getBooleanWrapperValue() {
52+
return booleanWrapperValue;
53+
}
54+
55+
public void setBooleanWrapperValue(Boolean booleanWrapperValue) {
56+
this.booleanWrapperValue = booleanWrapperValue;
57+
}
58+
59+
public int getIntValue() {
60+
return intValue;
61+
}
62+
63+
public void setIntValue(int intValue) {
64+
this.intValue = intValue;
65+
}
66+
67+
public Integer getIntWrapperValue() {
68+
return intWrapperValue;
69+
}
70+
71+
public void setIntWrapperValue(Integer intWrapperValue) {
72+
this.intWrapperValue = intWrapperValue;
73+
}
74+
75+
public long getLongValue() {
76+
return longValue;
77+
}
78+
79+
public void setLongValue(long longValue) {
80+
this.longValue = longValue;
81+
}
82+
83+
public Long getLongWrapperValue() {
84+
return longWrapperValue;
85+
}
86+
87+
public void setLongWrapperValue(Long longWrapperValue) {
88+
this.longWrapperValue = longWrapperValue;
89+
}
90+
91+
public double getDoubleValue() {
92+
return doubleValue;
93+
}
94+
95+
public void setDoubleValue(double doubleValue) {
96+
this.doubleValue = doubleValue;
97+
}
98+
99+
public Double getDoubleWrapperValue() {
100+
return doubleWrapperValue;
101+
}
102+
103+
public void setDoubleWrapperValue(Double doubleWrapperValue) {
104+
this.doubleWrapperValue = doubleWrapperValue;
105+
}
106+
107+
public float getFloatValue() {
108+
return floatValue;
109+
}
110+
111+
public void setFloatValue(float floatValue) {
112+
this.floatValue = floatValue;
113+
}
114+
115+
public Float getFloatWrapperValue() {
116+
return floatWrapperValue;
117+
}
118+
119+
public void setFloatWrapperValue(Float floatWrapperValue) {
120+
this.floatWrapperValue = floatWrapperValue;
121+
}
122+
123+
public char getCharValue() {
124+
return charValue;
125+
}
126+
127+
public void setCharValue(char charValue) {
128+
this.charValue = charValue;
129+
}
130+
131+
public Character getCharWrapperValue() {
132+
return charWrapperValue;
133+
}
134+
135+
public void setCharWrapperValue(Character charWrapperValue) {
136+
this.charWrapperValue = charWrapperValue;
137+
}
138+
139+
public RetentionPolicy getEnumValue() {
140+
return enumValue;
141+
}
142+
143+
public void setEnumValue(RetentionPolicy enumValue) {
144+
this.enumValue = enumValue;
145+
}
146+
147+
public Date getNoArgConstructorObjectValue() {
148+
return noArgConstructorObjectValue;
149+
}
150+
151+
public void setNoArgConstructorObjectValue(Date noArgConstructorObjectValue) {
152+
this.noArgConstructorObjectValue = noArgConstructorObjectValue;
153+
}
154+
}

0 commit comments

Comments
 (0)