1   // 
2   // Copyright (c) 2003, 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.web.mvc.finders;
29  
30  import junit.framework.TestCase;
31  
32  /***
33   * Generates a view lookup sequence based on a prefix, and fallback sequence.
34   * 
35   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
36   * @version $Id: ViewLookupSequenceTest.java,v 1.7 2004/06/16 08:34:03 fil Exp $
37   */
38  public class ViewLookupSequenceTest extends TestCase
39  {
40      /***
41       * Constructor for ViewLookupSequenceTest.
42       * @param arg0
43       */
44      public ViewLookupSequenceTest(String arg0)
45      {
46          super(arg0);
47      }
48      
49      public void testLookup()
50      {
51          ViewFallbackSequence fallbackSequence = 
52              new ViewFallbackSequence("a.b.c", '.', '/', "Default", false);
53          String[] prefices = { "one", "two" };
54          ViewLookupSequence sequence;
55          sequence = new ViewLookupSequence(prefices, '/', "views", fallbackSequence);
56          assertEquals("one/views/a/b/c/Default", sequence.next());
57          assertEquals("one/views/a/b/Default", sequence.next());
58          assertEquals("one/views/a/Default", sequence.next());
59          assertEquals("one/views/Default", sequence.next());
60          assertEquals("two/views/a/b/c/Default", sequence.next());
61          assertEquals("two/views/a/b/Default", sequence.next());
62          assertEquals("two/views/a/Default", sequence.next());
63          assertEquals("two/views/Default", sequence.next());
64          assertEquals(false, sequence.hasNext());
65          try
66          {
67              sequence.next();
68              fail("exception expected");
69          }
70          catch(Exception e)
71          {
72              // success
73          }
74          sequence.reset();
75          assertEquals(true, sequence.hasNext());
76  
77          prefices = new String[0];
78          sequence = new ViewLookupSequence(prefices, '/', "views", fallbackSequence);
79          assertEquals("views/a/b/c/Default", sequence.next());
80          assertEquals("views/a/b/Default", sequence.next());
81          assertEquals("views/a/Default", sequence.next());
82          assertEquals("views/Default", sequence.next());
83          assertEquals(false, sequence.hasNext());
84          try
85          {
86              sequence.next();
87              fail("exception expected");
88          }
89          catch(Exception e)
90          {
91              // success
92          }
93          sequence.reset();
94          assertEquals(true, sequence.hasNext());
95      }
96      
97      public void testDefault()
98      {
99          Sequence fallbackSequence = 
100             new ViewFallbackSequence("a.b.Default", '.', '/', "Default", false);
101         String[] prefices = { "one", "two" };
102         ViewLookupSequence sequence;
103         sequence = new ViewLookupSequence(prefices, '/', "views", fallbackSequence);
104         assertEquals("one/views/a/b/Default", sequence.next());
105         assertEquals("one/views/a/Default", sequence.next());
106         assertEquals("one/views/Default", sequence.next());
107         assertEquals("two/views/a/b/Default", sequence.next());
108         assertEquals("two/views/a/Default", sequence.next());
109         assertEquals("two/views/Default", sequence.next());
110         assertEquals(false, sequence.hasNext());
111         try
112         {
113             sequence.next();
114             fail("exception expected");
115         }
116         catch(Exception e)
117         {
118             // success
119         }
120         sequence.reset();
121         assertEquals(true, sequence.hasNext());
122     }
123 }