View Javadoc

1   package org.apache.turbine.services.intake;
2   
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  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.File;
27  import java.util.Vector;
28  
29  import javax.servlet.ServletConfig;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.fulcrum.intake.IntakeService;
33  import org.apache.fulcrum.intake.model.Group;
34  import org.apache.fulcrum.parser.DefaultParameterParser;
35  import org.apache.turbine.om.security.User;
36  import org.apache.turbine.services.TurbineServices;
37  import org.apache.turbine.services.rundata.RunDataService;
38  import org.apache.turbine.test.BaseTestCase;
39  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
40  import org.apache.turbine.util.RunData;
41  import org.apache.turbine.util.TurbineConfig;
42  import org.junit.AfterClass;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  
46  import com.mockobjects.servlet.MockHttpServletResponse;
47  import com.mockobjects.servlet.MockHttpSession;
48  import com.mockobjects.servlet.MockServletConfig;
49  
50  /**
51   * Unit test for Intake Tool, wrapping the Fulcrum Intake service.
52   *
53   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
54   * @version $Id: IntakeToolTest.java 1606111 2014-06-27 14:46:47Z gk $
55   */
56  public class IntakeToolTest extends BaseTestCase
57  {
58      private static TurbineConfig tc = null;
59  
60      @Test public void testGet() throws Exception
61      {
62          IntakeTool intakeTool = new IntakeTool();
63          intakeTool.init(getRunData());
64          File file = new File("./target/appData.ser");
65          assertTrue(
66              "Make sure serialized data file exists:" + file,
67              file.exists());
68          Group group = intakeTool.get("LoginGroup","loginGroupKey");
69          assertNotNull(group);
70          assertEquals("loginGroupKey", group.getGID());
71          assertEquals("LoginGroup", group.getIntakeGroupName());
72      }
73  
74  
75      /**
76       * Make sure refresh DOESN'T do anything
77       * @throws Exception
78       */
79      @Test public void testRefresh() throws Exception
80      {
81          IntakeTool intakeTool = new IntakeTool();
82          intakeTool.init(getRunData());
83          int numberOfGroups = intakeTool.getGroups().size();
84          intakeTool.refresh();
85          assertEquals(numberOfGroups,intakeTool.getGroups().size());
86      }
87      private RunData getRunData() throws Exception
88      {
89          RunDataService rds =
90              (RunDataService) TurbineServices.getInstance().getService(
91                  RunDataService.SERVICE_NAME);
92          EnhancedMockHttpServletRequest request =
93              new EnhancedMockHttpServletRequest();
94          request.setupServerName("bob");
95          request.setupGetProtocol("http");
96          request.setupScheme("scheme");
97          request.setupPathInfo("damn");
98          request.setupGetServletPath("damn2");
99          request.setupGetContextPath("wow");
100         request.setupGetContentType("html/text");
101         request.setupAddHeader("Content-type", "html/text");
102         request.setupAddHeader("Accept-Language", "en-US");
103         Vector<String> v = new Vector<String>();
104         request.setupGetParameterNames(v.elements());
105         MockHttpSession session = new MockHttpSession();
106         session.setupGetAttribute(User.SESSION_KEY, null);
107         request.setSession(session);
108         HttpServletResponse response = new MockHttpServletResponse();
109         ServletConfig config = new MockServletConfig();
110         RunData runData = rds.getRunData(request, response, config);
111         assertEquals("Verify we are using Fulcrum parameter parser",DefaultParameterParser.class,runData.getParameters().getClass());
112         return runData;
113     }
114 
115     @BeforeClass
116     public static void setUp() throws Exception
117     {
118         tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
119         tc.initialize();
120         TurbineServices.getInstance().getService(IntakeService.class.getName());
121     }
122 
123     @AfterClass
124     public static void tearDown() throws Exception
125     {
126         if (tc != null)
127         {
128             tc.dispose();
129         }
130     }
131 }