Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@
import com.sun.javafx.binding.BidirectionalBinding;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Arrays;
import java.util.Collection;
import javafx.beans.value.ObservableValue;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

@RunWith(Parameterized.class)
public class BidirectionalBindingTest<T> {

@FunctionalInterface
Expand Down Expand Up @@ -70,12 +69,8 @@ public T[] getValues() {
private Property<T> op4;
private T[] v;

public BidirectionalBindingTest(Factory<T> factory) {
private void setUp(Factory<T> factory) {
this.factory = factory;
}

@Before
public void setUp() {
op1 = factory.createProperty();
op2 = factory.createProperty();
op3 = factory.createProperty();
Expand All @@ -85,8 +80,10 @@ public void setUp() {
op2.setValue(v[1]);
}

@Test
public void testBind() {
@ParameterizedTest
@MethodSource("parameters")
public void testBind(Factory<T> factory) {
setUp(factory);
Bindings.bindBidirectional(op1, op2);
Bindings.bindBidirectional(op1, op2);
System.gc(); // making sure we did not not overdo weak references
Expand All @@ -102,8 +99,10 @@ public void testBind() {
assertEquals(v[3], op2.getValue());
}

@Test
public void testUnbind() {
@ParameterizedTest
@MethodSource("parameters")
public void testUnbind(Factory<T> factory) {
setUp(factory);
// unbind non-existing binding => no-op
Bindings.unbindBidirectional(op1, op2);

Expand All @@ -127,8 +126,10 @@ public void testUnbind() {
assertEquals(v[3], op2.getValue());
}

@Test
public void testChaining() {
@ParameterizedTest
@MethodSource("parameters")
public void testChaining(Factory<T> factory) {
setUp(factory);
op3.setValue(v[2]);
Bindings.bindBidirectional(op1, op2);
Bindings.bindBidirectional(op2, op3);
Expand Down Expand Up @@ -179,8 +180,10 @@ private int getListenerCount(ObservableValue<T> v) {
return ExpressionHelperUtility.getInvalidationListeners(v).size();
}

@Test
public void testWeakReferencing() {
@ParameterizedTest
@MethodSource("parameters")
public void testWeakReferencing(Factory<T> factory) {
setUp(factory);
Bindings.bindBidirectional(op1, op2);

assertEquals(1, getListenerCount(op1));
Expand All @@ -201,16 +204,20 @@ public void testWeakReferencing() {
assertEquals(0, getListenerCount(op2));
}

@Test
public void testHashCode() {
@ParameterizedTest
@MethodSource("parameters")
public void testHashCode(Factory<T> factory) {
setUp(factory);
final int hc1 = BidirectionalBinding.bind(op1, op2).hashCode();
final int hc2 = BidirectionalBinding.bind(op2, op1).hashCode();
assertEquals(hc1, hc2);
}

@SuppressWarnings("unlikely-arg-type")
@Test
public void testEquals() {
@ParameterizedTest
@MethodSource("parameters")
public void testEquals(Factory<T> factory) {
setUp(factory);
final BidirectionalBinding golden = BidirectionalBinding.bind(op1, op2);

assertTrue(golden.equals(golden));
Expand All @@ -224,8 +231,10 @@ public void testEquals() {
assertFalse(golden.equals(BidirectionalBinding.bind(op2, op3)));
}

@Test
public void testEqualsWithGCedProperty() {
@ParameterizedTest
@MethodSource("parameters")
public void testEqualsWithGCedProperty(Factory<T> factory) {
setUp(factory);
final BidirectionalBinding binding1 = BidirectionalBinding.bind(op1, op2);
final BidirectionalBinding binding2 = BidirectionalBinding.bind(op1, op2);
final BidirectionalBinding binding3 = BidirectionalBinding.bind(op2, op1);
Expand All @@ -242,38 +251,52 @@ public void testEqualsWithGCedProperty() {
assertFalse(binding3.equals(binding4));
}

@Test(expected=NullPointerException.class)
public void testBind_Null_X() {
Bindings.bindBidirectional(null, op2);
@ParameterizedTest
@MethodSource("parameters")
public void testBind_Null_X(Factory<T> factory) {
setUp(factory);
assertThrows(NullPointerException.class, () -> Bindings.bindBidirectional(null, op2));
}

@Test(expected=NullPointerException.class)
public void testBind_X_Null() {
Bindings.bindBidirectional(op1, null);
@ParameterizedTest
@MethodSource("parameters")
public void testBind_X_Null(Factory<T> factory) {
setUp(factory);
assertThrows(NullPointerException.class, () -> Bindings.bindBidirectional(op1, null));
}

@Test(expected=IllegalArgumentException.class)
public void testBind_X_Self() {
Bindings.bindBidirectional(op1, op1);
@ParameterizedTest
@MethodSource("parameters")
public void testBind_X_Self(Factory<T> factory) {
setUp(factory);
assertThrows(IllegalArgumentException.class, () -> Bindings.bindBidirectional(op1, op1));
}

@Test(expected=NullPointerException.class)
public void testUnbind_Null_X() {
Bindings.unbindBidirectional(null, op2);
@ParameterizedTest
@MethodSource("parameters")
public void testUnbind_Null_X(Factory<T> factory) {
setUp(factory);
assertThrows(NullPointerException.class, () -> Bindings.unbindBidirectional(null, op2));
}

@Test(expected=NullPointerException.class)
public void testUnbind_X_Null() {
Bindings.unbindBidirectional(op1, null);
@ParameterizedTest
@MethodSource("parameters")
public void testUnbind_X_Null(Factory<T> factory) {
setUp(factory);
assertThrows(NullPointerException.class, () -> Bindings.unbindBidirectional(op1, null));
}

@Test(expected=IllegalArgumentException.class)
public void testUnbind_X_Self() {
Bindings.unbindBidirectional(op1, op1);
@ParameterizedTest
@MethodSource("parameters")
public void testUnbind_X_Self(Factory<T> factory) {
setUp(factory);
assertThrows(IllegalArgumentException.class, () -> Bindings.unbindBidirectional(op1, op1));
}

@Test
public void testBrokenBind() {
@ParameterizedTest
@MethodSource("parameters")
public void testBrokenBind(Factory<T> factory) {
setUp(factory);
Bindings.bindBidirectional(op1, op2);
op1.bind(op3);
assertEquals(op3.getValue(), op1.getValue());
Expand All @@ -284,8 +307,10 @@ public void testBrokenBind() {
assertEquals(op2.getValue(), op1.getValue());
}

@Test
public void testDoubleBrokenBind() {
@ParameterizedTest
@MethodSource("parameters")
public void testDoubleBrokenBind(Factory<T> factory) {
setUp(factory);
Bindings.bindBidirectional(op1, op2);
op1.bind(op3);
op4.setValue(v[0]);
Expand All @@ -302,16 +327,17 @@ public void testDoubleBrokenBind() {
assertEquals(v[1], op2.getValue());
}

@Test
public void testSetValueWithoutIntermediateValidation() {
@ParameterizedTest
@MethodSource("parameters")
public void testSetValueWithoutIntermediateValidation(Factory<T> factory) {
setUp(factory);
BidirectionalBinding.bind(op1, op2);
op1.setValue(v[0]);
op2.setValue(v[1]);
assertEquals(v[1], op1.getValue());
assertEquals(v[1], op2.getValue());
}

@Parameterized.Parameters
public static Collection<Object[]> parameters() {
final Boolean[] booleanData = new Boolean[] {true, false, true, false};
final Double[] doubleData = new Double[] {2348.2345, -92.214, -214.0214, -908.214};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.util.StringConverter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.stream.Stream;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

@RunWith(Parameterized.class)
public class BidirectionalBindingWithConversionTest<S, T> {

public static interface Functions<U, V> {
Expand All @@ -57,29 +57,28 @@ public static interface Functions<U, V> {
void check1(V obj0, V obj1);
}

private final Functions<S, T> func;
private final S[] v0;
private final T[] v1;
private Functions<S, T> func;
private S[] v0;
private T[] v1;

private PropertyMock<S> op0;
private PropertyMock<T> op1;

public BidirectionalBindingWithConversionTest(Functions<S, T> func, S[] v0, T[] v1) {
private void setUP(Functions<S, T> func, S[] v0, T[] v1) {
this.op0 = func.create0();
this.op1 = func.create1();
this.func = func;
this.v0 = v0;
this.v1 = v1;
}

@Before
public void setUp() {

@ParameterizedTest
@MethodSource("parameters")
public void testBind(Functions<S, T> func, S[] v0, T[] v1) {
setUP(func, v0, v1);
op0.setValue(v0[0]);
op1.setValue(v1[1]);
}

@Test
public void testBind() {
func.bind(op0, op1);
System.gc(); // making sure we did not not overdo weak references
func.check0(v0[1], op0.getValue());
Expand All @@ -94,8 +93,12 @@ public void testBind() {
func.check1(v1[3], op1.getValue());
}

@Test
public void testUnbind() {
@ParameterizedTest
@MethodSource("parameters")
public void testUnbind(Functions<S, T> func, S[] v0, T[] v1) {
setUP(func, v0, v1);
op0.setValue(v0[0]);
op1.setValue(v1[1]);
// unbind non-existing binding => no-op
func.unbind(op0, op1);

Expand All @@ -119,8 +122,12 @@ public void testUnbind() {
func.check1(v1[3], op1.getValue());
}

@Test
public void testWeakReferencing() {
@ParameterizedTest
@MethodSource("parameters")
public void testWeakReferencing(Functions<S, T> func, S[] v0, T[] v1) {
setUP(func, v0, v1);
op0.setValue(v0[0]);
op1.setValue(v1[1]);
func.bind(op0, op1);
assertEquals(1, op0.getListenerCount());
assertEquals(1, op1.getListenerCount());
Expand All @@ -141,32 +148,47 @@ public void testWeakReferencing() {
assertEquals(0, op0.getListenerCount());
}

@Test(expected=NullPointerException.class)
public void testBind_Null_X() {
func.bind(null, op1);
@ParameterizedTest
@MethodSource("parameters")
public void testBind_Null_X(Functions<S, T> func, S[] v0, T[] v1) {
setUP(func, v0, v1);
op0.setValue(v0[0]);
op1.setValue(v1[1]);
assertThrows(NullPointerException.class, () -> func.bind(null, op1));
}

@Test(expected=NullPointerException.class)
public void testBind_X_Null() {
func.bind(op0, null);
public void testBind_X_Null(Functions<S, T> func, S[] v0, T[] v1) {
setUP(func, v0, v1);
op0.setValue(v0[0]);
op1.setValue(v1[1]);
assertThrows(NullPointerException.class, () -> func.bind(op0, null));
}

@Test(expected=NullPointerException.class)
public void testUnbind_Null_X() {
func.unbind(null, op1);
public void testUnbind_Null_X(Functions<S, T> func, S[] v0, T[] v1) {
setUP(func, v0, v1);
op0.setValue(v0[0]);
op1.setValue(v1[1]);
assertThrows(NullPointerException.class, () -> func.unbind(null, op1));
}

@Test(expected=NullPointerException.class)
public void testUnbind_X_Null() {
func.unbind(op0, null);
@ParameterizedTest
@MethodSource("parameters")
public void testUnbind_X_Null(Functions<S, T> func, S[] v0, T[] v1) {
setUP(func, v0, v1);
op0.setValue(v0[0]);
op1.setValue(v1[1]);
assertThrows(NullPointerException.class, () -> func.unbind(op0, null));
}

@Test(expected=IllegalArgumentException.class)
public void testUnbind_X_Self() {
func.unbind(op0, op0);
@ParameterizedTest
@MethodSource("parameters")
public void testUnbind_X_Self(Functions<S, T> func, S[] v0, T[] v1) {
setUP(func, v0, v1);
op0.setValue(v0[0]);
op1.setValue(v1[1]);
assertThrows(IllegalArgumentException.class, () -> func.unbind(op0, op0));
}

@Parameterized.Parameters
public static Collection<Object[]> parameters() {
final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
final Date[] dates = new Date[] {new Date(), new Date(0), new Date(Integer.MAX_VALUE), new Date(Long.MAX_VALUE)};
Expand Down
Loading