-
Notifications
You must be signed in to change notification settings - Fork 3
/
TestCode.java
367 lines (313 loc) · 9.87 KB
/
TestCode.java
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package finalproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestCode {
public TestCode()
{
}
// Checks the length of the username
// Used for Login page tests, Register page tests
public Boolean checkUsername(String username)
{
if (username.length() < 8 || username.length() > 20)
{
return false;
}
return true;
}
// Checks the length and character requirements for password
// Used for Login Page tests, Register page tests
public Boolean checkPassword(String password)
{
if (password.length() < 8 || password.length() > 20)
{
System.out.println("bad password1");
return false;
}
// make sure password has at least 1 uppercase letter and
// one special character using regex
Pattern regex = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,20}$");
Matcher matcher = regex.matcher(password);
while (matcher.find())
{
System.out.println("good password");
return true;
}
System.out.println("bad password2");
return false;
}
// Verify that user exists in the database
// Used for Login page tests to make sure that the user gives valid login info
// Used to check that Validate class validateUser() function returns correct result
// Used to check that LoginServlet validate() function returns correct result
public Boolean CheckValidateUser(String username, String password)
{
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Boolean found = true;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/StockAppDatabase?user=root&password=root&useSSL=false");
// Search for word1 substring in the fname column
st = conn.createStatement();
rs = st.executeQuery("Select * FROM Users where username='" + username + "' AND password='" + password + "'");
while (rs.next()) { // iterates from first to last row
found = true;
}
} catch (SQLException sqle) {
System.out.println("sqle: " + sqle.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println("cnfe: " + cnfe.getMessage());
} finally {
// close files and sockets here in the finally block
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
System.out.println("sqle closing streams " + sqle.getMessage());
}
}
Validation validationClass = new Validation();
Boolean vcResult = validationClass.validateUser(username, password);
if (vcResult == found)
return true;
return false;
}
// Used for Register page tests to make sure that a user with this username
// does not already exist in the database and that the password and
// confirmPassword are exact matches
// User should only be entered into the data base or allowed to proceed in
// creating an account when this returns true
// Use to check that RegisterServlet validate() function returns correct value
public Boolean checkRegisterInfo(String username, String password, String confirmPassword)
{
Boolean canRegister = true;
// See if someone with this username already exists in database
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/StockAppDatabase?user=root&password=root&useSSL=false");
// Search for word1 substring in the fname column
st = conn.createStatement();
rs = st.executeQuery("Select * FROM Users where username='" + username + "'");
while (rs.next()) { // iterates from first to last row
canRegister = false;
}
} catch (SQLException sqle) {
System.out.println("sqle: " + sqle.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println("cnfe: " + cnfe.getMessage());
} finally {
// close files and sockets here in the finally block
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
System.out.println("sqle closing streams " + sqle.getMessage());
}
}
Boolean goodUsername = checkUsername(username);
if (!goodUsername)
{
canRegister = false;
}
Boolean goodPassword = checkPassword(password);
if (!goodPassword)
{
canRegister = false;
}
if (!password.equals(confirmPassword))
{
canRegister = false;
}
return canRegister;
}
// Used on the Home page to see if new stocks can be added
public Boolean controlNumStocksOnGraph(int num)
{
Boolean canAddNewStock = false;
if (num >= 0 && num < 5)
{
canAddNewStock = true;
}
return canAddNewStock;
}
// Used on the home page to see if hte user is searching for a valid stock symbol
public Boolean IsValidStockQuery(String symbol)
{
Boolean validStockSymbol = false;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/StockAppDatabase?user=root&password=root&useSSL=false");
// Search for word1 substring in the fname column
st = conn.createStatement();
rs = st.executeQuery("Select * FROM Stocks where symbol='" + symbol + "'");
while (rs.next()) { // iterates from first to last row
validStockSymbol = true;
}
} catch (SQLException sqle) {
System.out.println("sqle: " + sqle.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println("cnfe: " + cnfe.getMessage());
} finally {
// close files and sockets here in the finally block
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
System.out.println("sqle closing streams " + sqle.getMessage());
}
}
return validStockSymbol;
}
// Checks the result of validateUsernameChange() on User Profile page
public Boolean verifyChangeUsername(String prevUsername, String newUsername)
{
Boolean canChangeUsername = true;
// See if someone with this username already exists in database
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/StockAppDatabase?user=root&password=root&useSSL=false");
// Search for word1 substring in the fname column
st = conn.createStatement();
rs = st.executeQuery("Select * FROM Users where username='" + newUsername + "'");
while (rs.next()) { // iterates from first to last row
canChangeUsername = false;
}
} catch (SQLException sqle) {
System.out.println("sqle: " + sqle.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println("cnfe: " + cnfe.getMessage());
} finally {
// close files and sockets here in the finally block
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
System.out.println("sqle closing streams " + sqle.getMessage());
}
}
Boolean goodUsername = checkUsername(newUsername);
if (!goodUsername)
{
canChangeUsername = false;
}
Validation vc = new Validation();
Boolean vcResult = vc.validateUsernameChange();
if (vcResult == canChangeUsername)
return true;
return false;
}
// Checks the result of validatePasswordChange() on User Profile page
public Boolean validateChangePassword(String newPassword, String confirmPassword)
{
Boolean canChangePassword = false;
Boolean goodPassword = checkPassword(newPassword);
if (!goodPassword)
{
canChangePassword = false;
}
if (!newPassword.equals(confirmPassword))
{
canChangePassword = false;
}
Validation vc = new Validation();
Boolean vcResult = vc.validateUsernameChange();
if (vc == canChangePassword)
return true;
return false;
}
public Boolean checkGetUsername(User user, String username)
{
return user.getUsername().equals(username);
}
public Boolean checkSetUsername(User user, String username)
{
user.setUsername(username);
return username.equals(user.getUsername());
}
public Boolean checkSetPassword(User user, String password)
{
return user.getPassword().equals(password);
}
public Boolean checkSetPassword(User user, String password)
{
user.setPassword(password);
return username.equals(user.getPassword());
}
public Boolean checkCalcProfit(int numberOfStocks, double priceOfStock)
{
double calculatedProfit = 0.0;
calculatedProfit = numberOfStocks * priceOfStock;
Calculation cc = new Calculation();
double ccResult = cc.calculateProfit(numberOfStocks, priceOfStock);
return calculatedProfit == ccResult;
}
public Boolean checkSetProfit(Bank bank, double newProfit)
{
bank.setProfit(newProfit);
return bank.getProfit() == newProfit;
}
public Boolean checkSetBalance(Bank bank, double newBalance)
{
bank.setBalance(newBalance);
return bank.getBalance() == newBalance;
}
public Boolean checkBuyResult(User user, String symbol, int numberOfStocks, double priceOfStock)
{
double prevBal = user.getBalance();
double prevProfit = user.getProfit();
user.setBalance(user.getBalance() - (numberOfStocks * priceOfStock));
user.setProfit(user.getProfit() + (numberOfStocks * priceOfStock));
if (user.getBalance() == (prevBal - (numberOfStocks * priceOfStock) && user.getProfit() == (prevProfit + (numberOfStocks * priceOfStock)))
return true;
else
return false;
// confirm database
}
public static void main(String [] args)
{
}
}