File: TestEvents.java

package info (click to toggle)
libnb-platform18-java 12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 729,800 kB
  • sloc: java: 5,059,097; xml: 574,432; php: 78,788; javascript: 29,039; ansic: 10,278; sh: 6,386; cpp: 4,612; jsp: 3,643; sql: 1,097; makefile: 540; objc: 288; perl: 277; haskell: 93
file content (397 lines) | stat: -rw-r--r-- 10,995 bytes parent folder | download | duplicates (3)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
/*
 *	TestEvents - test the events
 *
 *	The following test assumes that we know the content of the
 *	graph as we get elements, add and change them. Therefore, the TestBook.xml
 *	file and this java test should be kept in sync.
 *
 * 	Test the following:
 *
 *		listener on the root - change event for add/change/remove on the same root bean
 *		listener on the root - change event for add/change/remove 
 *			on a sub node bean (propagation)
 *		listener on the root and on a subnode: test no reception, both reception and 
 *			only one reception
 *		listener on a specific property
 *		remove listeners
 *		indexed and non indexed property events
 *		check utility methods to get the index, name and parent bean 
 *			from an event name (graphManager methods)
 *		remove a subtree - get event within the subtree (no propagation)
 *			and within the original tree (propagation)
 *
 */

import java.io.*;
import java.util.*;
import org.w3c.dom.*;

import java.beans.*;

import org.netbeans.modules.schema2beans.*;
import book.*;


public class TestEvents extends BaseTest
{
    public static void main(String[] argv) {
        BaseTest o = new TestEvents();
        if (argv.length > 0)
            o.setDocumentDir(argv[0]);
        try {
            o.run();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        System.exit(0);
    }
    
	public class MyListener implements PropertyChangeListener
	{
		GraphManager	gm;
		String			listenerName;
		Object			oldValue;
		Object			newValue;
		String			propertyName;
		Object			source;
		int				index;

		boolean			remove;
		
		//	Used to check that the event is triggered once the changed are done
		Chapter			tracePara;
		
		public MyListener(BaseBean bean)
		{
			this.listenerName = bean.name();
			gm = bean.graphManager();
			this.remove = false;
			out("new listener for " + this.listenerName);
		}

		public void reset()
		{
			this.oldValue = null;
			this.newValue = null;
			this.propertyName = null;
			this.source = null;
			this.index = -1;
		}

		public void traceParagraphs(Chapter c)
		{
			this.tracePara = c;
		}

		
		public void propertyChange(PropertyChangeEvent e)
		{
			this.oldValue = e.getOldValue();
			this.newValue = e.getNewValue();
			this.propertyName = e.getPropertyName();
			this.source = e.getSource();
			String n = this.propertyName;
			this.index = gm.getPropertyIndex(n);

			out("<Lnr:" + this.listenerName + " Evt:" + n + 
				" Src:" + this.source.getClass().getName() + ">");
			if (remove)
			{
				out("<" + gm.getPropertyName(n) + "[" + this.index + 
					"]" + " - Parent: " + gm.getPropertyParentName(n) + ">");
			}
			else
			{
				out("<" + gm.getPropertyName(n) + "[" + this.index + 
					"]" + " - Parent: " + gm.getPropertyParentName(n) +
					"/" + gm.getPropertyParent(n).getClass().getName() + ">");
			}

			if (this.tracePara != null)
			{
				String[] p = this.tracePara.getParagraph();
				for (int i=0; i<p.length; i++)
					out("From event listener: " + p[i]);
			}
			//out("received " + this.oldValue + "/" + this.newValue + "/" +
			//	this.propertyName);
		}

		public void removeMode()
		{
			this.remove = true;
		}
		
		public Object oldValue()
		{
			return this.oldValue;
		}

		public String stringOldValue()
		{
			if (this.oldValue == null)
				return "<null>";
			else
				return this.oldValue.toString();
		}

		public Object newValue()
		{
			return this.newValue;
		}

		public String stringNewValue()
		{
			if (this.newValue == null)
				return "<null>";
			else
				return this.newValue.toString();
		}
		
		public String name()
		{
			return this.propertyName;
		}

		public String toString()
		{
			return this.name() + " raised from source " +
				this.source.getClass().getName();
		}
	}
	
	public void run()
		throws Exception
	{
		Book book;

		this.readDocument();
		
		out("creating the bean graph");
		book = Book.createGraph(this.doc);
		GraphManager gm = book.graphManager();

		//	Check that we can read the graph an it is complete
		out("bean graph created");
		//out(book.toString());

		/*
		 *	Book
		 *	  Index[1,n]
		 *	         Word - String
		 *	         Ref[1,n]
		 *	                Page - String
		 *	                Line - String
		 *	  Chapter[1,n]
		 *	         Comment? - String
		 *	         Paragraph[0,n] - String
		 *	  Summary? - String
		 *	  Author[1,n] - String
		 *	  Good - Boolean
		 *	  Available - Boolean
		 */

		//
		//	Set a change event on the root
		//
		MyListener l = new MyListener(book);
		
		book.addPropertyChangeListener(l);
		
		setTest("simple change event on the root");
		l.reset();
		String s = "This book is about how to raise the event familly";
		//	Change a property on the root - this should raises an event
		book.setSummary(s);
		//	Check the received event
		check(l.oldValue() == null, "(old value)");
		check(l.newValue().equals(s), "(new value)");

		setTest("change the same property on the root");
		l.reset();
		String s2 = "This book is about nothing at all";
		//	Change a property on the root - this should raises an event
		book.setSummary(s2);
		//	Check the received event
		check(l.oldValue().equals(s), "(old value)");
		check(l.newValue().equals(s2), "(new value)");

		setTest("remove this same property");
		l.reset();
		//	Change a property on the root - this should raises an event
		book.setSummary(null);
		//	Check the received event
		check(l.oldValue().equals(s2), "(old value)");
		check(l.newValue() == null, "(new value)");


		//
		//	Keep the same event on the root, but change a property of another
		//	property (not a direct property of the root)
		//
		setTest("propagation of the event");
		Chapter c = book.getChapter(0);
		l.reset();
		s = c.getComment();
		s2 = "Comment on the first chapter";
		c.setComment(s2);
		check(l.oldValue.equals(s), "(oldvalue)");
		check(l.newValue.equals(s2), "(newvalue)");

		//	Add three paragraphs

		setTest("event on indexed property - add new");
		l.reset();
		s = "This is a new paragraph";
		int i = c.addParagraph(s);
		check(l.oldValue == null, "(no old value - new element)");
		check(l.newValue.equals(s), "(new value)");
		check(l.index == i, "(correct index)");

		setTest("event on indexed property - add new");
		l.reset();
		s2 = "This is another paragraph";
		int j = c.addParagraph(s2);
		check(l.oldValue == null, "(no old value - new element)");
		check(l.newValue.equals(s2), "(new value)");
		check(l.index == j, "(correct index)");

		setTest("event on indexed property - add new");
		l.reset();
		s2 = "This is yet another paragraph";
		j = c.addParagraph(s2);
		check(l.oldValue == null, "(no old element - new element)");
		check(l.newValue.equals(s2), "(new value)");
		check(l.index == j, "(correct index)");

		//	Remove the first added
		setTest("event on indexed property - remove index");
		l.reset();
		c.setParagraph(i, null);
		check(l.oldValue.equals(s), "(old value)");
		check(l.newValue == null, "(no new value)");
		check(l.index == i, "(correct index)");

		setTest("event on indexed property - reset new");
		l.reset();
		c.setParagraph(i, s);
		check(l.oldValue == null, "(old value)");
		check(l.newValue.equals(s), "(no new value)");
		check(l.index == i, "(correct index)");

		//
		//	Add another listener on an intermediate node and on a leaf
		//
		
		Chapter c2 = book.getChapter(1);
		
		MyListener l2 = new MyListener(c2);
		
		//	Get the events only for Paragraph changes
		c2.addPropertyChangeListener("Paragraph", l2);

		//	Check that we receive the event twice
		setTest("two listeners - both receiving");
		l2.reset();
		l.reset();
		s = "This is a brand new one";
		c2.addParagraph(s);
		check(l.oldValue == null, "(no old value)");
		check(l.newValue.equals(s), "(new value)");
		check(l.oldValue == l2.oldValue, "(same old value - both listeners)");
		check(l.newValue.equals(l2.newValue), "(same new value - both listeners)");
		check(l.index == l2.index, "(same index - both listeners)");
		check(l.source == l2.source, "(same source - both listeners)");

		//	Check that modifying the comment won't notify the Paragraph listener
		setTest("two listeners - one receiving");
		l2.reset();
		l.reset();
		s = "That's a new comment value";
		c2.setComment(s);
		check(l.newValue.equals(s), "(root listener: yep)");
		check(l2.newValue == null, "(chapter listener: noop)");


		//	Remove the book listener
		book.removePropertyChangeListener(l);
		setTest("one listener - no receiving");
		l2.reset();
		l.reset();
		s = "That's another new comment value";
		c2.setComment(s);
		check(l.newValue == null, "(root listener: noop)");
		check(l2.newValue == null, "(chapter listener: noop)");

		setTest("one listener - one receiving");
		l2.reset();
		l.reset();
		s = "That's a brand new paragraph";
		c2.addParagraph(s);
		check(l.newValue == null, "(root listener: noop)");
		check(l2.newValue.equals(s), "(chapter listener: yep)");

		setTest("no listener - no receiving");
		c2.removePropertyChangeListener("Paragraph", l2);
		l2.reset();
		l.reset();
		s = "That's yet another brand new paragraph";
		c2.addParagraph(s);
		check(l.newValue == null, "(root listener: noop)");
		check(l2.newValue == null, "(chapter listener: noop)");
		
		//	Register again the listeners
		book.addPropertyChangeListener(l);
		c2.addPropertyChangeListener("Paragraph", l2);
		l.removeMode();
		l2.removeMode();
		l2.reset();
		l.reset();
		book.removeChapter(c2);
		out("should have received paragraph events on Chapter and Chapter event on Book");

		//
		//	Make sure that the event is triggered after the property
		//	has changed.
		//
		c = new Chapter();
		c.addParagraph("1. this is a paragraph");
		c.addParagraph("2. this is a paragraph");
		c.addParagraph("3. this is a paragraph");
		c.addParagraph("4. this is a paragraph");
		out("should receive Chapter event and get 4 strings from the event");
		l.traceParagraphs(c);
		book.addChapter(c);
		String[] pp = c.getParagraph();
		String[] pp2 = new String[3];
		pp2[0] = pp[0];
		pp2[1] = pp[3];
		pp2[2] = pp[2];
		out("should receive Chapter event and get 3 strings from the event (1, 4, 3)");
		c.setParagraph(pp2);
		l.traceParagraphs(null);
 	}

}