Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pl.polsl.java.model.test;
- import java.util.Scanner;
- import org.junit.*;
- import java.lang.reflect.Field;
- import pl.polsl.java.model.Model;
- import static org.junit.Assert.*;
- /**
- * ModelTest class is used to test Model class.
- * @author Wojciech Karbownik
- * @version 2.0
- */
- public class ModelTest {
- Model model;
- @Before
- public void setUp() {
- model = new Model();
- model.setPassword("bomba");
- }
- @After
- public void tearDown() {
- model = null;
- }
- /**
- * Test of equals method, of class Model.
- * Test is successful if password equals to hashedPassword.
- */
- @Test
- public void testEquals() {
- model.setHashedPassword(model.getPassword()); // hashedPassword == "bomba"
- assertTrue(model.equals());
- }
- /**
- * Test of checkIfNoMoreChances method, of class Model.
- * Test is successful if the length of lMisses string equals 10.
- * @throws Exception
- */
- @Test
- public void testCheckIfNoMoreChances() throws Exception {
- Field fieldLMisses = Model.class.getDeclaredField("lMisses");
- fieldLMisses.setAccessible(true);
- fieldLMisses.set(model, "abcdefghij"); // lMisses.length() is 10 now
- assertTrue(model.checkIfNoMoreChances());
- }
- /**
- * Test of passHasher method, of class Model.
- * Test is successful if hashing method works correctly so the result is
- * as expected.
- */
- @Test
- public void testPassHasher() {
- String expResult = "*****";
- model.passHasher(model.getPassword());
- assertEquals(expResult, model.getHashedPassword());
- }
- /**
- * Test of isValidWord method, of class Model.
- *
- */
- @Test
- public void testIsValidWord() throws Exception {
- boolean exception = false;
- boolean result = false;
- try{
- result = model.isValidWord(model.getPassword());
- } catch(Exception e){
- exception = true;
- }
- assertFalse("Exception hasn't occured during word's validation.", exception);
- assertTrue("isValidWord tested positively with correct data.", result);
- model.setPassword("1bomba"); //password is now incorrect (contains a digit)
- result = false;
- try{
- result = model.isValidWord(model.getPassword());
- } catch(Exception e){
- exception = true;
- }
- assertTrue("Exception has occured during validation of an incorrect word.", exception);
- assertFalse("isValidWord tested positively with incorrect data.", result);
- }
- /**
- * Test of readLetterFromUser method, of class Model.
- * Test is positive if method returns validated charInput that is equal to expected result.
- */
- @Test
- public void testReadLetterFromUser() {
- char charInput = ' ';
- Scanner charReader = new Scanner("q"); //passed letter (simulate taking a letter from user)
- char expResult = 'q'; //expected letter
- assertEquals(expResult, model.readLetterFromUser(charInput, charReader)); //readLetterFromUser should correctly return validated charInput
- }
- /**
- * Test of checkWhereToAddLetter method, of class Model.
- * Test assumes that the input letter is already validated by internal method and it is correct.
- */
- @Test
- public void testCheckWhereToAddLetter() throws Exception {
- char charInput = 'b';
- String result;
- String expResult;
- model.checkWhereToAddLetter(charInput); // password "bomba" contains letter "b"
- Field fieldLHits = Model.class.getDeclaredField("lHits");
- fieldLHits.setAccessible(true);
- result = fieldLHits.get(model).toString();
- expResult = new StringBuilder().append(charInput).toString();
- assertEquals(expResult, result);
- charInput = 'y';
- model.checkWhereToAddLetter(charInput); // password "bomba" does not contain letter "y"
- Field fieldLMisses = Model.class.getDeclaredField("lMisses");
- fieldLMisses.setAccessible(true);
- result = fieldLMisses.get(model).toString();
- expResult = new StringBuilder().append(charInput).toString();
- assertEquals(expResult, result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement