<commit_before>/**
 * Copyright (C) 2011  jtalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library 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
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * Also add information on how to contact you by electronic and paper mail.
 * Creation date: Apr 12, 2011 / 8:05:19 PM
 * The jtalks.org Project
 */
package org.jtalks.jcommune.model.dao.hibernate;

import java.util.List;
import org.springframework.dao.DataIntegrityViolationException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jtalks.jcommune.model.dao.PrivateMessageDao;
import org.jtalks.jcommune.model.entity.PrivateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.*;
import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals;

/**
 *
 * @author <NAME>
 */
@ContextConfiguration(locations = {"classpath:/org/jtalks/jcommune/model/entity/applicationContext-dao.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class PrivateMessageHibernateDaoTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private SessionFactory sessionFactory;
    @Autowired
    private PrivateMessageDao dao;
    private Session session;

    @BeforeMethod
    public void setUp() throws Exception {
        session = sessionFactory.getCurrentSession();
        ObjectsFactory.setSession(session);
    }

    @Test
    public void testSave() {
        PrivateMessage pm = ObjectsFactory.getDefaultPrivateMessage();
        dao.saveOrUpdate(pm);
        assertNotSame(pm.getId(), 0, "Id not created");

        session.evict(pm);
        PrivateMessage result = (PrivateMessage) session.get(PrivateMessage.class, pm.getId());

        assertReflectionEquals(pm, result);
    }

    @Test(expectedExceptions = DataIntegrityViolationException.class)
    public void testSavePostWithDateNotNullViolation() {
        PrivateMessage pm = new PrivateMessage();
        dao.saveOrUpdate(pm);
    }

    @Test
    public void testGet() {
        PrivateMessage pm = ObjectsFactory.getDefaultPrivateMessage();
        session.save(pm);

        PrivateMessage result = dao.get(pm.getId());

        assertNotNull(result);
        assertEquals(result.getId(), pm.getId());
    }

    @Test
    public void testGetInvalidId() {
        PrivateMessage result = dao.get(-567890L);

        assertNull(result);
    }

    @Test
    public void testUpdate() {
        String newBody = "new content";
        PrivateMessage pm = ObjectsFactory.getDefaultPrivateMessage();
        session.saveOrUpdate(pm);
        pm.setBody(newBody);

        dao.saveOrUpdate(pm);
        session.evict(pm);
        PrivateMessage result = (PrivateMessage) session.get(PrivateMessage.class, pm.getId());

        assertEquals(result.getBody(), newBody);
    }
    
    @Test(expectedExceptions = DataIntegrityViolationException.class)
    public void testUpdateNotNullViolation() {
        PrivateMessage pm = ObjectsFactory.getDefaultPrivateMessage();
        session.save(pm);
        pm.setUserFrom(null);

        dao.saveOrUpdate(pm);
    }
    
     @Test
    public void testDelete() {
        PrivateMessage pm = ObjectsFactory.getDefaultPrivateMessage();
        session.save(pm);

        boolean result = dao.delete(pm.getId());
        int pmCount = getCount();

        assertTrue(result, "Entity is not deleted");
        assertEquals(pmCount, 0);
    }

    @Test
    public void testDeleteInvalidId() {
        boolean result = dao.delete(-100500L);

        assertFalse(result, "Entity deleted");
    }   
    
    @Test
    public void testGetAll() {
        PrivateMessage pm1 = ObjectsFactory.getDefaultPrivateMessage();
        session.save(pm1);       
        PrivateMessage pm2 = PrivateMessage.createNewPrivateMessage();
        pm2.setUserFrom(pm1.getUserFrom());
        pm2.setUserTo(pm1.getUserTo());
        pm2.setBody("body");
        pm2.setTitle("title");
        session.save(pm2);
        
        List<PrivateMessage> posts = dao.getAll();

        assertEquals(posts.size(), 2);
    }

    @Test
    public void testGetAllWithEmptyTable() {
        List<PrivateMessage> posts = dao.getAll();

        assertTrue(posts.isEmpty());
    }

    @Test
    public void testgetAllFromUser() {
        PrivateMessage pm = ObjectsFactory.getDefaultPrivateMessage();
        session.save(pm);
        
        List<PrivateMessage> listFrom = dao.getAllFromUser(pm.getUserFrom());
        
        assertEquals(listFrom.size(), 1);
        assertEquals(pm, listFrom.get(0));
    }
    
    @Test
    public void testgetAllToUser() {
        PrivateMessage pm = ObjectsFactory.getDefaultPrivateMessage();
        session.save(pm);
        
        List<PrivateMessage> listFrom = dao.getAllToUser(pm.getUserTo());
        
        assertEquals(listFrom.size(), 1);
        assertEquals(pm, listFrom.get(0));
    }   
    
    private int getCount() {
        return ((Number) session.createQuery("select count(*) from PrivateMessage").uniqueResult()).intValue();
    }
}<commit_msg>#JC-84 DAO is implemented and tested<commit_after>/**
 * Copyright (C) 2011  jtalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library 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
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * Also add information on how to contact you by electronic and paper mail.
 * Creation date: Apr 12, 2011 / 8:05:19 PM
 * The jtalks.org Project
 */
package org.jtalks.jcommune.model.dao.hibernate;

import java.util.List;
import org.hibernate.HibernateException;
import org.springframework.dao.DataIntegrityViolationException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jtalks.jcommune.model.dao.PrivateMessageDao;
import org.jtalks.jcommune.model.entity.PrivateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.*;
import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals;

/**
 *
 * @author <NAME>
 */
@ContextConfiguration(locations = {"classpath:/org/jtalks/jcommune/model/entity/applicationContext-dao.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class PrivateMessageHibernateDaoTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private SessionFactory sessionFactory;
    @Autowired
    private PrivateMessageDao dao;
    private Session session;

    @BeforeMethod
    public void setUp() throws Exception {
        session = sessionFactory.getCurrentSession();
        ObjectsFactory.setSession(session);
    }

    @Test
    public void testSave() {
        PrivateMessage pm = getSavedPm();
        assertNotSame(pm.getId(), 0, "Id not created");

        session.evict(pm);
        PrivateMessage result = (PrivateMessage) session.get(PrivateMessage.class, pm.getId());

        assertReflectionEquals(pm, result);
    }

    @Test(expectedExceptions = DataIntegrityViolationException.class)
    public void testSavePostWithDateNotNullViolation() {
        PrivateMessage pm = new PrivateMessage();
        dao.saveOrUpdate(pm);
    }

    @Test
    public void testGet() {
        PrivateMessage pm = getSavedPm();

        PrivateMessage result = dao.get(pm.getId());

        assertNotNull(result);
        assertEquals(result.getId(), pm.getId());
    }

    @Test
    public void testGetInvalidId() {
        PrivateMessage result = dao.get(-567890L);

        assertNull(result);
    }

    @Test
    public void testUpdate() {
        String newBody = "new content";
        PrivateMessage pm = getSavedPm();
        pm.setBody(newBody);

        dao.saveOrUpdate(pm);
        session.evict(pm);
        PrivateMessage result = (PrivateMessage) session.get(PrivateMessage.class, pm.getId());

        assertEquals(result.getBody(), newBody);
    }
    
    @Test(expectedExceptions = DataIntegrityViolationException.class)
    public void testUpdateNotNullViolation() {
        PrivateMessage pm = getSavedPm();
        pm.setUserFrom(null);

        dao.saveOrUpdate(pm);
    }
    
     @Test
    public void testDelete() {
        PrivateMessage pm = getSavedPm();

        boolean result = dao.delete(pm.getId());
        int pmCount = getCount();

        assertTrue(result, "Entity is not deleted");
        assertEquals(pmCount, 0);
    }

    @Test
    public void testDeleteInvalidId() {
        boolean result = dao.delete(-100500L);

        assertFalse(result, "Entity deleted");
    }   
    
    @Test
    public void testGetAll() {
        PrivateMessage pm1 = getSavedPm();
        PrivateMessage pm2 = PrivateMessage.createNewPrivateMessage();
        pm2.setUserFrom(pm1.getUserFrom());
        pm2.setUserTo(pm1.getUserTo());
        pm2.setBody("body");
        pm2.setTitle("title");
        session.save(pm2);
        
        List<PrivateMessage> posts = dao.getAll();

        assertEquals(posts.size(), 2);
    }

    @Test
    public void testGetAllWithEmptyTable() {
        List<PrivateMessage> posts = dao.getAll();

        assertTrue(posts.isEmpty());
    }

    @Test
    public void testgetAllFromUser() {
        PrivateMessage pm = getSavedPm();
        
        List<PrivateMessage> listFrom = dao.getAllFromUser(pm.getUserFrom());
        
        assertEquals(listFrom.size(), 1);
        assertEquals(pm, listFrom.get(0));
    }
    
    @Test
    public void testgetAllToUser() {
        PrivateMessage pm = getSavedPm();
        
        List<PrivateMessage> listFrom = dao.getAllToUser(pm.getUserTo());
        
        assertEquals(listFrom.size(), 1);
        assertEquals(pm, listFrom.get(0));
    }   
    
    private int getCount() {
        return ((Number) session.createQuery("select count(*) from PrivateMessage").uniqueResult()).intValue();
    }
    
    private PrivateMessage getSavedPm() throws HibernateException {
        PrivateMessage pm = ObjectsFactory.getDefaultPrivateMessage();
        session.saveOrUpdate(pm);
        return pm;
    }
}
{'fraction_non_alphanumeric': 0.09845416218897247, 'fraction_numerical': 0.009506489212201373, 'mean_word_length': 3.2946396876109336, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7666357', 'n_tokens_mistral': 3353, 'n_tokens_neox': 3263, 'n_words': 806}