View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.mock;
20  
21  import org.apache.myfaces.application.DefaultResourceHandlerSupport;
22  import org.apache.myfaces.shared.resource.ClassLoaderResourceLoader;
23  import org.apache.myfaces.shared.resource.ResourceLoader;
24  
25  /**
26   * Redirect resource request to the directory where the test class is,
27   * to make easier test composite components.
28   * 
29   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
30   * @version $Revision: 1151650 $ $Date: 2011-07-27 17:14:17 -0500 (Wed, 27 Jul 2011) $
31   *
32   */
33  public class MockResourceHandlerSupport extends DefaultResourceHandlerSupport
34  {
35      private ResourceLoader[] _resourceLoaders;
36      
37      private Class _referenceClass;
38      
39      public MockResourceHandlerSupport(Class class1)
40      {
41          super();
42          _referenceClass = class1;
43      }
44  
45      public ResourceLoader[] getResourceLoaders()
46      {
47          if (_resourceLoaders == null)
48          {
49              //The ExternalContextResourceLoader has precedence over
50              //ClassLoaderResourceLoader, so it goes first.
51              _resourceLoaders = new ResourceLoader[] {
52                      new ClassLoaderResourceLoader(getDirectory())
53              };
54          }
55          return _resourceLoaders;
56      }
57      
58      private String getDirectory()
59      {
60          return _referenceClass.getName().substring(0,
61                  _referenceClass.getName().lastIndexOf('.')).replace('.', '/');
62      }
63  }