In meinem Projekt habe ich keine Klasse für Kontakte:
Dieses ist eine Abstrakte Klasse, weil es für jeden EContactMethod (TELEPHON, ADDRESS, EMAIL, INTERNET, ...) wird es eine eigene Klasse geben wird.
Für meine Basisklasse Contact habe ich eine Testklasse
Im der Test-Datei befindet sich eine konkrete Klasse ContactTestClass.
Nun hätte ich gedacht das man auf die protected Methode setMethod() von der Test Klasse nicht zugreifen können. Also die Test Funktion hätte nicht funktionieren können, weil es in der ContactTestClass nicht überschrieben wird.
Aber es scheint zu funkionieren. Weis jemand warum?
Code:
@Entity
@Table(name = "contacts")
public abstract class Contact {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@CreationTimestamp
private LocalDateTime created_at;
@NotNull
@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)
@JoinColumn(name="created_by")
private Person created_by;
@UpdateTimestamp
private LocalDateTime updated_at;
@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)
@JoinColumn(name="updated_by")
private Person updated_by;
@Enumerated
@Column(columnDefinition = "smallint")
private EContactMethod method = EContactMethod.TELEPHON;
@Enumerated
@Column(columnDefinition = "smallint")
private EContactLocation location = EContactLocation.BUSINESS;
@NotBlank
@Column(length = 255)
private String content;
@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)
@JoinColumn(name="address_id")
private Address address;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(
name = "contact_infos",
joinColumns={ @JoinColumn(name="contact_id", referencedColumnName="id") },
inverseJoinColumns={ @JoinColumn(name="text_id", referencedColumnName="id", unique=true) }
)
private List<Text> infos = new ArrayList<Text>();
public Contact() {
}
public Contact(Contact contact) {
this.method = contact.method;
this.location = contact.location;
this.content = contact.content;
this.address = contact.address;
List<String> strings = new ArrayList<>();
contact.infos.stream().forEach((info) -> {
this.infos.add(new Text(info));
});
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDateTime getCreatedAt() {
return created_at;
}
public void setCreatedAt(LocalDateTime created_at) {
this.created_at = created_at;
}
public Person getCreatedBy() {
return created_by;
}
public void setCreatedBy(Person created_by) {
this.created_by = created_by;
}
public LocalDateTime getUpdatedAt() {
return updated_at;
}
public void setUpdatedAt(LocalDateTime updated_at) {
this.updated_at = updated_at;
}
public Person getUpdatedBy() {
return updated_by;
}
public void setUpdatedBy(Person updated_by) {
this.updated_by = updated_by;
}
public EContactMethod getMethod() {
return method;
}
protected void setMethod(EContactMethod method) {
this.method = method;
}
public EContactLocation getLocation() {
return location;
}
public void setLocation(EContactLocation location) {
this.location = location;
}
protected String getContent() {
return content;
}
protected void setContent(String content) {
this.content = content;
}
protected Address getAddress() {
return address;
}
protected void setAddress(Address address) {
this.address = address;
}
public List<Text> getInfos() {
return infos;
}
public void setInfos(List<Text> infos) {
this.infos = infos;
}
@Override
public String toString() {
return "Contact [id=" + id + ", created_at=" + created_at + ", created_by=" + created_by + ", updated_at="
+ updated_at + ", updated_by=" + updated_by + ", method=" + method + ", location=" + location
+ ", content=" + content + ", address=" + address + ", infos=" + infos + "]";
}
}
Für meine Basisklasse Contact habe ich eine Testklasse
Code:
class ContactTestClass extends Contact {
public ContactTestClass() {
super();
}
public ContactTestClass(Contact contact) {
super(contact);
}
}
public class ContactTest {
private Validator validator;
@Before
public void setUp() throws Exception {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
@Test
public void testCopyConstructor() {
EContactMethod methodValue = EContactMethod.TELEPHON;
EContactLocation locationValue = EContactLocation.BUSINESS;
String contentValue = "content";
Address addressValue = new Address();
String info1Value = "german text";
String info2Value = "english text";
String info3Value = "netherlands text";
List<Text> infoValue = new ArrayList<Text>();
infoValue.add(new Text(new Language(), info1Value));
infoValue.add(new Text(new Language(), info2Value));
infoValue.add(new Text(new Language(), info3Value));
ContactTestClass contact = new ContactTestClass();
contact.setMethod(methodValue);
contact.setLocation(locationValue);
contact.setContent(contentValue);
contact.setAddress(addressValue);
contact.setInfos(infoValue);
ContactTestClass copied_contact = new ContactTestClass(contact);
assertEquals(copied_contact.getMethod(), methodValue);
assertEquals(copied_contact.getLocation(), locationValue);
assertEquals(copied_contact.getContent(), contentValue);
assertEquals(copied_contact.getAddress(), addressValue);
assertEquals(copied_contact.getInfos().size(), 3);
assertNotEquals(copied_contact.getInfos().get(0), contact.getInfos().get(0));
assertEquals(copied_contact.getInfos().get(0).getText(), info1Value);
assertNotEquals(copied_contact.getInfos().get(1), contact.getInfos().get(1));
assertEquals(copied_contact.getInfos().get(1).getText(), info2Value);
assertNotEquals(copied_contact.getInfos().get(2), contact.getInfos().get(2));
assertEquals(copied_contact.getInfos().get(2).getText(), info3Value);
}
...
}
Nun hätte ich gedacht das man auf die protected Methode setMethod() von der Test Klasse nicht zugreifen können. Also die Test Funktion hätte nicht funktionieren können, weil es in der ContactTestClass nicht überschrieben wird.
Aber es scheint zu funkionieren. Weis jemand warum?