jp.co.opentone.bsol.framework.test.AbstractTestCase.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.opentone.bsol.framework.test.AbstractTestCase.java

Source

/*
 * Copyright 2016 OPEN TONE Inc.
 *
 * 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 jp.co.opentone.bsol.framework.test;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.subethamail.wiser.Wiser;
import org.w3c.dom.Document;

import jp.co.opentone.bsol.framework.test.util.ExpectedMessageStringGenerator;
import junit.framework.AssertionFailedError;

/**
 * AbstractTestCase.
 *
 * <p>
 * $Date: 2011-05-18 18:58:16 +0900 (, 18  5 2011) $
 * $Rev: 3929 $
 * $Author: tsuyoshi.hashimoto $
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:scope.xml", "classpath:applicationContextTest.xml",
        "classpath:daoContextTest.xml" })
@Transactional
@Rollback
public abstract class AbstractTestCase extends AbstractJUnit4SpringContextTests {

    protected static Wiser wiser;

    @BeforeClass
    public static void setupClass() throws IOException {
        Properties p = new Properties();
        try {
            p.load(AbstractTestCase.class.getResourceAsStream("/mail.properties"));
            wiser = new Wiser();
            wiser.setHostname(p.getProperty("mail.host"));
            wiser.setPort(Integer.valueOf(p.getProperty("mail.port")));

            wiser.start();
        } catch (IOException e) {
            throw e;
        }
    }

    @AfterClass
    public static void teardownClass() {
        if (wiser != null) {
            wiser.stop();
        }
    }

    /**
     * ?????.
     * <p>
     * ???????????{@link AssertionFailedError}??.
     * <ul>
     * <li>$action$</li>
     * <li>{n}</li>
     * </ul>
     * </p>
     * @param msg 
     * @param actionName ??
     * @param vars ???
     * @return ?
     */
    protected String createExpectedMessageString(String msg, String actionName, Object... vars) {
        return ExpectedMessageStringGenerator.generate(msg, actionName, vars);
    }

    /**
     * ???Excel????.
     * <p>
     * ????
     * <ul>
     * <li>Excel?</li>
     * <li>Excel????</li>
     * <li>Excel?</li>
     * <li>?????</li>
     * </ul>
     * </p>
     * @param sheetCount Excel?
     * @param sheetName Excel????
     * @param sheetRow Excel?????
     * @param expected 
     * @param actual Excel
     * @throws Exception ??
     */
    @SuppressWarnings("unchecked")
    protected void assertExcel(int sheetCount, String sheetName, int sheetRow, List<Object> expected, byte[] actual)
            throws Exception {
        // JXPathContext?
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(actual));
        JXPathContext context = JXPathContext.newContext(document);
        // ????
        context.registerNamespace("NS", "urn:schemas-microsoft-com:office:spreadsheet");

        // ?TEST
        assertEquals(sheetCount, ((Double) context.getValue("count(/NS:Workbook/NS:Worksheet)")).intValue());
        // ?TEST??
        assertEquals(sheetName, context.getValue("/NS:Workbook/NS:Worksheet/@ss:Name"));
        // ?TEST
        assertEquals(sheetRow + 1, Integer
                .parseInt((String) context.getValue("/NS:Workbook/NS:Worksheet/NS:Table/@ss:ExpandedRowCount")));

        // Row??
        int rows = ((Double) context.getValue("count(/NS:Workbook/NS:Worksheet/NS:Table/NS:Row)")).intValue();
        assertEquals(sheetRow + 1, rows);

        // ????
        for (int i = 1; i <= rows; i++) {
            // XPath??
            context.getVariables().declareVariable("index", i);
            // Pointer?????JXPathContext?
            Pointer pointer = context.getPointer("/NS:Workbook/NS:Worksheet/NS:Table/NS:Row[$index]");
            JXPathContext contextRow = context.getRelativeContext(pointer);
            contextRow.registerNamespace("NS", "urn:schemas-microsoft-com:office:spreadsheet");

            List<Object> expectedRow = (List<Object>) expected.get(i - 1);

            // Column??
            int columns = ((Double) contextRow.getValue("count(NS:Cell)")).intValue();
            // ????
            for (int j = 1; j <= columns; j++) {
                // Pointer?????JXPathContext?
                contextRow.getVariables().declareVariable("index", j);
                Pointer pointerColumn = contextRow.getPointer("NS:Cell[$index]");
                JXPathContext contextColumn = contextRow.getRelativeContext(pointerColumn);

                // 
                String actualColumn = (String) contextColumn.getValue("NS:Data");
                // actualColumn?String?????String??
                String expectedColumn;
                Object o = expectedRow.get(j - 1);
                if (o == null) {
                    expectedColumn = "";
                } else if (o instanceof Date) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
                    expectedColumn = dateFormat.format(o);
                } else {
                    expectedColumn = o.toString();
                }
                // ?TEST?
                assertEquals(expectedColumn, actualColumn);
            }
        }
    }
}