podd.dataaccess.hibernate.DeadlockInterceptorUnitTest.java Source code

Java tutorial

Introduction

Here is the source code for podd.dataaccess.hibernate.DeadlockInterceptorUnitTest.java

Source

/*
 * Copyright (c) 2009 - 2010. School of Information Technology and Electrical
 * Engineering, The University of Queensland.  This software is being developed
 * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project.
 * PODD is a National e-Research Architecture Taskforce (NeAT) project
 * co-funded by ANDS and ARCS.
 *
 * PODD 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.
 *
 * PODD 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 PODD.  If not, see <http://www.gnu.org/licenses/>.
 */

package podd.dataaccess.hibernate;

import org.apache.log4j.Logger;
import org.apache.log4j.Appender;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.WriterAppender;
import org.hibernate.exception.LockAcquisitionException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import podd.dataaccess.Idempotent;
import podd.dataaccess.PoddObjectDAO;
import podd.dataaccess.exception.DataAccessException;
import podd.dataaccess.fedora.FedoraDAO;
import podd.model.entity.PoddConcept;
import podd.model.entity.PoddObject;
import podd.model.entity.impl.PoddConceptImpl;
import podd.model.exception.EntityException;
import podd.model.project.impl.ProjectImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Method;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * @author Yuan-Fang Li
 * @version $Id$
 */
public class DeadlockInterceptorUnitTest {
    private static final ClassPathXmlApplicationContext BEAN_FACTORY = new ClassPathXmlApplicationContext(
            "test-application-wiring.xml");
    private static final Logger LOGGER = Logger.getLogger(DeadlockInterceptor.class.getName());

    private PoddObjectDAO dao;
    private DeadlockInterceptor interceptor;

    private Appender appender;
    private StringWriter writer;

    private PoddObject object;

    @Before
    public void setUp() throws EntityException {
        dao = (PoddObjectDAO) BEAN_FACTORY.getBean("errorObjectDao");
        interceptor = (DeadlockInterceptor) BEAN_FACTORY.getBean("deadlockInterceptor");

        // FIXME: Should not be setting up loggers in code
        writer = new StringWriter();
        appender = new WriterAppender(new SimpleLayout(), writer);
        LOGGER.addAppender(appender);

        PoddConcept concept = new PoddConceptImpl();
        concept.setConceptName("AConcept");
        object = new ProjectImpl();
        object.setId(1234L);
        object.setPid("changeme:1234");
        object.setObjectName("a miserable object");
        object.setConcept(concept);
    }

    @After
    public void tearDown() throws IOException {
        writer.close();
        LOGGER.removeAppender(appender);
    }

    @Test
    public void testExceptionInterceptedInSave() throws DataAccessException, IOException {
        try {
            dao.save(object);
        } catch (LockAcquisitionException ignore) {
        }
        checkInterceptorLog(writer, object.toString());
    }

    @Test
    public void testExceptionInterceptedInDelete() throws DataAccessException, IOException {
        try {
            dao.delete(object);
        } catch (LockAcquisitionException ignore) {
        }
        checkInterceptorLog(writer, object.toString());
    }

    @Test
    public void testExceptionInterceptedInLoadPid() throws DataAccessException, IOException {
        try {
            dao.load(object.getPid());
        } catch (LockAcquisitionException ignore) {
        }
        checkInterceptorLog(writer, object.getPid());
    }

    @Test
    public void testExceptionInterceptedInLoadId() throws DataAccessException, IOException {
        try {
            dao.load(object.getId());
        } catch (LockAcquisitionException ignore) {
        }
        checkInterceptorLog(writer, object.getId().toString());
    }

    @Test
    public void testExceptionInterceptedInRefresh() throws DataAccessException, IOException {
        try {
            dao.refresh(object);
        } catch (LockAcquisitionException ignore) {
        }
        checkInterceptorLog(writer, object.toString());
    }

    @Test
    public void testExceptionNotIntercepted() throws DataAccessException {
        try {
            dao.getAll();
        } catch (LockAcquisitionException ignore) {
        }
        assertTrue(writer.toString().isEmpty());
    }

    @Test
    public void testNotInterceptedMethods() throws DataAccessException {
        try {
            dao.purgeCache("abc");
            dao.update(object);
            dao.loadByLocalName("a name");
            dao.merge(object);
        } catch (Exception ignore) {
        }
        assertTrue(writer.toString().isEmpty());
    }

    @Test
    public void testInterceptedMethodsNull() throws Exception {
        object = null;

        final Method[] methods = FedoraDAO.class.getMethods();
        for (Method mtd : methods) {
            checkOneMethod(mtd);
        }
    }

    private void checkOneMethod(Method mtd) throws Exception {
        StringWriter writer = new StringWriter();

        // FIXME: Should not be doing this in code
        Appender appender = new WriterAppender(new SimpleLayout(), writer);
        LOGGER.addAppender(appender);

        try {
            if (mtd.isAnnotationPresent(Idempotent.class)) {
                Object[] params = new Object[] { null };
                try {
                    mtd.invoke(dao, params);
                } catch (Exception e) {
                    if (!interceptor.getExceptionClass().isInstance(e.getCause())) {
                        throw e;
                    }
                }
                checkInterceptorLog(writer, null);
            }
        } finally {
            LOGGER.removeAppender(appender);
        }
    }

    @Test
    public void testNotInterceptedMethodsNull() throws DataAccessException {
        try {
            dao.purgeCache(null);
            dao.loadByLocalName(null);
            dao.merge(null);
        } catch (Exception ignore) {
        }
        assertTrue(writer.toString().isEmpty());
    }

    private void checkInterceptorLog(StringWriter writer, String identity) throws IOException {
        final String exceptionName = interceptor.getExceptionClass().getName();
        String log = writer.toString();
        assertNotNull(log);
        BufferedReader reader = new BufferedReader(new StringReader(log));
        int logCount = 0;
        String line;
        while (null != (line = reader.readLine())) {
            if (line.contains(exceptionName)) {
                logCount++;
            }
        }
        assertEquals(interceptor.getRetry(), logCount);
        if (null != identity) {
            assertTrue(log.contains(identity));
        }
    }
}