Commit 0d9c6258 authored by sunye's avatar sunye
Browse files

Add tests to exercise 7, start implementing ex. 8

parent a16e1395
package fr.unantes.l3.construction.exercice7;
public class Action {
private ReferenceToOne<Action, Button> button = new ReferenceToOne<>(this) {
@Override
protected ReferenceToOne<Button, Action> opposite(Button target) {
return target.action();
}
};
public ReferenceToOne<Action, Button> button() {
return button;
}
}
package fr.unantes.l3.construction.exercice7;
public class Button {
private ReferenceToOne<Button, Action> action = new ReferenceToOne<>(this) {
@Override
protected ReferenceToOne<Action, Button> opposite(Action target) {
return target.button();
}
};
public ReferenceToOne<Button, Action> action() {
return action;
}
}
......@@ -2,7 +2,7 @@ package fr.unantes.l3.construction.exercice7;
import java.util.Optional;
public class ReferenceToOne<C, T> {
public abstract class ReferenceToOne<C, T> {
private final C container;
private Optional<T> target = Optional.empty();
......@@ -36,10 +36,7 @@ public class ReferenceToOne<C, T> {
}
}
private ReferenceToOne<T, C> opposite(T target) {
return null;
//return target.button();
}
protected abstract ReferenceToOne<T, C> opposite(T target);
public void set(T newTarget) {
this.basicSet(newTarget);
......
package fr.unantes.l3.construction.exercice8;
public class Field {
private ReferenceToWindow window = new ReferenceToWindow();
public ReferenceToWindow window() {
return window;
}
}
package fr.unantes.l3.construction.exercice8;
public class ReferenceToFields {
}
package fr.unantes.l3.construction.exercice8;
public class ReferenceToWindow {
private Window target;
public Window get() {
return target;
}
public void set(Window target) {
this.target = target;
}
public void unSet() {
target = null;
}
}
package fr.unantes.l3.construction.exercice8;
public class Window {
}
......@@ -9,12 +9,12 @@ public class TestWindow {
@Test
void testWidgets() {
Window w = new Window();
Field f = new Field();
Window w = new Window();
Field f = new Field();
w.getWidgets().add(f);
w.getWidgets().add(f);
w.getWidgets().remove(f);
w.getWidgets().remove(f);
Iterator<Field> it = w.getWidgets().iterator();
......
......@@ -6,11 +6,41 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ActionTest {
@Test
public void testHandshake() {
public void testHandshakeFromButton() {
Action a1 = new Action();
Button b1 = new Button();
a1.button().set(b1);
assertThat(b1.action().get()).isEqualTo(a1);
}
@Test
public void testHandShakeFromAction() {
Action a1 = new Action();
Button b1 = new Button();
b1.action().set(a1);
assertThat(a1.button().get()).isEqualTo(b1);
}
@Test
public void testHandshakeWithPreviousAssociation() {
Action a1 = new Action();
Button b1 = new Button();
Action a2 = new Action();
Button b2 = new Button();
a1.button().set(b2);
b1.action().set(a2);
a1.button().set(b1);
assertThat(a2.button().isSet()).isFalse();
assertThat(b2.action().isSet()).isFalse();
}
}
\ No newline at end of file
package fr.unantes.l3.construction.exercice7;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ActionTest {
@Test
public void testHandshakeFromButton() {
Action a1 = new Action();
Button b1 = new Button();
a1.button().set(b1);
assertThat(b1.action().get()).isEqualTo(a1);
}
@Test
public void testHandShakeFromAction() {
Action a1 = new Action();
Button b1 = new Button();
b1.action().set(a1);
assertThat(a1.button().get()).isEqualTo(b1);
}
@Test
public void testHandshakeWithPreviousAssociation() {
Action a1 = new Action();
Button b1 = new Button();
Action a2 = new Action();
Button b2 = new Button();
a1.button().set(b2);
b1.action().set(a2);
a1.button().set(b1);
assertThat(a2.button().isSet()).isFalse();
assertThat(b2.action().isSet()).isFalse();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment