summaryrefslogtreecommitdiff
path: root/israfil-foundation-notification/src/test/java/net/israfil/foundation/notification/NotificationCentreTest.java
blob: beb310eaf1309712a0a93d0335379519dbcea354 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (c) 2003, 2004, 2005, 2006 Israfil Consulting Services Corporation
 * Copyright (c) 2003, 2004, 2005, 2006 Christian Edward Gruber
 * All Rights Reserved
 * 
 * This software is licensed under the Berkeley Standard Distribution license,
 * (BSD license), as defined below:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this 
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of Israfil Consulting Services nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 * 
 * $Id: NotificationCentreTest.java 130 2006-12-31 23:22:17Z cgruber $
 */
package net.israfil.foundation.notification;

import java.util.HashMap;
import java.util.Map;

import net.israfil.foundation.core.Copyright;

import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 * @author cgruber
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
@Copyright(years={"2003","2004","2005","2006"},owner="Israfil Consulting Services Corporation",license="BSD")
@Test
public class NotificationCentreTest{

    private static String NOTIFICATION_1 = "notification1";
    private static String NOTIFICATION_2 = "notification2";
    private static String NOTIFICATION_3 = "notification3";
    
    private static String INFO_KEY = "key";
    private static String INFO_VALUE = "value";
    

    public boolean didReceiveNotifyWithNotification = false;
    public boolean didReceiveNotifyWithObject = false;
    public boolean didReceiveNotifyOtherWithObject = false;
    
    public Map<String,Object> info = null;

    NotificationCentre centre = null;
    ObserverFixture observer = null;

	@BeforeMethod
	protected void setUp() throws Exception {
        centre = NotificationCentre.defaultCentre();
        observer = new ObserverFixture();
        didReceiveNotifyWithNotification = false;
        didReceiveNotifyWithObject = false;
        didReceiveNotifyOtherWithObject = false;
        info = new HashMap<String,Object>();
        info.put(INFO_KEY,INFO_VALUE);
	}

	@AfterMethod
	protected void tearDown() throws Exception {
		centre.clearObservers();
        centre = null;
        observer = null;
        info = null;
	}

	/*
	 * Test for void postNotification(Notification)
	 */
	public void testPostNotificationNotification() {
        Notification n1 = new Notification(this,NOTIFICATION_1);
        Notification n2 = new Notification(this,NOTIFICATION_2);
        Notification n3 = new Notification(this,NOTIFICATION_3);
        centre.postNotification(n1);
        Assert.assertFalse(didReceiveNotifyWithNotification);
        Assert.assertFalse(observer.notification == n1);
        // register as global
        centre.addObserver(observer,"notify:net.israfil.foundation.notification.Notification",null,null);
        centre.postNotification(n2);
		Assert.assertTrue(didReceiveNotifyWithNotification);
		Assert.assertTrue(observer.notification == n2);
		
		// register as global
		centre.removeObserver(observer,null,null);
		centre.postNotification(n3);
		Assert.assertFalse(observer.notification == n3);
	}

	/*
	 * Test for void postNotification(Object, String)
	 */
	public void testPostNotificationObjectString() {
		centre.postNotification(this,NOTIFICATION_1);
		Assert.assertFalse(didReceiveNotifyWithNotification);
		Assert.assertEquals(null,observer.notification);
		// register as global
		centre.addObserver(observer,"notify:net.israfil.foundation.notification.Notification",null,null);
		centre.postNotification(this,NOTIFICATION_2);
		Assert.assertTrue(didReceiveNotifyWithNotification);
		Assert.assertEquals(NOTIFICATION_2,observer.notification.notification);
	}

	/*
	 * Test for void postNotification(Object, String, Map)
	 */
	public void testPostNotificationObjectStringMap() {
		centre.postNotification(this,NOTIFICATION_1,info);
		Assert.assertFalse(didReceiveNotifyWithNotification);
		Assert.assertEquals(null,observer.notification);
		// register as global
		centre.addObserver(observer,"notify:net.israfil.foundation.notification.Notification",null,null);
		centre.postNotification(this,NOTIFICATION_2,info);
		Assert.assertTrue(didReceiveNotifyWithNotification);
		Assert.assertEquals(NOTIFICATION_2,observer.notification.notification);
		Assert.assertEquals(INFO_VALUE,observer.notification.info.get(INFO_KEY));
	}

	/*
	 * Test for void removeObserver(Object, String, Object)
	 */
	public void testRemoveObserverObjectStringObject() {
		// register as global
		centre.addObserver(observer,"notify:net.israfil.foundation.notification.Notification",null,null);
		// remove right away (we know addObserver works from other tests.
		centre.removeObserver(observer,null,null);
		centre.postNotification(this,NOTIFICATION_1,info);
		Assert.assertEquals(null,observer.notification);
	}

    public class ObserverFixture {
    	public Notification notification = null;
        public void notify(Notification n) {
            NotificationCentreTest sender = (NotificationCentreTest)n.sender;
            sender.didReceiveNotifyWithNotification = true;
            this.notification = n;
        }
        public void notify(Object o) {
            Notification n = (Notification)o;
            NotificationCentreTest sender = (NotificationCentreTest)n.sender;
            sender.didReceiveNotifyWithObject = true;
			this.notification = n;
        }
        public void notifyOther(Object o) {
            Notification n = (Notification)o;
            NotificationCentreTest sender = (NotificationCentreTest)n.sender;
            sender.didReceiveNotifyWithObject = true;
			this.notification = n;
        }
        public void finalize() throws Throwable {
        	notification = null;
        	super.finalize();
        }
    }

}