Skip to content

Commit

Permalink
added: load by X and Y
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikWendt committed Apr 10, 2012
1 parent 5f1c8a3 commit 3e85198
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 11 deletions.
Expand Up @@ -126,4 +126,21 @@ public List<Animal> loadByDateBetweenIncluding(Date from, Date until) {
return result;
}

public List<Animal> loadBySpeciesAndDateAfterIncluding(String species, Date bornAtOrAfter) {
List<Animal> bySpecies = loadBySpecies(species);
List<Animal> byDate = loadByDateAfterIncluding(bornAtOrAfter);
List<Animal> result = new ArrayList<Animal>(bySpecies);
result.retainAll(byDate);
return result;
}

public List<Animal> loadBySpeciesAndNamePattern(String string, String string2) {
// TODO: both can be expressed as single SQL statement
List<Animal> bySpecies = loadBySpecies(string);
List<Animal> byName = loadByNamesMatching(string2);
List<Animal> result = new ArrayList<Animal>(bySpecies);
result.retainAll(byName);
return result;
}

}
Expand Up @@ -8,7 +8,21 @@
public class AnimalImpl implements Animal {

private long animalId;
private Map<String, Object> properties = new HashMap<String, Object>();
private Map<String, Object> properties = new HashMap<String, Object>();

@Override
public String toString() {
return get("name") + "," + get("species") + "," + get("dob");
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Animal) {
Animal other = (Animal) obj;
return properties.equals(other.getProperties());
}
return false;
}

@Override
public long getId() {
Expand Down
@@ -0,0 +1,42 @@
package se.jdojo.gbg.old;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import se.jdojo.gbg.Animal;

public class ContainsAnimals extends BaseMatcher<List<Animal>> {

private final Collection<String> names = new ArrayList<String>();

public ContainsAnimals(String[] names) {
for (String n : names) {
this.names.add(n);
}
}

public static ContainsAnimals containsAnimals(String... names) {
return new ContainsAnimals(names);
}

@Override
public boolean matches(Object arg0) {
@SuppressWarnings("unchecked")
List<Animal> result = (List<Animal>) arg0;
List<String> names = new ArrayList<String>();
for (Animal animal : result) {
names.add((String) animal.get("name"));
}
return names.containsAll(this.names);
}

@Override
public void describeTo(Description arg0) {
arg0.appendText("expected list to contain some animals, one or more was missing");
}

}
@@ -1,8 +1,11 @@
package se.jdojo.gbg.old;

import static se.jdojo.gbg.old.ContainsAnimals.*;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -91,7 +94,7 @@ public void load_By_Name_With_Pattern_Matching_Nothing() throws Exception {
@Test
public void load_By_Date_After_1981() throws Exception {
final Date pointInTime = dateUtil.asDate("1981-01-01");

List<Animal> result = testee.loadByDateAfterIncluding(pointInTime);

assertEquals(2, result.size());
Expand All @@ -104,7 +107,7 @@ public void load_By_Date_After_1981() throws Exception {
@Test
public void load_By_Date_After_1970() throws Exception {
final Date pointInTime = dateUtil.asDate("1970-01-01");

List<Animal> result = testee.loadByDateAfterIncluding(pointInTime);

assertEquals(6, result.size());
Expand All @@ -117,7 +120,7 @@ public void load_By_Date_After_1970() throws Exception {
@Test
public void load_By_Date_Before_1981() throws Exception {
Date pointInTime = dateUtil.asDate("1981-01-01");

List<Animal> result = testee.loadByDateBeforeIncluding(pointInTime);

assertEquals(5, result.size());
Expand All @@ -131,9 +134,9 @@ public void load_By_Date_Before_1981() throws Exception {
public void load_By_Date_Between_Two_Dates() throws Exception {
Date from = dateUtil.asDate("1980-11-01");
Date until = dateUtil.asDate("1980-12-31");

List<Animal> result = testee.loadByDateBetweenIncluding(from, until);

assertEquals(2, result.size());
verifyAnimalObjectIntegrity(result);
}
Expand All @@ -142,7 +145,7 @@ public void load_By_Date_Between_Two_Dates() throws Exception {
public void load_By_Date_Between_Two_Other_Dates() throws Exception {
Date from = dateUtil.asDate("1980-10-31");
Date until = dateUtil.asDate("1981-01-01");

List<Animal> result = testee.loadByDateBetweenIncluding(from, until);

assertEquals(4, result.size());
Expand All @@ -165,14 +168,34 @@ public void load_By_Date_Must_Be_Passed_Arguments_In_Cronological_Order() throws
@Test
public void load_By_Date_Between_Can_Be_Given_Two_Identical_Arguments() throws Exception {
Date date = dateUtil.asDate("1980-10-15");

List<Animal> result = testee.loadByDateBetweenIncluding(date, date);

assertEquals("adam", result.iterator().next().get("name"));
assertEquals(1, result.size());
}

// FIXME: test load by species, name, date
@Test
public void load_By_Species_And_Date() throws Exception {
List<Animal> result = testee.loadBySpeciesAndDateAfterIncluding("fly", dateUtil.asDate("1981-01-01"));

assertEquals(1, result.size());
assertEquals("fredrik", result.iterator().next().get("name"));
}

@Test
public void load_By_Species_And_Name_Pattern() throws Exception {
List<Animal> result = testee.loadBySpeciesAndNamePattern("fly", "e");

assertThat(result, containsAnimals("evan", "fredrik"));
}

@Test
public void load_By_Species_And_Name_Pattern_Two() throws Exception {
List<Animal> result = testee.loadBySpeciesAndNamePattern("fly", "f");

assertThat(result, containsAnimals("fredrik"));
}

// TODO: negative path tests ...
// TODO: rewrite matching with prettier Hamcrest matcHers
Expand Down

0 comments on commit 3e85198

Please sign in to comment.