001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.test;
016    
017    import com.liferay.portal.kernel.util.MapUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    
020    import java.io.InputStream;
021    
022    import java.sql.Blob;
023    
024    import java.util.Arrays;
025    import java.util.Map;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class TestCase extends junit.framework.TestCase {
031    
032            protected void assertEquals(Blob expectedBlob, Blob actualBlob)
033                    throws Exception {
034    
035                    InputStream expectInputStream = expectedBlob.getBinaryStream();
036                    InputStream actualInputStream = actualBlob.getBinaryStream();
037    
038                    while (true) {
039                            int expectValue = expectInputStream.read();
040                            int actualValue = actualInputStream.read();
041    
042                            assertEquals(expectValue, actualValue);
043    
044                            if (expectValue == -1) {
045                                    break;
046                            }
047                    }
048    
049                    expectInputStream.close();
050                    actualInputStream.close();
051            }
052    
053            protected void assertEquals(double expectedDouble, double actualDouble)
054                    throws Exception {
055    
056                    assertEquals(expectedDouble, actualDouble, 0);
057            }
058    
059            protected void assertEquals(
060                    Map<String, ?> expectedMap, Map<String, ?> actualMap) {
061    
062                    assertEquals(
063                            "The maps are different sizes", expectedMap.size(),
064                            actualMap.size());
065    
066                    for (String name : expectedMap.keySet()) {
067                            assertEquals(
068                                    "The values for key '" + name + "' are different",
069                                    MapUtil.getString(expectedMap, name),
070                                    MapUtil.getString(actualMap, name));
071                    }
072            }
073    
074            protected void assertEqualsIgnoreCase(
075                    String expectedString, String actualString) {
076    
077                    if (expectedString != null) {
078                            expectedString = expectedString.toLowerCase();
079                    }
080    
081                    if (actualString != null) {
082                            actualString = actualString.toLowerCase();
083                    }
084    
085                    assertEquals(expectedString, actualString);
086            }
087    
088            protected void assertEqualsSorted(
089                    String[] expectedStringArray, String[] actualStringArray) {
090    
091                    if (expectedStringArray != null) {
092                            Arrays.sort(expectedStringArray);
093                    }
094    
095                    if (actualStringArray != null) {
096                            Arrays.sort(actualStringArray);
097                    }
098    
099                    assertEquals(
100                            StringUtil.merge(expectedStringArray),
101                            StringUtil.merge(actualStringArray));
102            }
103    
104            protected void assertLessThan(double expectedDouble, double actualDouble)
105                    throws Exception {
106    
107                    if (actualDouble > expectedDouble) {
108                            fail(actualDouble + " is not less than " + expectedDouble);
109                    }
110            }
111    
112    }