Skip to content

Commit b5318fe

Browse files
author
Nicolai Parlog
committed
Improved tests for nested properties and wrote tests for property builders.
1 parent f65b7c0 commit b5318fe

11 files changed

+314
-69
lines changed

test/org/codefx/AllTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.codefx;
22

3-
import org.codefx.nesting.AllNestingTests;
3+
import org.codefx.nesting._AllNestingTests;
44
import org.junit.runner.RunWith;
55
import org.junit.runners.Suite;
66
import org.junit.runners.Suite.SuiteClasses;
@@ -10,7 +10,7 @@
1010
*/
1111
@RunWith(Suite.class)
1212
@SuiteClasses({
13-
AllNestingTests.class,
13+
_AllNestingTests.class,
1414
})
1515
public class AllTests {
1616
// no body needed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.codefx.nesting;
22

3-
import org.codefx.nesting.property.AllNestedPropertyTests;
3+
import org.codefx.nesting.property._AllNestedPropertyTests;
44
import org.junit.runner.RunWith;
55
import org.junit.runners.Suite;
66
import org.junit.runners.Suite.SuiteClasses;
@@ -10,10 +10,10 @@
1010
*/
1111
@RunWith(Suite.class)
1212
@SuiteClasses({
13-
AllNestedPropertyTests.class,
13+
_AllNestedPropertyTests.class,
1414
DeepNestingTest.class,
1515
ShallowNestingTest.class,
1616
})
17-
public class AllNestingTests {
17+
public class _AllNestingTests {
1818
// no body needed
1919
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.codefx.nesting.property;
2+
3+
import javafx.beans.property.IntegerProperty;
4+
import javafx.beans.property.SimpleIntegerProperty;
5+
6+
import org.codefx.nesting.Nesting;
7+
8+
/**
9+
* Abstract superclass to tests for {@link NestedObjectProperty NestedObjectProperties} which only leaves the creation
10+
* of the tested properties (by {@link #createNestedPropertyFromNesting(Nesting)}) to the subclasses.
11+
*/
12+
public abstract class AbstractNestedIntegerPropertyTest extends AbstractNestedPropertyTest<Number, IntegerProperty> {
13+
14+
/**
15+
* The next value returned by {@link #createNewValue()}.
16+
*/
17+
private int nextValue = 1;
18+
19+
@Override
20+
protected boolean allowsNullValues() {
21+
return false;
22+
}
23+
24+
@Override
25+
protected Number createNewValue() {
26+
return ++nextValue;
27+
}
28+
29+
@Override
30+
protected IntegerProperty createNewObservableWithValue(Number value) {
31+
return new SimpleIntegerProperty(value.intValue());
32+
}
33+
34+
@Override
35+
protected IntegerProperty createNewObservableWithSomeValue() {
36+
return createNewObservableWithValue(0);
37+
}
38+
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.codefx.nesting.property;
2+
3+
import javafx.beans.property.Property;
4+
import javafx.beans.property.SimpleObjectProperty;
5+
6+
import org.codefx.nesting.Nesting;
7+
import org.codefx.nesting.testhelper.SomeValue;
8+
9+
/**
10+
* Abstract superclass to tests for {@link NestedObjectProperty NestedObjectProperties} which only leaves the creation
11+
* of the tested properties (by {@link #createNestedPropertyFromNesting(Nesting)}) to the subclasses.
12+
*/
13+
public abstract class AbstractNestedObjectPropertyTest
14+
extends AbstractNestedPropertyTest<SomeValue, Property<SomeValue>> {
15+
16+
@Override
17+
protected final boolean allowsNullValues() {
18+
return true;
19+
}
20+
21+
@Override
22+
protected final SomeValue createNewValue() {
23+
return new SomeValue();
24+
}
25+
26+
@Override
27+
protected final Property<SomeValue> createNewObservableWithValue(SomeValue value) {
28+
return new SimpleObjectProperty<>(value);
29+
}
30+
31+
@Override
32+
protected final Property<SomeValue> createNewObservableWithSomeValue() {
33+
SomeValue someValue = new SomeValue();
34+
return createNewObservableWithValue(someValue);
35+
}
36+
37+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.codefx.nesting.property;
2+
3+
import static org.junit.Assert.assertNotSame;
4+
import static org.junit.Assert.assertSame;
5+
import javafx.beans.property.Property;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
/**
11+
* Abstract superclass to tests for nested property builders.
12+
*
13+
* @param <N>
14+
* the nesting hierarchy's innermost type of {@link Property}
15+
* @param <P>
16+
* the type of {@link Property} which will be built
17+
*/
18+
public abstract class AbstractNestedPropertyBuilderTest<N extends Property<?>, P extends NestedProperty<?>> {
19+
20+
// #region TESTED INSTANCES
21+
22+
/**
23+
* The tested builder.
24+
*/
25+
private AbstractNestedPropertyBuilder<N, P> builder;
26+
27+
//#end TESTED INSTANCES
28+
29+
/**
30+
* Creates a new builder before each test.
31+
*/
32+
@Before
33+
public void setUp() {
34+
builder = createBuilder();
35+
}
36+
37+
// #region TESTS
38+
39+
/**
40+
* Tests whether calling {@link AbstractNestedPropertyBuilder#setBean(Object)} sets the bean for the created nested
41+
* property.
42+
*/
43+
@Test
44+
public void testSetBean() {
45+
// set a bean on the builder and let it build the property
46+
Object bean = "Mr. Bean";
47+
builder.setBean(bean);
48+
P nestedProperty = builder.build();
49+
50+
assertSame(bean, nestedProperty.getBean());
51+
}
52+
53+
/**
54+
* Tests whether calling {@link AbstractNestedPropertyBuilder#setBean(Object)} with null causes a
55+
* {@link NullPointerException}.
56+
*/
57+
@Test(expected = NullPointerException.class)
58+
public void testSetBeanToNull() {
59+
builder.setBean(null);
60+
}
61+
62+
/**
63+
* Tests whether calling {@link AbstractNestedPropertyBuilder#setName(String)} sets the name for the created nested
64+
* property.
65+
*/
66+
@Test
67+
public void testSetName() {
68+
// set a name on the builder and let it build the property
69+
String name = "The Name";
70+
builder.setName(name);
71+
P nestedProperty = builder.build();
72+
73+
assertSame(name, nestedProperty.getName());
74+
}
75+
76+
/**
77+
* Tests whether calling {@link AbstractNestedPropertyBuilder#setName(String)} with null causes a
78+
* {@link NullPointerException}.
79+
*/
80+
@Test(expected = NullPointerException.class)
81+
public void testSetNameToNull() {
82+
builder.setName(null);
83+
}
84+
85+
/**
86+
* Tests whether repeatedly calling {@link AbstractNestedPropertyBuilder#build()} returns different instances of
87+
* nested properties.
88+
*/
89+
@Test
90+
public void testBuildCreatesNewInstances() {
91+
P firstNestedProperty = builder.build();
92+
P secondNestedProperty = builder.build();
93+
94+
assertNotSame(firstNestedProperty, secondNestedProperty);
95+
}
96+
97+
//#end TESTS
98+
99+
// #region ABSTRACT METHODS
100+
101+
/**
102+
* Creates the tested builder. Each call must return a new instance
103+
*
104+
* @return an {@link AbstractNestedPropertyBuilder}
105+
*/
106+
protected abstract AbstractNestedPropertyBuilder<N, P> createBuilder();
107+
108+
//#end ABSTRACT METHODS
109+
110+
}

test/org/codefx/nesting/property/AbstractNestedPropertyTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
*
2323
* @param <T>
2424
* the type wrapped by the nested property
25+
* @param <P>
26+
* the type of property wrapped by the nesting
2527
*/
26-
public abstract class AbstractNestedPropertyTest<T> {
28+
public abstract class AbstractNestedPropertyTest<T, P extends Property<T>> {
2729

2830
// #region INSTANCES USED FOR TESTING
2931

3032
/**
3133
* The nesting on which the tested property is based.
3234
*/
33-
private NestingAccess.EditableNesting<Property<T>> nesting;
35+
private NestingAccess.EditableNesting<P> nesting;
3436

3537
/**
3638
* The tested property.
@@ -44,7 +46,7 @@ public abstract class AbstractNestedPropertyTest<T> {
4446
*/
4547
@Before
4648
public void setUp() {
47-
Property<T> innerObservable = createNewObservableWithSomeValue();
49+
P innerObservable = createNewObservableWithSomeValue();
4850
nesting = NestingAccess.EditableNesting.createWithInnerObservable(innerObservable);
4951
property = createNestedPropertyFromNesting(nesting);
5052
}
@@ -98,7 +100,7 @@ public void testChangingValueToNull() {
98100
@Test
99101
public void testChangingObservable() {
100102
T newValue = createNewValue();
101-
Property<T> newObservable = createNewObservableWithValue(newValue);
103+
P newObservable = createNewObservableWithValue(newValue);
102104
setNestingObservable(nesting, newObservable);
103105
// assert that setting the observable worked
104106
assertSame(newObservable, getNestingObservable(nesting));
@@ -132,7 +134,7 @@ public void testChangingObservableToNull() {
132134
@Test
133135
public void testChangingNewObservablesValue() {
134136
// set a new observable ...
135-
Property<T> newObservable = createNewObservableWithSomeValue();
137+
P newObservable = createNewObservableWithSomeValue();
136138
setNestingObservable(nesting, newObservable);
137139
// (assert that setting the observable worked)
138140
assertSame(newObservable, getNestingObservable(nesting));
@@ -156,7 +158,7 @@ public void testChangingOldObservablesValue() {
156158

157159
// ... set a new observable ...
158160
T newValueInNewObservable = createNewValue();
159-
Property<T> newObservable = createNewObservableWithValue(newValueInNewObservable);
161+
P newObservable = createNewObservableWithValue(newValueInNewObservable);
160162
setNestingObservable(nesting, newObservable);
161163
// (assert that setting the observable worked)
162164
assertNotSame(oldObservable, getNestingObservable(nesting));
@@ -190,7 +192,7 @@ public void testChangingOldObservablesValue() {
190192
* the nesting from which the nested property is created
191193
* @return a new {@link NestedProperty} instance
192194
*/
193-
protected abstract NestedProperty<T> createNestedPropertyFromNesting(Nesting<Property<T>> nesting);
195+
protected abstract NestedProperty<T> createNestedPropertyFromNesting(Nesting<P> nesting);
194196

195197
/**
196198
* Creates a new value. Each call must return a new instance.
@@ -206,15 +208,15 @@ public void testChangingOldObservablesValue() {
206208
* the new observable's value
207209
* @return a new {@link Property} instance with the specified value
208210
*/
209-
protected abstract Property<T> createNewObservableWithValue(T value);
211+
protected abstract P createNewObservableWithValue(T value);
210212

211213
/**
212214
* Creates a new observable which holds some arbitrary value (there are no constraints for this value). Each call
213215
* must return a new instance.
214216
*
215217
* @return a new {@link Property} instance with the specified value
216218
*/
217-
protected abstract Property<T> createNewObservableWithSomeValue();
219+
protected abstract P createNewObservableWithSomeValue();
218220

219221
//#end ABSTRACT METHODS
220222

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.codefx.nesting.property;
2+
3+
import javafx.beans.property.IntegerProperty;
4+
import javafx.beans.property.SimpleIntegerProperty;
5+
6+
import org.codefx.nesting.Nesting;
7+
import org.codefx.nesting.testhelper.NestingAccess.EditableNesting;
8+
import org.junit.runner.RunWith;
9+
import org.junit.runners.Suite;
10+
import org.junit.runners.Suite.SuiteClasses;
11+
12+
/**
13+
* Tests the class {@link NestedIntegerPropertyBuilder}.
14+
*/
15+
@RunWith(Suite.class)
16+
@SuiteClasses({
17+
NestedIntegerPropertyBuilderTest.AbstractBuilderContract.class,
18+
NestedIntegerPropertyBuilderTest.CreatedProperties.class,
19+
})
20+
public class NestedIntegerPropertyBuilderTest {
21+
22+
/**
23+
* Tests whether the builder fulfills the contract defined by {@link AbstractNestedPropertyBuilder}.
24+
*/
25+
public static class AbstractBuilderContract
26+
extends AbstractNestedPropertyBuilderTest<IntegerProperty, NestedIntegerProperty> {
27+
28+
@Override
29+
protected AbstractNestedPropertyBuilder<IntegerProperty, NestedIntegerProperty> createBuilder() {
30+
IntegerProperty innerObservable = new SimpleIntegerProperty(0);
31+
EditableNesting<IntegerProperty> nesting = EditableNesting.createWithInnerObservable(innerObservable);
32+
return NestedIntegerPropertyBuilder.forNesting(nesting);
33+
}
34+
35+
}
36+
37+
/**
38+
* Uses the builder to create properties which are then tested.
39+
*/
40+
public static class CreatedProperties extends AbstractNestedIntegerPropertyTest {
41+
42+
@Override
43+
protected NestedProperty<Number> createNestedPropertyFromNesting(Nesting<IntegerProperty> nesting) {
44+
// use the builder to create the property
45+
NestedIntegerPropertyBuilder builder = NestedIntegerPropertyBuilder.forNesting(nesting);
46+
return builder.build();
47+
}
48+
49+
}
50+
51+
}

0 commit comments

Comments
 (0)