-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPasswordCheckerTest.java
More file actions
211 lines (194 loc) · 6.62 KB
/
PasswordCheckerTest.java
File metadata and controls
211 lines (194 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//package _solution;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Test the methods of PasswordChecker
* @author Professor Kartchner
*
*/
public class PasswordCheckerTest {
ArrayList<String> passwords;
String password1, password2;
@Before
public void setUp() throws Exception {
String[] p = {"334455BB", "Im2#cool4U", "george2ZZZ", "4sal#", "bertha22#", "4wardMarch#",
"august30", "abcdef", "Applesxx#", "aa11b", "pilotProject", "myPassword",
"myPassword2"};
passwords = new ArrayList<String>();
passwords.addAll(Arrays.asList(p)); // puts strings into the ArrayList
/* ************* STUDENT ***************
Create another Arraylist of String to
use for the testValidPasswordsSTUDENT test
*/
}
@After
public void tearDown() throws Exception {
passwords = null;
}
/**
* Test if the password is less than 8 characters long.
* This test should throw a LengthException for second case.
*/
@Test
public void testIsValidPasswordTooShort()
{
try{
assertTrue(PasswordCheckerUtility.isValidPassword("abcABC12#"));
PasswordCheckerUtility.isValidPassword("ab12#");
assertTrue("Did not throw lengthException",false);
}
catch(LengthException e)
{
assertTrue("Successfully threw a lengthExcepetion",true);
}
catch(Exception e)
{
assertTrue("Threw some other exception besides lengthException",false);
}
}
/**
* Test if the password has at least one uppercase alpha character
* This test should throw a NoUpperAlphaException for second case
*/
@Test
public void testIsValidPasswordNoUpperAlpha()
{
try{
assertTrue(PasswordCheckerUtility.isValidPassword("1234567aA#"));
PasswordCheckerUtility.isValidPassword("1234567a#");
assertTrue("Did not throw NoUpperAlphaException",false);
}
catch(NoUpperAlphaException e)
{
assertTrue("Successfully threw a NoUpperAlphaExcepetion",true);
}
catch(Exception e)
{
assertTrue("Threw some other exception besides NoUpperAlphaException",false);
}
}
/**
* Test if the password has at least one lowercase alpha character
* This test should throw a NoLowerAlphaException for second case
*/
@Test
public void testIsValidPasswordNoLowerAlpha()
{
try{
assertTrue(PasswordCheckerUtility.isValidPassword("1234567Aa#"));
PasswordCheckerUtility.isValidPassword("1234567A#");
assertTrue("Did not throw NoLowerAlphaException",false);
}
catch(NoLowerAlphaException e)
{
assertTrue("Successfully threw a NoLowerAlphaExcepetion",true);
}
catch(Exception e)
{
assertTrue("Threw some other exception besides NoLowerAlphaException",false);
}
}
/**
* Test if the password has at least one special symbol
* This test should throw a NoSpecialSymbolException for second case
*/
@Test
public void testIsValidPasswordNoSpecialSymbol()
{
try{
assertTrue(PasswordCheckerUtility.isValidPassword("1234567Aa#"));
PasswordCheckerUtility.isValidPassword("1234567Aa");
System.out.println("Did not throw a NoSpecialSymbolException");
assertTrue("Did not throw NoSpecialSymbolException",false);
}
catch(NoSpecialCharacterException e)
{
System.out.println("Successfully threw a NoSpecialSymbolException");
assertTrue("Successfully threw a NoSpecialSymbolException",true);
}
catch(Exception e)
{
System.out.println("Threw some other exception besides NoSpecialSymbolException");
assertTrue("Threw some other exception besides NoSpecialSymbolException",false);
}
}
/**
* Test if the password has more than 2 of the same character in sequence
* This test should throw a InvalidSequenceException for second case
*/
@Test
public void testIsWeakPassword()
{
try{
assertEquals(true,PasswordCheckerUtility.isValidPassword("1234aaAA#"));
boolean weakPwd = PasswordCheckerUtility.isWeakPassword("1234aaA#");
assertTrue(weakPwd);
}
catch(Exception e)
{
System.out.println(e.getMessage());
assertTrue("Threw some incorrect exception",false);
}
}
/**
* Test if the password has more than 2 of the same character in sequence
* This test should throw a InvalidSequenceException for second case
*/
@Test
public void testIsValidPasswordInvalidSequence()
{
try{
assertEquals(true,PasswordCheckerUtility.isValidPassword("1234aaAA#"));
PasswordCheckerUtility.isValidPassword("1234aAAA#");
assertTrue("Did not throw an InvalidSequenceException",false);
}
catch(InvalidSequenceException e)
{
assertTrue("Successfully threw an InvalidSequenceExcepetion",true);
}
catch(Exception e)
{
System.out.println(e.getMessage());
assertTrue("Threw some other exception besides an InvalidSequenceException",false);
}
}
/**
* Test the getInvalidPasswords method
* Check the results of the ArrayList of Strings returned by the validPasswords method
*/
@Test
public void testGetInvalidPasswords() {
ArrayList<String> results;
results = PasswordCheckerUtility.isInvalidPasswords(passwords);
Scanner scan = new Scanner(results.get(0)); //
assertEquals(scan.next(), "334455BB");
String nextResults = scan.nextLine().toLowerCase();
assertTrue(nextResults.contains("lowercase"));
//assertEquals(scan.nextLine(), " The password must contain at least one lowercase alphabetic character.");
scan = new Scanner(results.get(1)); //
assertEquals(scan.next(), "george2ZZZ");
nextResults = scan.nextLine().toLowerCase();
assertTrue(nextResults.contains("special"));
//assertEquals(scan.nextLine(), " The password cannot contain more than two of the same character in sequence.");
scan = new Scanner(results.get(3)); //
assertEquals(scan.next(), "bertha22#");
nextResults = scan.nextLine().toLowerCase();
assertTrue(nextResults.contains("uppercase"));
//assertEquals(scan.nextLine(), " The password must contain at least one uppercase alphabetic character.");
scan = new Scanner(results.get(5)); //
assertEquals(scan.next(), "abcdef");
nextResults = scan.nextLine().toLowerCase();
assertTrue(nextResults.contains("uppercase") || nextResults.contains("digit"));
//assertEquals(scan.nextLine(), " The password must contain at least one digit.");
scan = new Scanner(results.get(6)); //a
assertEquals(scan.next(), "Applesxx#");
nextResults = scan.nextLine().toLowerCase();
assertTrue(nextResults.contains("digit"));
//assertEquals(scan.nextLine(), " The password must contain at least one digit.");
}
}