| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SimpleMathsConfirmationStrategy |
|
| 2.0;2 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, Simon Brown | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * - Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * - Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in | |
| 13 | * the documentation and/or other materials provided with the | |
| 14 | * distribution. | |
| 15 | * | |
| 16 | * - Neither the name of Pebble nor the names of its contributors may | |
| 17 | * be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 | * POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | package net.sourceforge.pebble.confirmation; | |
| 33 | ||
| 34 | import javax.servlet.http.HttpServletRequest; | |
| 35 | import java.util.Random; | |
| 36 | ||
| 37 | /** | |
| 38 | * Simple maths confirmation strategy that asks the user to add/subtract/multiply | |
| 39 | * two random numbers together. | |
| 40 | * | |
| 41 | * @author Simon Brown | |
| 42 | */ | |
| 43 | 0 | public class SimpleMathsConfirmationStrategy extends AbstractConfirmationStrategy { |
| 44 | ||
| 45 | private static final String ARGUMENT1 = "SimpleMathsConfirmationStrategyArg1"; | |
| 46 | private static final String ARGUMENT2 = "SimpleMathsConfirmationStrategyArg2"; | |
| 47 | private static final String OPERATOR = "SimpleMathsConfirmationStrategyOperator"; | |
| 48 | private static final String ANSWER = "SimpleMathsConfirmationStrategyAnswer"; | |
| 49 | ||
| 50 | /** | |
| 51 | * Called before showing the confirmation page. | |
| 52 | * | |
| 53 | * @param request the HttpServletRequest used in the confirmation | |
| 54 | */ | |
| 55 | public void setupConfirmation(HttpServletRequest request) { | |
| 56 | 0 | Random r = new Random(); |
| 57 | 0 | int arg1 = r.nextInt(10) + 1; |
| 58 | 0 | int arg2 = r.nextInt(10) + 1; |
| 59 | 0 | int op = r.nextInt(3); |
| 60 | 0 | request.getSession().setAttribute(ARGUMENT1, arg1); |
| 61 | 0 | request.getSession().setAttribute(ARGUMENT2, arg2); |
| 62 | ||
| 63 | 0 | switch (op) { |
| 64 | case 0 : | |
| 65 | 0 | request.getSession().setAttribute(OPERATOR, "+"); |
| 66 | 0 | request.getSession().setAttribute(ANSWER, arg1 + arg2); |
| 67 | 0 | break; |
| 68 | case 1 : | |
| 69 | 0 | request.getSession().setAttribute(OPERATOR, "-"); |
| 70 | 0 | request.getSession().setAttribute(ANSWER, arg1 - arg2); |
| 71 | 0 | break; |
| 72 | case 2 : | |
| 73 | 0 | request.getSession().setAttribute(OPERATOR, "*"); |
| 74 | 0 | request.getSession().setAttribute(ANSWER, arg1 * arg2); |
| 75 | break; | |
| 76 | } | |
| 77 | 0 | } |
| 78 | ||
| 79 | /** | |
| 80 | * Gets the URI of the confirmation page. | |
| 81 | * | |
| 82 | * @return a URI, relative to the web application root. | |
| 83 | */ | |
| 84 | public String getUri() { | |
| 85 | 0 | return "/WEB-INF/jsp/confirmation/maths.jsp"; |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Called to determine whether confirmation was successful. | |
| 90 | * | |
| 91 | * @param request the HttpServletRequest used in the confirmation | |
| 92 | * @return true if the confirmation was successful, false otherwise | |
| 93 | */ | |
| 94 | public boolean isConfirmed(HttpServletRequest request) { | |
| 95 | 0 | Integer answer = (Integer)request.getSession().getAttribute(ANSWER); |
| 96 | 0 | String userAnswer = request.getParameter("answer"); |
| 97 | ||
| 98 | 0 | return answer.toString().equals(userAnswer); |
| 99 | } | |
| 100 | ||
| 101 | } |