Skip to content

Commit 8087bf8

Browse files
committed
Made changes to UserDatabase. Added some Tests, need to resolve some issues
1 parent 8943edd commit 8087bf8

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/main/java/UserDatabase.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import java.io.PrintWriter;
44
import java.util.Scanner;
55
public class UserDatabase implements UserExists, UserCreator{
6+
File accounts;
7+
public UserDatabase(File accounts){
8+
this.accounts = accounts;
9+
}
610
@Override
711
public boolean UserExists(String username, String email) {
8-
File accounts = new File("UserAccounts.csv");
9-
try(Scanner x = new Scanner(accounts)){
12+
try(Scanner x = new Scanner(this.accounts)){
1013
x.useDelimiter("\n");
1114
while(x.hasNext()) {
1215
String[] y = x.next().split(",");
@@ -25,8 +28,7 @@ public boolean UserExists(String username, String email) {
2528
//
2629
@Override
2730
public void createUser(String username, String password, String email) {
28-
File accounts = new File("UserAccounts.csv");
29-
try(PrintWriter out = new PrintWriter(accounts)){
31+
try(PrintWriter out = new PrintWriter(this.accounts)){
3032
out.println(username + "," + password + "," + email + "," + "no" + "," + "offline");
3133
out.flush();
3234
}catch (FileNotFoundException e){

src/test/java/TestUserDatabase.csv

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
import java.io.File;
5+
6+
public class UserDatabaseTest {
7+
@Test
8+
public void addingFilesRightEmailAndUser(){
9+
File accounts = new File("TestUserDatabase.csv");
10+
UserDatabase accountDatabase = new UserDatabase(accounts);
11+
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]");
12+
Assertions.assertTrue(accountDatabase.UserExists("MadhavGopakumar", "[email protected]"));
13+
}
14+
@Test
15+
public void addingMultipleFiles(){
16+
File accounts = new File("TestUserDatabase.csv");
17+
UserDatabase accountDatabase = new UserDatabase(accounts);
18+
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]");
19+
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "[email protected]"));
20+
}
21+
@Test
22+
public void rightEmailWrongUser(){
23+
File accounts = new File("TestUserDatabase.csv");
24+
UserDatabase accountDatabase = new UserDatabase(accounts);
25+
Assertions.assertTrue(accountDatabase.UserExists("MadG", "[email protected]"));
26+
}
27+
@Test
28+
public void rightUserWrongEmail(){
29+
File accounts = new File("TestUserDatabase.csv");
30+
UserDatabase accountDatabase = new UserDatabase(accounts);
31+
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "ma"));
32+
}
33+
}

0 commit comments

Comments
 (0)