Added JUnit tests for Employee-Dependent Example
This commit is contained in:
parent
b474ac4167
commit
cc159354ce
@ -0,0 +1,17 @@
|
||||
package net.abhinavsarkar.jywrapper.example;
|
||||
|
||||
public interface Dependent {
|
||||
|
||||
public Dependent initialize();
|
||||
|
||||
public String getFirstName();
|
||||
|
||||
public String getLastName();
|
||||
|
||||
public void setFirstName(String firstName);
|
||||
|
||||
public void setLastName(String lastName);
|
||||
|
||||
public Employee getEmployee();
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package net.abhinavsarkar.jywrapper.example;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import net.abhinavsarkar.jywrapper.JyWrapper;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DependentTest {
|
||||
|
||||
private final Employee Employee_ = JyWrapper.wrap(Employee.class, "person.Employee");
|
||||
|
||||
private final Dependent Dependent_ = JyWrapper.wrap(Dependent.class, "person.Dependent");
|
||||
|
||||
@Test
|
||||
public void testSetGetFirstName() {
|
||||
final Dependent dependent = Dependent_.initialize();
|
||||
dependent.setFirstName("jet");
|
||||
assertEquals("jet", dependent.getFirstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetGetLastName() {
|
||||
final Dependent dependent = Dependent_.initialize();
|
||||
dependent.setLastName("li");
|
||||
assertEquals("li", dependent.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetEmployee() {
|
||||
final Dependent dependent = Dependent_.initialize();
|
||||
dependent.setFirstName("hawk");
|
||||
dependent.setLastName("hawkster");
|
||||
final Employee employee = Employee_.initialize("abhinav", "sarkar");
|
||||
employee.addDependent(dependent);
|
||||
|
||||
assertEquals(employee, dependent.getEmployee());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package net.abhinavsarkar.jywrapper.example;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface Employee {
|
||||
|
||||
public Employee initialize(String firstName, String lastName);
|
||||
|
||||
public String getFirstName();
|
||||
|
||||
public String getLastName();
|
||||
|
||||
public Set<Dependent> getDependents();
|
||||
|
||||
public void addDependent(Dependent dependent);
|
||||
|
||||
public void removeDependent(Dependent dependent);
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package net.abhinavsarkar.jywrapper.example;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import net.abhinavsarkar.jywrapper.JyWrapper;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class EmployeeTest {
|
||||
|
||||
private final Employee Employee_ = JyWrapper.wrap(Employee.class, "person.Employee");
|
||||
|
||||
private final Dependent Dependent_ = JyWrapper.wrap(Dependent.class, "person.Dependent");
|
||||
|
||||
@Test
|
||||
public void testGetFirstName() {
|
||||
final Employee employee = Employee_.initialize("abhinav", "sarkar");
|
||||
assertEquals("abhinav", employee.getFirstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLastName() {
|
||||
final Employee employee = Employee_.initialize("abhinav", "sarkar");
|
||||
assertEquals("sarkar", employee.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddDependent() {
|
||||
final Dependent dependent = Dependent_.initialize();
|
||||
dependent.setFirstName("hawk");
|
||||
dependent.setLastName("hawkster");
|
||||
final Employee employee = Employee_.initialize("abhinav", "sarkar");
|
||||
employee.addDependent(dependent);
|
||||
|
||||
assertTrue(employee.getDependents().contains(dependent));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveDependent() {
|
||||
final Dependent dependent = Dependent_.initialize();
|
||||
dependent.setFirstName("hawk");
|
||||
dependent.setLastName("hawkster");
|
||||
final Employee employee = Employee_.initialize("abhinav", "sarkar");
|
||||
employee.addDependent(dependent);
|
||||
employee.removeDependent(dependent);
|
||||
|
||||
assertFalse(employee.getDependents().contains(dependent));
|
||||
}
|
||||
|
||||
}
|
66
src/example/java/person.py
Normal file
66
src/example/java/person.py
Normal file
@ -0,0 +1,66 @@
|
||||
class Employee():
|
||||
def __init__(self, first_name, last_name):
|
||||
self._first_name = first_name
|
||||
self._last_name = last_name
|
||||
self._dependents = set()
|
||||
|
||||
@property
|
||||
def first_name(self):
|
||||
return self._first_name
|
||||
|
||||
@property
|
||||
def last_name(self):
|
||||
return self._last_name
|
||||
|
||||
@property
|
||||
def dependents(self):
|
||||
return self._dependents
|
||||
|
||||
def add_dependent(self, dependent):
|
||||
dependent.set_employee(self)
|
||||
self._dependents.add(dependent)
|
||||
|
||||
def remove_dependent(self, dependent):
|
||||
if dependent in self._dependents:
|
||||
self._dependents.remove(dependent)
|
||||
dependent.set_employee(None)
|
||||
|
||||
def __eq__(self, other):
|
||||
if self == other:
|
||||
return True
|
||||
if isinstance(other, Employee):
|
||||
return (self.first_name == other.first_name
|
||||
and self.last_name == other.last_name)
|
||||
return False
|
||||
|
||||
def __str__(self):
|
||||
return "<Employee: %s %s>" % (self._first_name, self._last_name)
|
||||
|
||||
def __hash__(self):
|
||||
return 31 + hash(self.__class__) + hash(self._first_name) + hash(self._last_name)
|
||||
|
||||
class Dependent(object):
|
||||
def __init__(self):
|
||||
self.first_name = None
|
||||
self.last_name = None
|
||||
self.employee = None
|
||||
|
||||
def set_employee(self, employee):
|
||||
self._employee = employee
|
||||
|
||||
def get_employee(self):
|
||||
return self._employee
|
||||
|
||||
def __eq__(self, other):
|
||||
if self == other:
|
||||
return True
|
||||
if isinstance(other, Dependent):
|
||||
return (self.first_name == other.first_name
|
||||
and self.last_name == other.last_name)
|
||||
return False
|
||||
|
||||
def __str__(self):
|
||||
return "<Dependent: %s %s>" % (self.first_name, self.last_name)
|
||||
|
||||
def __hash__(self):
|
||||
return 31 + hash(self.__class__) + hash(self.first_name) + hash(self.last_name)
|
Loading…
Reference in New Issue
Block a user