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