1
/* $Id: DigesterTestCase.java 156414 2005-03-07 10:12:40Z skitching $
1
/* $Id: DigesterTestCase.java 479207 2006-11-25 21:53:07Z craigmcc $
3
* Copyright 2001-2004 The Apache Software Foundation.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
3
* Licensed to the Apache Software Foundation (ASF) under one or more
4
* contributor license agreements. See the NOTICE file distributed with
5
* this work for additional information regarding copyright ownership.
6
* The ASF licenses this file to You under the Apache License, Version 2.0
7
* (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
10
* http://www.apache.org/licenses/LICENSE-2.0
19
20
package org.apache.commons.digester;
22
import java.io.StringReader;
21
23
import java.math.BigDecimal;
22
24
import java.net.URL;
23
import java.io.StringReader;
25
import java.util.ArrayList;
26
import java.util.EmptyStackException;
24
27
import java.util.Iterator;
25
import java.util.List;
26
28
import java.util.Map;
27
import java.util.EmptyStackException;
29
30
import junit.framework.Test;
30
31
import junit.framework.TestCase;
31
32
import junit.framework.TestSuite;
34
import org.xml.sax.Attributes;
33
35
import org.xml.sax.ErrorHandler;
34
import org.xml.sax.Attributes;
36
import org.xml.sax.InputSource;
35
37
import org.xml.sax.helpers.AttributesImpl;
36
import org.xml.sax.InputSource;
497
496
assertNotNull("root object not retrieved", root);
498
497
assertTrue("root object not a TestRule instance", (root instanceof TestBean));
500
/** Utility class for method testStackAction */
501
private static class TrackingStackAction implements StackAction {
502
public ArrayList events = new ArrayList();
503
public Object onPush(Digester d, String stackName, Object o) {
504
String msg = "push:" + stackName + ":" + o.toString();
507
String str = o.toString();
508
if (str.startsWith("replpush")) {
509
return new String(str);
514
public Object onPop(Digester d, String stackName, Object o) {
515
String msg = "pop:" + stackName + ":" + o.toString();
517
String str = o.toString();
518
if (str.startsWith("replpop")) {
519
return new String(str);
527
* Test custom StackAction subclasses.
529
public void testStackAction() {
530
TrackingStackAction action = new TrackingStackAction();
532
Object obj1 = new String("obj1");
533
Object obj2 = new String("obj2");
534
Object obj3 = new String("replpop.obj3");
535
Object obj4 = new String("replpush.obj4");
537
Object obj8 = new String("obj8");
538
Object obj9 = new String("obj9");
540
Digester d = new Digester();
541
d.setStackAction(action);
543
assertEquals(0, action.events.size());
549
assertNotNull(d.peek(0));
550
// for obj4, a copy should have been pushed
551
assertFalse(obj4 == d.peek(0));
552
assertEquals(obj4, d.peek(0));
553
// for obj3, replacement only occurs on pop
554
assertSame(obj3, d.peek(1));
555
assertSame(obj2, d.peek(2));
556
assertSame(obj1, d.peek(3));
558
Object obj4a = d.pop();
559
Object obj3a = d.pop();
560
Object obj2a = d.pop();
561
Object obj1a = d.pop();
563
assertFalse(obj4 == obj4a);
564
assertEquals(obj4, obj4a);
565
assertFalse(obj3 == obj4a);
566
assertEquals(obj3, obj3a);
567
assertSame(obj2, obj2a);
568
assertSame(obj1, obj1a);
570
d.push("stack1", obj8);
571
d.push("stack1", obj9);
572
Object obj9a = d.pop("stack1");
573
Object obj8a = d.pop("stack1");
575
assertSame(obj8, obj8a);
576
assertSame(obj9, obj9a);
578
assertEquals(12, action.events.size());
579
assertEquals("push:null:obj1", action.events.get(0));
580
assertEquals("push:null:obj2", action.events.get(1));
581
assertEquals("push:null:replpop.obj3", action.events.get(2));
582
assertEquals("push:null:replpush.obj4", action.events.get(3));
583
assertEquals("pop:null:replpush.obj4", action.events.get(4));
584
assertEquals("pop:null:replpop.obj3", action.events.get(5));
585
assertEquals("pop:null:obj2", action.events.get(6));
586
assertEquals("pop:null:obj1", action.events.get(7));
588
assertEquals("push:stack1:obj8", action.events.get(8));
589
assertEquals("push:stack1:obj9", action.events.get(9));
590
assertEquals("pop:stack1:obj9", action.events.get(10));
591
assertEquals("pop:stack1:obj8", action.events.get(11));