001package edu.pdx.cs.joy.family; 002 003import org.junit.jupiter.api.Test; 004 005import java.util.Date; 006 007import static org.junit.jupiter.api.Assertions.*; 008 009/** 010 * This class tests the functionality of the <code>Person</code> class. 011 */ 012public class PersonTest { 013 014 /** 015 * Create a person with an invalid id. Make sure it throws an 016 * FamilyTreeException. 017 */ 018 @Test 019 public void testInvalidPersonId() { 020 try { 021 new Person(-7, Person.FEMALE); 022 fail("Should have thrown an FamilyTreeException"); 023 024 } catch (FamilyTreeException ex) { 025 // pass... 026 } 027 } 028 029 /** 030 * Create a person with an invalid gender. Make sure it throws an 031 * FamilyTreeException. 032 */ 033 @Test 034 public void testInvalidGender() { 035 try { 036 new Person(1, Person.Gender.UNKNOWN); 037 fail("Should have thrown an FamilyTreeException"); 038 039 } catch (FamilyTreeException ex) { 040 // pass... 041 } 042 } 043 044 @Test 045 public void testGetId() { 046 int id = 4; 047 Person p = new Person(id, Person.MALE); 048 assertEquals(id, p.getId()); 049 } 050 051 @Test 052 public void testGetGenderMale() { 053 Person.Gender gender = Person.MALE; 054 Person p = new Person(4, gender); 055 assertEquals(gender, p.getGender()); 056 } 057 058 @Test 059 public void testGetGenderFemale() { 060 Person.Gender gender = Person.FEMALE; 061 Person p = new Person(4, gender); 062 assertEquals(gender, p.getGender()); 063 } 064 065 @Test 066 public void testFirstName() { 067 String name = "Bob"; 068 Person p = new Person(4, Person.MALE); 069 p.setFirstName(name); 070 assertEquals(name, p.getFirstName()); 071 } 072 073 @Test 074 public void testLastName() { 075 String name = "Bob"; 076 Person p = new Person(4, Person.MALE); 077 p.setLastName(name); 078 assertEquals(name, p.getLastName()); 079 } 080 081 @Test 082 public void testMiddleName() { 083 String name = "Bob"; 084 Person p = new Person(4, Person.MALE); 085 p.setMiddleName(name); 086 assertEquals(name, p.getMiddleName()); 087 } 088 089 @Test 090 public void testFullName() { 091 String first = "First"; 092 String middle = "Middle"; 093 String last = "Last"; 094 095 Person p = new Person(3, Person.FEMALE); 096 p.setFirstName(first); 097 p.setMiddleName(middle); 098 p.setLastName(last); 099 100 String full = first + " " + middle + " " + last; 101 assertEquals(full, p.getFullName()); 102 } 103 104 @Test 105 public void testFather() { 106 int id = 2; 107 Person child = new Person(1, Person.FEMALE); 108 Person father = new Person(id, Person.MALE); 109 child.setFather(father); 110 assertEquals(father, child.getFather()); 111 assertEquals(id, child.getFatherId()); 112 } 113 114 @Test 115 public void testFemaleFather() { 116 Person child = new Person(1, Person.FEMALE); 117 Person father = new Person(2, Person.FEMALE); 118 try { 119 child.setFather(father); 120 fail("Should have thrown an FamilyTreeException"); 121 122 } catch (FamilyTreeException ex) { 123 // pass... 124 } 125 } 126 127 @Test 128 public void testGetFatherIdUnknownFather() { 129 Person child = new Person(1, Person.FEMALE); 130 assertEquals(Person.UNKNOWN, child.getFatherId()); 131 } 132 133 @Test 134 public void testMother() { 135 int id = 2; 136 Person child = new Person(1, Person.FEMALE); 137 Person mother = new Person(id, Person.FEMALE); 138 child.setMother(mother); 139 assertEquals(mother, child.getMother()); 140 assertEquals(id, child.getMotherId()); 141 } 142 143 @Test 144 public void testMaleMother() { 145 Person child = new Person(1, Person.FEMALE); 146 Person mother = new Person(2, Person.MALE); 147 try { 148 child.setMother(mother); 149 fail("Should have thrown an FamilyTreeException"); 150 151 } catch (FamilyTreeException ex) { 152 // pass... 153 } 154 } 155 156 @Test 157 public void testGetMotherIdUnknownMother() { 158 Person child = new Person(1, Person.FEMALE); 159 assertEquals(Person.UNKNOWN, child.getMotherId()); 160 } 161 162 @Test 163 public void testDateOfBirth() { 164 Date dob = new Date(123456L); 165 Person p = new Person(1, Person.FEMALE); 166 p.setDateOfBirth(dob); 167 assertEquals(dob, p.getDateOfBirth()); 168 } 169 170 @Test 171 public void testDateOfDeath() { 172 Date dod = new Date(123456L); 173 Person p = new Person(1, Person.FEMALE); 174 p.setDateOfDeath(dod); 175 assertEquals(dod, p.getDateOfDeath()); 176 } 177 178 @Test 179 public void testDateOfDeathBeforeDateOfBirth() { 180 Date dob = new Date(120000L); 181 Date dod = new Date(100000L); 182 Person p = new Person(1, Person.MALE); 183 p.setDateOfBirth(dob); 184 try { 185 p.setDateOfDeath(dod); 186 fail("Should have thrown FamilyTreeException"); 187 188 } catch (FamilyTreeException ex) { 189 // pass... 190 } 191 } 192 193 @Test 194 public void testAddMarriage() { 195 Person husband = new Person(1, Person.MALE); 196 Person wife = new Person(2, Person.FEMALE); 197 Marriage m = new Marriage(husband, wife); 198 husband.addMarriage(m); 199 assertTrue(husband.getMarriages().contains(m)); 200 } 201 202 @Test 203 public void testMaleNotInMarriage() { 204 Person husband = new Person(1, Person.MALE); 205 Person wife = new Person(2, Person.FEMALE); 206 Marriage m = new Marriage(husband, wife); 207 208 Person p = new Person(3, Person.MALE); 209 try { 210 p.addMarriage(m); 211 fail("Should have thrown an FamilyTreeException"); 212 213 } catch (FamilyTreeException ex) { 214 // pass... 215 } 216 } 217 218 @Test 219 public void testFemaleNotInMarriage() { 220 Person husband = new Person(1, Person.MALE); 221 Person wife = new Person(2, Person.FEMALE); 222 Marriage m = new Marriage(husband, wife); 223 224 Person p = new Person(3, Person.FEMALE); 225 try { 226 p.addMarriage(m); 227 fail("Should have thrown an FamilyTreeException"); 228 229 } catch (FamilyTreeException ex) { 230 // pass... 231 } 232 } 233 234 @Test 235 public void testEquals() { 236 Person p1 = new Person(1, Person.MALE); 237 Person p2 = new Person(1, Person.MALE); 238 assertEquals(p1, p2); 239 } 240 241 @Test 242 public void testNotEquals() { 243 Person p1 = new Person(1, Person.MALE); 244 Person p2 = new Person(2, Person.MALE); 245 assertTrue(!p1.equals(p2)); 246 } 247 248}