Skip to content

Instantly share code, notes, and snippets.

@khellan
Created October 5, 2016 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khellan/6d34eacb25cb3a30eb3e7568ff9d9e61 to your computer and use it in GitHub Desktop.
Save khellan/6d34eacb25cb3a30eb3e7568ff9d9e61 to your computer and use it in GitHub Desktop.
Simple enough
ackage no.companybook.extraction.tables;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.*;
public class PersonTest {
class AccessException extends Exception {
public AccessException(String message) { super(message); }
}
class Person {
private Integer GENDER = 1;
private Integer NAME = 2;
private Set<Integer> fields = new HashSet<>();
private String firstName;
private boolean isMale;
private String lastName;
private boolean lazy;
private boolean readOnly = true;
public Person(String firstName, String lastName, boolean isMale) {
fields.add(GENDER);
fields.add(NAME);
this.firstName = firstName;
this.lastName = lastName;
this.readOnly = readOnly;
this.isMale = isMale;
}
public void writeable() {
readOnly = false;
}
public String getFullName() throws AccessException {
if (!fields.contains(NAME)) throw new AccessException("Nameless person");
return firstName + " " + lastName;
}
public String getGender() throws AccessException {
if (!fields.contains(GENDER)) throw new AccessException("Genderless person");
if (isMale) return "Male";
return "Female";
}
}
@Test
public void testNameAndGender() throws Exception {
Person person = new Person("John", "Doe", true);
person.writeable();
assertEquals(person.getFullName(), "John Doe");
assertEquals(person.getGender(), "Male");
}
@Test
public void testNameAndGenderReadOnly() throws Exception {
Person person = new Person("John", "Doe", true);
assertEquals(person.getFullName(), "John Doe");
assertEquals(person.getGender(), "Male");
}
@Test
public void testName() throws Exception {
Person person = new Person("John", "Doe", true);
person.writeable();
assertEquals(person.getFullName(), "John Doe");
}
@Test
public void testGender() throws Exception {
Person person = new Person("John", "Doe", true);
person.writeable();
assertEquals(person.getFullName(), "John Doe");
assertEquals(person.getGender(), "Male");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment