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  
20  package org.apache.myfaces.tobago.internal.mock.faces;
21  
22  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
23  import org.apache.myfaces.test.mock.MockFacesContext;
24  import org.apache.myfaces.test.mock.MockHttpServletRequest;
25  import org.apache.myfaces.tobago.component.ComponentTypes;
26  import org.apache.myfaces.tobago.component.UIButton;
27  import org.apache.myfaces.tobago.component.UICommand;
28  import org.apache.myfaces.tobago.component.UIIn;
29  import org.apache.myfaces.tobago.component.UILink;
30  import org.apache.myfaces.tobago.component.UIOut;
31  import org.apache.myfaces.tobago.component.UIPanel;
32  import org.apache.myfaces.tobago.component.UIPopup;
33  import org.apache.myfaces.tobago.component.UIViewRoot;
34  import org.apache.myfaces.tobago.config.TobagoConfig;
35  import org.apache.myfaces.tobago.context.ClientProperties;
36  import org.apache.myfaces.tobago.context.Theme;
37  import org.apache.myfaces.tobago.internal.config.TobagoConfigImpl;
38  import org.apache.myfaces.tobago.internal.context.ResourceManagerFactory;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import java.util.Arrays;
45  import java.util.Collections;
46  import java.util.HashMap;
47  import java.util.Locale;
48  import java.util.Map;
49  
50  /**
51   * <p>Abstract JUnit test case base class, which sets up the JavaServer Faces
52   * mock object environment for a particular simulated request.
53   * </p>
54   * <p>
55   * This is a port of the class AbstractJsfTestCase from myfaces-test12 to JUnit 4.
56   * It also contains Tobago specifics.
57   * </p>
58   */
59  
60  public abstract class AbstractTobagoTestBase extends AbstractJsfTestCase {
61  
62    private static final Logger LOG = LoggerFactory.getLogger(AbstractTobagoTestBase.class);
63  
64    /**
65     * <p>Set up instance variables required by Tobago test cases.</p>
66     */
67    @Before
68    public void setUp() throws Exception {
69  
70      super.setUp();
71  
72      // Tobago specific extensions
73  
74      TobagoConfigImpl tobagoConfig = new TobagoConfigImpl();
75      Theme theme = new MockTheme("default", "Default Mock Theme", Collections.EMPTY_LIST);
76      Theme one = new MockTheme("one", "Mock Theme One", Arrays.asList(theme));
77      Map<String, Theme> availableThemes = new HashMap<String, Theme>();
78      availableThemes.put(theme.getName(), theme);
79      availableThemes.put(one.getName(), one);
80      tobagoConfig.setAvailableThemes(availableThemes);
81      tobagoConfig.resolveThemes();
82      tobagoConfig.initProjectState(servletContext);
83      tobagoConfig.initDefaultValidatorInfo();
84      servletContext.setAttribute(TobagoConfig.TOBAGO_CONFIG, tobagoConfig);
85  
86      final ClientProperties clientProperties = new ClientProperties();
87      clientProperties.setTheme(one);
88      clientProperties.setLocale(Locale.ENGLISH);
89      session.setAttribute(ClientProperties.MANAGED_BEAN_NAME, clientProperties);
90  
91      // XXX is there a better way? Get it from Tobagos generated faces-config.xml?
92      application.addComponent(ComponentTypes.IN, UIIn.class.getName());
93      application.addComponent(ComponentTypes.OUT, UIOut.class.getName());
94      application.addComponent(ComponentTypes.PANEL, UIPanel.class.getName());
95      application.addComponent("javax.faces.ViewRoot", UIViewRoot.class.getName());
96      application.addComponent("javax.faces.Command", javax.faces.component.UICommand.class.getName());
97      application.addComponent(ComponentTypes.COMMAND, UICommand.class.getName());
98      application.addComponent(ComponentTypes.LINK, UILink.class.getName());
99      application.addComponent(ComponentTypes.BUTTON, UIButton.class.getName());
100     application.addComponent(ComponentTypes.POPUP, UIPopup.class.getName());
101 
102     try {
103       ResourceManagerFactory.init(servletContext, tobagoConfig);
104     } catch (AssertionError e) {
105       // ignored in the moment. TODO
106       LOG.error("Todo: remove this hack", e);
107     }
108   }
109 
110   @After
111   public void tearDown() throws Exception {
112     try {
113       ResourceManagerFactory.release(servletContext);
114     } catch (AssertionError e) {
115       // ignored in the moment. TODO
116       LOG.error("Todo: remove this hack", e);
117     }
118 
119     super.tearDown();
120   }
121 
122   public MockFacesContext getFacesContext() {
123     return facesContext;
124   }
125 
126   public MockHttpServletRequest getRequest() {
127     return request;
128   }
129 }