Skip to content

Add exception handling for instance ids #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/instanceids/AddressId.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class AddressId extends Identifiable {

private AddressId(Integer id) {
super(id);
super(id , AddressId.class.getSimpleName());
}

public static AddressId get(Integer id) {
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/instanceids/Identifiable.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package instanceids;


public abstract class Identifiable {

private Integer id;
private final Integer id;
private final String className;

public Identifiable(Integer id) {
public Identifiable(Integer id, String className){
this.id = id;
this.className = className;
checkValue();
}

private void throwException() {
throw new IllegalArgumentException(String.format("Invalid value %s for class %s" , id , className));
}

private void checkValue() {
if(id == null){
throwException();
}
if(id != null && id <= 0 ){
throwException();
}
}

public Integer getValue() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/instanceids/PersonId.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class PersonId extends Identifiable {

private PersonId(Integer id) {
super(id);
super(id , PersonId.class.getSimpleName());
}

public static PersonId get(Integer id) {
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/instanceids/InstanceIdInputterTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package instanceids;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

public class InstanceIdInputterTest {

@Test
public void test_ThrowsExceptionIfNullInput() {
public void test_ValidInput() {
InstanceIdInputter inputter = new InstanceIdInputter(PersonId.get(1), AddressId.get(2));
assertEquals(new Integer(1), inputter.getPersonId());
assertEquals(new Integer(2), inputter.getAddressId());
}

@Test(expected=IllegalArgumentException.class)
public void test_ThrowsExceptionIfInvalidInteger() {
InstanceIdInputter inputter = new InstanceIdInputter(PersonId.get(0), AddressId.get(0));
}

@Test(expected=IllegalArgumentException.class)
public void test_ThrowsExceptionIfNullInput() {
InstanceIdInputter inputter = new InstanceIdInputter(PersonId.get(null), AddressId.get(null));
}
}