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