Skip to content

Commit

Permalink
Photo.location, Coordinate, CoordinateTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Pfennig committed Oct 25, 2015
1 parent 3c24d48 commit fa601ce
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/classes
/src/main/webapp/WEB-INF/lib/
/.settings/
/WEB-INF/
*.iml
.gradle
.idea
.project
.classpath
.classpath
60 changes: 60 additions & 0 deletions src/main/java/org/wahlzeit/model/Coordinate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
*/
package org.wahlzeit.model;


public class Coordinate
{
private double latitude;
private double longitude;

Coordinate(double latitude, double longitude)
{
if(boundsOK(latitude))
{
this.latitude = latitude;
}
if(boundsOK(longitude))
{
this.longitude = longitude;
}
}

private boolean boundsOK(double var)
{
if(-90 <= var && var <= 90)
{
return true;
}
throw new IllegalArgumentException("-90 <= lat|lon <= 90");
}

public double getLatitude()
{
return latitude;
}

public double getLongitude()
{
return longitude;
}



public double getLatitudinalDistance(Coordinate coordinate)
{
return Math.abs(coordinate.getLatitude() - latitude);
}

public double getLongitudinalDistance(Coordinate coordinate)
{
return Math.abs(coordinate.getLongitude() - longitude);
}

public Coordinate getDistance(Coordinate coordinate)
{
return new Coordinate( getLatitudinalDistance(coordinate),
getLongitudinalDistance(coordinate));
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/wahlzeit/model/Photo.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public class Photo extends DataObject {
protected int height;
protected PhotoSize maxPhotoSize = PhotoSize.MEDIUM; // derived

/**
*
*/
protected Coordinate location = null;

/**
*
*/
Expand Down Expand Up @@ -418,4 +423,14 @@ public void setNoNewPraise() {
noVotesAtLastNotification = noVotes;
incWriteCount();
}

public void setLocation(Coordinate coordinate)
{
location = coordinate;
}

public Coordinate getLocation()
{
return location;
}
}
132 changes: 132 additions & 0 deletions src/test/java/org/wahlzeit/model/CoordinateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package org.wahlzeit.model;

import org.junit.Test;
import org.junit.Before;
import org.junit.Assert;

/**
*
*/
public class CoordinateTest {
private Coordinate testCoordinate;

private Coordinate paramLessCoordinate;
private Coordinate paramGreaterCoordinate;
private Coordinate paramMixed1Coordinate;
private Coordinate paramMixed2Coordinate;

private final double Epsilon = 1e-5;


@Before
public void setUp()
{
testCoordinate = new Coordinate(5.5, 5.5);

paramLessCoordinate = new Coordinate(4.4, 4.4);
paramGreaterCoordinate = new Coordinate(6.6, 6.6);
paramMixed1Coordinate = new Coordinate(4.4, 6.6);
paramMixed2Coordinate = new Coordinate(6.6, 4.4);
}


@Test
public void testGetter()
{
Assert.assertEquals(5.5, testCoordinate.getLatitude(), Epsilon);
Assert.assertEquals(5.5, testCoordinate.getLongitude(), Epsilon);
}

@Test
public void testLatitudeDistance()
{
Assert.assertEquals(1.1, testCoordinate.getLatitudinalDistance(paramLessCoordinate), Epsilon);
Assert.assertEquals(1.1, testCoordinate.getLatitudinalDistance(paramGreaterCoordinate), Epsilon);
Assert.assertEquals(1.1, testCoordinate.getLatitudinalDistance(paramMixed1Coordinate), Epsilon);
Assert.assertEquals(1.1, testCoordinate.getLatitudinalDistance(paramMixed2Coordinate), Epsilon);
}

@Test
public void testLongitudeDistance()
{
Assert.assertEquals(1.1, testCoordinate.getLongitudinalDistance(paramLessCoordinate), Epsilon);
Assert.assertEquals(1.1, testCoordinate.getLatitudinalDistance(paramGreaterCoordinate), Epsilon);
Assert.assertEquals(1.1, testCoordinate.getLatitudinalDistance(paramMixed1Coordinate), Epsilon);
Assert.assertEquals(1.1, testCoordinate.getLatitudinalDistance(paramMixed2Coordinate), Epsilon);
}

@Test
public void testDistance()
{
Coordinate returnCoordinate;

returnCoordinate = testCoordinate.getDistance(paramLessCoordinate);
Assert.assertEquals(1.1, returnCoordinate.getLatitude(), Epsilon);
Assert.assertEquals(1.1, returnCoordinate.getLongitude(), Epsilon);

returnCoordinate = testCoordinate.getDistance(paramGreaterCoordinate);
Assert.assertEquals(1.1, returnCoordinate.getLatitude(), Epsilon);
Assert.assertEquals(1.1, returnCoordinate.getLongitude(), Epsilon);

returnCoordinate = testCoordinate.getDistance(paramMixed1Coordinate);
Assert.assertEquals(1.1, returnCoordinate.getLatitude(), Epsilon);
Assert.assertEquals(1.1, returnCoordinate.getLongitude(), Epsilon);

returnCoordinate = testCoordinate.getDistance(paramMixed2Coordinate);
Assert.assertEquals(1.1, returnCoordinate.getLatitude(), Epsilon);
Assert.assertEquals(1.1, returnCoordinate.getLongitude(), Epsilon);
}

@Test(expected = NullPointerException.class)
public void testDistanceArgumentNull()
{
testCoordinate.getDistance(null);
}

@Test(expected = IllegalArgumentException.class)
public void testArgumentlessBoundaries()
{
Coordinate c = new Coordinate(-90.1, -90.1);
}
@Test(expected = IllegalArgumentException.class)
public void testArgumentGreaterBoundaries()
{
Coordinate c = new Coordinate(90.1, 90.1);
}
@Test(expected = IllegalArgumentException.class)
public void testArgumentMixed1Boundaries()
{
Coordinate c = new Coordinate(-90.1, 0);
}
@Test(expected = IllegalArgumentException.class)
public void testArgumentMixed2Boundaries()
{
Coordinate c = new Coordinate(90.1, 0);
}
@Test(expected = IllegalArgumentException.class)
public void testArgumentMixed3Boundaries()
{
Coordinate c = new Coordinate(0, -90.1);
}
@Test(expected = IllegalArgumentException.class)
public void testArgumentMixed4Boundaries()
{
Coordinate c = new Coordinate(0, 90.1);
}
@Test(expected = IllegalArgumentException.class)
public void testArgumentMixed5Boundaries()
{
Coordinate cLess = new Coordinate(-90.1, 90.1);
}
@Test(expected = IllegalArgumentException.class)
public void testArgumentMixed6Boundaries()
{
Coordinate cLess = new Coordinate(90.1, -90.1);
}
}

0 comments on commit fa601ce

Please sign in to comment.