Skip to content
Snippets Groups Projects

Update PartitioningTests.java

1 file
+ 254
65
Compare changes
  • Side-by-side
  • Inline
package swen90006.passbook;
package swen90006.passbook;
import java.util.List;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import static org.junit.Assert.assertFalse;
import java.nio.charset.Charset;
import static org.junit.Assert.assertTrue;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.*;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
public class PartitioningTests
import org.junit.Test;
{
protected PassBook pb;
public class PartitioningTests {
protected PassBook pb;
//Any method annotated with "@Before" will be executed before each test,
public int sessionID;
//allowing the tester to set up some shared resources.
@Before public void setUp()
// Any method annotated with "@Before" will be executed before each test,
{
// allowing the tester to set up some shared resources.
pb = new PassBook();
@Before
}
public void setUp() throws DuplicateUserException, WeakPassphraseException, NoSuchUserException,
AlreadyLoggedInException, IncorrectPassphraseException {
//Any method annotated with "@After" will be executed after each test,
pb = new PassBook();
//allowing the tester to release any shared resources used in the setup.
pb.addUser("123abc", "1234abCD");
@After public void tearDown()
pb.addUser("123ABC", "1234abXY");
{
sessionID = pb.loginUser("123ABC", "1234abXY");
}
// System.out.println(sessionID);
}
//Any method annotation with "@Test" is executed as a test.
@Test public void aTest()
// Any method annotated with "@After" will be executed after each test,
{
// allowing the tester to release any shared resources used in the setup.
//the assertEquals method used to check whether two values are
@After
//equal, using the equals method
public void tearDown() {
final int expected = 2;
}
final int actual = 1 + 1;
assertEquals(expected, actual);
// Any method annotation with "@Test" is executed as a test.
}
/*
@Test public void anotherTest()
* Test cases for addUser
throws DuplicateUserException, WeakPassphraseException
*/
{
pb.addUser("passbookUsername", "properPassphrase1");
@Test
public void testDuplicatedAdd() throws DuplicateUserException, WeakPassphraseException {
//the assertTrue method is used to check whether something holds.
// EC1 addUser
assertTrue(pb.isUser("passbookUsername"));
assertThrows(DuplicateUserException.class, () -> pb.addUser("123abc", "1234aA234"));
assertFalse(pb.isUser("nonUser"));
}
}
@Test
//To test an exception, specify the expected exception after the @Test
public void testNotEnoughLength() throws DuplicateUserException, WeakPassphraseException {
@Test(expected = java.io.IOException.class)
// EC2 addUser
public void anExceptionTest()
assertThrows(WeakPassphraseException.class, () -> pb.addUser("456abc", "1234aA"));
throws Throwable
}
{
throw new java.io.IOException();
@Test
}
public void testNoLowerClass() throws DuplicateUserException, WeakPassphraseException {
// EC3 addUser
//This test should fail.
assertThrows(WeakPassphraseException.class, () -> pb.addUser("456abc", "12345678"));
//To provide additional feedback when a test fails, an error message
}
//can be included
@Test public void aFailedTest()
@Test
{
public void testNoUpperClass() throws DuplicateUserException, WeakPassphraseException {
//include a message for better feedback
// EC4 addUser
final int expected = 2;
assertThrows(WeakPassphraseException.class, () -> pb.addUser("456abc", "1234567a"));
final int actual = 1 + 2;
}
assertEquals("Some failure message", expected, actual);
}
@Test
 
public void testSuccessAdd() throws DuplicateUserException, WeakPassphraseException {
 
// EC5 addUser
 
pb.addUser("456abc", "123456aA");
 
assertTrue(pb.isUser("456abc"));
 
// assertFalse(pb.isUser("456abc"));
 
}
 
 
@Test
 
public void testNoNumber() throws DuplicateUserException, WeakPassphraseException {
 
// EC6 addUser
 
assertThrows(WeakPassphraseException.class, () -> pb.addUser("456abc", "aaaaAAAA"));
 
}
 
 
/*
 
* Test cases for loginUser
 
*/
 
@Test
 
public void testNoSuchUser() throws NoSuchUserException, AlreadyLoggedInException, IncorrectPassphraseException {
 
// EC 1
 
assertThrows(NoSuchUserException.class, () -> pb.loginUser("abc123", "12345aAaa"));
 
}
 
 
@Test
 
public void testAlreadyLoggedIn()
 
throws NoSuchUserException, AlreadyLoggedInException, IncorrectPassphraseException {
 
// EC 2
 
assertThrows(AlreadyLoggedInException.class, () -> pb.loginUser("123abc", "11234abCD"));
 
}
 
 
@Test
 
public void testIncorrectPassphrase()
 
throws NoSuchUserException, AlreadyLoggedInException, IncorrectPassphraseException {
 
// EC 3
 
assertThrows(IncorrectPassphraseException.class, () -> pb.loginUser("123ABC", "1123abcABC"));
 
}
 
 
@Test
 
public void testSuccessLogin() throws DuplicateUserException, WeakPassphraseException, NoSuchUserException,
 
AlreadyLoggedInException, IncorrectPassphraseException {
 
// EC 4 log in successfully
 
sessionID = pb.loginUser("123abc", "1234abCD");
 
assertThrows(AlreadyLoggedInException.class, () -> pb.loginUser("123abc", "1234abCD"));
 
}
 
 
/*
 
* Test cases for updateDetails
 
*/
 
 
@Test
 
public void testSessionIDExists() throws MalformedURLException {
 
// EC 3
 
URL url = new URL("http://google.com");
 
assertThrows(InvalidSessionIDException.class, () -> pb.updateDetails(1122966, url, null, null));
 
}
 
 
@Test
 
public void testMaledFormedURL() throws MalformedURLException, DuplicateUserException, WeakPassphraseException,
 
NoSuchUserException, AlreadyLoggedInException, IncorrectPassphraseException {
 
// EC 4
 
URL url = new URL("ftp://abc");
 
assertThrows(MalformedURLException.class, () -> pb.updateDetails(sessionID, url, null, null));
 
}
 
 
@Test
 
public void testPutDetails() throws MalformedURLException, InvalidSessionIDException, NoSuchURLException {
 
// EC 5
 
URL url = new URL("https://google.com");
 
pb.updateDetails(sessionID, url, "Xi", "1234");
 
Pair<String, String> expected = new Pair<String, String>("Xi", "1234");
 
// Pair<String, String> actual = pb.retrieveDetails(sessionID, url);
 
assertTrue(pb.retrieveDetails(sessionID, url).equals(expected));
 
}
 
 
@Test
 
public void testRemoveDetails() throws MalformedURLException, InvalidSessionIDException, NoSuchURLException {
 
// EC 6
 
URL url = new URL("https://students.com");
 
pb.updateDetails(sessionID, url, null, null);
 
assertThrows(NoSuchURLException.class, () -> pb.retrieveDetails(sessionID, url));
 
}
 
 
@Test
 
public void testPutDetails_http() throws MalformedURLException, InvalidSessionIDException, NoSuchURLException {
 
// EC 7
 
URL url = new URL("http://teachers.com");
 
pb.updateDetails(sessionID, url, "Xi", "1234");
 
Pair<String, String> expected = new Pair<String, String>("Xi", "1234");
 
assertTrue(pb.retrieveDetails(sessionID, url).equals(expected));
 
}
 
 
@Test
 
public void testRemoveDetails_http() throws MalformedURLException, InvalidSessionIDException, NoSuchURLException {
 
// EC 8
 
URL url = new URL("http://mymusic.com");
 
pb.updateDetails(sessionID, url, null, null);
 
assertThrows(NoSuchURLException.class, () -> pb.retrieveDetails(sessionID, url));
 
}
 
 
/*
 
* Test cases for retrieveDetails
 
*/
 
@Test
 
public void testInvalidSessionID() throws MalformedURLException {
 
// EC 3
 
URL url = new URL("https://google.com");
 
assertThrows(InvalidSessionIDException.class, () -> pb.retrieveDetails(sessionID, url));
 
}
 
 
@Test
 
public void testMaleformedURL() throws MalformedURLException {
 
// EC 4
 
URL url = new URL("ftp://google.com");
 
assertThrows(MalformedURLException.class, () -> pb.retrieveDetails(sessionID, url));
 
}
 
 
@Test
 
public void testNoSuchURL() throws MalformedURLException {
 
// EC 5
 
URL url = new URL("https://google.com");
 
assertThrows(NoSuchURLException.class, () -> pb.retrieveDetails(sessionID, url));
 
}
 
 
@Test
 
public void testNoSuchIncorrectURL() throws MalformedURLException, InvalidSessionIDException {
 
// EC 6
 
URL original_url = new URL("https://google.com");
 
pb.updateDetails(sessionID, original_url, "Xi", "1234");
 
URL new_url = new URL("https://students.com");
 
assertThrows(NoSuchURLException.class, () -> pb.retrieveDetails(sessionID, new_url));
 
}
 
 
@Test
 
public void testNoSuchURL_http() throws MalformedURLException {
 
// EC 7
 
URL url = new URL("http://myvideo.com");
 
assertThrows(NoSuchURLException.class, () -> pb.retrieveDetails(sessionID, url));
 
}
 
 
@Test
 
public void testNoSuchIncorrectURL_http() throws MalformedURLException, InvalidSessionIDException {
 
// EC 8
 
URL original_url = new URL("http://teachers.com");
 
pb.updateDetails(sessionID, original_url, "Xi", "1234");
 
URL new_url = new URL("https://students.com");
 
assertThrows(NoSuchURLException.class, () -> pb.retrieveDetails(sessionID, new_url));
 
}
 
 
@Test
 
public void testRetrieveSuccess() throws MalformedURLException, InvalidSessionIDException, NoSuchURLException {
 
// EC 9
 
URL url = new URL("https://studentss.com");
 
pb.updateDetails(sessionID, url, "Xi", "1234");
 
Pair<String, String> expected = new Pair<String, String>("Xi", "1234");
 
assertTrue(pb.retrieveDetails(sessionID, url).equals(expected));
 
}
 
 
@Test
 
public void testRetrieveSuccess_http() throws MalformedURLException, InvalidSessionIDException, NoSuchURLException {
 
// EC 9
 
URL url = new URL("http://teachers.com");
 
pb.updateDetails(sessionID, url, "Xi", "1234");
 
Pair<String, String> expected = new Pair<String, String>("Xi", "1234");
 
assertTrue(pb.retrieveDetails(sessionID, url).equals(expected));
 
}
 
 
@Test
 
public void anotherTest() throws DuplicateUserException, WeakPassphraseException {
 
pb.addUser("passbookUsername", "properPassphrase1");
 
// the assertTrue method is used to check whether something holds.
 
assertTrue(pb.isUser("passbookUsername"));
 
assertFalse(pb.isUser("nonUser"));
 
}
 
 
// To test an exception, specify the expected exception after the @Test
 
@Test(expected = java.io.IOException.class)
 
public void anExceptionTest() throws Throwable {
 
throw new java.io.IOException();
 
}
 
 
// This test should fail.
 
// To provide additional feedback when a test fails, an error message
 
// can be included
 
@Test
 
public void aFailedTest() {
 
// include a message for better feedback
 
final int expected = 2;
 
final int actual = 1 + 2;
 
assertEquals("Some failure message", expected, actual);
 
}
}
}
Loading