edu.byu.mpn.test.MpnDomainTest.java Source code

Java tutorial

Introduction

Here is the source code for edu.byu.mpn.test.MpnDomainTest.java

Source

/*
 *   Copyright 2016 Brigham Young University
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package edu.byu.mpn.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

import static org.junit.Assert.assertNotNull;

/**
 * Created by cwoodfie on 5/5/16.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-datasource-context.xml" })
public class MpnDomainTest {
    private static final Logger LOG = LogManager.getLogger(MpnDomainTest.class);

    private static final Pattern IS_MPN_DOMAIN = Pattern.compile("^edu\\.byu\\.byu-mpn\\.domain\\..+$");

    @Autowired
    private ApplicationContext ctx;

    @Test
    public void testDomainObjects() {
        assertNotNull("need application context", ctx);
        final SessionFactory sf = (SessionFactory) ctx.getBean("mpnSessionFactory");
        assertNotNull("need session factory", sf);
        final List<String> ents = new LinkedList<String>(sf.getAllClassMetadata().keySet());
        Collections.sort(ents);
        final Session session = sf.openSession();

        boolean queryPass = true;

        for (final String en : ents) {
            if (!IS_MPN_DOMAIN.matcher(en).matches()) {
                continue;
            }
            LOG.info("executing test for " + en);
            queryPass = queryPass && testEntityValidity(en, session);
        } //for entities
        Assert.assertTrue("not all queries passed", queryPass);
    }

    private boolean testEntityValidity(final String entityName, final Session sess) {
        try {
            final List results = sess.createQuery("from " + entityName).setMaxResults(5).list();
            return true;
        } catch (Exception e) {
            LOG.fatal("query failed for " + entityName, e);
            return false;
        }
    }//testEntityValidity
}