Skip to content

Commit

Permalink
Fix bug in GeoCoordinates.toString and extend tests #86
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Jul 6, 2015
1 parent 0f8cb43 commit c3588d7
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/com/lambdaworks/redis/GeoCoordinates.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lambdaworks.redis;

import com.lambdaworks.redis.output.DoubleOutput;

/**
* A tuple consisting of numerical geo data points to describe geo coordinates.
*
Expand Down Expand Up @@ -39,7 +41,8 @@ public int hashCode() {

@Override
public String toString() {
return String.format("(%f, %f)", x, y);

return String.format("(%s, %s)", x, y);
}

}
145 changes: 145 additions & 0 deletions src/test/java/com/lambdaworks/redis/GeoModelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.lambdaworks.redis;

import static org.assertj.core.api.Assertions.*;

import java.util.Map;

import org.junit.Test;

import com.google.common.collect.ImmutableMap;

/**
* @author <a href="mailto:[email protected]">Mark Paluch</a>
* @since 06.07.15 14:18
*/
public class GeoModelTest {

@Test
public void geoWithin() throws Exception {

GeoWithin<String> sut = new GeoWithin<String>("me", 1.0, 1234L, new GeoCoordinates(1, 2));
GeoWithin<String> equalsToSut = new GeoWithin<String>("me", 1.0, 1234L, new GeoCoordinates(1, 2));

Map<GeoWithin<String>, String> map = ImmutableMap.of(sut, "value");

assertThat(map.get(equalsToSut)).isEqualTo("value");
assertThat(sut).isEqualTo(equalsToSut);
assertThat(sut.hashCode()).isEqualTo(equalsToSut.hashCode());
assertThat(sut.toString()).isEqualTo(equalsToSut.toString());

}

@Test
public void geoWithinSlightlyDifferent() throws Exception {

GeoWithin<String> sut = new GeoWithin<String>("me", 1.0, 1234L, new GeoCoordinates(1, 2));
GeoWithin<String> slightlyDifferent = new GeoWithin<String>("me", 1.0, 1234L, new GeoCoordinates(1.1, 2));

Map<GeoWithin<String>, String> map = ImmutableMap.of(sut, "value");

assertThat(map.get(slightlyDifferent)).isNull();
assertThat(sut).isNotEqualTo(slightlyDifferent);
assertThat(sut.hashCode()).isNotEqualTo(slightlyDifferent.hashCode());
assertThat(sut.toString()).isNotEqualTo(slightlyDifferent.toString());

slightlyDifferent = new GeoWithin<String>("me1", 1.0, 1234L, new GeoCoordinates(1, 2));
assertThat(sut).isNotEqualTo(slightlyDifferent);
}

@Test
public void geoWithinEmpty() throws Exception {

GeoWithin<String> sut = new GeoWithin<String>(null, null, null, null);
GeoWithin<String> equalsToSut = new GeoWithin<String>(null, null, null, null);

assertThat(sut).isEqualTo(equalsToSut);
assertThat(sut.hashCode()).isEqualTo(equalsToSut.hashCode());
}

@Test
public void geoCoordinates() throws Exception {

GeoCoordinates sut = new GeoCoordinates(1, 2);
GeoCoordinates equalsToSut = new GeoCoordinates(1, 2);

Map<GeoCoordinates, String> map = ImmutableMap.of(sut, "value");

assertThat(map.get(equalsToSut)).isEqualTo("value");
assertThat(sut).isEqualTo(equalsToSut);
assertThat(sut.hashCode()).isEqualTo(equalsToSut.hashCode());
assertThat(sut.toString()).isEqualTo(equalsToSut.toString());

}

@Test
public void geoCoordinatesSlightlyDifferent() throws Exception {

GeoCoordinates sut = new GeoCoordinates(1, 2);
GeoCoordinates slightlyDifferent = new GeoCoordinates(1.1, 2);

Map<GeoCoordinates, String> map = ImmutableMap.of(sut, "value");

assertThat(map.get(slightlyDifferent)).isNull();
assertThat(sut).isNotEqualTo(slightlyDifferent);
assertThat(sut.hashCode()).isNotEqualTo(slightlyDifferent.hashCode());
assertThat(sut.toString()).isNotEqualTo(slightlyDifferent.toString());

}

@Test
public void geoCoordinatesEmpty() throws Exception {

GeoCoordinates sut = new GeoCoordinates(null, null);
GeoCoordinates equalsToSut = new GeoCoordinates(null, null);

assertThat(sut).isEqualTo(equalsToSut);
assertThat(sut.hashCode()).isEqualTo(equalsToSut.hashCode());
}

@Test
public void geoEncoded() throws Exception {

GeoEncoded sut = new GeoEncoded(1234, new GeoCoordinates(null, 1), new GeoCoordinates(1, null),
new GeoCoordinates(1, 2));
GeoEncoded equalsToSut = new GeoEncoded(1234, new GeoCoordinates(null, 1), new GeoCoordinates(1, null),
new GeoCoordinates(1, 2));

Map<GeoEncoded, String> map = ImmutableMap.of(sut, "value");

assertThat(map.get(sut)).isEqualTo("value");
assertThat(sut).isEqualTo(equalsToSut);
assertThat(sut.hashCode()).isEqualTo(equalsToSut.hashCode());
assertThat(sut.toString()).isEqualTo(equalsToSut.toString());

}

@Test
public void geoEncodedSlightlyDifferent() throws Exception {

GeoEncoded sut = new GeoEncoded(1234, new GeoCoordinates(null, 1), new GeoCoordinates(1, 555.5), new GeoCoordinates(1,
2));
GeoEncoded slightlyDifferent = new GeoEncoded(1234, new GeoCoordinates(null, 1), new GeoCoordinates(1, null),
new GeoCoordinates(1, 2));

Map<GeoEncoded, String> map = ImmutableMap.of(sut, "value");

assertThat(map.get(slightlyDifferent)).isNull();
assertThat(sut).isNotEqualTo(slightlyDifferent);
assertThat(sut).isNotEqualTo(
slightlyDifferent = new GeoEncoded(0, new GeoCoordinates(null, 1), new GeoCoordinates(1, null),
new GeoCoordinates(1, 2)));
assertThat(sut.hashCode()).isNotEqualTo(slightlyDifferent.hashCode());
assertThat(sut.toString()).isNotEqualTo(slightlyDifferent.toString());

}

@Test
public void geoEncodedEmpty() throws Exception {

GeoEncoded sut = new GeoEncoded(0, null, null, null);
GeoEncoded equalsToSut = new GeoEncoded(0, null, null, null);

assertThat(sut).isEqualTo(equalsToSut);
assertThat(sut.hashCode()).isEqualTo(equalsToSut.hashCode());
}
}

0 comments on commit c3588d7

Please sign in to comment.