File: TestAttr.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 (503 lines) | stat: -rw-r--r-- 12,968 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
 * 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.
 */
/*
 *	TestAttr - test the attribute features
 *
 *	TestAttr.dtd and TestAttr.xml has to be kept in sync with this test.
 */

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

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


public class TestAttr extends BaseTest
{
    public static void main(String[] argv) {
        BaseTest o = new TestAttr();
        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;
		boolean			mute;
		boolean			remove;
		
		public MyListener(BaseBean bean)
		{
			this.listenerName = bean.name();
			gm = bean.graphManager();
			this.remove = false;
			this.mute = false;
			out("new listener for " + this.listenerName);
		}

		public void reset()
		{
			this.oldValue = null;
			this.newValue = null;
			this.propertyName = null;
			this.source = null;
		}
		
		public void propertyChange(PropertyChangeEvent e)
		{
			if (this.mute)
				return;
			
			String k;
			this.oldValue = e.getOldValue();
			this.newValue = e.getNewValue();
			this.propertyName = e.getPropertyName();
			this.source = e.getSource();
			
			String 	n = this.propertyName;
			int 	i = gm.getPropertyIndex(n);
			String 	pn;
			
			if (gm.isAttribute(n))
			{
				pn = "Attr:" + gm.getPropertyName(n);
				if (i != -1)
					pn += "[" + i + "]";
				pn += "." + gm.getAttributeName(n);
			}
			else
			{
				pn = "Prop:" + gm.getPropertyName(n);
				if (i != -1)
					pn += "[" + i + "]";
			}
				
			if (this.newValue == null)
				k = "Rmv";
			else
			if (this.oldValue == null)
				k = "Add";
			else
				k = "Chg";
			
			out("<" + k + " Lnr:" + this.listenerName + " Evt:" + n + 
				" Src:" + this.source.getClass().getName() + ">");
			
			if (remove)
			{
				out("<" +  pn + " - ParentName: " +
					gm.getPropertyParentName(n) + ">");
			}
			else
			{
				BaseBean bb = gm.getPropertyParent(n);
				String nm = "<no class>";
				
				if (bb != null)
					nm = bb.getClass().getName();

				out("<" + pn + " - ParentName: " +
					gm.getPropertyParentName(n) +
					" - ParentClass:" + nm + ">");
			}

			//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 void mute(boolean mute)
		{
			this.mute = mute;
		}
		
		public String toString()
		{
			return this.name() + " raised from source " +
				this.source.getClass().getName();
		}
	}

	private MyListener l1;

	public void run()
		throws Exception
	{
		Book book;

		this.readDocument();
		out("creating the bean graph");
		//out(DDFactory.XmlToString(doc));
		book = Book.createGraph(doc);
		out("bean graph created");

		l1 = new MyListener(book);
		book.addPropertyChangeListener(l1);
		l1.mute(true);
				
		//out(book.dumpBeanNode());
		//out(((BaseBean)book.clone()).dumpBeanNode());
		//((BaseBean)book.clone()).write(System.out);
		
		//	Get/Change an enum attribute on the root
		{
			String s1, s2;
			
			setTest("get enum attribute from root");
			s1 = book.getAttributeValue("Good");
			check(s1.equals("no"));
			out("Changing to another value - should get an event");
			s2 = "yes";
			l1.mute(false);
			book.setAttributeValue("good", s2);
			l1.mute(false);
			s1 = book.getAttributeValue("good");
			check(s1.equals(s2));
			out("Book DOM content should be yes", book.dumpDomNode(0));
			out("Changing to a non-enum value (should get an exception)");
			boolean gotException = false;
			try
			{
				book.setAttributeValue("good", "maybe");
			}
			catch(IllegalArgumentException e)
			{
				check(true, "got the proper exception");
				gotException = true;
			}
			catch(Exception e)
			{
				check(false, "got the wrong exception type: " +
					  e.getClass().getName() + ", it should be " +
					  "IllegalArgumentException");
				gotException = true;
			}
			if (!gotException)
				check(false, "didnt' get any exception");
		}

		//	Get/Change attributes on a non-root element
		{
			String s1, s2;
			
			setTest("get #REQUIRED attribute");
			s1 = book.getAttributeValue("Summary", "length");
			check(s1.equals("132"));
			out("Changing to another value");
			s2 = "133";
			book.setAttributeValue("Summary", "length", s2);
			s1 = book.getAttributeValue("Summary", "length");
			check(s1.equals(s2));

			setTest("get #IMPLIED attribute");
			s1 = book.getAttributeValue("Summary", "lang");
			check(s1.equals("en"));
			out("Setting a new value");
			s2 = "fr";
			book.setAttributeValue("Summary", "lang", s2);
			s1 = book.getAttributeValue("Summary", "lang");
			check(s1.equals(s2));
			s2 = "";
			book.setAttributeValue("Summary", "lang", s2);
			s1 = book.getAttributeValue("Summary", "lang");
			check(s1.equals(s2));
			book.setAttributeValue("Summary", "lang", null);
			s1 = book.getAttributeValue("Summary", "lang");
			check(s1 == null);
			s2 = "fr";
			book.setAttributeValue("Summary", "lang", s2);

			
			setTest("get #FIXED attribute");
			s1 = book.getAttributeValue("Summary", "size");
			check(s1.equals("12"));
			out("Summary DOM content should be 133/fr/12",
				book.dumpDomNode(1));
			out("Setting a new value (should get an exception)");
			s2 = "15";
			boolean gotException = false;
			try
			{
				book.setAttributeValue("Summary", "size", s2);
			}
			catch(IllegalStateException e)
			{
				check(true, "got the proper exception");
				gotException = true;
			}
			catch(Exception e)
			{
				check(false, "got the wrong exception type: " +
					  e.getClass().getName() + ", it should be " +
					  "IllegalStateException");
				gotException = true;
			}
			if (!gotException)
				check(false, "didnt' get any exception");
		}

		//	Set from non defined
		{
			String s1, s2, s3, s4;
			
			setTest("get/set non set #IMPLIED attribute");
			Chapter c = book.getChapter(0);
			out("Chapter DOM should have no attribute",
				c.dumpDomNode(0));
			s1 = c.getAttributeValue("title");
			check(s1 == null);
			s2 = "My chapter";
			c.setAttributeValue("title", s2);
			s1 = c.getAttributeValue("title");
			check(s1.equals(s2));
			out("Chapter DOM should have one title attribute",
				c.dumpDomNode(0));

			//	Check that we access the same from the bean itself
			//	and from the parent that contains the attribute.
			setTest("access from parent & current bean");
			s1 = book.getAttributeValue("Chapter", 0, "title");
			s2 = c.getAttributeValue("title");
			check(s1.equals(s2));			

			//	Mix the elements, the attributes should follow
			setTest("attribute stick with elt when mixing");
			s1 = book.getAttributeValue("Chapter", 0, "title");
			s2 = book.getAttributeValue("Chapter", 1, "title");
			check(s1 != null);
			check(s2 == null);
			Chapter[] ac = book.getChapter();
			c = ac[1];
			ac[1] = ac[0];
			ac[0] = c;
			book.setChapter(ac);
			// Attribute of 0 should be what 1 was, and 1 what 0 was
			s3 = book.getAttributeValue("Chapter", 0, "title");
			s4 = book.getAttributeValue("Chapter", 1, "title");
			check(s3 == null);
			check(s4.equals(s1));
			
			setTest("get/set non set #IMPLIED attribute (idx != 0)");
			c = book.getChapter(2);
			out("Chapter DOM should have no attribute",
				c.dumpDomNode(0));
			s1 = c.getAttributeValue("title");
			check(s1 == null);
			s2 = "My chapter2";
			c.setAttributeValue("title", s2);
			s1 = c.getAttributeValue("title");
			check(s1.equals(s2));
			out("Chapter DOM should have one title attribute",
				c.dumpDomNode(0));
		}

		//	Test unknown attribute
		{
			String s1;
			
			setTest("get unknown attribute");
			boolean gotException = false;
			try
			{
				s1 = book.getAttributeValue("Summary", "Splash");
			}
			catch(IllegalArgumentException e)
			{
				check(true, "got the proper exception");
				gotException = true;
			}
			catch(Exception e)
			{
				check(false, "got the wrong exception type: " +
					  e.getClass().getName() + ", it should be " +
					  "IllegalArgumentException");
				gotException = true;
			}
			if (!gotException)
				check(false, "didnt' get any exception");
		}

		//	Add a brand new element with attributes
		{
			l1.mute(true);
                        String s1, s2;
			setTest("add a brand new element with default attributes");
			Index idx = new Index();
			int i = book.addIndex(idx);
			s1 = idx.getAttributeValue("cross-ref");
			s2 = book.getAttributeValue("Index", i, "CrossRef");
			check(s1.equals(s2));
			out("should have created: cross-ref & glossary, and not color",
				book.dumpDomNode(1));

			//	Add a brand new element, setting attributes
			setTest("add a brand new element, setting attributes");
			idx = new Index();
			idx.setAttributeValue(Index.CROSSREF, "yes");
			idx.setAttributeValue("color", "blue");
			idx.setWord("my word");
			idx.setAttributeValue("word", "freq", "123");
			book.setIndex(i, idx);
			out("should have created: cross-ref (yes), glossary (nope) " +
				"and color (blue)", book.dumpDomNode(3));
		}

		//	Dynamic parsing of the graph of beans
		{
			BaseBean root = book.graphManager().getBeanRoot();
			this.parseGraph(root, "\t");
		}

        out("Make sure that default attributes get set.");
        Book anotherBook = Book.createGraph();
        anotherBook.setSummary("This is my summary.");
        out(anotherBook);

        setTest("attributes as properties");
        book.setGood("no");
        check("no".equals(book.getGood()));
        out("Checking chapter title");
        Chapter chap0 = book.getChapter(0);
        chap0.setTitle("My title");
        out(chap0.getTitle());

		/*setTest("get non set attribute");
		book.setAttributeValue("Summary", "lang", "");
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		book.write(bout);
		out("--------------------");
		out(bout.toString());

		book.setAttributeValue("Summary", "lang", null);
		String s1 = book.getAttributeValue("Summary", "lang");
		check(s1 == null);
		bout = new ByteArrayOutputStream();
		book.write(bout);
		out("---- 222 ----------------");
		out(bout.toString());
		*/

        // Test cloned attributes
        Chapter tca = new Chapter();
        tca.setTitle("Dolly: A Good Clone & A Day");
        out("Title before cloning:");
        out(tca.getTitle());
        Chapter theClone = (Chapter) tca.clone();
        out("Title after cloning:");
        out(theClone.getTitle());
        Book fullGraph = Book.createGraph();
        fullGraph.addChapter(theClone);
        out("And here is the clone in it's own graph");
        out(fullGraph);
	}

	void parseGraph(BaseBean bean, String indent)
	{
		if (bean == null)
			return;
		
		out(indent + "[" + bean.name() + "]"); 
		
		BaseProperty[] props = bean.listProperties();
		for (int i=0; i<props.length; i++)
		{
			BaseProperty p = props[i];
			String		 name = p.getName();

			//	Prop name & size
			String str = "<" + name;
			if (p.isIndexed())
				str += "[" + p.size() + "]";
			str += "> - " + p.getPropertyClass();
			out(indent + str);

			//	Prop attributes
			String[] attrs = p.getAttributeNames();
			for (int j=0; j<attrs.length; j++)
				out(indent + " a:" + attrs[j]);

			//	recurse
			if (p.isBean() && p.isIndexed())
			{
				BaseBean[] ba = (BaseBean[])bean.getValues(name);
				for (int k=0; k<ba.length; k++)
					this.parseGraph(ba[k], indent + "\t");
			}
			else
			if (p.isBean())
			{
				BaseBean b = (BaseBean)bean.getValue(name);
				this.parseGraph(b, indent + "\t");				
			}
		}
	}
}