This project has retired. For details please refer to its
Attic page.
KeyManagerTestCase xref
1 package org.apache.archiva.redback.keys;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import junit.framework.TestCase;
20 import org.apache.archiva.redback.keys.AuthenticationKey;
21 import org.apache.archiva.redback.keys.KeyManager;
22 import org.apache.archiva.redback.keys.KeyManagerException;
23 import org.apache.archiva.redback.keys.KeyNotFoundException;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.springframework.test.context.ContextConfiguration;
27 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
28
29 import java.text.SimpleDateFormat;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.Date;
33 import java.util.List;
34
35
36
37
38
39
40
41 @RunWith( SpringJUnit4ClassRunner.class )
42 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath*:/spring-context.xml"} )
43 public class KeyManagerTestCase
44 extends TestCase
45 {
46 private KeyManager manager;
47
48 public KeyManager getKeyManager()
49 {
50 return manager;
51 }
52
53 public void setKeyManager( KeyManager manager )
54 {
55 this.manager = manager;
56 }
57
58 private void assertSameDates( Date expected, Date actual )
59 {
60 if ( ( expected == null ) && ( actual != null ) )
61 {
62 fail( "Expected date is null, actual date [" + actual + "]." );
63 }
64
65 if ( ( expected != null ) && ( actual == null ) )
66 {
67 fail( "Expected date [" + expected + "], actual date is null." );
68 }
69
70 SimpleDateFormat format = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z" );
71 assertEquals( format.format( expected ), format.format( actual ) );
72 }
73
74 @Test
75 public void testNormal()
76 throws KeyNotFoundException, KeyManagerException
77 {
78 String principal = "foo";
79 String purpose = "Testing";
80
81 AuthenticationKey created = getKeyManager().createKey( principal, purpose, 15 );
82
83 assertNotNull( created );
84 assertNotNull( created.getKey() );
85 assertNotNull( created.getDateCreated() );
86 assertNotNull( created.getDateExpires() );
87
88 assertEquals( principal, created.getForPrincipal() );
89 assertEquals( purpose, created.getPurpose() );
90
91 Date expectedCreated = created.getDateCreated();
92 Date expectedExpires = created.getDateExpires();
93
94 String expectedKey = created.getKey();
95
96 AuthenticationKey found = getKeyManager().findKey( expectedKey );
97
98 assertEquals( expectedKey, found.getKey() );
99 assertEquals( principal, found.getForPrincipal() );
100 assertEquals( purpose, found.getPurpose() );
101 assertSameDates( expectedCreated, found.getDateCreated() );
102 assertSameDates( expectedExpires, found.getDateExpires() );
103 }
104
105 @Test
106 public void testGetAllKeys()
107 throws KeyManagerException
108 {
109 getKeyManager().eraseDatabase();
110 AuthenticationKey created1 = getKeyManager().createKey( "foo", "Testing", 15 );
111 AuthenticationKey created2 = getKeyManager().createKey( "bar", "Something", 23 );
112
113 assertNotNull( created1 );
114 assertNotNull( created2 );
115
116 assertEquals( "foo", created1.getForPrincipal() );
117 assertEquals( "Testing", created1.getPurpose() );
118
119 assertEquals( "bar", created2.getForPrincipal() );
120 assertEquals( "Something", created2.getPurpose() );
121
122 List<AuthenticationKey> keys = getKeyManager().getAllKeys();
123 Collections.sort( keys, new Comparator<AuthenticationKey>()
124 {
125 public int compare( AuthenticationKey key1, AuthenticationKey key2 )
126 {
127 return key2.getForPrincipal().compareTo( key1.getForPrincipal() );
128 }
129 } );
130
131 AuthenticationKey found = (AuthenticationKey) keys.get( 0 );
132 assertEquals( created1.getKey(), found.getKey() );
133 assertEquals( "foo", found.getForPrincipal() );
134 assertEquals( "Testing", found.getPurpose() );
135 assertSameDates( created1.getDateCreated(), found.getDateCreated() );
136 assertSameDates( created1.getDateExpires(), found.getDateExpires() );
137
138 found = (AuthenticationKey) keys.get( 1 );
139 assertEquals( created2.getKey(), found.getKey() );
140 assertEquals( "bar", found.getForPrincipal() );
141 assertEquals( "Something", found.getPurpose() );
142 assertSameDates( created2.getDateCreated(), found.getDateCreated() );
143 assertSameDates( created2.getDateExpires(), found.getDateExpires() );
144 }
145
146 @Test
147 public void testNotThere()
148 throws KeyManagerException
149 {
150 String principal = "foo";
151 String purpose = "Testing";
152
153 AuthenticationKey created = getKeyManager().createKey( principal, purpose, 15 );
154
155 assertNotNull( created );
156 assertNotNull( created.getKey() );
157 assertNotNull( created.getDateCreated() );
158 assertNotNull( created.getDateExpires() );
159
160 assertEquals( principal, created.getForPrincipal() );
161 assertEquals( purpose, created.getPurpose() );
162
163 try
164 {
165 getKeyManager().findKey( "deadbeefkey" );
166 fail( "Invalid Key should not have been found." );
167 }
168 catch ( KeyNotFoundException e )
169 {
170
171 }
172 }
173
174 @Test
175 public void testExpired()
176 throws KeyManagerException, InterruptedException
177 {
178 String principal = "foo";
179 String purpose = "Testing";
180
181 AuthenticationKey created = getKeyManager().createKey( principal, purpose, 0 );
182
183 assertNotNull( created );
184 assertNotNull( created.getKey() );
185 assertNotNull( created.getDateCreated() );
186 assertNotNull( created.getDateExpires() );
187
188 assertEquals( principal, created.getForPrincipal() );
189 assertEquals( purpose, created.getPurpose() );
190
191 String expectedKey = created.getKey();
192
193 try
194 {
195 Thread.sleep( 500 );
196 getKeyManager().findKey( expectedKey );
197 fail( "Expired Key should not have been found." );
198 }
199 catch ( KeyNotFoundException e )
200 {
201
202 }
203 }
204
205 @Test
206 public void testPermanent()
207 throws KeyManagerException
208 {
209 String principal = "foo";
210 String purpose = "Testing";
211
212 AuthenticationKey created = getKeyManager().createKey( principal, purpose, -1 );
213
214 assertNotNull( created );
215 assertNotNull( created.getKey() );
216 assertNotNull( created.getDateCreated() );
217 assertNull( created.getDateExpires() );
218
219 assertEquals( principal, created.getForPrincipal() );
220 assertEquals( purpose, created.getPurpose() );
221
222 Date expectedCreated = created.getDateCreated();
223
224 String expectedKey = created.getKey();
225
226 AuthenticationKey found = getKeyManager().findKey( expectedKey );
227
228 assertEquals( expectedKey, found.getKey() );
229 assertEquals( principal, found.getForPrincipal() );
230 assertEquals( purpose, found.getPurpose() );
231 assertSameDates( expectedCreated, found.getDateCreated() );
232 assertNull( found.getDateExpires() );
233 }
234 }