Java tutorial
/* * TestTaskListService.java * Copyright (c) 2009 Felix Cachaldora Sanchez * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */ package com.wickettasks.business.services.tasklist; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.wickettasks.business.AbstractWicketTasksTests; import com.wickettasks.business.entities.tasklist.TaskList; import com.wickettasks.business.entities.user.User; import com.wickettasks.business.exceptions.user.AccessRestrictionException; import com.wickettasks.business.exceptions.user.ExistingUserException; import com.wickettasks.business.services.user.UserService; public class TestTaskListService extends AbstractWicketTasksTests { @Autowired private UserService userService; @Autowired private TaskListService taskListService; private User user; @Before public void addUser() { try { this.user = this.userService.add("test@email.com", "password"); } catch (ExistingUserException e) { throw new InternalError(e.toString()); } } @Test public void testAddTaskList() { for (int i = 0; i < 100; i++) { this.taskListService.add(String.valueOf(i), this.user.getId()); } assertEquals(Integer.valueOf(100), Integer.valueOf(this.taskListService.findByUserId(this.user.getId()).size())); } @Test(expected = IllegalArgumentException.class) public void aNameIsMandatoryToAddATask() { this.taskListService.add(null, this.user.getId()); } @Test(expected = IllegalArgumentException.class) public void anUserIdIsMandatoryToAddATask() { this.taskListService.add("testTaskList", null); } @Test public void testDeleteTaskList() { TaskList taskList = this.taskListService.add("testTaskList", this.user.getId()); Integer taskListId = taskList.getId(); try { this.taskListService.delete(taskListId, this.user.getId()); } catch (AccessRestrictionException e) { throw new AssertionError(e); } List<TaskList> userTaskLists = this.taskListService.findByUserId(this.user.getId()); assertEquals(Boolean.TRUE, Boolean.valueOf(userTaskLists.isEmpty())); } @Test(expected = AccessRestrictionException.class) public void testDeleteTaskListWithWrongUserId() throws AccessRestrictionException { TaskList taskList = this.taskListService.add("testTaskList", this.user.getId()); Integer taskListId = taskList.getId(); this.taskListService.delete(taskListId, Integer.valueOf(1000)); } @Test public void testFindTaskListsByUserId() { List<TaskList> taskLists = this.taskListService.findByUserId(this.user.getId()); assertEquals(Boolean.TRUE, Boolean.valueOf(taskLists.isEmpty())); for (int i = 0; i < 100; i++) { this.taskListService.add(String.valueOf(i), this.user.getId()); } taskLists = this.taskListService.findByUserId(this.user.getId()); assertEquals(Boolean.TRUE, Boolean.valueOf(taskLists.size() == 100)); } }