View Javadoc
1   /*
2    * Copyright (C) 2011, Red Hat, Inc.
3    * Written by Darryl L. Pierce <dpierce@redhat.com>.
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   *
19   */
20  package org.dbunit.ext.mssql;
21  
22  import java.sql.SQLException;
23  import java.util.UUID;
24  
25  import junit.framework.TestCase;
26  
27  import org.dbunit.dataset.datatype.TypeCastException;
28  
29  import com.mockobjects.sql.MockSingleRowResultSet;
30  
31  /**
32   * <code>UniqueIdentifierTypeTest</code> ensures that the {@link UniqueIdentifierType} works as expected.
33   *
34   * @author Darryl L. Pierce <dpierce@redhat.com>
35   */
36  // TODO add tests for setSqlValue(Object, int, PreparedStatement)
37  public class UniqueIdentifierTypeTest extends TestCase {
38      private UUID existingUuid;
39      private UniqueIdentifierType uuidType;
40      private MockSingleRowResultSet resultSet;
41  
42      protected void setUp() throws Exception {
43          super.setUp();
44  
45          uuidType = new UniqueIdentifierType();
46  
47          resultSet = new MockSingleRowResultSet();
48          existingUuid = UUID.randomUUID();
49      }
50  
51      protected void tearDown() throws Exception {
52          resultSet.verify();
53      }
54  
55      /**
56       * Ensures that an exception occurs if the UUID value is invalid.
57       *
58       * @throws SQLException
59       */
60      public void testGetSqlValueWithBadValue() throws SQLException {
61          resultSet.addExpectedIndexedValues(new String[] { existingUuid.toString() + "Z" });
62  
63          try {
64              uuidType.getSqlValue(1, resultSet);
65  
66              fail("Method should have throw an exception");
67          } catch (TypeCastException e) {
68              assertTrue(true);
69          }
70      }
71  
72      /**
73       * Ensures that unmarshalling a UUID value works correctly.
74       *
75       * @throws SQLException
76       * @throws TypeCastException
77       */
78      public void testGetValue() throws TypeCastException, SQLException {
79          resultSet.addExpectedIndexedValues(new String[] { existingUuid.toString() });
80  
81          UUID result = (UUID) uuidType.getSqlValue(1, resultSet);
82  
83          assertEquals(existingUuid, result);
84  
85          resultSet.verify();
86      }
87  }