/**
 * Multimud Helper.
 * 
 * Copyright (C) 2009 Benjamin Lerman<br>
 * Copyright (C) 2009 Jacques ???
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.ambre.multimud_helper.hibernate;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import net.ambre.multimud_helper.bean.Item;
import net.ambre.multimud_helper.config.TestBase;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Unit testing for hibernate access.
 */
public class HibernateHelperTest extends TestBase {

    @Autowired
    private SessionFactory hibernateSessionFactory;

    /**
     * Testing that a session can be created.
     */
    @Test
    @Transactional
    public void testSessionCreation() {
        Session session = hibernateSessionFactory.getCurrentSession();
        assertNotNull(session);
    }

    /**
     * Testing than an object can be created, and then retrieved from the databse.
     */
    @Test
    @Transactional
    public void testPersistObject() {
        Session session = hibernateSessionFactory.getCurrentSession();
        session.save(new Item());
        assertEquals(1, session.createCriteria(Item.class).list().size());
    }

    /**
     * Testing that session is empty at the beginning of a new test, even if the last test did durty the database.
     */
    @Test
    @Transactional
    public void testEmptySession() {
        Session session = hibernateSessionFactory.getCurrentSession();
        assertEquals(0, session.createCriteria(Item.class).list().size());
    }
}
