Skip to content

Commit

Permalink
test SortBy class IQSS#5634
Browse files Browse the repository at this point in the history
  • Loading branch information
alexscheitlin committed Mar 13, 2019
1 parent 8082371 commit e622831
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/edu/harvard/iq/dataverse/search/SortBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public SortBy(String field, String order) {

@Override
public String toString() {
return "SortBy{" + "field=" + field + ", order=" + order + '}';
return "SortBy{" + "field=" + field + ", order=" + order + "}";
}

public String getField() {
Expand Down
101 changes: 101 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/search/SortByTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package edu.harvard.iq.dataverse.search;

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SortByTest {

private String field;
private String order;
private SortBy instance;

@Before
public void setUp() {
this.field = "field";
this.order = SortBy.ASCENDING;
this.instance = new SortBy(field, order);
}

@After
public void tearDown() {
this.field = null;
this.order = null;
this.instance = null;
}

@Test
public void testToString() {
String expected = "SortBy{field=" + this.field + ", order=" + this.order + "}";
String actual = this.instance.toString();
assertEquals(expected, actual);
}

@Test
public void testGetField() {
String expected = this.field;
String actual = this.instance.getField();
assertEquals(expected, actual);
}

@Test
public void testGetOrder() {
String expected = this.order;
String actual = this.instance.getOrder();
assertEquals(expected, actual);
}

@Test
public void testHashCodeIdentityOfSameObject() {
// Whenever it is invoked on the same object more than once during an execution of
// a Java application, the hashCode method must consistently return the same
// integer, ...
// according to:
// https://docs.oracle.com/javase/7/donulli/java/lang/Object.html#hashCode()

int firstHash = this.instance.hashCode();
int secondHash = this.instance.hashCode();
assertEquals(firstHash, secondHash);
}

@Test
public void testHashCodeIdentityOfDifferentObjects() {
// If two objects are equal according to the equals(Object) method, then calling
// the hashCode method on each of the two objects must produce the same integer
// result.
// according to:
// https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()

SortBy secondInstance = new SortBy(this.field, this.order);

int firstHash = this.instance.hashCode();
int secondHash = secondInstance.hashCode();
assertEquals(firstHash, secondHash);
}

@Test
public void testEqualsWithSameObject() {
assertTrue(this.instance.equals(this.instance));
}

@Test
public void testEqualsWithNull() {
assertFalse(this.instance.equals(null));
}

@Test
public void testEqualsWithAnotherClass() {
assertFalse(this.instance.equals(new String("some string")));
}

@Test
public void testEqualsWithAnotherButEqualObject() {
SortBy secondInstance = new SortBy(this.field, this.order);
assertTrue(this.instance.equals(secondInstance));
}

}

0 comments on commit e622831

Please sign in to comment.