Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cz.swi2.mendeluis.service; import cz.swi2.mendeluis.dataaccesslayer.core.DatabaseConfig; import cz.swi2.mendeluis.dataaccesslayer.domain.Portlet; import cz.swi2.mendeluis.service.config.ServiceConfiguration; import cz.swi2.mendeluis.service.exception.UniqueViolationException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.log4j.varia.NullAppender; import static org.junit.Assert.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; /** * * @author Ivo */ @ContextConfiguration(classes = ServiceConfiguration.class) @TestExecutionListeners(TransactionalTestExecutionListener.class) @Transactional public class PortletServiceTest extends AbstractTestNGSpringContextTests { @Autowired private PortletService portletService; public PortletServiceTest() { org.apache.log4j.BasicConfigurator.configure(new NullAppender()); } @org.testng.annotations.Test public void testCreate() { Portlet portlet; try { // Create portlet portlet = this.portletService.createNewPortlet("name ABC", "description", "/bin/script.sh"); // Find portlet Portlet result = this.portletService.getPortletById(portlet.getId()); // assert equal assertEquals(portlet, result); } catch (UniqueViolationException ex) { Logger.getLogger(PortletServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); } } @org.testng.annotations.Test public void testGetAll() { Portlet portlet, portlet2; try { // Create portlet portlet = this.portletService.createNewPortlet("name ABC2", "description", "/bin/script.sh"); portlet2 = this.portletService.createNewPortlet("name ABC3", "description2", "/bin/script2.sh"); // Find portlet List<Portlet> results = this.portletService.getAllPortlets(); // assert equal List<Portlet> portlets = new ArrayList(); portlets.add(portlet); portlets.add(portlet2); // We do not use a different db for testing. Thefore we must test only matching items, not all. assertTrue(results.containsAll(portlets)); } catch (UniqueViolationException ex) { Logger.getLogger(PortletServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); } } @org.testng.annotations.Test public void testDelete() { Portlet portlet; try { // Create portlet portlet = this.portletService.createNewPortlet("name ABC4", "description", "/bin/script.sh"); // Delete portlet int id = portlet.getId(); this.portletService.deletePortlet(id); // Find portlet Portlet result = this.portletService.getPortletById(id); // assert equal assertEquals(result, null); } catch (UniqueViolationException ex) { Logger.getLogger(PortletServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); } } }