Author: painter Date: Thu Jan 17 13:19:52 2019 New Revision: 1851528 URL: http://svn.apache.org/viewvc?rev=1851528&view=rev Log: Add unit tests for CookieParser
Added: turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/CookieParserTest.java Modified: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultCookieParser.java Modified: turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultCookieParser.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultCookieParser.java?rev=1851528&r1=1851527&r2=1851528&view=diff ============================================================================== --- turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultCookieParser.java (original) +++ turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/DefaultCookieParser.java Thu Jan 17 13:19:52 2019 @@ -107,14 +107,17 @@ public class DefaultCookieParser setCharacterEncoding(enc != null ? enc : "US-ASCII"); Cookie[] cookies = request.getCookies(); - getLogger().debug ("Number of Cookies "+cookies.length); - - for (Cookie cookie : cookies) + if ( cookies != null ) { - String name = convert(cookie.getName()); - String value = cookie.getValue(); - getLogger().debug ("Adding " + name + "=" + value); - add(name, value); + getLogger().debug ("Number of Cookies "+cookies.length); + + for (Cookie cookie : cookies) + { + String name = convert(cookie.getName()); + String value = cookie.getValue(); + getLogger().debug ("Adding " + name + "=" + value); + add(name, value); + } } this.request = request; @@ -127,14 +130,16 @@ public class DefaultCookieParser */ public void set (String name, String value) { - set (name, value, AGE_SESSION); + set(name, value, AGE_SESSION); } - /** - * Set a persisten cookie on the client that will expire + /* (non-Javadoc) + * @see org.apache.fulcrum.parser.CookieParser#set(java.lang.String, java.lang.String, int) + * + * Set a persistent cookie on the client that will expire * after a maximum age (given in seconds). */ - public void set (String name, String value, int seconds_age) + public void set(String name, String value, int seconds_age) { if (response == null) { @@ -146,18 +151,29 @@ public class DefaultCookieParser cookie.setPath(request.getServletPath()); response.addCookie(cookie); } + - /** + /* (non-Javadoc) + * @see org.apache.fulcrum.parser.CookieParser#unset(java.lang.String) + * * Remove a previously set cookie from the client machine. + * */ - public void unset (String name) + public void unset(String name) { - set (name, " ", AGE_DELETE); + set(name, " ", AGE_DELETE); } + /* (non-Javadoc) + * @see org.apache.fulcrum.parser.BaseValueParser#isValid() + */ public boolean isValid() { - return true; + if ( this.request == null ) + { + return true; + } + return false; } } Added: turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/CookieParserTest.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/CookieParserTest.java?rev=1851528&view=auto ============================================================================== --- turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/CookieParserTest.java (added) +++ turbine/fulcrum/trunk/parser/src/test/org/apache/fulcrum/parser/CookieParserTest.java Thu Jan 17 13:19:52 2019 @@ -0,0 +1,107 @@ +package org.apache.fulcrum.parser; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.avalon.framework.component.ComponentException; +import org.apache.fulcrum.testcontainer.BaseUnit5Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + + + +/** + * Test the DefaultCookieParser class. Since CookieParser + * extends ValueParser, we only need to add test cases to insure + * that the CookieParser can be loaded and test setting and un-setting cookies + * + * @author <a href="mailto:pain...@apache.org">Jeffery Painter</a> + * @version $Id: CookieParserTest.java 222043 2019-01-17 08:17:33Z painter $ + */ +public class CookieParserTest extends BaseUnit5Test +{ + + private DefaultCookieParser parser; + private ParserService parserService; + + /** + * Performs any initialization that must happen before each test is run. + * @throws Exception if parser service not found + */ + @BeforeEach + public void setUp() throws Exception + { + try + { + parserService = (ParserService) this.lookup(ParserService.ROLE); + parser = parserService.getParser(DefaultCookieParser.class); + + // Populate parser with mock servlet data + HttpServletRequest request = getMockRequest(); + HttpServletResponse response = mock(HttpServletResponse.class); + parser.setData(request, response); + } + catch (ComponentException e) + { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + /** + * Clean up after each test is run. + */ + @AfterEach + public void tearDown() + { + parserService.putParser(parser); + this.release(parserService); + } + + /** + * The API allows us to add a cookie to the response + * but we cannot read it back unless the request is dispatched + * + * Test set and unset of cookie + */ + @Test + public void testCookieOperations() + { + try + { + // set a cookie + parser.set("cookie1", "test"); + + // unset a cookie + parser.unset("cookie1"); + + } catch ( Exception e ) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + +}