Package com.iiiss.ssh.tests

Source Code of com.iiiss.ssh.tests.ColumnDaoTest

/*
* References:
*   https://src.springframework.org/svn/spring-samples/templates/spring-jpa/trunk/template/src/test/java/mytld/mycompany/myapp/OrderPersistenceTests.java
*   http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html
*     4.9.2 @Autowired
*
* Libraries:
*   org.springframework.test-X.X.X.RELEASE.jar
*/
package com.iiiss.ssh.tests;

import static org.junit.Assert.*;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.iiiss.ssh.core.daos.ColumnDao;
import com.iiiss.ssh.core.entities.Column;

@ContextConfiguration(locations = "file:ssh/WEB-INF/app-context-test.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class ColumnDaoTest {

  @PersistenceContext
  private EntityManager entityManager;

  @Autowired
  private ColumnDao columnDao;

  @Before
  public void setUp() {
    this.verifyNoColumn();
  }

  @After
  public void tearDown() {
    this.verifyNoColumn();
  }

  @Test
  public void testOne() {
    Column column;
    List<Column> columns;

    column = columnDao.insert("example");
    verifyPersistedColumn(column, "example");

    column = columnDao.selectOne(column.getId());
    verifyPersistedColumn(column, "example");

    columns = columnDao.selectAll();
    assertEquals(1, columns.size());
    column = columns.get(0);
    verifyPersistedColumn(column, "example");

    columnDao.delete(column);
  }

  @Test
  public void testTwo() {
    Column column1, column2;
    List<Column> columns;

    column1 = columnDao.insert("column1");
    verifyPersistedColumn(column1, "column1");
    column2 = columnDao.insert("column2", column1);
    verifyPersistedColumn(column2, "column2");

    column1 = columnDao.selectOne(column1.getId());
    verifyPersistedColumn(column1, "column1");
    column2 = columnDao.selectOne(column2.getId());
    verifyPersistedColumn(column2, "column2");

    columns = columnDao.selectAll();
    assertEquals(2, columns.size());
    column1 = columns.get(0);
    column2 = columns.get(1);
    verifyPersistedColumn(column1, "column1");
    verifyPersistedColumn(column2, "column2");

    assertNull(column1.getParent());
    assertEquals(1, column1.getChildren().size());
    assertEquals(column2, column1.getChildren().get(0));

    assertEquals(column1, column2.getParent());
    assertEquals(0, column2.getChildren().size());

    columnDao.delete(column1);
  }

  private void verifyPersistedColumn(Column column, String correctName) {
    assertNotNull(column);
    assertNotNull(column.getId());
    assertEquals(correctName, column.getName());
  }

  @SuppressWarnings("unchecked")
  private void verifyNoColumn() {
    Query query = entityManager.createQuery("SELECT c FROM " + Column.ENTITY_NAME + " c ");
    List<Column> columns = query.getResultList();
    assertTrue(columns.isEmpty());
  }

}
TOP

Related Classes of com.iiiss.ssh.tests.ColumnDaoTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.