Skip to content
Snippets Groups Projects

Update PartitioningTests.java

Open Xi Chen requested to merge XCC2/swen90006-a1-2019:patch-9 into master
1 file
+ 254
65
Compare changes
  • Side-by-side
  • Inline
package swen90006.passbook;
import java.util.List;
import java.util.ArrayList;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import org.junit.*;
import static org.junit.Assert.*;
public class PartitioningTests
{
protected PassBook pb;
//Any method annotated with "@Before" will be executed before each test,
//allowing the tester to set up some shared resources.
@Before public void setUp()
{
pb = new PassBook();
}
//Any method annotated with "@After" will be executed after each test,
//allowing the tester to release any shared resources used in the setup.
@After public void tearDown()
{
}
//Any method annotation with "@Test" is executed as a test.
@Test public void aTest()
{
//the assertEquals method used to check whether two values are
//equal, using the equals method
final int expected = 2;
final int actual = 1 + 1;
assertEquals(expected, actual);
}
@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);
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class PartitioningTests {
protected PassBook pb;
public int sessionID;
// Any method annotated with "@Before" will be executed before each test,
// allowing the tester to set up some shared resources.
@Before
public void setUp() throws DuplicateUserException, WeakPassphraseException, NoSuchUserException,
AlreadyLoggedInException, IncorrectPassphraseException {
pb = new PassBook();
pb.addUser("123abc", "1234abCD");
pb.addUser("123ABC", "1234abXY");
sessionID = pb.loginUser("123ABC", "1234abXY");
// System.out.println(sessionID);
}
// Any method annotated with "@After" will be executed after each test,
// allowing the tester to release any shared resources used in the setup.
@After
public void tearDown() {
}
// Any method annotation with "@Test" is executed as a test.
/*
* Test cases for addUser
*/
@Test
public void testDuplicatedAdd() throws DuplicateUserException, WeakPassphraseException {
// EC1 addUser
assertThrows(DuplicateUserException.class, () -> pb.addUser("123abc", "1234aA234"));
}
@Test
public void testNotEnoughLength() throws DuplicateUserException, WeakPassphraseException {
// EC2 addUser
assertThrows(WeakPassphraseException.class, () -> pb.addUser("456abc", "1234aA"));
}
@Test
public void testNoLowerClass() throws DuplicateUserException, WeakPassphraseException {
// EC3 addUser
assertThrows(WeakPassphraseException.class, () -> pb.addUser("456abc", "12345678"));
}
@Test
public void testNoUpperClass() throws DuplicateUserException, WeakPassphraseException {
// EC4 addUser
assertThrows(WeakPassphraseException.class, () -> pb.addUser("456abc", "1234567a"));
}
@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