File Source: simplemathsconfirmationstrategy.java
/*
P/P * Method: net.sourceforge.pebble.confirmation.SimpleMathsConfirmationStrategy__static_init
*/
1 /*
2 * Copyright (c) 2003-2006, 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
/*
P/P * Method: void net.sourceforge.pebble.confirmation.SimpleMathsConfirmationStrategy()
*/
34 import javax.servlet.ServletRequest;
35 import javax.servlet.http.HttpServletRequest;
36 import java.util.Random;
37
38 /**
39 * Simple maths confirmation strategy that asks the user to add/subtract/multiply
40 * two random numbers together.
41 *
42 * @author Simon Brown
43 */
44 public class SimpleMathsConfirmationStrategy extends AbstractConfirmationStrategy {
45
46 private static final String ARGUMENT1 = "SimpleMathsConfirmationStrategyArg1";
47 private static final String ARGUMENT2 = "SimpleMathsConfirmationStrategyArg2";
48 private static final String OPERATOR = "SimpleMathsConfirmationStrategyOperator";
49 private static final String ANSWER = "SimpleMathsConfirmationStrategyAnswer";
50
51 /**
52 * Called before showing the confirmation page.
53 *
54 * @param request the HttpServletRequest used in the confirmation
55 */
56 public void setupConfirmation(HttpServletRequest request) {
/*
P/P * Method: void setupConfirmation(HttpServletRequest)
*
* Preconditions:
* request != null
*
* Presumptions:
* (java.util.Random:nextInt(...)@58 + 1)*(java.util.Random:nextInt(...)@59 + 1) in -231..232-1
* java.util.Random:nextInt(...)@58 <= 232-2
* java.util.Random:nextInt(...)@58 + java.util.Random:nextInt(...)@59 in -231-2..232-3
* java.util.Random:nextInt(...)@59 <= 232-2
* java.util.Random:nextInt(...)@59 - java.util.Random:nextInt(...)@58 in -232+1..231
* ...
*
* Test Vectors:
* java.util.Random:nextInt(...)@60: {0}, {1}, {2}, {-231..-1, 3..232-1}
*/
57 Random r = new Random();
58 int arg1 = r.nextInt(10) + 1;
59 int arg2 = r.nextInt(10) + 1;
60 int op = r.nextInt(3);
61 ((ServletRequest) request.getSession()).setAttribute(ARGUMENT1, arg1);
62 ((ServletRequest) request.getSession()).setAttribute(ARGUMENT2, arg2);
63
64 switch (op) {
65 case 0 :
66 ((ServletRequest) request.getSession()).setAttribute(OPERATOR, "+");
67 ((ServletRequest) request.getSession()).setAttribute(ANSWER, arg1 + arg2);
68 break;
69 case 1 :
70 ((ServletRequest) request.getSession()).setAttribute(OPERATOR, "-");
71 ((ServletRequest) request.getSession()).setAttribute(ANSWER, arg1 - arg2);
72 break;
73 case 2 :
74 ((ServletRequest) request.getSession()).setAttribute(OPERATOR, "*");
75 ((ServletRequest) request.getSession()).setAttribute(ANSWER, arg1 * arg2);
76 break;
77 }
78 }
79
80 /**
81 * Gets the URI of the confirmation page.
82 *
83 * @return a URI, relative to the web application root.
/*
P/P * Method: String getUri()
*
* Postconditions:
* return_value == &".WEB-INF.jsp.confirmation.maths.jsp"
*/
84 */
85 public String getUri() {
86 return "/WEB-INF/jsp/confirmation/maths.jsp";
87 }
88
89 /**
90 * Called to determine whether confirmation was successful.
91 *
92 * @param request the HttpServletRequest used in the confirmation
93 * @return true if the confirmation was successful, false otherwise
94 */
95 public boolean isConfirmed(HttpServletRequest request) {
/*
P/P * Method: bool isConfirmed(HttpServletRequest)
*
* Preconditions:
* request != null
*
* Presumptions:
* javax.servlet.ServletRequest:getAttribute(...)@96 != null
* javax.servlet.http.HttpServletRequest:getSession(...)@96 != null
*
* Postconditions:
* init'ed(return_value)
*/
96 Integer answer = (Integer)((ServletRequest) request.getSession()).getAttribute(ANSWER);
97 String userAnswer = request.getParameter("answer");
98
99 return answer.toString().equals(userAnswer);
100 }
101
102 }
SofCheck Inspector Build Version : 2.22510
| simplemathsconfirmationstrategy.java |
2010-Jul-19 17:35:14 |
| simplemathsconfirmationstrategy.class |
2010-Jul-19 20:23:40 |