1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.ezmorph.test;
18  
19  import junit.framework.AssertionFailedError;
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  import junit.textui.TestRunner;
24  
25  /**
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public class ByteArrayAssertionsTest extends TestCase
29  {
30     public static void main( String[] args )
31     {
32        TestRunner.run( suite() );
33     }
34  
35     public static Test suite()
36     {
37        TestSuite suite = new TestSuite( ByteArrayAssertionsTest.class );
38        suite.setName( "ByteArrayAssertions Tests" );
39        return suite;
40     }
41  
42     public ByteArrayAssertionsTest( String name )
43     {
44        super( name );
45     }
46  
47     // -----------------------------------------------------------------------
48  
49     public void testAssertEquals_byte_byte()
50     {
51        byte[] expecteds = new byte[] { 1, 2 };
52        byte[] actuals = new byte[] { 1, 2 };
53        ArrayAssertions.assertEquals( expecteds, actuals );
54     }
55  
56     public void testAssertEquals_byte_Byte()
57     {
58        byte[] expecteds = new byte[] { 1, 2 };
59        Byte[] actuals = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
60        ArrayAssertions.assertEquals( expecteds, actuals );
61     }
62  
63     public void testAssertEquals_Byte_byte()
64     {
65        Byte[] expecteds = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
66        byte[] actuals = new byte[] { 1, 2 };
67        ArrayAssertions.assertEquals( expecteds, actuals );
68     }
69  
70     public void testAssertEquals_Byte_Byte()
71     {
72        Byte[] expecteds = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
73        Byte[] actuals = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
74        ArrayAssertions.assertEquals( expecteds, actuals );
75     }
76  
77     public void testAssertEquals_byte_byte_actuals_is_null()
78     {
79        boolean errorThrown = false;
80        byte[] expecteds = new byte[] { 1, 2 };
81        try{
82           ArrayAssertions.assertEquals( expecteds, (byte[]) null );
83        }
84        catch( AssertionFailedError expected ){
85           errorThrown = true;
86        }
87        assertTrue( "Expected a failure", errorThrown );
88     }
89  
90     public void testAssertEquals_byte_Byte_actuals_is_null()
91     {
92        boolean errorThrown = false;
93        byte[] expecteds = new byte[] { 1, 2 };
94        try{
95           ArrayAssertions.assertEquals( expecteds, (Byte[]) null );
96        }
97        catch( AssertionFailedError expected ){
98           errorThrown = true;
99        }
100       assertTrue( "Expected a failure", errorThrown );
101    }
102 
103    public void testAssertEquals_Byte_byte_actuals_is_null()
104    {
105       boolean errorThrown = false;
106       Byte[] expecteds = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
107       try{
108          ArrayAssertions.assertEquals( expecteds, (byte[]) null );
109       }
110       catch( AssertionFailedError expected ){
111          errorThrown = true;
112       }
113       assertTrue( "Expected a failure", errorThrown );
114    }
115 
116    public void testAssertEquals_byte_byte_different_length()
117    {
118       byte[] expecteds = new byte[] { 1 };
119       byte[] actuals = new byte[] { 1, 2 };
120       boolean errorThrown = false;
121       try{
122          ArrayAssertions.assertEquals( expecteds, actuals );
123       }
124       catch( AssertionFailedError expected ){
125          errorThrown = true;
126       }
127       assertTrue( "Expected a failure", errorThrown );
128    }
129 
130    public void testAssertEquals_byte_Byte_different_length()
131    {
132       byte[] expecteds = new byte[] { 1 };
133       Byte[] actuals = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
134       boolean errorThrown = false;
135       try{
136          ArrayAssertions.assertEquals( expecteds, actuals );
137       }
138       catch( AssertionFailedError expected ){
139          errorThrown = true;
140       }
141       assertTrue( "Expected a failure", errorThrown );
142    }
143 
144    public void testAssertEquals_Byte_byte_different_length()
145    {
146       Byte[] expecteds = new Byte[] { new Byte( (byte) 1 ) };
147       byte[] actuals = new byte[] { 1, 2 };
148       boolean errorThrown = false;
149       try{
150          ArrayAssertions.assertEquals( expecteds, actuals );
151       }
152       catch( AssertionFailedError expected ){
153          errorThrown = true;
154       }
155       assertTrue( "Expected a failure", errorThrown );
156    }
157 
158    public void testAssertEquals_byte_byte_expecteds_is_null()
159    {
160       boolean errorThrown = false;
161       byte[] actuals = new byte[] { 1, 2 };
162       try{
163          ArrayAssertions.assertEquals( (byte[]) null, actuals );
164       }
165       catch( AssertionFailedError expected ){
166          errorThrown = true;
167       }
168       assertTrue( "Expected a failure", errorThrown );
169    }
170 
171    public void testAssertEquals_byte_Byte_expecteds_is_null()
172    {
173       boolean errorThrown = false;
174       Byte[] actuals = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
175       try{
176          ArrayAssertions.assertEquals( (byte[]) null, actuals );
177       }
178       catch( AssertionFailedError expected ){
179          errorThrown = true;
180       }
181       assertTrue( "Expected a failure", errorThrown );
182    }
183 
184    public void testAssertEquals_Byte_byte_expecteds_is_null()
185    {
186       boolean errorThrown = false;
187       byte[] actuals = new byte[] { 1, 2 };
188       try{
189          ArrayAssertions.assertEquals( (Byte[]) null, actuals );
190       }
191       catch( AssertionFailedError expected ){
192          errorThrown = true;
193       }
194       assertTrue( "Expected a failure", errorThrown );
195    }
196 
197    public void testAssertEquals_multi_byte_byte()
198    {
199       byte[][] expecteds = new byte[][] { { 1, 2 }, { 1, 2 } };
200       byte[][] actuals = new byte[][] { { 1, 2 }, { 1, 2 } };
201       ArrayAssertions.assertEquals( expecteds, actuals );
202    }
203 
204    public void testAssertEquals_multi_byte_Byte()
205    {
206       byte[][] expecteds = new byte[][] { { 1, 2 }, { 1, 2 } };
207       Byte[][] actuals = new Byte[][] { { new Byte( (byte) 1 ), new Byte( (byte) 2 ) },
208             { new Byte( (byte) 1 ), new Byte( (byte) 2 ) } };
209       ArrayAssertions.assertEquals( expecteds, actuals );
210    }
211 
212    public void testAssertEquals_multi_Byte_byte()
213    {
214       Byte[][] expecteds = new Byte[][] { { new Byte( (byte) 1 ), new Byte( (byte) 2 ) },
215             { new Byte( (byte) 1 ), new Byte( (byte) 2 ) } };
216       byte[][] actuals = new byte[][] { { 1, 2 }, { 1, 2 } };
217       ArrayAssertions.assertEquals( expecteds, actuals );
218    }
219 
220    public void testAssertEquals_multi_Byte_Byte()
221    {
222       Byte[][] expecteds = new Byte[][] { { new Byte( (byte) 1 ), new Byte( (byte) 2 ) },
223             { new Byte( (byte) 1 ), new Byte( (byte) 2 ) } };
224       Byte[][] actuals = new Byte[][] { { new Byte( (byte) 1 ), new Byte( (byte) 2 ) },
225             { new Byte( (byte) 1 ), new Byte( (byte) 2 ) } };
226       ArrayAssertions.assertEquals( expecteds, actuals );
227    }
228 
229    public void testAssertEquals_OO_byte_byte()
230    {
231       Object expecteds = new byte[] { 1, 2 };
232       Object actuals = new byte[] { 1, 2 };
233       ArrayAssertions.assertEquals( expecteds, actuals );
234    }
235 
236    public void testAssertEquals_OO_byte_Byte()
237    {
238       Object expecteds = new byte[] { 1, 2 };
239       Object actuals = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
240       ArrayAssertions.assertEquals( expecteds, actuals );
241    }
242 
243    public void testAssertEquals_OO_Byte_byte()
244    {
245       Object expecteds = new Byte[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
246       Object actuals = new byte[] { 1, 2 };
247       ArrayAssertions.assertEquals( expecteds, actuals );
248    }
249 
250    public void testAssertEquals_OO_byte_double()
251    {
252       boolean errorThrown = false;
253       Object expecteds = new byte[] { 1, 2 };
254       Object actuals = new double[] { 1, 2 };
255       try{
256          ArrayAssertions.assertEquals( expecteds, actuals );
257       }
258       catch( AssertionFailedError expected ){
259          errorThrown = true;
260       }
261       assertTrue( "Expected a failure", errorThrown );
262    }
263 
264    public void testAssertEquals_OO_byte_Object_array()
265    {
266       Object expecteds = new byte[] { 1, 2 };
267       Object actuals = new Object[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
268       ArrayAssertions.assertEquals( expecteds, actuals );
269    }
270 
271    public void testAssertEquals_OO_Object_array_byte()
272    {
273       Object expecteds = new Object[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
274       Object actuals = new byte[] { 1, 2 };
275       ArrayAssertions.assertEquals( expecteds, actuals );
276    }
277 
278    public void testAssertEquals_OO_Object_array_Object_array()
279    {
280       Object expecteds = new Object[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
281       Object actuals = new Object[] { new Byte( (byte) 1 ), new Byte( (byte) 2 ) };
282       ArrayAssertions.assertEquals( expecteds, actuals );
283    }
284 }