Skip to content

Commit

Permalink
unit test for issue hazelcast#3047 (throws exception in case of non-c…
Browse files Browse the repository at this point in the history
…omparable)
  • Loading branch information
serkanozal committed Aug 28, 2014
1 parent c7fba22 commit 054dc5e
Showing 1 changed file with 56 additions and 31 deletions.
Expand Up @@ -25,11 +25,9 @@
import com.hazelcast.query.PagingPredicate;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.Predicates;
import com.hazelcast.query.SampleObjects;
import com.hazelcast.query.impl.QueryEntry;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ProblematicTest;
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.util.IterationType;
import org.junit.After;
Expand Down Expand Up @@ -244,13 +242,11 @@ public void testNextPageAfterResultSetEmpty() {
}

@Test
@Category(ProblematicTest.class)
public void mapPagingPredicateEmployeeObjectWithOrderedIndexSmallTest() {
mapPagingPredicateEmployeeObjectWithOrderedIndex(10);
}

@Test
@Category(ProblematicTest.class)
public void mapPagingPredicateEmployeeObjectWithOrderedIndexLargeTest() {
mapPagingPredicateEmployeeObjectWithOrderedIndex(5000);
}
Expand Down Expand Up @@ -278,7 +274,6 @@ private void mapPagingPredicateEmployeeObjectWithOrderedIndex(int maxEmployee) {

// https://github.com/hazelcast/hazelcast/issues/3047
@Test
@Category(ProblematicTest.class)
public void betweenPagingPredicateWithEmployeeTest() {
final int minId = 10;
final int maxId = 15;
Expand All @@ -303,7 +298,6 @@ public void betweenPagingPredicateWithEmployeeTest() {

// https://github.com/hazelcast/hazelcast/issues/3047
@Test
@Category(ProblematicTest.class)
public void lessThanPredicateWithEmployeeTest() {
final int maxId = 500;
final int pageSz = 5;
Expand All @@ -327,7 +321,6 @@ public void lessThanPredicateWithEmployeeTest() {

// https://github.com/hazelcast/hazelcast/issues/3047
@Test
@Category(ProblematicTest.class)
public void equalsPredicateWithEmployeeTest() {
final String name = Employee.getRandomName();
final int pageSz = 5;
Expand Down Expand Up @@ -379,7 +372,7 @@ private List<Employee> pagingPredicateWithEmployeeObjectTest(IMap<Integer, Emplo
// https://github.com/hazelcast/hazelcast/issues/3047
@Test
public void testIssue3047() {
final IMap<Integer, SampleObjects.Employee> map = client.getMap("employeeMap");
final IMap<Integer, Employee> map = client.getMap("employeeMap");
final int PAGE_SIZE = 5;
final int START_ID_FOR_QUERY = 0;
final int FINISH_ID_FOR_QUERY = 50;
Expand All @@ -389,21 +382,15 @@ public void testIssue3047() {
(queriedElementCount % PAGE_SIZE == 0 ? 0 : 1);

for(int i = 0; i < 1000; i++) {
map.put(i,
new SampleObjects.Employee(
i,
"Employee-" + i,
(int)(20 + Math.random() * 60),
true,
Math.random() * 1000));
map.put(i, new Employee(i));
}

map.addIndex("id", true);

Predicate pred = Predicates.between("id", START_ID_FOR_QUERY, FINISH_ID_FOR_QUERY);

PagingPredicate predicate = new PagingPredicate(pred, PAGE_SIZE);
Collection<SampleObjects.Employee> values;
Collection<Employee> values;
int passedPageCount = 0;

for ( values = map.values(predicate); !values.isEmpty() &&
Expand All @@ -416,6 +403,32 @@ public void testIssue3047() {
assertEquals(expectedPageCount, passedPageCount);
}

// https://github.com/hazelcast/hazelcast/issues/3047
@Test(expected = IllegalArgumentException.class)
public void testIssue3047ForNonComparableEntitiesThrowsIllegalArgumentException() {
final IMap<Integer, BaseEmployee> map = client.getMap("baseEmployeeMap");
final int PAGE_SIZE = 5;
final int START_ID_FOR_QUERY = 0;
final int FINISH_ID_FOR_QUERY = 50;

for (int i = 0; i < 100; i++) {
map.put(i, new BaseEmployee(i));
}

map.addIndex("id", true);

Predicate pred = Predicates.between("id", START_ID_FOR_QUERY, FINISH_ID_FOR_QUERY);

PagingPredicate predicate = new PagingPredicate(pred, PAGE_SIZE);
Collection<BaseEmployee> values;

for ( values = map.values(predicate); !values.isEmpty() &&
values != null;
values = map.values(predicate)) {
predicate.nextPage();
}
}

private static class TestComparator implements Comparator<Map.Entry>, Serializable {

int ascending = 1;
Expand Down Expand Up @@ -456,26 +469,26 @@ public int compare(Employee e1, Employee e2) {
}
}

private static class Employee implements Serializable, Comparable<Employee> {
private static class BaseEmployee implements Serializable {

public static final int MAX_AGE = 75;
public static final double MAX_SALARY = 1000.0;

public static final String[] names = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg"};
public static Random random = new Random();

private int id;
private String name;
private int age;
private boolean active;
private double salary;
protected int id;
protected String name;
protected int age;
protected boolean active;
protected double salary;

public Employee(int id) {
this.id = id;
setAtributesRandomly();
public BaseEmployee() {
}

public Employee() {
public BaseEmployee(int id) {
this.id = id;
setAtributesRandomly();
}

public void setAtributesRandomly(){
Expand Down Expand Up @@ -507,11 +520,6 @@ public boolean isActive() {
return active;
}

@Override
public int compareTo(Employee employee) {
return id - employee.id;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Expand All @@ -530,6 +538,23 @@ public String toString() {
", salary=" + salary +
'}';
}

}

private static class Employee extends BaseEmployee implements Comparable<Employee> {

public Employee() {
}

public Employee(int id) {
super(id);
}

@Override
public int compareTo(Employee employee) {
return id - employee.id;
}

}

}

0 comments on commit 054dc5e

Please sign in to comment.