View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2008, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.dataset.datatype;
23  
24  import java.sql.Types;
25  import java.util.Arrays;
26  
27  import org.dbunit.database.statement.MockPreparedStatement;
28  
29  /**
30   * @author Timur Strekalov
31   */
32  public class UuidAwareBytesDataTypeTest extends BytesDataTypeTest
33  {
34      private final static DataType[] TYPES = {DataType.BINARY,
35          DataType.VARBINARY, DataType.LONGVARBINARY};
36  
37      public UuidAwareBytesDataTypeTest(final String name)
38      {
39          super(name);
40      }
41  
42      @Override
43      public void testTypeCast() throws Exception
44      {
45          final Object[] values =
46              {null, "uuid'2aad615a-d8e1-11e2-b8ed-50e549c9b654'"};
47  
48          final byte[][] expected =
49              {
50                  null,
51                  new byte[] {(byte) 0x2a, (byte) 0xad, (byte) 0x61,
52                          (byte) 0x5a, (byte) 0xd8, (byte) 0xe1,
53                          (byte) 0x11, (byte) 0xe2, (byte) 0xb8,
54                          (byte) 0xed, (byte) 0x50, (byte) 0xe5,
55                          (byte) 0x49, (byte) 0xc9, (byte) 0xb6,
56                          (byte) 0x54}};
57  
58          assertEquals("actual vs expected count", values.length, expected.length);
59  
60          for (DataType element : TYPES)
61          {
62              for (int j = 0; j < values.length; j++)
63              {
64                  byte[] actual = (byte[]) element.typeCast(values[j]);
65                  assertTrue("typecast " + j, Arrays.equals(expected[j], actual));
66              }
67          }
68      }
69  
70      public void testCompareEqualsUuidAware() throws Exception
71      {
72          final Object[] values1 =
73              {null, "uuid'2aad615a-d8e1-11e2-b8ed-50e549c9b654'"};
74  
75          final byte[][] values2 =
76              {
77                  null,
78                  new byte[] {(byte) 0x2a, (byte) 0xad, (byte) 0x61,
79                          (byte) 0x5a, (byte) 0xd8, (byte) 0xe1,
80                          (byte) 0x11, (byte) 0xe2, (byte) 0xb8,
81                          (byte) 0xed, (byte) 0x50, (byte) 0xe5,
82                          (byte) 0x49, (byte) 0xc9, (byte) 0xb6,
83                          (byte) 0x54}};
84  
85          assertEquals("values count", values1.length, values2.length);
86  
87          for (DataType element : TYPES)
88          {
89              for (int j = 0; j < values1.length; j++)
90              {
91                  assertEquals("compare1 " + j, 0,
92                          element.compare(values1[j], values2[j]));
93                  assertEquals("compare2 " + j, 0,
94                          element.compare(values2[j], values1[j]));
95              }
96          }
97      }
98  
99      public void testSetSqlValueWithUuid() throws Exception
100     {
101         final MockPreparedStatement preparedStatement =
102                 new MockPreparedStatement();
103 
104         final String[] given = {"uuid'2aad615a-d8e1-11e2-b8ed-50e549c9b654'"};
105 
106         final byte[][] expected =
107             {new byte[] {(byte) 0x2a, (byte) 0xad, (byte) 0x61,
108                     (byte) 0x5a, (byte) 0xd8, (byte) 0xe1, (byte) 0x11,
109                     (byte) 0xe2, (byte) 0xb8, (byte) 0xed, (byte) 0x50,
110                     (byte) 0xe5, (byte) 0x49, (byte) 0xc9, (byte) 0xb6,
111                     (byte) 0x54}};
112 
113         final int[] expectedSqlTypesForDataType =
114             {Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY};
115 
116         for (int i = 0; i < expected.length; i++)
117         {
118             final String givenValue = given[i];
119             final byte[] expectedValue = expected[i];
120 
121             for (int j = 0; j < TYPES.length; j++)
122             {
123                 final DataType dataType = TYPES[j];
124                 final int expectedSqlType = expectedSqlTypesForDataType[j];
125 
126                 dataType.setSqlValue(givenValue, 1, preparedStatement);
127 
128                 assertEquals("Loop " + i + " Type " + dataType, 1,
129                         preparedStatement.getLastSetObjectParamIndex());
130                 assertEquals("Loop " + i + " Type " + dataType,
131                         expectedSqlType,
132                         preparedStatement.getLastSetObjectTargetSqlType());
133 
134                 final byte[] actualValue =
135                         (byte[]) preparedStatement.getLastSetObjectParamValue();
136 
137                 assertTrue("Loop " + i + " Type " + dataType,
138                         Arrays.equals(expectedValue, actualValue));
139             }
140         }
141     }
142 
143     /**
144      * Historically, the wrongly formatted uuids of this test would be recorded
145      * as 'null' in the database.  However, now that the
146      * {@link org.dbunit.dataset.datatype.BytesDataType} class attempts to
147      * save any data it finds, these wrongly formatted uuids now do find
148      * their way into the database, as any other text.
149      *
150      * @throws Exception
151      */
152     public void testSetSqlValueWithSomethingThatLooksLikeUuidButIsNot()
153             throws Exception
154             {
155         final MockPreparedStatement preparedStatement =
156                 new MockPreparedStatement();
157 
158         final String[] given =
159             {"2aad615a-d8e1-11e2-b8ed-50e549c9b654",
160             "uuid'2aad615a-d8e1-11e2-b8ed-50e549c9b65'"};
161 
162         final byte[][] expected = { given[0].getBytes(), given[1].getBytes()};
163 
164         final int[] expectedSqlTypesForDataType =
165             {Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY};
166 
167         for (int i = 0; i < expected.length; i++)
168         {
169             final String givenValue = given[i];
170             final Object expectedValue = expected[i];
171 
172             for (int j = 0; j < TYPES.length; j++)
173             {
174                 final DataType dataType = TYPES[j];
175                 final int expectedSqlType = expectedSqlTypesForDataType[j];
176 
177                 dataType.setSqlValue(givenValue, 1, preparedStatement);
178 
179                 assertEquals("Loop " + i + " Type " + dataType, 1,
180                         preparedStatement.getLastSetObjectParamIndex());
181                 assertEquals("Loop " + i + " Type " + dataType,
182                         expectedSqlType,
183                         preparedStatement.getLastSetObjectTargetSqlType());
184 
185                 final Object actualValue =
186                         preparedStatement.getLastSetObjectParamValue();
187 
188                 assertEquals("Loop " + i + " Type " + dataType, true,
189                     Arrays.equals((byte[]) expectedValue, (byte[]) actualValue));
190             }
191         }
192     }
193 }