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.trinidadbuild.test;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  
28  import org.apache.shale.test.jmock.AbstractJmockJsfTestCase;
29  import org.jmock.Mock;
30  
31  public abstract class AbstractBaseTestCase extends AbstractJmockJsfTestCase
32  {
33  
34    public AbstractBaseTestCase(String name)
35    {
36      super(name);
37    }
38    
39    protected void setFacesContext(FacesContext context)
40    {
41      FacesTestCase.TestFacesContext.setCurrentInstance(context);
42    }
43  
44    protected Mock buildMockUIComponent()
45    {
46      return buildMockUIComponent(1);
47    }
48  
49    protected Mock buildMockUIComponent(
50      int iterations
51      )
52    {
53      return buildMockUIComponent(iterations, new String[] {"label"});
54    }
55  
56    @Override
57    protected void setUp() throws Exception
58    {
59      super.setUp();
60      // Set up a JSF 1.2 FacesContext
61      FacesContext oldFacesContext = facesContext;
62      UIViewRoot oldViewRoot = oldFacesContext.getViewRoot();
63      oldFacesContext.release();
64      facesContext = new MockFacesContext12(externalContext,
65                                            lifecycle,
66                                            application);
67      facesContext.setViewRoot(oldViewRoot);
68      facesContext.setApplication(application);
69    }
70  
71    @Override
72    protected void tearDown() throws Exception
73    {
74      super.tearDown();
75    }
76  
77    /**
78     * Builds a MockUIComponent with attributes setup for the requested number of
79     * test iterations.
80     */
81    protected Mock buildMockUIComponent(
82      int iterations,
83      String attributeNames[]
84       )
85    {
86      int i;
87      Mock c = mock(UIComponent.class);
88      Map<String, Object> attrs = new HashMap<String, Object>();
89      for (i = 0; i < attributeNames.length; i++)
90        attrs.put(attributeNames[i], attributeNames[i]);
91      for (i = 0; i < iterations; i++)
92      {
93        c.stubs().method("getAttributes").will(returnValue(attrs));
94        c.stubs().method("getId").will(returnValue("mockId"));
95      }
96  
97      return c;
98    }
99  }