KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > junit > ExtendedAssert


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.common.junit;
12
13 import junit.framework.Assert;
14
15 import java.util.Arrays JavaDoc;
16
17 /**
18  * Add more assert methods.
19  *
20  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
21  * @version $Revision: 1.1 $
22  */

23 public class ExtendedAssert extends Assert
24 {
25
26    /**
27     * @see #assertEquals(Object[], Object[])
28     */

29    public static void assertEquals(Object JavaDoc[] expected, Object JavaDoc[] actual)
30    {
31       assertEquals(null, (Object JavaDoc[])expected, (Object JavaDoc[])actual);
32    }
33
34    /**
35     * Test equality as defined by java.util.Array#equals(Object[], Object[]).
36     */

37    public static void assertEquals(String JavaDoc message, Object JavaDoc[] expected, Object JavaDoc[] actual)
38    {
39       if (Arrays.equals(expected, actual))
40       {
41          return;
42       }
43       fail(format(message, expected, actual));
44    }
45
46    private static String JavaDoc format(String JavaDoc message, Object JavaDoc expected, Object JavaDoc actual) {
47       String JavaDoc formatted= "";
48       if (message != null)
49       {
50          formatted = message + " ";
51       }
52       return formatted + "expected:<" + format(expected) + "> but was:<" + format(actual) + ">";
53    }
54
55    private static String JavaDoc format(Object JavaDoc o)
56    {
57       if (o instanceof Object JavaDoc[])
58       {
59          Object JavaDoc[] array = (Object JavaDoc[])o;
60          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("[");
61          for (int i = 0; i < array.length; i++)
62          {
63             buffer.append(i == 0 ? "" : ",").append(String.valueOf(array[i]));
64          }
65          buffer.append("]");
66          return buffer.toString();
67       }
68       else
69       {
70          return String.valueOf(o);
71       }
72    }
73
74 }
75
Popular Tags