com.pureinfo.force.junit.AssertUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.force.junit.AssertUtil.java

Source

/**
 * PureInfo Force
 * @(#)AssertUtil.java   1.0 Dec 10, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.force.junit;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.Assert;

import org.dom4j.Attribute;
import org.dom4j.Element;

import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on Dec 10, 2005 8:38:21 PM <BR>
 * Last modified on Dec 10, 2005
 * </P>
 * 
 * @author Freeman.Hu
 * @version 1.0, Dec 10, 2005
 * @since Force 1.0
 */
public class AssertUtil {
    public static void assertEquals(String[] _expected, String[] _actual) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i]);
            }
        }
    }

    public static void assertEquals(int[] _expected, int[] _actual) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i]);
            }
        }
    }

    public static void assertEquals(long[] _expected, long[] _actual) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i]);
            }
        }
    }

    public static void assertEquals(short[] _expected, short[] _actual) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i]);
            }
        }
    }

    public static void assertEquals(byte[] _expected, byte[] _actual) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i]);
            }
        }
    }

    public static void assertEquals(boolean[] _expected, boolean[] _actual) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i]);
            }
        }
    }

    public static void assertEquals(char[] _expected, char[] _actual) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i]);
            }
        }
    }

    public static void assertEquals(double[] _expected, double[] _actual, double _delta) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i], _delta);
            }
        }
    }

    public static void assertEquals(float[] _expected, float[] _actual, double _delta) {
        if (_expected == null) {
            Assert.assertNull("expected is null", _actual);
        } else {
            Assert.assertNotNull(_actual);
            Assert.assertEquals("assert length", _expected.length, _actual.length);
            for (int i = 0; i < _expected.length; i++) {
                Assert.assertEquals("assert index[" + i + "]", _expected[i], _actual[i], _delta);
            }
        }
    }

    /**
     * ElementnameElement<br>
     * Attribute
     * 
     * @param _sXMLString1
     * @param _sXMLString2
     * @throws PureException
     */
    public final static void assertXMLEquals(String _sXMLString1, String _sXMLString2) throws PureException {
        Element expected = XMLUtil.strToElement(_sXMLString1);
        Element actual = XMLUtil.strToElement(_sXMLString2);
        assertXMLEquals(expected, actual);

    }

    /**
     * ElementnameElement<br>
     * Attribute
     * 
     * @param _expectedElement
     * @param _actualElement
     */
    public static void assertXMLEquals(Element _expectedElement, Element _actualElement) {
        Assert.assertEquals("element name", _expectedElement.getName(), _actualElement.getName());
        List expectedAttributes = _expectedElement.attributes();
        List actualAttributes = _actualElement.attributes();
        Assert.assertEquals("attribute size", expectedAttributes.size(), actualAttributes.size());

        Map expectedNames = new HashMap();
        Map actualNames = new HashMap();
        for (Iterator iter = expectedAttributes.iterator(); iter.hasNext();) {
            Attribute attr = (Attribute) iter.next();
            expectedNames.put(attr.getName(), attr.getValue());
        }
        for (Iterator iter = actualAttributes.iterator(); iter.hasNext();) {
            Attribute attr = (Attribute) iter.next();
            actualNames.put(attr.getName(), attr.getValue());
        }
        Assert.assertEquals("attribute", expectedNames, actualNames);

        List expectedElements = _expectedElement.elements();
        List actualElements = _actualElement.elements();
        Assert.assertEquals("element size", expectedElements.size(), actualElements.size());

        for (Iterator iter1 = expectedElements.iterator(), iter2 = actualElements.iterator(); iter1.hasNext();) {
            Element expecteElement = (Element) iter1.next();
            Element actualElement = (Element) iter2.next();
            assertXMLEquals(expecteElement, actualElement);
        }
    }
}