Skip to content

Commit

Permalink
add ObserverTests; made fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Sep 17, 2023
1 parent c94b380 commit 49a7ec4
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/main/java/lib/gecom/observer/ObserverTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
public abstract class ObserverTemplate<NOTE_TYPE> implements Observing<NOTE_TYPE> {

@NonNull
private final List<Subject<NOTE_TYPE>> subjects = new ArrayList<>();
protected final List<Subject<NOTE_TYPE>> subjects = new ArrayList<>();

@Override
public void observe(@NonNull final Subject<NOTE_TYPE> subject) {
subject.beObservedBy(this);
subjects.add(subject);
}

Expand All @@ -22,8 +23,13 @@ public abstract void takeNotice(
);

@Override
public void takeDeadthNoticeFrom(final @NonNull Subject<NOTE_TYPE> subject) {
public void takeDeathNoticeFrom(final @NonNull Subject<NOTE_TYPE> subject) {
subjects.remove(subject);
}

public void close() {
subjects.forEach(subject -> subject.observationStoppedThrough(this));
subjects.clear();
}

}
5 changes: 3 additions & 2 deletions src/main/java/lib/gecom/observer/Observing.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.NonNull;

public interface Observing<NOTE_TYPE> {
public interface Observing<NOTE_TYPE> extends AutoCloseable {

void observe(@NonNull final Subject<NOTE_TYPE> subject);

Expand All @@ -11,5 +11,6 @@ void takeNotice(
@NonNull final Note<NOTE_TYPE> note
);

void takeDeadthNoticeFrom(@NonNull final Subject<NOTE_TYPE> subject);
void takeDeathNoticeFrom(@NonNull final Subject<NOTE_TYPE> subject);

}
2 changes: 1 addition & 1 deletion src/main/java/lib/gecom/observer/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void notifyObserversWith(@NonNull final Note<NOTE_TYPE> note) {

@Override
public void reportMyDeath() {
observersWatchingMe.forEach(observer -> observer.takeDeadthNoticeFrom(this));
observersWatchingMe.forEach(observer -> observer.takeDeathNoticeFrom(this));
}

@Override
Expand Down
107 changes: 107 additions & 0 deletions src/test/java/lib/gecom/observer/ObserverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package lib.gecom.observer;

import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;

import java.util.List;

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

public class ObserverTest {

@Test
public void notifyObserversWith_sendTestEvent_withObserversRetrieveMessage() {
// Arrange
final Subject<TestEvent> testEventSubject = new Subject<>();
final TestObserver testObserver1 = new TestObserver();
final TestObserver testObserver2 = new TestObserver();

// Act
testObserver1.observe(testEventSubject);
testObserver2.observe(testEventSubject);

final Note<TestEvent> testEventNote = new TestEvent("test");
testEventSubject.notifyObserversWith(testEventNote);

// Assert
assertEquals(testEventSubject, testObserver1.notifiedBySubject);
assertEquals(testEventNote, testObserver1.notifiedWithEvent);

assertEquals(testEventSubject, testObserver2.notifiedBySubject);
assertEquals(testEventNote, testObserver2.notifiedWithEvent);
}

@Test
public void reportMyDeath_subjectIsRemovedOnObservers() {
// Arrange
final Subject<TestEvent> testEventSubject = new Subject<>();
final TestObserver testObserver1 = new TestObserver();
final TestObserver testObserver2 = new TestObserver();

// Act
testObserver1.observe(testEventSubject);
testObserver2.observe(testEventSubject);

final Note<TestEvent> testEventNote = new TestEvent("test");
testEventSubject.reportMyDeath();

// Assert
assertTrue(testObserver1.getSubject().isEmpty());
assertTrue(testObserver2.getSubject().isEmpty());
}

@Test
public void observationStoppedThrough_observerGetsRemovedFromObserverList_noNotificationIsSendAnymore() {
// Arrange
final Subject<TestEvent> testEventSubject = new Subject<>();
final TestObserver testObserver1 = new TestObserver();
final TestObserver testObserver2 = new TestObserver();

// Act
testObserver1.observe(testEventSubject);
testObserver2.observe(testEventSubject);

final Note<TestEvent> testEventNote = new TestEvent("test");
testEventSubject.observationStoppedThrough(testObserver1);
testEventSubject.observationStoppedThrough(testObserver2);
testEventSubject.notifyObserversWith(testEventNote);

// Assert
assertFalse(testObserver1.getSubject().isEmpty());
assertFalse(testObserver2.getSubject().isEmpty());

assertTrue(testObserver1.getNotifiedWithEvent() == null);
assertTrue(testObserver2.getNotifiedWithEvent() == null);
}

@Getter
public class TestObserver extends ObserverTemplate<TestEvent> {

private Subject<TestEvent> notifiedBySubject = null;
private Note<TestEvent> notifiedWithEvent = null;

@Override
public void takeNotice(
@NonNull final Subject<TestEvent> subject,
@NonNull final Note<TestEvent> event
) {
notifiedBySubject = subject;
notifiedWithEvent = event;

}

public List<Subject<TestEvent>> getSubject() {
return super.subjects;
}

}

@RequiredArgsConstructor
public class TestEvent implements Note<TestEvent> {
@NonNull
private final String name;
}

}

0 comments on commit 49a7ec4

Please sign in to comment.