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    import org.junit.Assert;
028    
029    /**
030     * @author Miguel Pastor
031     */
032    public class AssertUtils {
033    
034            public static void assertEquals(Blob expectedBlob, Blob actualBlob)
035                    throws Exception {
036    
037                    InputStream expectInputStream = expectedBlob.getBinaryStream();
038                    InputStream actualInputStream = actualBlob.getBinaryStream();
039    
040                    while (true) {
041                            int expectValue = expectInputStream.read();
042                            int actualValue = actualInputStream.read();
043    
044                            assertEquals(expectValue, actualValue);
045    
046                            if (expectValue == -1) {
047                                    break;
048                            }
049                    }
050    
051                    expectInputStream.close();
052                    actualInputStream.close();
053            }
054    
055            public static void assertEquals(
056                    double expectedDouble, double actualDouble) {
057    
058                    Assert.assertEquals(expectedDouble, actualDouble, 0);
059            }
060    
061            public static void assertEquals(
062                    double[] expectedArray, double[] actualArray) {
063    
064                    Assert.assertArrayEquals(expectedArray, actualArray, 0);
065            }
066    
067            public static void assertEquals(
068                    Map<String, ?> expectedMap, Map<String, ?> actualMap) {
069    
070                    Assert.assertEquals(
071                            "The maps are different sizes", expectedMap.size(),
072                            actualMap.size());
073    
074                    for (String name : expectedMap.keySet()) {
075                            Assert.assertEquals(
076                                    "The values for key '" + name + "' are different",
077                                    MapUtil.getString(expectedMap, name),
078                                    MapUtil.getString(actualMap, name));
079                    }
080            }
081    
082            public static void assertEqualsIgnoreCase(
083                    String expectedString, String actualString) {
084    
085                    if (expectedString != null) {
086                            expectedString = StringUtil.toLowerCase(expectedString);
087                    }
088    
089                    if (actualString != null) {
090                            actualString = StringUtil.toLowerCase(actualString);
091                    }
092    
093                    Assert.assertEquals(expectedString, actualString);
094            }
095    
096            public static void assertEqualsSorted(
097                    String[] expectedStringArray, String[] actualStringArray) {
098    
099                    if (expectedStringArray != null) {
100                            Arrays.sort(expectedStringArray);
101                    }
102    
103                    if (actualStringArray != null) {
104                            Arrays.sort(actualStringArray);
105                    }
106    
107                    Assert.assertEquals(
108                            StringUtil.merge(expectedStringArray),
109                            StringUtil.merge(actualStringArray));
110            }
111    
112            public static void assertLessThan(
113                            double expectedDouble, double actualDouble)
114                    throws Exception {
115    
116                    if (actualDouble > expectedDouble) {
117                            Assert.fail(actualDouble + " is not less than " + expectedDouble);
118                    }
119            }
120    
121    }