Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package pl.polsl.java.model.test;
  7.  
  8. import java.util.Scanner;
  9. import org.junit.*;
  10. import java.lang.reflect.Field;
  11. import pl.polsl.java.model.Model;
  12. import static org.junit.Assert.*;
  13.  
  14. /**
  15.  * ModelTest class is used to test Model class.
  16.  * @author Wojciech Karbownik
  17.  * @version 2.0
  18.  */
  19. public class ModelTest {
  20.    
  21.     Model model;
  22.    
  23.     @Before
  24.     public void setUp() {
  25.         model = new Model();
  26.         model.setPassword("bomba");
  27.     }
  28.    
  29.     @After
  30.     public void tearDown() {
  31.         model = null;
  32.     }
  33.  
  34.  
  35.     /**
  36.      * Test of equals method, of class Model.
  37.      * Test is successful if password equals to hashedPassword.
  38.      */
  39.     @Test
  40.     public void testEquals() {
  41.         model.setHashedPassword(model.getPassword()); // hashedPassword == "bomba"
  42.         assertTrue(model.equals());
  43.     }
  44.    
  45.     /**
  46.      * Test of checkIfNoMoreChances method, of class Model.
  47.      * Test is successful if the length of lMisses string equals 10.
  48.      * @throws Exception
  49.      */
  50.     @Test
  51.     public void testCheckIfNoMoreChances() throws Exception {
  52.         Field fieldLMisses = Model.class.getDeclaredField("lMisses");
  53.         fieldLMisses.setAccessible(true);
  54.         fieldLMisses.set(model, "abcdefghij"); // lMisses.length() is 10 now
  55.         assertTrue(model.checkIfNoMoreChances());
  56.     }
  57.  
  58.  
  59.     /**
  60.      * Test of passHasher method, of class Model.
  61.      * Test is successful if hashing method works correctly so the result is
  62.      * as expected.
  63.      */
  64.     @Test
  65.     public void testPassHasher() {
  66.         String expResult = "*****";
  67.         model.passHasher(model.getPassword());
  68.         assertEquals(expResult, model.getHashedPassword());
  69.     }
  70.  
  71.     /**
  72.      * Test of isValidWord method, of class Model.
  73.      *
  74.      */
  75.     @Test
  76.     public void testIsValidWord() throws Exception {
  77.        
  78.         boolean exception = false;
  79.         boolean result = false;
  80.         try{
  81.             result = model.isValidWord(model.getPassword());
  82.         } catch(Exception e){
  83.             exception = true;
  84.         }
  85.        
  86.         assertFalse("Exception hasn't occured during word's validation.", exception);
  87.         assertTrue("isValidWord tested positively with correct data.", result);
  88.        
  89.         model.setPassword("1bomba"); //password is now incorrect (contains a digit)
  90.         result = false;
  91.         try{
  92.             result = model.isValidWord(model.getPassword());
  93.         } catch(Exception e){
  94.             exception = true;
  95.         }
  96.         assertTrue("Exception has occured during validation of an incorrect word.", exception);
  97.         assertFalse("isValidWord tested positively with incorrect data.", result);
  98.     }
  99.  
  100.     /**
  101.      * Test of readLetterFromUser method, of class Model.
  102.      * Test is positive if method returns validated charInput that is equal to expected result.
  103.      */
  104.     @Test
  105.     public void testReadLetterFromUser() {
  106.         char charInput = ' ';
  107.         Scanner charReader = new Scanner("q"); //passed letter (simulate taking a letter from user)
  108.         char expResult = 'q'; //expected letter
  109.         assertEquals(expResult, model.readLetterFromUser(charInput, charReader)); //readLetterFromUser should correctly return validated charInput
  110.     }
  111.  
  112.     /**
  113.      * Test of checkWhereToAddLetter method, of class Model.
  114.      * Test assumes that the input letter is already validated by internal method and it is correct.
  115.      */
  116.     @Test
  117.     public void testCheckWhereToAddLetter() throws Exception {
  118.         char charInput = 'b';
  119.         String result;
  120.         String expResult;
  121.        
  122.         model.checkWhereToAddLetter(charInput); // password "bomba" contains letter "b"
  123.         Field fieldLHits = Model.class.getDeclaredField("lHits");
  124.         fieldLHits.setAccessible(true);
  125.         result = fieldLHits.get(model).toString();
  126.         expResult = new StringBuilder().append(charInput).toString();
  127.         assertEquals(expResult, result);
  128.        
  129.         charInput = 'y';
  130.         model.checkWhereToAddLetter(charInput); // password "bomba" does not contain letter "y"
  131.         Field fieldLMisses = Model.class.getDeclaredField("lMisses");
  132.         fieldLMisses.setAccessible(true);
  133.         result = fieldLMisses.get(model).toString();
  134.         expResult = new StringBuilder().append(charInput).toString();
  135.         assertEquals(expResult, result);
  136.     }    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement