📄 testeetestutils.java
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2004 CyberDemia Research and Services Pty Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file COPYING); if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * See the COPYING file located in the top-level-directory of * the archive of this program for complete text of license. */package com.cyberdemia.cybertester.testee.test;import javax.servlet.http.*;import javax.servlet.*;/** * TesteeTestUtils is a helper class containing some useful utility methods * for conducting testee's tests. * * @version $Revision: 1.3 $ at $Date: 2004/03/31 14:16:10 $ by $Author: alexycyap $ * @author Alexander Yap */public abstract class TesteeTestUtils{ private TesteeTestUtils() { // Nothing here } private static final String TEST_TIME_TOKEN_KEY = "testTimeToken"; private static final String TEST_TIME_TOKEN_SEPARATOR = ":"; private static final String QUESTION_TIME_TOKEN_KEY = "quesTimeToken"; private static final String QUESTION_TIME_TOKEN_SEPARATOR = ":"; /** * Stores a token at the start of a test as an attribute of the user session, * in order to measure the time taken for the test. * @param session User session * @param testId Test Id. */ static void storeTestTimeToken(HttpSession session, Integer testId) { session.setAttribute(TEST_TIME_TOKEN_KEY,testId.toString()+TEST_TIME_TOKEN_SEPARATOR+String.valueOf(System.currentTimeMillis())); } /** * Checks the amount of time taken for a test by comparing the current * time with that stored as the testTimeToken. * @param session User session * @param testId Test Id * @return Amount of time taken for the test in milliseconds. * @throws ServletException if the testTimeToken is missing. * @throws IllegalArgumentException if the testTimeToken is invalid. * @see #storeTestTimeToken(HttpSession, String) */ static long checkTestTimeToken(HttpSession session, Integer testId) throws ServletException { String token = (String)session.getAttribute(TEST_TIME_TOKEN_KEY); if (token==null) { throw new ServletException("Missing session attribute "+TEST_TIME_TOKEN_KEY); } String[] tokenStrs = token.split(TEST_TIME_TOKEN_SEPARATOR); if (tokenStrs.length!=2) { throw new IllegalArgumentException("Invalid value "+token+" in session attribute "+TEST_TIME_TOKEN_KEY); } if (!testId.equals(new Integer(tokenStrs[0]))) { throw new IllegalArgumentException("Wrong testId token "+tokenStrs[0]+" for in session attribute "+TEST_TIME_TOKEN_KEY); } long tokenTime = -1; try { tokenTime = Long.parseLong(tokenStrs[1]); } catch (NumberFormatException nfex) { throw new IllegalArgumentException("Invalid value "+token+" in session attribute "+TEST_TIME_TOKEN_KEY); } return System.currentTimeMillis()-tokenTime; } /** * Simply removes the testTimeToken without processing it. * @param session User session. */ static void removeTestTimeToken(HttpSession session) { session.removeAttribute(TEST_TIME_TOKEN_KEY); } /** * Stores a token at the start of a question as an attribute of the user session, * in order to measure the time taken to answer the question as part of a test. * @param session User session * @param testId Test Id. * @param quesId Question Id. */ static void storeQuestionTimeToken(HttpSession session, Integer testId, Integer quesId) { session.setAttribute(QUESTION_TIME_TOKEN_KEY,testId+QUESTION_TIME_TOKEN_SEPARATOR+quesId+QUESTION_TIME_TOKEN_SEPARATOR+String.valueOf(System.currentTimeMillis())); } /** * Checks the amount of time taken to answer a question by comparing the current * time with that stored as the questionTimeToken. * @param session User session * @param testId Test Id * @param quesId Question Id. * @return Amount of time taken to answer the test in milliseconds. * @throws ServletException if the questionTimeToken is missing. * @throws IllegalArgumentException if the questionTimeToken is invalid. * @see #storeQuestionTimeToken(HttpSession, String, String) */ static long checkQuestionTimeToken(HttpSession session, Integer testId, Integer quesId) throws ServletException { String token = (String)session.getAttribute(QUESTION_TIME_TOKEN_KEY); if (token==null) { throw new ServletException("Missing session attribute "+QUESTION_TIME_TOKEN_KEY); } String[] tokenStrs = token.split(QUESTION_TIME_TOKEN_SEPARATOR); if (tokenStrs.length!=3) { throw new IllegalArgumentException("Invalid value "+token+" in session attribute "+QUESTION_TIME_TOKEN_KEY); } if (!testId.equals( new Integer(tokenStrs[0]) )) { throw new IllegalArgumentException("Wrong testId token "+tokenStrs[0]+" for session attribute "+QUESTION_TIME_TOKEN_KEY); } if (!quesId.equals( new Integer(tokenStrs[1]) )) { throw new IllegalArgumentException("Wrong quesId token "+tokenStrs[1]+" for session attribute "+QUESTION_TIME_TOKEN_KEY); } long tokenTime = -1; try { tokenTime = Long.parseLong(tokenStrs[2]); } catch (NumberFormatException nfex) { throw new IllegalArgumentException("Invalid value "+token+" in session attribute "+QUESTION_TIME_TOKEN_KEY); } return System.currentTimeMillis()-tokenTime; } /** * Simply removes the questionTimeToken without processing it. * @param session User session. */ static void removeQuestionTimeToken(HttpSession session) { session.removeAttribute(QUESTION_TIME_TOKEN_KEY); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -