1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
package org .springframework .security .acls .jdbc ;
17
17
18
18
19
- import static org .assertj .core .api .Assertions .assertThat ;
20
- import static org .mockito .BDDMockito .given ;
19
+ import org .junit .Before ;
20
+ import org .junit .Test ;
21
+ import org .junit .runner .RunWith ;
22
+ import org .mockito .Mock ;
23
+ import org .mockito .junit .MockitoJUnitRunner ;
24
+ import org .springframework .core .convert .ConversionService ;
21
25
22
26
import java .io .Serializable ;
23
27
import java .sql .ResultSet ;
24
28
import java .sql .SQLException ;
25
29
import java .util .UUID ;
26
30
27
- import org .junit .Before ;
28
- import org .junit .Test ;
29
- import org .junit .runner .RunWith ;
30
- import org .mockito .InjectMocks ;
31
- import org .mockito .Mock ;
32
- import org .mockito .runners .MockitoJUnitRunner ;
33
- import org .springframework .core .convert .ConversionService ;
31
+ import static org .assertj .core .api .Assertions .assertThat ;
32
+ import static org .mockito .BDDMockito .given ;
34
33
35
34
/**
36
35
* Tests for {@link AclClassIdUtils}.
@@ -46,40 +45,23 @@ public class AclClassIdUtilsTest {
46
45
private ResultSet resultSet ;
47
46
@ Mock
48
47
private ConversionService conversionService ;
49
- @ InjectMocks
48
+
50
49
private AclClassIdUtils aclClassIdUtils ;
51
50
52
51
@ Before
53
- public void setUp () throws Exception {
54
- given (conversionService .canConvert (String .class , Long .class )).willReturn (true );
55
- given (conversionService .convert (DEFAULT_IDENTIFIER , Long .class )).willReturn (new Long (DEFAULT_IDENTIFIER ));
56
- given (conversionService .convert (DEFAULT_IDENTIFIER_AS_STRING , Long .class )).willReturn (new Long (DEFAULT_IDENTIFIER ));
52
+ public void setUp () {
53
+ aclClassIdUtils = new AclClassIdUtils ();
57
54
}
58
55
59
56
@ Test
60
- public void shouldReturnLongIfIdentifierIsNotStringAndNoConversionService () throws SQLException {
61
- // given
62
- AclClassIdUtils aclClassIdUtilsWithoutConversionSvc = new AclClassIdUtils ();
63
-
57
+ public void shouldReturnLongIfIdentifierIsLong () throws SQLException {
64
58
// when
65
- Serializable newIdentifier = aclClassIdUtilsWithoutConversionSvc .identifierFrom (DEFAULT_IDENTIFIER , resultSet );
59
+ Serializable newIdentifier = aclClassIdUtils .identifierFrom (DEFAULT_IDENTIFIER , resultSet );
66
60
67
61
// then
68
62
assertThat (newIdentifier ).isEqualTo (DEFAULT_IDENTIFIER );
69
63
}
70
64
71
- @ Test
72
- public void shouldReturnLongIfIdentifierIsNotString () throws SQLException {
73
- // given
74
- Long prevIdentifier = 999L ;
75
-
76
- // when
77
- Serializable newIdentifier = aclClassIdUtils .identifierFrom (prevIdentifier , resultSet );
78
-
79
- // then
80
- assertThat (newIdentifier ).isEqualTo (prevIdentifier );
81
- }
82
-
83
65
@ Test
84
66
public void shouldReturnLongIfClassIdTypeIsNull () throws SQLException {
85
67
// given
@@ -117,10 +99,11 @@ public void shouldReturnLongIfTypeClassNotFound() throws SQLException {
117
99
}
118
100
119
101
@ Test
120
- public void shouldReturnLongIfTypeClassCannotBeConverted () throws SQLException {
102
+ public void shouldReturnLongEvenIfCustomConversionServiceDoesNotSupportLongConversion () throws SQLException {
121
103
// given
122
104
given (resultSet .getString ("class_id_type" )).willReturn ("java.lang.Long" );
123
105
given (conversionService .canConvert (String .class , Long .class )).willReturn (false );
106
+ aclClassIdUtils .setConversionService (conversionService );
124
107
125
108
// when
126
109
Serializable newIdentifier = aclClassIdUtils .identifierFrom (DEFAULT_IDENTIFIER_AS_STRING , resultSet );
@@ -145,10 +128,7 @@ public void shouldReturnLongWhenLongClassIdType() throws SQLException {
145
128
public void shouldReturnUUIDWhenUUIDClassIdType () throws SQLException {
146
129
// given
147
130
UUID identifier = UUID .randomUUID ();
148
- String identifierAsString = identifier .toString ();
149
131
given (resultSet .getString ("class_id_type" )).willReturn ("java.util.UUID" );
150
- given (conversionService .canConvert (String .class , UUID .class )).willReturn (true );
151
- given (conversionService .convert (identifierAsString , UUID .class )).willReturn (UUID .fromString (identifierAsString ));
152
132
153
133
// when
154
134
Serializable newIdentifier = aclClassIdUtils .identifierFrom (identifier .toString (), resultSet );
@@ -157,4 +137,28 @@ public void shouldReturnUUIDWhenUUIDClassIdType() throws SQLException {
157
137
assertThat (newIdentifier ).isEqualTo (identifier );
158
138
}
159
139
140
+ @ Test
141
+ public void shouldReturnStringWhenStringClassIdType () throws SQLException {
142
+ // given
143
+ String identifier = "MY_STRING_IDENTIFIER" ;
144
+ given (resultSet .getString ("class_id_type" )).willReturn ("java.lang.String" );
145
+
146
+ // when
147
+ Serializable newIdentifier = aclClassIdUtils .identifierFrom (identifier , resultSet );
148
+
149
+ // then
150
+ assertThat (newIdentifier ).isEqualTo (identifier );
151
+ }
152
+
153
+ @ Test (expected = IllegalArgumentException .class )
154
+ public void shouldNotAcceptNullConversionServiceInConstruction () throws SQLException {
155
+ // when
156
+ new AclClassIdUtils (null );
157
+ }
158
+
159
+ @ Test (expected = IllegalArgumentException .class )
160
+ public void shouldNotAcceptNullConversionServiceInSetter () throws SQLException {
161
+ // when
162
+ aclClassIdUtils .setConversionService (null );
163
+ }
160
164
}
0 commit comments