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