JavaEE, Technical

RepositoryTest

Repository Using Core Java

In order to explore Core Java after having worked so much with this or that framework, it is good to return to the core, so I created a repository to add, remove, list and find a specific Type.  In order to do this, the first thing I looked at was the Repository Interface that expects some Type as follows:

package core.repository;

import java.util.List;

/**
* Repository
* User: David King, enterprise.saas@gmail.com
* Date: 4/28/12
* Time: 7:46 PM
*/
public interface Repository<T> {

void add(T t);

void remove(T t);

List<T> list();

T findById(String id);

}

The specific type used in this case is called Item, a simple POJO that also implements Comparable for sorting and comparison, overrides hashCode and equals as shown.

package core.repository;

import javax.net.ssl.SSLEngineResult;
import java.util.UUID;

/**
* Item
* User: David King, enterprise.saas@gmail.com
* Date: 4/28/12
* Time: 7:50 PM
*/
public class Item implements Comparable<Item> {
private String id;
private String title;
private Status status;

public Item(String id, String title, Status status) {
this.id = id;
this.title = title;
this.status = status;
}

public String getId() {
return id;
}

public String getTitle() {
return title;
}

public Status getStatus() {
return status;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Item item = (Item) o;

if (id != null ? !id.equals(item.id) : item.id != null) return false;
if (status != item.status) return false;
if (title != null ? !title.equals(item.title) : item.title != null) return false;

return true;
}

@Override
// if two objects has a different hash code, then they are not equal.
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (status != null ? status.hashCode() : 0);
return result;
}

@Override
public int compareTo(Item compItem) {
if (!(compItem instanceof Item))
throw new ClassCastException(“An Item object expected”);
String compItemId = ((Item)compItem).getId();
return this.id.hashCode() – compItemId.hashCode();
}

@Override
public String toString() {
return “Item{” +
“id='” + id + ‘\” +
“, title='” + title + ‘\” +
“, status=” + status +
‘}’;
}
}

Each of the above was created iteratively using the following JUnit test.

package core.coretest;

import core.repository.Item;
import core.repository.ItemRepository;
import core.repository.Repository;
import core.repository.Status;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.UUID;
import java.util.logging.Logger;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* CoreTest
* User: David King, enterprise.saas@gmail.com
* Date: 4/28/12
* Time: 7:47 PM
*/
public class CoreTest {
private Repository<Item> repository;
private final String TEST_ID = UUID.randomUUID().toString();

@Test
public void testCreateRepository() {
assertNotNull(“Repository for item was null”, repository);
out(“createRepository successful ” + repository);
}

@Test
public void testAddItemToRepository() {
Item item = singleItem();
assertNotNull(“Item was null”, item);
repository.add(item);
}

@Test
public void testRemoveItemFromRepository() {
repository.remove(singleItem());
out(“Removed Item From Repository ” + repository);
}

@Test
public void testFindItemById() {
Item itemToAdd = singleItem();
String id = null;

repository.add(itemToAdd);

Item itemToFind = repository.findById(TEST_ID);
out(“Found Item in Repository ” + itemToFind);

}

@Test
public void testGetRepositoryFullList() {
repository.list().clear();

repository.add(new Item(UUID.randomUUID().toString(),
“Create the wireframes for the login page.”, Status.DESIGN));

repository.add(new Item(UUID.randomUUID().toString(),
“Create project structure.”, Status.DEVELOPMENT));

assertTrue(“Repository is empty”, repository.list().size() > 0);

out(“Full Repository List ” + repository.list().size() + ” = ” + repository.list());
}
private Item singleItem() {
return new Item(TEST_ID, “Create the wireframes for the login page.”, Status.DESIGN);
}

@Before
public void setUp() {
repository = new ItemRepository();

}

@After
public void tearDown() {
repository = null;
}

private void out(String s) {
Logger.getAnonymousLogger().info(s);
}
}

Other players in this code session includes the implementation of the Repository<T>, Item Repository as shown.

package core.repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

/**
* ItemRepository
* User: David King, enterprise.saas@gmail.com
* Date: 4/28/12
* Time: 8:07 PM
*/
public class ItemRepository implements Repository<Item> {
private List<Item> itemList = Collections.EMPTY_LIST;

public ItemRepository() {
itemList = new ArrayList<Item>();
}

@Override
public void add(Item item) {
nullCheck(item);
if (!itemList.contains(item)) {
out(“Item added to repository. ” + item);
itemList.add(item);
}
}

@Override
public void remove(Item item) {
nullCheck(item);
if (itemList.contains(item)) {
out(“Removing item from repository. ” + item);
itemList.remove(item);
}
}

@Override
public List<Item> list() {
return itemList;
}

@Override
public Item findById(String id) {
Item itemToRet = null;
for (Item item : itemList) {
if (item.getId().trim().equalsIgnoreCase(id.trim())) {
itemToRet = new Item(id, item.getTitle(), item.getStatus());
}
}
return itemToRet;
}

private void nullCheck(Item item) {
if (null == item) throw new IllegalArgumentException(“Item was null”);
}

@Override
public String toString() {
return getClass().getName() + “, ” + itemList.toString();
}

private void out(String s) {
Logger.getAnonymousLogger().info(s);
}
}

Finally, the remaining code items are the Status Enum and a Item Title Comparator as shown.

package core.repository;

/**
* Status
* User: David King, enterprise.saas@gmail.com
* Date: 4/28/12
* Time: 7:51 PM
*/
public enum Status {
DESIGN,
DEVELOPMENT,
TESTING,
PRODUCTION;
}

package core.repository;

import java.util.Comparator;

/**
* ItemTitleComparator
* User: David King, enterprise.saas@gmail.com
* Date: 4/29/12
* Time: 5:55 PM
*/
public class ItemTitleComparator implements Comparator<Item>{

@Override
public int compare(Item item, Item secItem) {
String title = ((Item)item).getTitle().toUpperCase();
String secTitle = ((Item)secItem).getTitle().toUpperCase();

if (!title.equals(secTitle)) {
return item.compareTo(secItem);
}

return 0;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s