View Javadoc

1   /* $Id: LocationTrackerTestCase.java 1125368 2011-05-20 13:06:27Z simonetripodi $
2    *
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
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.digester3;
20  
21  import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  
26  import java.io.StringReader;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.commons.digester3.Digester;
32  import org.apache.commons.digester3.StackAction;
33  import org.apache.commons.digester3.binder.AbstractRulesModule;
34  import org.junit.Test;
35  import org.xml.sax.Locator;
36  
37  /**
38   * Tests that StackAction can be used to track the source location of objects created from input xml stream.
39   */
40  
41  public class LocationTrackerTestCase
42  {
43  
44      private static class LocationTracker
45          implements StackAction
46      {
47          public Map<Object, String> locations = new HashMap<Object, String>();
48  
49          public <T> T onPush( Digester d, String stackName, T o )
50          {
51              if ( stackName == null )
52              {
53                  // we only care about the real object stack
54  
55                  // note that a Locator object can also provide
56                  // publicId and systemId info.
57                  Locator l = d.getDocumentLocator();
58                  StringBuilder locn = new StringBuilder();
59                  locn.append( "line=" );
60                  locn.append( l.getLineNumber() );
61                  locations.put( o, locn.toString() );
62              }
63              return o;
64          }
65  
66          public <T> T onPop( Digester d, String stackName, T o )
67          {
68              return o;
69          }
70      }
71  
72      @Test
73      public void testAll()
74          throws Exception
75      {
76          final String TEST_XML =
77              "<?xml version='1.0'?>\n" + "<box id='root'>\n" + "  <subBox id='box1'/>\n" + "  <ignoreme/>\n"
78                  + "  <subBox id='box2'/> <subBox id='box3'/>\n" + "</box>";
79  
80          LocationTracker locnTracker = new LocationTracker();
81  
82          Digester digester = newLoader( new AbstractRulesModule()
83          {
84  
85              @Override
86              protected void configure()
87              {
88                  forPattern( "box" ).createObject().ofType( Box.class )
89                      .then()
90                      .setProperties();
91                  forPattern( "box/subBox" ).createObject().ofType( Box.class )
92                      .then()
93                      .setProperties()
94                      .then()
95                      .setNext( "addChild" );
96              }
97  
98          })
99          .setStackAction( locnTracker )
100         .newDigester();
101 
102         Box root = digester.parse( new StringReader( TEST_XML ) );
103         assertNotNull( root );
104         List<Box> children = root.getChildren();
105         assertEquals( 3, children.size() );
106         Box box1 = children.get( 0 );
107         Box box2 = children.get( 1 );
108         Box box3 = children.get( 2 );
109 
110         assertEquals( "line=2", locnTracker.locations.get( root ) );
111         assertEquals( "line=3", locnTracker.locations.get( box1 ) );
112         assertEquals( "line=5", locnTracker.locations.get( box2 ) );
113         assertEquals( "line=5", locnTracker.locations.get( box3 ) );
114     }
115 }