技术开发 频道

单元测试指导



三.用AppFuse构建项目
1. 建立新的项目(ant new
  在命令行中进入appfuse所在目录,输入ant new命令,系统显示如下信息:
E:\appfuse>ant new
Buildfile: build.xml
Trying to override old definition of datatype resources
……
[input] What would you like to name your application [myapp]? [myapp]
   命令行提示输入应用程序的名称,例如输入testAppfuse,要注意项目名称不能使用"test"或者包含"appfuse"或者以数字开头,命令行显示如下语句提示输入数据库的名称:
[input] What would you like to name your database [mydb]? [mydb]
输入数据库名appfuse,命令行显示如下语句提示输入包名:
[input] What package name would you like to use [org.appfuse]? [org.appfuse]
输入包名org.amigo,命令行显示选择Web MVC框架:
[input] What web framework would you like to use [webwork,tapestry,spring,jsf,struts]? [struts]
输入选择的Web MVC框架struts,命令行显示如下:
     [echo] Creating new application named 'testAppFuse'...
     [copy] Copying 358 files to E:\testAppFuse
     ……
BUILD SUCCESSFUL
Total time: 10 minutes 12 seconds
在末尾可看到BUILD SUCCESSFUL字样,显示操作成功。在appfuse同级目录E:下,可看到新建的项目testAppFuse,在该目录下生成了项目源码。
2. 建立数据库与编译源码(ant setup
   进入在1中建立的项目目录E:\testAppFuse,运行ant setup,这一命令将会在MySql中创建前面指定的名称的数据库,并编译源码,打包发布到Tomcat中。
注意:AppFuse默认情况下认为数据库的root帐号密码为空,如果你给数据库设置了密码,需要修改根目录下的properties.xml文件。搜索属性名为database.admin.password的标签,将value改写为实际的数据库密码。
输入ant setup后,显示操作信息,末尾显示“BUILD SUCCESSFUL”表示操作成功。该操作成功后,将会看到数据库中新增了数据库appfuse,包含app_user,role和user_role三个表。在CATALINA_HOME\webapps下新增了一个项目:testAppFuse。启动Tomcat后,在浏览器中输入:http://localhost:8080/testAppFuse,可看到登录页面。
3使用 AppGen生成CRUD相关代码(install-detailed
AppGen 是AppFuse 中提供的一个基于 Ant 和 XDoclet 的代码生成工具。默认情况下,常见的 DAO 和管理器都可以允许我们对任何普通老式 Java 对象(POJO)进行 CRUD 操作,但是在 Web 层上这样做有些困难。
下面让我们看看AppGen是如何工作的。
1)首先在第二步中创建的数据库appfuse中创建friends表,建表语句如下:
            
create table friends ( friend_id int(8) auto_increment, name varchar(20) not null, gender varchar(20) not null, address varchar(100) null, created_date datetime not null, primary key (friend_id) ) type=InnoDB;
2)进入E:\testAppFuse\extras\appgen 目录,运行ant install-detailed,显示如下:
E:\testAppFuse\extras\appgen>ant install-detailed
Buildfile: build.xml
clean:
init:
    [mkdir] Created dir: E:\testAppFuse\extras\appgen\build
    ……
[input] Would you like to generate code from a table or POJO? (table, pojo)
3)提示是从table还是从POJO产生代码,输入table,选择从table生成代码,屏幕输出如下:
[input] What is the name of your table (i.e. person)?
4)提示输入表名,输入刚才创建的表的名字friends,输出如下:
[input] What is the name, if any, of the module for your table (i.e. organiz
ation)?
5)提示输入表的模块的名字,若无,按Enter键,开始生成代码。操作成功后可看到项目下已经生成对friends表的CRUD代码。
6)要查看页面来观察结果,还需做如下操作:为 Hibernate 添加 Friend.hbm.xml 映射文件。方法如下:
Friend.hbm.xml 添加到E:\testAppFuse\src\dao\org\amigo\dao\hibernate下的applicationContext-hibernate.xml文件中,需修改sessionFactory bean
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>org/amigo/model/Friend.hbm.xml</value> <value>org/amigo/model/Role.hbm.xml</value> <value>org/amigo/model/User.hbm.xml</value> </list> </property> …… </bean>

7
运行ant setup deploy之后,我们可以在部署的应用程序中对 friends表执行 CRUD 操作,并且在friends表中,AppFuse已经为我们插入了测试数据。
0
相关文章