KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ra > UsernameGeneratorTest


1 package se.anatom.ejbca.ra;
2
3 import junit.framework.TestCase;
4
5 import org.ejbca.core.model.ra.UsernameGenerator;
6 import org.ejbca.core.model.ra.UsernameGeneratorParams;
7
8 public class UsernameGeneratorTest extends TestCase {
9
10     /*
11      * Test method for 'org.ejbca.core.model.ra.UsernameGenerator.UsernameGenerator(String)'
12      */

13     public void testUsernameGeneratorRandom() {
14         UsernameGenerator gen = UsernameGenerator.getInstance(UsernameGeneratorParams.RANDOM);
15         String JavaDoc u = gen.generateUsername();
16         assertEquals(u.length(), 12);
17         
18         UsernameGeneratorParams params = gen.getParams();
19         params.setRandomNameLength(5);
20         gen.setParams(params);
21         u = gen.generateUsername();
22         assertEquals(u.length(), 5);
23         
24         params.setPrefix("foo-");
25         gen.setParams(params);
26         u = gen.generateUsername();
27         assertEquals(u.length(), 9);
28         assertTrue(u.startsWith("foo-"));
29
30         params.setPostfix("-foo");
31         params.setPrefix(null);
32         gen.setParams(params);
33         u = gen.generateUsername();
34         assertEquals(u.length(), 9);
35         assertTrue(u.endsWith("-foo"));
36         
37         params.setPrefix("foo-${RANDOM}");
38         params.setPostfix(null);
39         params.setRandomPrefixLength(6);
40         gen.setParams(params);
41         u = gen.generateUsername();
42         assertEquals(u.length(), 15);
43         assertTrue(u.startsWith("foo-"));
44
45         params.setPostfix("${RANDOM}-foo");
46         params.setPrefix(null);
47         params.setRandomPrefixLength(5);
48         gen.setParams(params);
49         u = gen.generateUsername();
50         assertEquals(u.length(), 14);
51         assertTrue(u.endsWith("-foo"));
52         
53         params.setPrefix("foo-");
54         gen.setParams(params);
55         u = gen.generateUsername();
56         assertEquals(u.length(), 18);
57         assertTrue(u.endsWith("-foo"));
58         assertTrue(u.startsWith("foo-"));
59     }
60
61     public void testUsernameGeneratorDN() {
62         String JavaDoc dn = "C=SE, O=FooO, UID=foo, CN=bar";
63         UsernameGenerator gen = UsernameGenerator.getInstance(UsernameGeneratorParams.DN);
64         String JavaDoc u = gen.generateUsername(dn);
65         assertEquals(u, "bar");
66         
67         UsernameGeneratorParams params = gen.getParams();
68         params.setDNGeneratorComponent("UID");
69         gen.setParams(params);
70         u = gen.generateUsername(dn);
71         assertEquals(u, "foo");
72         
73         params.setPrefix("pre-");
74         gen.setParams(params);
75         u = gen.generateUsername(dn);
76         assertEquals(u, "pre-foo");
77
78         params.setPostfix("-post");
79         params.setPrefix(null);
80         gen.setParams(params);
81         u = gen.generateUsername(dn);
82         assertEquals(u, "foo-post");
83         
84         params.setPrefix("pre-${RANDOM}-");
85         params.setPostfix(null);
86         params.setRandomPrefixLength(6);
87         gen.setParams(params);
88         u = gen.generateUsername(dn);
89         assertEquals(u.length(), 14);
90         assertTrue(u.startsWith("pre-"));
91         assertTrue(u.endsWith("-foo"));
92
93         params.setPostfix("-${RANDOM}-post");
94         params.setPrefix(null);
95         params.setRandomPrefixLength(5);
96         gen.setParams(params);
97         u = gen.generateUsername(dn);
98         assertEquals(u.length(), 14);
99         assertTrue(u.startsWith("foo-"));
100         assertTrue(u.endsWith("-post"));
101     }
102
103     public void testUsernameGeneratorUsername() {
104         String JavaDoc username = "foo";
105         UsernameGenerator gen = UsernameGenerator.getInstance(UsernameGeneratorParams.USERNAME);
106         String JavaDoc u = gen.generateUsername(username);
107         assertEquals(u, "foo");
108         
109         UsernameGeneratorParams params = gen.getParams();
110         params.setPrefix("pre-");
111         gen.setParams(params);
112         u = gen.generateUsername(username);
113         assertEquals(u, "pre-foo");
114
115         params.setPostfix("-post");
116         params.setPrefix(null);
117         gen.setParams(params);
118         u = gen.generateUsername(username);
119         assertEquals(u, "foo-post");
120         
121         params.setPrefix("pre-${RANDOM}-");
122         params.setPostfix(null);
123         params.setRandomPrefixLength(6);
124         gen.setParams(params);
125         u = gen.generateUsername(username);
126         assertEquals(u.length(), 14);
127         assertTrue(u.startsWith("pre-"));
128         assertTrue(u.endsWith("-foo"));
129
130         params.setPostfix("-${RANDOM}-post");
131         params.setPrefix(null);
132         params.setRandomPrefixLength(5);
133         gen.setParams(params);
134         u = gen.generateUsername(username);
135         assertEquals(u.length(), 14);
136         assertTrue(u.startsWith("foo-"));
137         assertTrue(u.endsWith("-post"));
138     }
139
140 }
141
Popular Tags