1   // 
2   // Copyright (c) 2003-2005, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.visitor;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import junit.framework.TestCase;
36  
37  
38  /***
39   * Tests for the general-purpose visitor.
40   *
41   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
42   * @version $Id: VisitorTest.java,v 1.4 2006/02/08 18:26:51 zwierzem Exp $
43   */
44  public class VisitorTest
45      extends TestCase
46  {
47      private List<String> recorder = new ArrayList<String>();
48      
49      public void setUp()
50      {
51          recorder.clear();
52      }
53      
54      public void testBreadthFirst()
55      {
56          Item g = new Item("a", new Item("b", new Item("c", null)));
57          ItemVisitor iv = new ItemRecorderVisitor();
58          iv.traverseBreadthFirst(g);
59          assertEquals("[a, b, c]", recorder.toString());
60      }
61  
62      public void testDepthFirst()
63      {
64          Item g = new Item("a", new Item("b", new Item("c", null)));
65          ItemVisitor iv = new ItemRecorderVisitor();
66          iv.traverseDepthFirst(g);
67          assertEquals("[c, b, a]", recorder.toString());
68      }
69      
70      public void testSelective()
71      {
72          Item g = new Item("a", new SubItem("b", new Item("c", null)));
73          ItemVisitor iv = new SubItemOnlyRecorderVisitor();
74          iv.traverseBreadthFirst(g);
75          assertEquals("[b]", recorder.toString());        
76      }
77      
78      public void testPolymorphic()
79      {
80          Item g = new Item("a", new SubItem("b", new Item("c", null)));
81          ItemVisitor iv = new PolymorphicRecorderVisitor();
82          iv.traverseBreadthFirst(g);
83          assertEquals("[a, B, c]", recorder.toString());                
84      }
85  
86      public void testInexactPolymorphic()
87      {
88          Item g = new Item("a", new SubItem("b", new SubSubItem("c", null)));
89          ItemVisitor iv = new InexactPolymorphicRecorderVisitor();
90          iv.traverseBreadthFirst(g);
91          assertEquals("[a, B, c]", recorder.toString());
92      }   
93      
94      public void testInnerVisitor()
95      {
96          Item g = new Item("a", new SubItem("b", new SubSubItem("c", null)));
97          final List localRecorder = new ArrayList();
98          ItemVisitor iv = new ItemVisitor()
99          {
100             public void visit(Item i)
101             {
102                 localRecorder.add(i.value());
103             }
104         };
105         iv.traverseBreadthFirst(g);
106         assertEquals("[a, b, c]", localRecorder.toString());
107     }
108     
109     public class ItemRecorderVisitor
110         extends ItemVisitor
111     {
112         public void visit(Item i)
113         {
114             recorder.add(i.value());
115         }
116     };
117     
118     public class SubItemOnlyRecorderVisitor
119         extends ItemVisitor
120     {
121         public void visit(SubItem i)
122         {
123             recorder.add(i.value());
124         }
125     }
126     
127     public class PolymorphicRecorderVisitor
128         extends ItemVisitor
129     {        
130         public void visit(Item i)
131         {
132             recorder.add(i.value());
133         }        
134 
135         public void visit(SubItem i)
136         {
137             recorder.add(i.value().toUpperCase());
138         }
139     }
140     
141     public class InexactPolymorphicRecorderVisitor
142         extends ItemVisitor
143     {
144         // in case of inexact match will be prefered over visit(SubItem) due to lower order value
145         @DispatchOrder(1)
146         public void visit(Item i)
147         {
148             recorder.add(i.value());
149         }        
150 
151         @DispatchOrder(2)
152         public void visit(SubItem i)
153         {
154             recorder.add(i.value().toUpperCase());
155         }        
156     }
157     
158     public static class ItemVisitor
159         extends Visitor<Item>
160     {
161         @Override
162         protected Iterator<Item> successors(Item o)
163         {
164             Item next = o.next();
165             if(next == null)
166             {
167                 List<Item> list = Collections.emptyList(); 
168                 return list.iterator();
169             }
170             else
171             {
172                 return Collections.singletonList(next).iterator();
173             }
174         }
175     }
176 
177     public static class Item
178     {
179         private final String value;
180         
181         private final Item next;
182         
183         public Item(String valueArg, Item nextArg)
184         {
185             value = valueArg;
186             next = nextArg;
187         }
188         
189         public String value()
190         {
191             return value;
192         }
193         
194         public Item next()
195         {
196            return next;
197         }
198     }
199     
200     public static class SubItem
201         extends Item
202     {
203         public SubItem(String valueArg, Item nextArg)
204         {
205             super(valueArg, nextArg);
206         }
207     }
208 
209     public static class SubSubItem
210         extends SubItem
211     {
212         public SubSubItem(String valueArg, Item nextArg)
213         {
214             super(valueArg, nextArg);
215         }
216     }
217 }