关闭

教学管理系统-EJB3单元测试

3060人阅读 评论(0) 收藏 举报

之前采用连接服务直接测试,看了一篇PPT上面说可以脱离服务器进行测试,到http://sourceforge.net/project/showfiles.php?group_id=22866&package_id=132063下载jboss-EJB-3.0_Embeddable_ALPHA_7.zip

正在实验中...睡觉...实验完毕。

步骤

1.新建测试项目,将jboss-EJB-3.0_Embeddable.zip中的conf和lib目录解压到项目文件夹中。
2.将conf添加到classpath中。
3.将mysql-connector-java-3.1.12-bin.jar复制到lib目录,将lib目录下的jar文件中添加到classpath中。
4.修改embedded-jboss-beans.xml文件中"DefaultDSBootstrap"一节(倒数第2个bean)中的属性。
5.在Java Build Path中添加org.bnuitse.tms.ejb项目引用。EJB3StandaloneBootstrap会扫描里面的EJB3定义。
6.参考Tutorial写了测试,运行通过。感觉挺好,不需要打包发布到服务器就可以进行测试了。

向在开源方面做出贡献的专家们致敬。C

问题

出现两个警告,其它运行正常。
WARN  31-05 11:49:43,924 (BeanSchemaBinding.java:init:227)  -You should use the 2.0 version of the Microcontainer xml. xmlns='urn:jboss:bean-deployer:2.0'
WARN  31-05 11:49:49,041 (Log4JLogger.java:warn:104)  -Persistence provider caller does not implements the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.

第一个警告解决:

将conf下的XML文件中

<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_1_0.xsd"
            xmlns="urn:jboss:bean-deployer">

改为

<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_2_0.xsd"
            xmlns="urn:jboss:bean-deployer:2.0">
第二个警告对功能没有影响,见http://www.jboss.com/index.html?module=bb&op=viewtopic&t=83204
代码
package org.bnuitse.tms;
import java.util.Hashtable;
import javax.naming.InitialContext;
import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public class BaseTestCase {
 public static InitialContext getInitialContext() throws Exception {
  Hashtable props = getInitialContextProperties();
  return new InitialContext(props);
 }
 @SuppressWarnings("unchecked")
 private static Hashtable getInitialContextProperties() {
  Hashtable props = new Hashtable();
  props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
  props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
  return props;
 }
 @BeforeClass
 public static void startupEmbeddedJboss() {
  EJB3StandaloneBootstrap.boot(null);
  EJB3StandaloneBootstrap.scanClasspath();
 }
 @AfterClass
 public static void shutdownEmbeddedJboss() {
  EJB3StandaloneBootstrap.shutdown();
 }
}
package org.bnuitse.tms.security;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.naming.InitialContext;
import org.bnuitse.tms.BaseTestCase;
import org.bnuitse.tms.model.security.College;
import org.bnuitse.tms.model.security.CollegeClass;
import org.bnuitse.tms.model.security.Student;
import org.bnuitse.tms.service.security.CollegeClassService;
import org.bnuitse.tms.service.security.CollegeService;
import org.bnuitse.tms.service.security.StudentService;
import org.hibernate.Hibernate;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SecurityTestCase extends BaseTestCase{
 StudentService studentService;
 CollegeClassService collegeClassService;
 CollegeService collegeService;
 College college;
 @Before
 public void setUp() throws Exception {
  InitialContext ctx = getInitialContext();
  collegeService = (CollegeService) ctx.lookup("tms/security/CollegeService");
  collegeClassService = (CollegeClassService) ctx.lookup("tms/security/CollegeClassService");
  studentService = (StudentService) ctx.lookup("tms/security/StudentService");
  College college = new College();
  college.setName(String.valueOf(System.currentTimeMillis()));
  int collegeId = collegeService.persist(college);
  this.college = collegeService.find(collegeId);
 }
 @After
 public void tearDown() throws Exception {
  collegeService.remove(college.getId());
 }
 private College createCollege() {
  String testName = String.valueOf(System.currentTimeMillis());
  College college = new College();
  college.setName(testName);
  return college;
 }
 private CollegeClass createCollegeClass() {
  String testName = String.valueOf(System.currentTimeMillis());
  CollegeClass collegeClass = new CollegeClass();
  collegeClass.setName(testName);
  collegeClass.setGrade(Calendar.getInstance().get(Calendar.YEAR));
  try {
   Thread.sleep(10);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  return collegeClass;
 }
 private Student createStudent() {
  Student s1 = new Student();
  s1.setId((int) System.currentTimeMillis());
  s1.setCode(String.valueOf(System.currentTimeMillis()));
  s1.setName(String.valueOf(System.currentTimeMillis()).substring(0, 10));
  try {
   Thread.sleep(10);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  return s1;
 }
 @Test
 public void testCreateCollege() {
  College college = createCollege();
  int id = collegeService.persist(college);
  assertEquals("本地Id不变", 0, college.getId());
  assertTrue("持久化后产生新Id", id != 0);
  College newCollege = collegeService.find(id);
  assertEquals("id相同", id, newCollege.getId());
  assertEquals("name相同", college.getName(), newCollege.getName());
  College queryByName = collegeService.find(college.getName());
  assertEquals(id, queryByName.getId());
  assertEquals(college.getName(), queryByName.getName());
  collegeService.remove(id);
 }
 @Test
 public void testCreateCollegeClass() throws Exception {
  CollegeClass collegeClass = createCollegeClass();
  collegeClass.setCollege(college);
  Student s1 = createStudent();
  s1.setCollegeClass(collegeClass);
  List<Student> students = new ArrayList<Student>();
  students.add(s1);
  Thread.sleep(10);
  Student s2 = createStudent();
  s2.setCollegeClass(collegeClass);
  students.add(s2);
  collegeClass.setStudents(students);
  int classId = collegeClassService.persist(collegeClass);
  assertTrue(classId != 0);
  CollegeClass reload = collegeClassService.find(classId, true);
  assertEquals(collegeClass.getName(), reload.getName());
  assertEquals(collegeClass.getCollege().getId(), reload.getCollege().getId());
  assertEquals(collegeClass.getCollege().getName(), reload.getCollege().getName());
  assertEquals(2, collegeClass.getStudents().size());
 }
 @Test
 public void testCollegeClassMerge() {
  CollegeClass collegeClass = createCollegeClass();
  collegeClass.setCollege(college);
  int classId = collegeClassService.persist(collegeClass);
  collegeClass = collegeClassService.find(classId, true);
  assertEquals(0, collegeClass.getStudents().size());
  Student s1 = createStudent();
  collegeClass.addStudent(s1);
  Student s2 = createStudent();
  collegeClass.addStudent(s2);
  assertEquals(2, collegeClass.getStudents().size());
  collegeClass = collegeClassService.merge(collegeClass);
  CollegeClass reload1 = collegeClassService.find(classId, true);
  assertEquals(2, reload1.getStudents().size());
  collegeClass.setName("newName");
  // 删除学生
  Student s = collegeClass.getStudents().iterator().next();
  collegeClass.removeStudent(s);
  collegeClass = collegeClassService.merge(collegeClass);
  assertEquals("newName", collegeClass.getName());
  assertEquals(1, collegeClass.getStudents().size());
  System.out.println("after add 2 students merge result:" + collegeClass.getStudents().size());
  CollegeClass reload2 = collegeClassService.find(classId, true);
  assertEquals(1, reload2.getStudents().size());
  // 确保学生已删除
  Student orphenStudent = studentService.find(s.getId());
  assertEquals(null, orphenStudent);
 }
 @Test
 public void testStudentAlong() {
  CollegeClass collegeClass = createCollegeClass();
  collegeClass.setCollege(college);
  int classId = collegeClassService.persist(collegeClass);
  collegeClass = collegeClassService.find(classId, true);
  assertEquals(0, collegeClass.getStudents().size());
  Student s1 = createStudent();
  s1.setCollegeClass(collegeClass);
  collegeClass.getStudents().add(s1);
  studentService.persist(s1);
  Student s2 = createStudent();
  s2.setCollegeClass(collegeClass);
  collegeClass.getStudents().add(s2);
  assertEquals(2, collegeClass.getStudents().size());
  studentService.persist(s2);
  collegeClass = collegeClassService.find(classId, true);
  System.out.println("in test student");
  assertEquals(2, collegeClass.getStudents().size());
 }
 @Test
 public void testGetAllCollege() {
  int initCount = collegeService.findAll().size();
  College college = createCollege();
  int id = collegeService.persist(college);
  List<College> colleges = collegeService.findAll();
  assertEquals("对象数量增加", 1, colleges.size() - initCount);
  collegeService.remove(id);
 }
 @Test
 public void testCollegeClass() {
  College college = createCollege();
  int classCount = 10;
  for (int i = 0; i < classCount; i++) {
   CollegeClass cls1 = new CollegeClass();
   cls1.setCollege(college);
   cls1.setName("class" + i);
   cls1.setGrade(2005);
   college.getClasses().add(cls1);
  }
  int id = collegeService.persist(college);
  College college1 = collegeService.find(id);
  assertFalse(Hibernate.isInitialized(college1.getClasses()));
  assertFalse(Hibernate.isInitialized(college1.getDepts()));
  College college2 = collegeService.find(id, true);
  assertTrue(Hibernate.isInitialized(college2.getClasses()));
  assertTrue(Hibernate.isInitialized(college2.getDepts()));
  collegeService.remove(id);
 }
 /**
  * 测试查询所有学院信息(包含班级数据)
  *
  */
 @Test
 public void testGetAllColleges() {
  List<College> colleges = collegeService.findAllColleges();
  for (int i = 0; i < colleges.size() - 1; i++) {
   assertFalse(colleges.get(i).getId() == colleges.get(i + 1).getId());
  }
 }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:139065次
    • 积分:2099
    • 等级:
    • 排名:第18775名
    • 原创:62篇
    • 转载:0篇
    • 译文:4篇
    • 评论:47条
    最新评论