4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.events
;
27 import static net
.sourceforge
.aprog
.events
.EventsTestingTools
.*;
29 import static org
.junit
.Assert
.*;
31 import net
.sourceforge
.aprog
.events
.Observable
.Event
;
33 import org
.junit
.Test
;
36 * Automated tests using JUnit 4 for {@link Observable}.
38 * @author codistmonk (creation 2010-06-23)
40 public final class ObservableTest
{
43 public final <R
extends EventRecorder
<Event
<?
>> & DummyObservable
.Listener
> void testFireEvent() {
44 final DummyObservable observable
= new DummyObservable();
45 @SuppressWarnings("unchecked")
46 final R recorder1
= (R
) newEventRecorder(DummyObservable
.Listener
.class);
47 @SuppressWarnings("unchecked")
48 final R recorder2
= (R
) newEventRecorder(DummyObservable
.Listener
.class);
50 observable
.addListener(recorder1
);
51 observable
.addListener(recorder2
);
52 observable
.fireNewEvent();
53 observable
.removeListener(recorder2
);
54 observable
.fireNewEvent();
56 assertTrue(recorder1
.getEvent(0) instanceof DummyObservable
.EventFiredEvent
);
57 assertTrue(recorder1
.getEvent(1) instanceof DummyObservable
.EventFiredEvent
);
58 assertSame(recorder1
.getEvent(0), recorder2
.getEvent(0));
59 assertEquals(2, recorder1
.getEvents().size());
60 assertEquals(1, recorder2
.getEvents().size());
65 * @author codistmonk (creation 2010-06-23)
67 private static final class DummyObservable
extends AbstractObservable
<DummyObservable
.Listener
> {
70 * Package-private default constructor to suppress visibility warnings.
76 public final void fireNewEvent() {
77 new EventFiredEvent().fire();
82 * @author codistmonk (creation 2010-06-23)
84 public static interface Listener
{
91 public abstract void eventFired(final EventFiredEvent event
);
97 * @author codistmonk (creation 2010-06-23)
99 public final class EventFiredEvent
extends AbstractEvent
<DummyObservable
, Listener
> {
102 protected final void notifyListener(final Listener listener
) {
103 listener
.eventFired(this);