这个星期日的任务

游客帐号可以作为一个公用的注册帐号使用

http://www.learndiary.com/disDiaryContentAction.do?goalID=1321

http://learndiary.tigris.org/issues/show_bug.cgi?id=8

现在写日记不大方便,应该使这个主要的功能变方便

http://www.learndiary.com/disDiaryContentAction.do?goalID=1322

http://learndiary.tigris.org/issues/show_bug.cgi?id=9

(转帖)Struts资源文件:Struts Message Resources

转自:http://www.javafan.net/article/20040430102526725.html

 

页面功能  【加入收藏】 【推荐给朋友】 【字体:大 中 小】 【关闭】   

  

 

Struts Message Resources

作者:Nick Heudecker    来自:未知

总览

许多刚刚学习Struts的程序员在使用Struts的MessageResources特性的时候会遭遇很多困难。本文将试图阐述MessageResources特性的优点并给出了具体的例子说明它的用法。

作者: Nick Heudecker, System Mobile Inc.

概述

类MessageResources可以使开发者方便地支持多语言,包括支持多时间格式和数字格式。使用资源包的另一个好处是允许开发者将标签字符串集中存储在一个位置,而不是分散在不同的JSP页面里。例如,对于每个用户的名字的标签"First Name" ,我们可以将它写在资源包中,在适当的地方通过Struts标签简单的进行引用:

<bean:write key="label.first.name"/>

这样做将会让你对程序的更改变的简单容易,你不必在每一个JSP页面里更改标签的内容了。

用法

使用消息资源包需要你做下面的事情:

1. 为你想要支持的地方创建一个消息资源包。

2. 配置WEB应用,加载消息资源包。

3. 使用相应的JSP标签加载资源或者...

4. ...在一个Action类中加载资源。

创建资源包

MessageResources 类的默认的实现是一个包含"key=value" 对的文件,下面的一个消息资源包文件的例子。

label.username=Username

label.password=Password

label.first.name=First Name

label.last.name=Last Name

label.email=Email Address

label.phone.number=Phone Number

label.welcome=Welcome back {0} {1}!

error.min.length=The input must be at least {0} characters in length.

error.max.length=The input cannot be longer than {0} characters in length.

大括号包围的整数是对java.text.MessageFormat 类的支持,程序员可以向value字符串中传递参数,对每个value字符串你最多可以传递4个参数。

配置

有两种途径通知Struts你的资源包的位置:web.xml 文件或者struts-config.xml 文件。首先来看web.xml 文件的配置:

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>

    org.apache.struts.action.ActionServlet

</servlet-class>

<init-param>

<param-name>

    application

</param-name>

<param-value>

    com.systemmobile.example.ApplicationResources

</param-value>

</init-param>

</servlet>

这个配置说明你的资源包的名字是ApplicationResources.properties,它位于com.systemmobile.example 包中。后缀".properties" 是隐含的,你不必显式地写出来。如果你还有另一个资源文件在相同的包中,例如ApplicationResources_fr.properties ,用来支持法语,你只需要象上面定义的那样列出文件名字即可。

定义资源文件的第二中方法(上面已经提到),是在struts-config.xml 文件中配置:

<message-resources parameter="com.systemmobile.example.ApplicationResources"/>

属性parameter 是必须的。和在web.xml文件中配置一样, 需要注意的是文件在包中的位置。

使用struts-config.xml 文件来配置消息资源文件是推荐的做法,因为它更有可扩展性,更灵活。

你可以使用message-resources 标签从不同的资源文件取不同的消息,前提是在配置的时候为不同的资源文件给出不同的key 属性的值。例如: <message-resources key="myResources" parameter="com.systemmobile.example.ApplicationResources"/>

<message-resources key="moreResources" parameter="com.systemmobile.example.MoreApplicationResources"/>

然后你必须这样使用bean:message 标签: <bean:message bundle="moreResources" key="some.message.key"/>

设置属性null 的值为"false" 后,如果某个资源字符串不存在将返回???key??? 而不是仅仅显示null。这样很容易在JSP页面中看到你没有定义的资源,使得内部测试的速度更快。(关于如何从资源文件中获得消息的详细内容参见国际化 一节) <message-resources parameter="com.systemmobile.example.ApplicationResources" null="false"/>

另外,message-resources 标签允许你使用自己实现的MessageResourcesFactory 接口,这不在本文的讨论范围。

资源文件放在哪里

关于资源文件最常见的问题是将资源文件放在WAR文件的哪里。简单的回答是该文件必须放在你的classpath下面,这意味着将资源文件放在一个JAR 文件中,或者放在/WEB-INF/classes 目录极其子目录下。下表给出了资源文件的位置,在message-resources 标签中"parameter" 属性的值以及简短的说明。

Resources Location parameter Value Description

/WEB-INF/classes/ApplicationResources.properties ApplicationResources 文件放在classes 目录下, 该目录在web应用的classpath中.

/WEB-INF/classes/resources/ApplicationResources.properties resources.ApplicationResources 该文件放在"resources" 目录下, 所以包名也就是路径名要给出。

In the app.jar file, in the com.systemmobile.example package/directory. com.systemmobile.example.ApplicationResources 文件在JAR文件中的全路径。

Tags

最常用Struts 标签是bean:message 标签。使用这个标签的"key" 可以从资源文件中读特定的消息资源。你还可以传入四个参数中的一个或全部:

<bean:message key="label.password"/>

<bean:message key="error.min.length" arg0="6"/>

<bean:message key="label.welcome" arg0="Ralph" arg1="Nader"/>

html:message 可以让你向用户显示错误信息(默认)或消息信息,而html:errors 只显示错误信息。很明显,错误信息或消息信息一定要保存在request里,否则就什么也不会显示。这里有一个显示消息信息的例子:

<logic:messagesPresent message="true">

  <html:messages id="msg" message="true">

    <div class="success">

      <bean:write name="msg"/>

    </div><br/>

  </html:messages>

</logic:messagesPresent>

还有一些标签也有限地支持消息资源,比如html:link。html:link标签通过定义"titleKey" 属性来显示标题文本。许多html 使用 "altKey" 属性从资源文件里获得alternate(替代)文本。

Actions

你还可以在Action 类中使用消息资源文件。Action 类有两个方法得到一个MessageResource 类的实例:

// 返回一个request里的资源文件

protected MessageResources getResources(HttpServletRequest request);

// 返回一个request里的资源文件,

// 该资源文件的标志上<message-resources/> 元素的内容

protected MessageResources getResources(javax.servlet.http.HttpServletRequest request, java.lang.String key);

MessageResources类可以让你从资源文件中得到本地化的消息。The API for MessageResources 可以在资源中找到。比较常用的方法有:

// these methods load a resources key for the given locale

public String getMessage(java.util.Locale locale, java.lang.String key);

public String getMessage(java.util.Locale locale, java.lang.String key,

       java.lang.Object arg0);

public String getMessage(java.util.Locale locale, java.lang.String key,

       java.lang.Object[] args);

public String getMessage(java.util.Locale locale, java.lang.String key,

       java.lang.Object arg0, java.lang.Object arg1)

public String getMessage(java.util.Locale locale, java.lang.String key,

       java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2);

public String getMessage(java.util.Locale locale, java.lang.String key, java.lang.Object arg0,

       java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3);

// these methods load a resources key for the locale retrieved

// from the HttpServletRequest

public String getMessage(java.lang.String key);

public String getMessage(java.lang.String key, java.lang.Object arg0);

public String getMessage(java.lang.String key, java.lang.Object[] args);

public String getMessage(java.lang.String key, java.lang.Object arg0,

       java.lang.Object arg1);

public String getMessage(java.lang.String key, java.lang.Object arg0,

       java.lang.Object arg1, java.lang.Object arg2);

public String getMessage(java.lang.String key, java.lang.Object arg0,

       java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3);

这些返回的字符串可以被设置成request 或 session 的参数并串会表现层。你可能注意到了一些重载方法getMessage(...) 选择了参数Object,而另外一些采用了参数arg0...arg3。这和 bean:message arg0...arg3 属性等价。

除了MessageResources 类,还有一些类使用了资源文件。ActionMessage类被用来从action 向JSP之间传递消息资源中的keys 。消息被用来作为bean 的属性。ActionError, ActionMessage的子类,使用消息资源中的keys 存储验证失败后的错误信息。

国际化

从资源文件中提取一个本地化信息可以由类MessageResources 来处理,或者由它的直接子类PropertyMessageResources类处理。既然类PropertyMessageResources 等经常地被用到,那么我们就来看看它是怎样使用getMessage(Locale, String) 方法来从资源文件中读取消息的。

举例说明:

1. 如果你在ApplicationResources_pt_br.properties (Brazilian Portuguese)中没有发现消息的定义,系统将在ApplicationResources_pt.properties 文件中找,如果ApplicationResources_pt.properties 文件不存在或者也没有该消息,那就去ApplicationResources.properties 文件里查找了。

2. 如果消息找到了,它就被加到本地化的缓存中并返回java.lang.String型数据。

3. 如果消息没有找到,此时如果returnNull 属性被为默认的true,将返回 null。 否则将返回类似 ???key??? 的字符串,key 就是那个被传递的参数。

JSTL

JSTL (JavaServer Pages Standard Tag Library) 的fmt标签最近开始流行起来,用来向JSP中显示资源文件的信息。它能很好地和Struts结合在一起。使用它非常简单,只要下载JSTL 的jar 文件和TLDs 并把它们拷贝到你的应用的正确的位置,然后在web.xml文件中加入下面的内容:

<context-param>

  <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>

  <param-value>ApplicationResources</param-value>

</context-param>

上面的配置是假定你的ApplicationResources.properties文件是放在/WEB-INF/classes 目录下的。 参见above 更多情况。

然后将这个标签库直接放在你的JSP中:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

最后,下面的标签将显示资源文件的内容:

<fmt:message key="label.first.name"/>

还有一个使用fmt 标签获得资源的例子。(注意: 该段程序取自Jakarta JSTL examples。)

// loading a resource from a specific bundle and populating a parameter

<fmt:message key="currentTime" bundle="${deBundle}">

 <fmt:param value="${currentDateString}"/>

</fmt:message>

// using the forEach iterator to populate paramters

<fmt:message key="serverInfo" bundle="${deBundle}">

 <c:forEach var="arg" items="${serverInfoArgs}">

  <fmt:param value="${arg}"/>

 </c:forEach>

</fmt:message>

结论

在向JSP文件方便地传递消息的同时,Struts使用消息资源文件还帮助我们创建国际化的Web应用。我们既可以使用正在快速发展中的JSTL标签,也可以使用Struts标签,或直接在action中得到一条具体的消息。我希望这篇文章为您阐明了一些Struts中常用的但有时会混淆的东西。

关于作者

Nick Heudecker 是一位软件开发人员,具有6年的企业应用的开发经验。 他所在的公司, System Mobile, Inc.,专门从事应用集成,定制软件开发和无线应用。 他还是Sun认证JAVA程序员,现在居住在Ann Arbor, Michigan。

资源

下面的资源也许对您了解更多的关于Struts资源文件有帮助:

JavaDoc for the classes of interest:

java.util.ResourceBundle

java.util.Locale

org.apache.struts.util.MessageResources

org.apache.struts.action.ActionError

org.apache.struts.action.ActionMessage

Ted Husted's Struts Tips: Very handy advice, especially for resource bundle usage.

Struts Home Page

Struts-User Mailing List Archive: Many people use Struts, so there is a very good chance that your question has be answered already. Please use all available resources before asking your question on the mailing list.

JSTL Homepage and the Jakarta JSTL Implementation

注意事项

Packages are just directory structures used to avoid naming conflicts. If you put a message bundle in a directory named resources under /WEB-INF/classes, this is the equivalent of putting the file in a package named resrouces. While basic, this point seems to trip up many new programmers.

 

 

  

 

页面功能  【加入收藏】 【推荐给朋友】 【字体:大 中 小】 【关闭】    

 

 

Copyright © 2003 - 2005 JavaFan.NET All Rights Reserved

慎用setActionForm() 在使用strutstestcase测试struts程序时

setActionForm()方法会调用ActionForm里的reset()方法清掉你传入actionForm中的测试数据。

如:

   EmailResetPsdForm form = new EmailResetPsdForm();

  form.setUserName("ppig");

  setActionForm(form);

在这里,测试数据userName="ppig"不会被传入使用这个form bean的action中。

应该使用替代的方法来传递测试数据:

addRequestParameter("userName", "ppig");

下面是strutstestcase里的文档对setActionForm方法的说明。

    /*

     * setActionForm

     * public void setActionForm(ActionForm form)Sets an ActionForm instance to be used in this

     * test. The given ActionForm instance will be stored in the scope specified in the Struts

     * configuration file (ie: request or session). Note that while this ActionForm instance

     * is passed to the test, Struts will still control how it is used. In particular, it

     * will call the ActionForm.reset() method, so if you override this method in your

     * ActionForm subclass, you could potentially reset attributes in the form passed

     * through this method.

     *

     * Parameters:

     * form - the ActionForm instance to be used in this test.

     *

     */

ant初始化数据库的问题

如果数据库是utf-8编码,用下面的初始化数据库任务会出错:

<target name="initdb" >

<sql driver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/learndiarydb"

userid="dbuser"

password="1234"

classpath="lib/mysql-connector-java-3.1.12-bin.jar"

autocommit="true"

rdbms="mysql"

print="true"

src="database/testdata_mysql.sql" />

</target>

错误报告为:

Buildfile: build.xml

initdb:

      [sql] Executing file: F:\learndiary\learndiary\old\database\testdata_mysql.sql

      [sql] 0 rows affected

      [sql] Failed to execute:    INSERT INTO article VALUES (1,1,0,'鍏憡鐗?,'鍏憡鐗?,'2004-09-28 08:38:54','',1,'2005-11-10 08:27:50',6,1,'admin',1)

BUILD FAILED

F:\learndiary\learndiary\old\build.xml:124: java.sql.SQLException: Syntax error or access violation,  message from server: "You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near '閸忣剙鎲¢悧锟?,'2004-09-28 08:38:54','',1,'2005-11-10 08:27:50"

Total time: 12 seconds

而这个初始化通过mysql的source learndiarydb yourpath\testdata_mysql.sql的命令来进行则不会出错。

不知是什么原因?

Junit不能用于多线程的测试吗?

  我在为(改善学习日记的登录模式 (0篇) http://www.learndiary.com/disGoalContentAction.do?goalID=1214&naviStr=a10a60)写发送重置密码信件的测试代码时,发现测试过程中,测试代码中另一个发送邮件的线程没有执行,测试程序就结束了。

  难道junit不能用于多线程的程序的测试吗?

  

  这是含发送邮件的程序:

// $Id: EmailResetPsdAction.java,v 1.8 2005/12/19 16:00:49 dashing_meng Exp $

// Copyright (c) 2004-2005 Http://www.learndiary.com. All Rights Reserved.

// Permission to use, copy, modify, and distribute this software and its

// documentation without fee, and without a written agreement is hereby

// granted, provided that the above copyright notice and this paragraph

// appear in all copies.  This software program and documentation are

// copyrighted by http://www.learndiary.com. The software program and

// documentation are supplied "AS IS", without any accompanying services

// from The LearnDiary. The LearnDiary does not warrant that the operation

// of the program will be uninterrupted or error-free. The end-user

// understands that the program was developed for research purposes and is

// advised not to rely exclusively on the program for any reason.

// IN NO EVENT SHALL HTTP://WWW.LEARNDIARY.COM BE LIABLE TO ANY PARTY FOR

// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,

// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS

// DOCUMENTATION, EVEN IF HTTP://WWW.LEARNDIARY.COM HAS BEEN ADVISED OF THE

// POSSIBILITY OF SUCH DAMAGE. HTTP://WWW.LEARNDIARY.COM SPECIFICALLY

// DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE

// SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND

// HTTP://WWW.LEARNDIARY.COM HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,

// SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

package com.learndiary.website.action.account;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.*;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;

import com.learndiary.website.actionform.EmailResetPsdForm;

import com.learndiary.website.manager.EmailResetPsdManager;

import com.learndiary.website.manager.EmailSender;

import com.learndiary.website.manager.UserManager;

import com.learndiary.website.model.Email;

import com.learndiary.website.model.EmailResetPsdInfo;

import com.learndiary.website.model.UserInfo;

import com.learndiary.website.util.Util;

/**

 * Process user's asking for sending resetting password token email request.

 * @author http://www.learndiary.com,LearnDiary Develop Group

 */

public class EmailResetPsdAction extends Action {

  /**

   *  Function:

   *  Process user's asking for sending resetting password token email request.

   *  Pseudo Coding:

   * 

   *  {

   *    String userName;

   *    UserInfo userInfo;

   *    int userID;

   *    UserManager userManager;

   *    EmailResetPsdManager emailResetPsdManager;

   *    EmailResetPsdInfo emailResetPsdInfo;

   *    Email email;

   * 

   *    userName=((EmailResetPsdForm)form).getUserName();//Get the userName from form

   *    userInfo= userManager. findByName(userName);//Get userInfo

   *    if (userInfo==null){

   *      forward to emailFailure.jsp;

   *    }

   *    userID=userInfo.getUserID();

   *    emailResetPsdInfo=emailResetPsdManager.findByID();// Get emailResetPsdInfo

   *    if (emailResetPsdInfo!=null){

   *      foward to emailFailure.jsp;

   *    }

   * 

   *    //set emaiResetPsdInfo

   *    emaiResetPsdInfo.setUserID(userID);

   *    emaiResetPsdInfo.setToken(com.learndiary.website.util.Util.genRandomStr());

   *    emailResetPsdInfo.setSendDate(System.getCurrentTime());

   *    emailResetPsdManager.insertResetPsdInfo(emailResetPsdInfo);//record emailResetPsdInfo

   *    //set reset passwrod email's content

   *    email.setRecipient(userInfo.getEmail());

   *    email.setSubject("your reset password request.");

   *    email.setText( "http://localhost:8080/learndiary/resetPsdAction.do?userID=" + userID + "token=" + emaiResetPsdInfo.getToken());

   *    emailSender.send(email);

   *    forward to emailSuccess.jsp;

   *  }

   * 

   * 

   *   

   */

    private org.apache.commons.logging.Log __log = LogFactory.getFactory().getInstance(this.getClass());

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

String myaction = mapping.getPath(); 

    __log.info("Enter action: "+myaction);

    String target=null;

    String userName=null;

    UserInfo userInfo=null;

    int userID;

    UserManager userManager=new UserManager();

    EmailResetPsdManager emailResetPsdManager=new EmailResetPsdManager();

    EmailResetPsdInfo emailResetPsdInfo=null;

    Email email=null;

    EmailSender emailSender=new EmailSender();

   

    userName=((EmailResetPsdForm)form).getUserName();//Get the userName from form

    try {

__log.debug("Before findByName,and name is: "+userName);

        userInfo= userManager.findByName(userName);//Get userInfo

__log.debug("After findByName,and userInfo is: "+userInfo);

    } catch (ClassNotFoundException e1) {

e1.printStackTrace();

    }

    if (userInfo==null){

      target=new String("failure");

  return (mapping.findForward(target));//no such a user,forward to emailFailure.jsp;

    }

    userID=userInfo.getUserID();

    __log.debug("userID: "+userID);

    try {

        emailResetPsdInfo=emailResetPsdManager.findByID(userID);// Get emailResetPsdInfo

    } catch (ClassNotFoundException e) {

e.printStackTrace();

    }

   

    __log.debug("existed emailResetPsdInfo is: " + emailResetPsdInfo);

    if (emailResetPsdInfo!=null){//user requested emailing resetting password token in 72 hours

      target=new String("failure");

  return (mapping.findForward(target));//foward to emailFailure.jsp;

    }

 

    //set emaiResetPsdInfo

emailResetPsdInfo=new EmailResetPsdInfo();

    emailResetPsdInfo.setUserID(userID);

    emailResetPsdInfo.setToken(Util.genRandomStr());

    emailResetPsdInfo.setSendTime(new Timestamp(System.currentTimeMillis()));

    __log.debug("new emailResetPsdInfo is: "+emailResetPsdInfo);

    try {

      emailResetPsdManager.insertResetPsdInfo(emailResetPsdInfo);

      __log.debug("insert emailResetPsdInfo ok!");

    } catch (Exception e2) {

      e2.printStackTrace();

    }//record emailResetPsdInfo

    //set reset passwrod email's content

    email=new Email();

    email.setRecipient(userInfo.getEmail());

    email.setSubject("Your resetting password request.");

    email.setText( "http://localhost:8080/learndiary_login/account/resetPsd.jsp?userID="

      + userID + "&token=" + emailResetPsdInfo.getToken());

    __log.debug("email is: " + email); 

    emailSender.send(email);//这里要产生另一个线程发送邮件,但在测试程序中没有执行,在正常运行时会执行

   

    target=new String("success");

return (mapping.findForward(target));//email resetting password token success,

                                     //forward to emailSuccess.jsp;

  }

 

  /**

   * process general error

   * @param e error object

  

  private void generalError(Exception e) {

    e.printStackTrace();

    __log.error(" [EmailResetPsd] Error - " + e.getMessage());

  }*/

}

这时对应的测试程序:

//$Id: EmailResetPsdActionTest.java,v 1.3 2005/12/19 16:00:49 dashing_meng Exp $

//Copyright (c) 2004-2005 Http://www.learndiary.com. All Rights Reserved.

//Permission to use, copy, modify, and distribute this software and its

//documentation without fee, and without a written agreement is hereby

//granted, provided that the above copyright notice and this paragraph

//appear in all copies.  This software program and documentation are

//copyrighted by http://www.learndiary.com. The software program and

//documentation are supplied "AS IS", without any accompanying services

//from The LearnDiary. The LearnDiary does not warrant that the operation

//of the program will be uninterrupted or error-free. The end-user

//understands that the program was developed for research purposes and is

//advised not to rely exclusively on the program for any reason.

//IN NO EVENT SHALL HTTP://WWW.LEARNDIARY.COM BE LIABLE TO ANY PARTY FOR

//DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,

//INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS

//DOCUMENTATION, EVEN IF HTTP://WWW.LEARNDIARY.COM HAS BEEN ADVISED OF THE

//POSSIBILITY OF SUCH DAMAGE. HTTP://WWW.LEARNDIARY.COM SPECIFICALLY

//DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE

//SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND

//HTTP://WWW.LEARNDIARY.COM HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,

//SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

package com.learndiary.website.action.account;

import java.sql.Timestamp;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import servletunit.struts.MockStrutsTestCase;

import com.learndiary.website.actionform.EmailResetPsdForm;

import com.learndiary.website.dao.EmailResetPsdDAO;

import com.learndiary.website.dao.TransContext;

import com.learndiary.website.dao.UserDAO;

import com.learndiary.website.model.EmailResetPsdInfo;

import com.learndiary.website.model.UserInfo;

/**

 * test EmailResetPsdAction

 * @author http://www.learndiary.com, LearnDiary Develop Group

 */

public class EmailResetPsdActionTest extends MockStrutsTestCase {

Log log = LogFactory.getLog(EmailResetPsdActionTest.class.getName());

private transient TransContext globalTran = null;

UserDAO userDAO=null;

EmailResetPsdDAO emailResetPsdDAO=null;

//the contextDir can be commented by adding classpath:${web} into build.xml

//String contextDir = "E:\zhangwei\eclipse_workspace1\learndiary_login\web";

public EmailResetPsdActionTest(String testName) {

super(testName);

}

   

    /**

     * empty table "user" and "emailresetpsd";

     * insert two user:"ppig" and "tom" into table user,

     * "ppig" to test successful case,

     * "tom" to test duplicate request failure case;

     * insert a record into table emailresetpsd for

     * "tom" to test duplicate request failure case.

     * @see junit.framework.TestCase#setUp()

     */

public void setUp() throws Exception {

super.setUp();

//this.setContextDirectory(new File(contextDir));

log.debug("ahah");

globalTran = new TransContext();

userDAO = new UserDAO(globalTran);

emailResetPsdDAO=new EmailResetPsdDAO(globalTran);

//empty two tables

userDAO.deleteAll();

        emailResetPsdDAO.deleteAll();

       

        UserInfo info1=new UserInfo();//for testing successful request

info1.setUserName("ppig");

info1.setPsd("123456");

info1.setEmail("learndiary@126.com");

userDAO.insertObject(info1);

UserInfo info2=new UserInfo();//for testing duplicate request failure

info2.setUserName("tom");

info2.setPsd("223456");

info2.setEmail("tom@tom.com");

userDAO.insertObject(info2);

        int userID2=((UserInfo)userDAO.findByName("tom")).getUserID();

        

EmailResetPsdInfo emailResetPsdInfo=new EmailResetPsdInfo();

emailResetPsdInfo.setUserID(userID2);

emailResetPsdInfo.setToken("12345678");

emailResetPsdInfo.setSendTime(new Timestamp(System.currentTimeMillis()-1000*60*2));

emailResetPsdDAO.insertObject(emailResetPsdInfo);

}

   

    /**

     * empty table "user" and table "emailresetpsd"

     * @see junit.framework.TestCase#tearDown()

     */

public void tearDown() throws Exception {

super.tearDown();

        userDAO.deleteAll();

        emailResetPsdDAO.deleteAll();

}

/**

* request emailing resetting password token successfully

*/

public void testSuccess() {

// a valid request

setRequestPathInfo("/emailResetPsdAction");

EmailResetPsdForm form = new EmailResetPsdForm();

form.setUserName("ppig");

setActionForm(form);

actionPerform();

verifyForward("success");

// verifyForwardPath("/success.jsp");

// assertEquals("deryl",getSession().getAttribute("authentication"));

verifyNoActionErrors();

}

    /**

     *  request emailing resetting password token failure

     *

     */

public void testFailure() {

// fail when duplicate request

EmailResetPsdForm form = new EmailResetPsdForm();

form.setUserName("tom");

setRequestPathInfo("/emailResetPsdAction");

setActionForm(form);

actionPerform();

verifyForward("failure");

// fail when user not found

form = new EmailResetPsdForm();

form.setUserName("000000000000000");

setRequestPathInfo("/emailResetPsdAction");

setActionForm(form);

actionPerform();

verifyForward("failure");

verifyNoActionErrors();

}

public static void main(String[] args) {

junit.textui.TestRunner.run(EmailResetPsdActionTest.class);

}

}

呵呵,测试了一下新买的浴霸(非程序:))

  新买某种品牌的浴霸,说明书称:出厂经4。C的冰水测试通过。

  这两天在学测试程序,为了验证这个浴霸的安全性,我对它测试了一番。用还是热的水泼在取暖泡上,哧的一响,经受了考验,再来一下,在另一个取暖泡上测试。结果取暖泡里面冒烟了。表面也有裂纹了。失败。

  看来,出厂的测试是夸大其辞呀。

  老婆骂我。。。

  我只是告诉她,经过测试,这个浴霸不能被淋浴头淋到。

  浴霸真的不能被水淋吗?还是我们这个浴霸本身就有问题?还是测试过度了。我自己也糊涂了。。。

(转帖)MYSQL 修改root密码命令

转自:http://it.dg.gd.cn/print.php/770

                  正文

××××××××××××××××××××××××××××××××××××××××××××××××××××

MYSQL 修改root密码命令

2005-10-14       

打印自: IT学院,linux应用,linux技巧,web技术,windows相关,数据库,网管技术,网页技术,unix,轻松一刻,精品网文,精品图片,博客文章

地址: http://it.dg.gd.cn/article.php/770

MYSQL 修改root密码命令

cmd下切换到 mysql 安装目录



d:/mysql/bin

前提:mysql用户root密码为空.

输入 mysql -u root mysql

mysql> 状态下输入 update user set password=password('新密码') where user='root';

回显

Query OK, 0 rows affected (0.00 sec)

Rows matched: 2 Changed: 0 Warnings: 0

mysql> 状态下输入 FLUSH PRIVILEGES;

回显

Query OK, 0 rows affected (0.00 sec)

mysql> 状态下输入 quit

退出 sql

注意每个命令后都要加上一个分号 ";"

mysql 才开始执行该行命令

而第二个指令会让已载入记忆体的 mysql 系统资料库更新

重起 mysql .

在更新 root 密码後,日後要与 MySQL 连线的方法为:

mysql -uroot -p新密码

 

 

责任编辑: yufish