    001    package org.apache.turbine.services.intake;
    002
    003    /*
    004     * Licensed to the Apache Software Foundation (ASF) under one
    005     * or more contributor license agreements.  See the NOTICE file
    006     * distributed with this work for additional information
    007     * regarding copyright ownership.  The ASF licenses this file
    008     * to you under the Apache License, Version 2.0 (the
    009     * "License"); you may not use this file except in compliance
    010     * with the License.  You may obtain a copy of the License at
    011     *
    012     *   http://www.apache.org/licenses/LICENSE-2.0
    013     *
    014     * Unless required by applicable law or agreed to in writing,
    015     * software distributed under the License is distributed on an
    016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017     * KIND, either express or implied.  See the License for the
    018     * specific language governing permissions and limitations
    019     * under the License.
    020     */
    021
    022    import java.io.File;
    023    import java.util.Vector;
    024
    025    import javax.servlet.ServletConfig;
    026    import javax.servlet.http.HttpServletResponse;
    027
    028    import org.apache.fulcrum.intake.IntakeService;
    029    import org.apache.fulcrum.intake.model.Group;
    030    import org.apache.fulcrum.parser.DefaultParameterParser;
    031    import org.apache.turbine.om.security.User;
    032    import org.apache.turbine.services.TurbineServices;
    033    import org.apache.turbine.services.rundata.RunDataService;
    034    import org.apache.turbine.test.BaseTestCase;
    035    import org.apache.turbine.test.EnhancedMockHttpServletRequest;
    036    import org.apache.turbine.util.RunData;
    037    import org.apache.turbine.util.TurbineConfig;
    038
    039    import com.mockobjects.servlet.MockHttpServletResponse;
    040    import com.mockobjects.servlet.MockHttpSession;
    041    import com.mockobjects.servlet.MockServletConfig;
    042
    043    /**
    044     * Unit test for Intake Tool, wrapping the Fulcrum Intake service.
    045     *
    046     * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    047     * @version $Id: IntakeToolTest.java 615328 2008-01-25 20:25:05Z tv $
    048     */
    049    public class IntakeToolTest extends BaseTestCase
    050    {
    051        private static TurbineConfig tc = null;
    052        public IntakeToolTest(String name) throws Exception
    053        {
    054            super(name);
    055        }
    056        public void testGet() throws Exception
    057        {
    058            IntakeTool intakeTool = new IntakeTool();
    059            intakeTool.init(getRunData());
    060            File file = new File("./target/appData.ser");
    061            assertTrue(
    062                "Make sure serialized data file exists:" + file,
    063                file.exists());
    064            Group group = intakeTool.get("LoginGroup","loginGroupKey");
    065            assertNotNull(group);
    066            assertEquals("loginGroupKey", group.getGID());
    067            assertEquals("LoginGroup", group.getIntakeGroupName());
    068        }
    069
    070
    071        /**
    072         * Make sure refresh DOESN'T do anything
    073         * @throws Exception
    074         */
    075        public void testRefresh() throws Exception
    076        {
    077            IntakeTool intakeTool = new IntakeTool();
    078            intakeTool.init(getRunData());
    079            int numberOfGroups = intakeTool.getGroups().size();
    080            intakeTool.refresh();
    081            assertEquals(numberOfGroups,intakeTool.getGroups().size());
    082        }
    083        private RunData getRunData() throws Exception
    084        {
    085            RunDataService rds =
    086                (RunDataService) TurbineServices.getInstance().getService(
    087                    RunDataService.SERVICE_NAME);
    088            EnhancedMockHttpServletRequest request =
    089                new EnhancedMockHttpServletRequest();
    090            request.setupServerName("bob");
    091            request.setupGetProtocol("http");
    092            request.setupScheme("scheme");
    093            request.setupPathInfo("damn");
    094            request.setupGetServletPath("damn2");
    095            request.setupGetContextPath("wow");
    096            request.setupGetContentType("html/text");
    097            request.setupAddHeader("Content-type", "html/text");
    098            request.setupAddHeader("Accept-Language", "en-US");
    099            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    }