View Javadoc

1   /*
2    * $Id: EnhancedTilesApplicationContextTest.java 527536 2007-04-11 15:44:51Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.context.enhanced;
22  
23  import junit.framework.TestCase;
24  import org.apache.tiles.TilesApplicationContext;
25  import org.easymock.EasyMock;
26  
27  import java.io.IOException;
28  import java.net.MalformedURLException;
29  import java.net.URL;
30  import java.util.Enumeration;
31  import java.util.Vector;
32  import java.util.HashSet;
33  
34  
35  /***
36   * @version $Rev: 527536 $ $Date: 2007-04-11 17:44:51 +0200 (Wed, 11 Apr 2007) $
37   */
38  public class EnhancedTilesApplicationContextTest extends TestCase {
39  
40      /***
41       * Number of properties container inside the test.properties file.
42       */
43      private static final int TEST_PROPERTIES_SIZE = 4;
44  
45      /***
46       * The root Tiles application context.
47       */
48      private TilesApplicationContext root;
49  
50      /***
51       * The enhanced Tiles application context.
52       */
53      private EnhancedTilesApplicationContext context;
54  
55      /*** {@inheritDoc} */
56      @Override
57      public void setUp() {
58          root = EasyMock.createMock(TilesApplicationContext.class);
59          context = new EnhancedTilesApplicationContext(root);
60      }
61  
62      /***
63       * Tests resource getting.
64       *
65       * @throws IOException If something goes wrong.
66       */
67      public void testGetResources() throws IOException {
68          ClassLoader original = Thread.currentThread().getContextClassLoader();
69          try {
70              String url = "test.properties";
71              HashSet<URL> set = new HashSet<URL>();
72              URL u = new URL("file://tiles/test.properties");
73              set.add(u);
74              EasyMock.expect(root.getResources(url)).andReturn(set);
75              EasyMock.replay(root);
76              Thread.currentThread().setContextClassLoader(new MockClassLoader());
77  
78              assertEquals(TEST_PROPERTIES_SIZE, context.getResources(
79                      "test.properties").size());
80              EasyMock.verify(root);
81          } finally {
82              Thread.currentThread().setContextClassLoader(original);
83          }
84      }
85  
86      /***
87       * An mock class loader.
88       */
89      public class MockClassLoader extends ClassLoader {
90  
91          /***
92           * A vector of resources.
93           */
94          private Vector<URL> resources;
95  
96          /***
97           * Constructor.
98           *
99           * @throws MalformedURLException If the URL is not valid (that should
100          * not happen).
101          */
102         public MockClassLoader() throws MalformedURLException {
103             resources = new Vector<URL>();
104             resources.add(new URL("file://tiles/test/test.properties"));
105             resources.add(new URL("file://tiles/two/test.properties"));
106             resources.add(new URL("file://tiles/three/test.properties"));
107         }
108 
109         /*** {@inheritDoc} */
110         @Override
111         public Enumeration<URL> findResources(String path) {
112             return resources.elements();
113         }
114     }
115 
116 
117 }