Teaching Management System-ejb3 unit test

Source: Internet
Author: User

Before using the Connection Service direct test, read a PPT above that can be separated from the server for testing, to http://sourceforge.net/project/showfiles.php? Group_id1_22866&package_id=132063download jboss-ejb-3.0_embeddable_alpha_7.zip

In the experiment... sleeping... the experiment is complete.

Procedure

1. Create a new test project and decompress the conf and Lib directories in jboss-ejb-3.0_embeddable.zip to the project folder.
2. Add the conf file to classpath.
3. Copy the mysql-connector-java-3.1.12-bin.jar to the lib directory and add the JAR file under the lib directory to the classpath.
4. modify attributes in the defaultdsbootstrap section of the embedded-jboss-beans.xml file (the last 2nd beans.
5. Add org. bnuitse. TMS. EJB project reference in Java build path. Ejb3standalonebootstrap scans the ejb3 definition in it.
6. Refer to tutorial to write the test and run it successfully. It feels good. You can test it without packaging and publishing it to the server.

We would like to pay tribute to the experts who have contributed to open-source. C

Problem

Two Warnings are displayed, and other warnings are normal.
Warn 31-05 11:49:43, 924 (beanschemabinding. java: init: 227)-You shoshould use the 2.0 version of The microcontainer XML. xmlns = 'urn: JBoss: bean-deployer: 8080'
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.

The first warning is resolved:

In the XML file under Conf

<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">

Change

<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">
The second warning has no effect on the feature, see the http://www.jboss.com/index.html? Module = BB & OP = Viewtopic & t = 83204
Code
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 ("local ID unchanged", 0, college. GETID ());
Asserttrue ("new ID generated after persistence", ID! = 0 );
College newcollege = collegeservice. Find (ID );
Assertequals ("same ID", ID, newcollege. GETID ());
Assertequals ("Same 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 ");
// Delete student
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 ());
// Ensure that the student has been deleted
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 ("increasing the number of objects", 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);
 }
/**
* Query all school information (including class data)
*
*/
@ 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 ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.