|
20 | 20 | import java.sql.SQLException; |
21 | 21 |
|
22 | 22 | import org.junit.Assert; |
| 23 | +import org.junit.Before; |
23 | 24 | import org.junit.Test; |
24 | 25 | import org.junit.runner.RunWith; |
25 | 26 | import org.mockito.Mock; |
26 | 27 | import org.mockito.Mockito; |
27 | | -import org.mockito.runners.MockitoJUnitRunner; |
| 28 | +import org.mockito.junit.MockitoJUnitRunner; |
| 29 | + |
| 30 | +import javax.persistence.EntityExistsException; |
28 | 31 |
|
29 | 32 | @RunWith(MockitoJUnitRunner.class) |
30 | 33 | public class GenericDaoBaseTest { |
31 | 34 | @Mock |
32 | 35 | ResultSet resultSet; |
| 36 | + @Mock |
| 37 | + SQLException mockedSQLException; |
33 | 38 |
|
34 | | - @Test |
35 | | - public void getObjectBoolean() throws SQLException { |
| 39 | + private static final String INTEGRITY_CONSTRAINT_VIOLATION = "23000"; |
| 40 | + private static final int DUPLICATE_ENTRY_ERRO_CODE = 1062; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void prepareTests() throws SQLException { |
36 | 44 | Mockito.when(resultSet.getObject(1)).thenReturn(false); |
37 | 45 | Mockito.when(resultSet.getBoolean(1)).thenReturn(false); |
| 46 | + Mockito.when(resultSet.getObject(1)).thenReturn((short) 1); |
| 47 | + Mockito.when(resultSet.getShort(1)).thenReturn((short) 1); |
| 48 | + Mockito.when(resultSet.getObject(1)).thenReturn(0.1f); |
| 49 | + Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f); |
| 50 | + Mockito.when(resultSet.getObject(1)).thenReturn(0.1d); |
| 51 | + Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d); |
| 52 | + Mockito.when(resultSet.getObject(1)).thenReturn(1l); |
| 53 | + Mockito.when(resultSet.getLong(1)).thenReturn(1l); |
| 54 | + Mockito.when(resultSet.getInt(1)).thenReturn(1); |
| 55 | + Mockito.when(resultSet.getObject(1)).thenReturn((byte) 1); |
| 56 | + Mockito.when(resultSet.getByte(1)).thenReturn((byte) 1); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void getObjectBoolean() throws SQLException { |
38 | 61 | Assert.assertFalse(GenericDaoBase |
39 | 62 | .getObject(Boolean.class, resultSet, 1)); |
40 | 63 | Mockito.verify(resultSet).getBoolean(1); |
41 | 64 | } |
42 | 65 |
|
43 | 66 | @Test |
44 | 67 | public void getObjectPrimitiveBoolean() throws SQLException { |
45 | | - Mockito.when(resultSet.getObject(1)).thenReturn(false); |
46 | | - Mockito.when(resultSet.getBoolean(1)).thenReturn(false); |
47 | 68 | Assert.assertFalse(GenericDaoBase |
48 | 69 | .getObject(boolean.class, resultSet, 1)); |
49 | 70 | Mockito.verify(resultSet).getBoolean(1); |
50 | 71 | } |
51 | 72 |
|
52 | 73 | @Test |
53 | 74 | public void getObjectPrimitiveShort() throws SQLException { |
54 | | - Mockito.when(resultSet.getObject(1)).thenReturn((short) 1); |
55 | | - Mockito.when(resultSet.getShort(1)).thenReturn((short) 1); |
56 | 75 | Assert.assertEquals(Short.valueOf((short) 1), |
57 | 76 | GenericDaoBase.getObject(short.class, resultSet, 1)); |
58 | 77 | Mockito.verify(resultSet).getShort(1); |
59 | 78 | } |
60 | 79 |
|
61 | 80 | @Test |
62 | 81 | public void getObjectShort() throws SQLException { |
63 | | - Mockito.when(resultSet.getObject(1)).thenReturn((short) 1); |
64 | | - Mockito.when(resultSet.getShort(1)).thenReturn((short) 1); |
65 | 82 | Assert.assertEquals(Short.valueOf((short) 1), |
66 | 83 | GenericDaoBase.getObject(Short.class, resultSet, 1)); |
67 | 84 | Mockito.verify(resultSet).getShort(1); |
68 | 85 | } |
69 | 86 |
|
70 | 87 | @Test |
71 | 88 | public void getObjectFloat() throws SQLException { |
72 | | - Mockito.when(resultSet.getObject(1)).thenReturn(0.1f); |
73 | | - Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f); |
74 | 89 | Assert.assertEquals(0.1f, |
75 | 90 | GenericDaoBase.getObject(Float.class, resultSet, 1), 0.1); |
76 | 91 | Mockito.verify(resultSet).getFloat(1); |
77 | 92 | } |
78 | 93 |
|
79 | 94 | @Test |
80 | 95 | public void getObjectPrimitiveFloat() throws SQLException { |
81 | | - Mockito.when(resultSet.getObject(1)).thenReturn(0.1f); |
82 | | - Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f); |
83 | 96 | Assert.assertEquals(0.1f, |
84 | 97 | GenericDaoBase.getObject(float.class, resultSet, 1), 0.1); |
85 | 98 | Mockito.verify(resultSet).getFloat(1); |
86 | 99 | } |
87 | 100 |
|
88 | 101 | @Test |
89 | 102 | public void getObjectPrimitiveDouble() throws SQLException { |
90 | | - Mockito.when(resultSet.getObject(1)).thenReturn(0.1d); |
91 | | - Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d); |
92 | 103 | Assert.assertEquals(0.1d, |
93 | 104 | GenericDaoBase.getObject(double.class, resultSet, 1), 0.1); |
94 | 105 | Mockito.verify(resultSet).getDouble(1); |
95 | 106 | } |
96 | 107 |
|
97 | 108 | @Test |
98 | 109 | public void getObjectDouble() throws SQLException { |
99 | | - Mockito.when(resultSet.getObject(1)).thenReturn(0.1d); |
100 | | - Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d); |
101 | 110 | Assert.assertEquals(0.1d, |
102 | 111 | GenericDaoBase.getObject(Double.class, resultSet, 1), 0.1); |
103 | 112 | Mockito.verify(resultSet).getDouble(1); |
104 | 113 | } |
105 | 114 |
|
106 | 115 | @Test |
107 | 116 | public void getObjectLong() throws SQLException { |
108 | | - Mockito.when(resultSet.getObject(1)).thenReturn(1l); |
109 | | - Mockito.when(resultSet.getLong(1)).thenReturn(1l); |
110 | 117 | Assert.assertEquals((Long) 1l, |
111 | 118 | GenericDaoBase.getObject(Long.class, resultSet, 1)); |
112 | 119 | Mockito.verify(resultSet).getLong(1); |
113 | 120 | } |
114 | 121 |
|
115 | 122 | @Test |
116 | 123 | public void getObjectPrimitiveLong() throws SQLException { |
117 | | - Mockito.when(resultSet.getObject(1)).thenReturn(1l); |
118 | | - Mockito.when(resultSet.getLong(1)).thenReturn(1l); |
119 | 124 | Assert.assertEquals((Long) 1l, |
120 | 125 | GenericDaoBase.getObject(long.class, resultSet, 1)); |
121 | 126 | Mockito.verify(resultSet).getLong(1); |
122 | 127 | } |
123 | 128 |
|
124 | 129 | @Test |
125 | 130 | public void getObjectPrimitiveInt() throws SQLException { |
126 | | - Mockito.when(resultSet.getObject(1)).thenReturn(1l); |
127 | | - Mockito.when(resultSet.getInt(1)).thenReturn(1); |
128 | 131 | Assert.assertEquals((Integer) 1, |
129 | 132 | GenericDaoBase.getObject(int.class, resultSet, 1)); |
130 | 133 | Mockito.verify(resultSet).getInt(1); |
131 | 134 | } |
132 | 135 |
|
133 | 136 | @Test |
134 | 137 | public void getObjectInteger() throws SQLException { |
135 | | - Mockito.when(resultSet.getObject(1)).thenReturn(1l); |
136 | | - Mockito.when(resultSet.getInt(1)).thenReturn(1); |
137 | 138 | Assert.assertEquals((Integer) 1, |
138 | 139 | GenericDaoBase.getObject(Integer.class, resultSet, 1)); |
139 | 140 | Mockito.verify(resultSet).getInt(1); |
140 | 141 | } |
141 | 142 |
|
142 | 143 | @Test |
143 | 144 | public void getObjectPrimitiveByte() throws SQLException { |
144 | | - Mockito.when(resultSet.getObject(1)).thenReturn((byte) 1); |
145 | | - Mockito.when(resultSet.getByte(1)).thenReturn((byte) 1); |
146 | 145 | Assert.assertTrue((byte) 1 == GenericDaoBase.getObject(byte.class, |
147 | 146 | resultSet, 1)); |
148 | 147 | Mockito.verify(resultSet).getByte(1); |
149 | 148 | } |
150 | 149 |
|
| 150 | + @Test |
| 151 | + public void handleEntityExistsExceptionTestNoMatchForEntityExists() { |
| 152 | + Mockito.when(mockedSQLException.getErrorCode()).thenReturn(123); |
| 153 | + Mockito.when(mockedSQLException.getSQLState()).thenReturn("123"); |
| 154 | + GenericDaoBase.handleEntityExistsException(mockedSQLException); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void handleEntityExistsExceptionTestIntegrityConstraint() { |
| 159 | + Mockito.when(mockedSQLException.getErrorCode()).thenReturn(123); |
| 160 | + Mockito.when(mockedSQLException.getSQLState()).thenReturn(INTEGRITY_CONSTRAINT_VIOLATION); |
| 161 | + GenericDaoBase.handleEntityExistsException(mockedSQLException); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void handleEntityExistsExceptionTestIntegrityConstraintNull() { |
| 166 | + Mockito.when(mockedSQLException.getErrorCode()).thenReturn(123); |
| 167 | + Mockito.when(mockedSQLException.getSQLState()).thenReturn(null); |
| 168 | + GenericDaoBase.handleEntityExistsException(mockedSQLException); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void handleEntityExistsExceptionTestDuplicateEntryErrorCode() { |
| 173 | + Mockito.when(mockedSQLException.getErrorCode()).thenReturn(DUPLICATE_ENTRY_ERRO_CODE); |
| 174 | + Mockito.when(mockedSQLException.getSQLState()).thenReturn("123"); |
| 175 | + GenericDaoBase.handleEntityExistsException(mockedSQLException); |
| 176 | + } |
| 177 | + |
| 178 | + @Test(expected = EntityExistsException.class) |
| 179 | + public void handleEntityExistsExceptionTestExpectEntityExistsException() { |
| 180 | + Mockito.when(mockedSQLException.getErrorCode()).thenReturn(DUPLICATE_ENTRY_ERRO_CODE); |
| 181 | + Mockito.when(mockedSQLException.getSQLState()).thenReturn(INTEGRITY_CONSTRAINT_VIOLATION); |
| 182 | + GenericDaoBase.handleEntityExistsException(mockedSQLException); |
| 183 | + } |
| 184 | + |
151 | 185 | } |
0 commit comments