Java tutorial
/* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package example2; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import net.kamhon.ieagle.application.Application; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import example.vo.Dept; import example2.service.DeptService; public class JpaExampleAppConfigTest { ApplicationContext context; DeptService service; @Before public void init() { context = new AnnotationConfigApplicationContext(JpaExampleAppConfig.class); Application.setApplicationContext(context); service = context.getBean(DeptService.BEAN_NAME, DeptService.class); } @After public void destroy() { Application.reset(); } @Test public void testGetDeptService() { assertNotNull(service); } @Test public void testTransactionWhereRepositoryPersistAndDaoRetrieve() { assertNotNull(service); int size = service.findAllDeptsWithDao().size(); Dept dept = createDummyDept(); service.saveDept(dept); service.saveDepts(createDummyDept(), createDummyDept()); assertNotNull(dept.getDeptId()); assertEquals(size + 3, service.findAllDeptsWithDao().size()); } private Dept createDummyDept() { Dept dept = new Dept(true); dept.setDeptName("testing"); dept.setCompId("compId"); return dept; } @Test public void testTransactionWhereRepositoryPersistAndRepositoryRetrieve() { assertNotNull(service); int size = service.findAllDepts().size(); Dept dept = createDummyDept(); service.saveDept(dept); service.saveDepts(createDummyDept(), createDummyDept()); assertNotNull(dept.getDeptId()); assertEquals(size + 3, service.findAllDepts().size()); } @Test public void testTransactionWhereDaoPersistAndRepositoryRetrieve() { int size = service.findAllDepts().size(); Dept dept = createDummyDept(); service.saveDeptWithDao(dept); service.saveDeptsWithDao(createDummyDept(), createDummyDept()); assertNotNull(dept.getDeptId()); // assertEquals(size + 1, service.findAllDeptsWithDao().size()); assertEquals(size + 3, service.findAllDepts().size()); } @Test public void testTransactionWhereDaoPersistAndDaoRetrieve() { int size = service.findAllDeptsWithDao().size(); Dept dept = createDummyDept(); service.saveDeptWithDao(dept); service.saveDeptsWithDao(createDummyDept(), createDummyDept()); assertNotNull(dept.getDeptId()); assertEquals(size + 3, service.findAllDeptsWithDao().size()); } @Test public void testRollbackTransactionWhereRepositoryPersistAndRepositoryRetrieve() { assertNotNull(service); int size = service.findAllDepts().size(); try { Dept dept = createDummyDept(); // compId is blank should throw error Dept dept2 = new Dept(true); dept2.setDeptName("testing"); service.saveDepts(dept, dept2); } catch (Exception ex) { } assertEquals(size, service.findAllDepts().size()); } @Test public void testRollbackTransactionWhereRepositoryPersistAndDaoRetrieve() { assertNotNull(service); int size = service.findAllDeptsWithDao().size(); try { Dept dept = createDummyDept(); // compId is blank should throw error Dept dept2 = new Dept(true); dept2.setDeptName("testing"); service.saveDepts(dept, dept2); } catch (Exception ex) { } assertEquals(size, service.findAllDepts().size()); } @Test public void testRollbackTransactionWhereDaoPersistAndRepositoryRetrieve() { assertNotNull(service); int size = service.findAllDepts().size(); try { Dept dept = createDummyDept(); // compId is blank should throw error Dept dept2 = new Dept(true); dept2.setDeptName("testing"); service.saveDeptsWithDao(dept, dept2); } catch (Exception ex) { } assertEquals(size, service.findAllDepts().size()); } @Test public void testRollbackTransactionWhereDaoPersistAndDaoRetrieve() { assertNotNull(service); int size = service.findAllDepts().size(); try { Dept dept = createDummyDept(); // compId is blank should throw error Dept dept2 = new Dept(true); dept2.setDeptName("testing"); service.saveDeptsWithDao(dept, dept2); } catch (Exception ex) { } assertEquals(size, service.findAllDepts().size()); } }