Permalink
Browse files

Moved the UnknownLayerException into the proper package. Moved the Mo…

…delHelper into the src package so it could be used outside the testing framework.
  • Loading branch information...
1 parent e761704 commit 8785c7d681992a400dbc643ebd1bc93a3169d3b2 dsmith committed Jun 29, 2010
View
2 examples/src/RecordExample.java
@@ -5,8 +5,8 @@
import org.apache.http.client.ClientProtocolException;
import com.simplegeo.client.SimpleGeoClient;
-import com.simplegeo.client.exceptions.ValidLayerException;
import com.simplegeo.client.geojson.GeoJSONObject;
+import com.simplegeo.client.http.exceptions.ValidLayerException;
import com.simplegeo.client.model.DefaultRecord;
import com.simplegeo.client.model.GeoJSONRecord;
import com.simplegeo.client.model.IRecord;
View
2 src/com/simplegeo/client/encoder/GeoJSONEncoder.java
@@ -212,7 +212,7 @@ public static GeoJSONObject getGeoJSONRecord(List<IRecord> records) {
features.put(jsonObject);
}
- geoJSONObject.put("features", geoJSONObject);
+ geoJSONObject.setFeatures(features);
} catch (JSONException e) {
View
4 src/com/simplegeo/client/geojson/GeoJSONObject.java
@@ -76,21 +76,23 @@ public GeoJSONObject(String type, String jsonString) throws JSONException {
* @see <a href="http://geojson.org/geojson-spec.html#feature-objects"</a>
*/
public GeoJSONObject(String type) {
+ super();
setupStructure(type);
}
/**
* Initializes a new object of type Feature.
*/
public GeoJSONObject() {
+ super();
setupStructure("Feature");
}
private void setupStructure(String type) {
try {
- this.put("type",type);
+ this.put("type", type);
if(type.equals("Feature")) {
View
2 ...lient/exceptions/ValidLayerException.java → .../http/exceptions/ValidLayerException.java
@@ -26,7 +26,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-package com.simplegeo.client.exceptions;
+package com.simplegeo.client.http.exceptions;
public class ValidLayerException extends Exception {
View
6 src/com/simplegeo/client/model/DefaultRecord.java
@@ -31,6 +31,8 @@
import org.json.JSONException;
import org.json.JSONObject;
+import com.simplegeo.client.types.Point;
+
/**
* The default implementation of a {@link com.simplegeo.client.model.IRecord}
*
@@ -305,6 +307,10 @@ public void setObjectProperty(String key, Object value) {
}
}
+
+ public Point getOrigin() {
+ return new Point(this.latitude, this.longitude);
+ }
/* (non-Javadoc)
* @see java.lang.Object#toString()
View
2 src/com/simplegeo/client/model/GeoJSONRecord.java
@@ -51,7 +51,7 @@
* @param expiration
*/
public GeoJSONRecord(String recordId, String layer, String type, long created, long expiration) {
-
+ super();
setProperties(new JSONObject());
setRecordId(recordId);
View
2 src/com/simplegeo/client/model/Layer.java
@@ -40,7 +40,7 @@
import ch.hsr.geohash.GeoHash;
import com.simplegeo.client.SimpleGeoClient;
-import com.simplegeo.client.exceptions.ValidLayerException;
+import com.simplegeo.client.http.exceptions.ValidLayerException;
import com.simplegeo.client.query.GeohashNearbyQuery;
import com.simplegeo.client.query.LatLonNearbyQuery;
import com.simplegeo.client.query.NearbyQuery;
View
2 src/com/simplegeo/client/query/GeohashNearbyQuery.java
@@ -30,7 +30,7 @@
import java.util.List;
-import com.simplegeo.client.exceptions.ValidLayerException;
+import com.simplegeo.client.http.exceptions.ValidLayerException;
import ch.hsr.geohash.GeoHash;
View
2 src/com/simplegeo/client/query/LatLonNearbyQuery.java
@@ -31,7 +31,7 @@
import java.util.List;
import java.util.Map;
-import com.simplegeo.client.exceptions.ValidLayerException;
+import com.simplegeo.client.http.exceptions.ValidLayerException;
/**
* A nearby query that uses latitude, longitude and radius as its search
View
2 src/com/simplegeo/client/query/NearbyQuery.java
@@ -32,7 +32,7 @@
import java.util.List;
import java.util.Map;
-import com.simplegeo.client.exceptions.ValidLayerException;
+import com.simplegeo.client.http.exceptions.ValidLayerException;
import com.simplegeo.client.utilities.SimpleGeoUtilities;
/**
View
50 ...implegeo/client/test/ModelHelperTest.java → ...mplegeo/client/utilities/ModelHelper.java
@@ -26,7 +26,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-package com.simplegeo.client.test;
+package com.simplegeo.client.utilities;
import java.util.ArrayList;
import java.util.List;
@@ -35,29 +35,25 @@
import org.json.JSONException;
import junit.framework.*;
-//import android.test.AndroidTestCase;
-import com.simplegeo.client.test.TestEnvironment;
import com.simplegeo.client.geojson.GeoJSONObject;
import com.simplegeo.client.model.DefaultRecord;
import com.simplegeo.client.model.GeoJSONRecord;
import com.simplegeo.client.model.IRecord;
/**
+ * A simple class that can create random records. This is mainly used for testing
+ * purposes.
+ *
* @author Derek Smith
- *
*/
-public class ModelHelperTest extends TestCase {
-
- private static final String TAG = ModelHelperTest.class.getName();
+public class ModelHelper extends TestCase {
- public void testTestsAreAlive() {
- assertTrue("Tests exist!", true);
- }
-
- public DefaultRecord getRandomDefaultRecord() throws Exception {
+ private static final String TAG = ModelHelper.class.getName();
+
+ public static DefaultRecord getRandomDefaultRecord() {
- DefaultRecord record = new DefaultRecord(getRandomRecordId(), TestEnvironment.getLayer(), "object");
+ DefaultRecord record = new DefaultRecord(getRandomRecordId(), "random_layer", "object");
record.setLatitude(getRandomLatitude());
record.setLongitude(getRandomLongitude());
@@ -66,31 +62,31 @@ public DefaultRecord getRandomDefaultRecord() throws Exception {
public static void waitForWrite() {
try {
- Thread.sleep(3000);
+ Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println(TAG+"unable to sleep for 5 seconds");
}
}
- public List<IRecord> getRandomDefaultRecordList(int length) throws Exception {
+ public static List<IRecord> getRandomDefaultRecordList(int length) throws Exception {
List<IRecord> list = new ArrayList<IRecord>(length);
for(int i = 0; i < length; i++)
- list.add(getRandomDefaultRecord());
+ list.add(ModelHelper.getRandomDefaultRecord());
return list;
}
- public GeoJSONRecord getRandomGeoJSONRecord() throws Exception {
+ public static GeoJSONRecord getRandomGeoJSONRecord() throws Exception {
- GeoJSONRecord record = new GeoJSONRecord(getRandomRecordId(), TestEnvironment.getLayer(), "object");
+ GeoJSONRecord record = new GeoJSONRecord(getRandomRecordId(), "random_layer", "object");
record.setLatitude(getRandomLatitude());
record.setLongitude(getRandomLongitude());
return record;
}
- public GeoJSONRecord getRandomGeoJSONRecordList(int length) throws Exception {
+ public static GeoJSONRecord getRandomGeoJSONRecordList(int length) throws Exception {
GeoJSONRecord bigGeoJSONRecord = new GeoJSONRecord("FeatureCollection");
@@ -107,7 +103,7 @@ public GeoJSONRecord getRandomGeoJSONRecordList(int length) throws Exception {
return bigGeoJSONRecord;
}
- public boolean equals(IRecord recordOne, IRecord recordTwo) {
+ public static boolean equals(IRecord recordOne, IRecord recordTwo) {
boolean areEqual = true;
areEqual &= roundDouble(recordOne.getLatitude()) == roundDouble(recordTwo.getLatitude());
@@ -119,7 +115,7 @@ public boolean equals(IRecord recordOne, IRecord recordTwo) {
return areEqual;
}
- public boolean equals(List<IRecord> recordsOne, List<IRecord> recordsTwo) {
+ public static boolean equals(List<IRecord> recordsOne, List<IRecord> recordsTwo) {
int length = recordsOne.size();
boolean areEqual = length == recordsTwo.size();
@@ -129,7 +125,7 @@ public boolean equals(List<IRecord> recordsOne, List<IRecord> recordsTwo) {
return areEqual;
}
- public boolean equals(List<IRecord> records, GeoJSONObject geoJSONObject) {
+ public static boolean equals(List<IRecord> records, GeoJSONObject geoJSONObject) {
int recordSize = records.size();
try {
@@ -164,19 +160,19 @@ public boolean equals(List<IRecord> records, GeoJSONObject geoJSONObject) {
return false;
}
- private String getRandomRecordId() {
- return String.format("testing-%d", (int)(Math.random() * 10000.0));
+ public static String getRandomRecordId() {
+ return String.format("testing-%d", (int)(Math.random() * 1000000.0));
}
- private double getRandomLatitude() {
+ public static double getRandomLatitude() {
return Math.random() * 90.0 * (((int)Math.random() * 2) == 0 ? (-1.0) : (1.0));
}
- private double getRandomLongitude() {
+ public static double getRandomLongitude() {
return Math.random() * 180.0 * (((int)Math.random() * 2) == 0 ? (-1.0) : (1.0));
}
- private double roundDouble(double d) {
+ public static double roundDouble(double d) {
int c = 10;
int temp=(int)((d*Math.pow(10,c)));
return (((double)temp)/Math.pow(10,c));
View
73 tests/src/com/simplegeo/client/SimpleGeoClientTest.java
@@ -1,5 +1,30 @@
/**
- * Copyright 2010 SimpleGeo. All rights reserved.
+ * Copyright (c) 2009-2010, SimpleGeo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer. Redistributions
+ * in binary form must reproduce the above copyright notice, this list of
+ * conditions and the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ *
+ * Neither the name of the SimpleGeo nor the names of its contributors may
+ * be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.simplegeo.client;
@@ -10,15 +35,15 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
+import junit.framework.TestCase;
+
import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import ch.hsr.geohash.GeoHash;
-import com.simplegeo.client.SimpleGeoClient;
-import com.simplegeo.client.test.TestEnvironment;
import com.simplegeo.client.SimpleGeoClient.Handler;
import com.simplegeo.client.encoder.GeoJSONEncoder;
import com.simplegeo.client.geojson.GeoJSONObject;
@@ -31,9 +56,10 @@
import com.simplegeo.client.query.GeohashNearbyQuery;
import com.simplegeo.client.query.HistoryQuery;
import com.simplegeo.client.query.LatLonNearbyQuery;
-import com.simplegeo.client.test.ModelHelperTest;
+import com.simplegeo.client.test.TestEnvironment;
+import com.simplegeo.client.utilities.ModelHelper;
-public class SimpleGeoClientTest extends ModelHelperTest {
+public class SimpleGeoClientTest extends TestCase {
private DefaultRecord defaultRecord;
private GeoJSONRecord feature;
@@ -44,14 +70,14 @@ public void setUp() throws Exception {
SimpleGeoClient.getInstance().getHttpClient().setToken(TestEnvironment.getKey(), TestEnvironment.getSecret());
- defaultRecord = getRandomDefaultRecord();
+ defaultRecord = ModelHelper.getRandomDefaultRecord();
defaultRecord.setObjectProperty("name", "derek");
- feature = getRandomGeoJSONRecord();
+ feature = ModelHelper.getRandomGeoJSONRecord();
feature.setObjectProperty("mickey", "mouse");
- defaultRecordList = getRandomDefaultRecordList(10);
- featureCollection = getRandomGeoJSONRecordList(10);
+ defaultRecordList = ModelHelper.getRandomDefaultRecordList(10);
+ featureCollection = ModelHelper.getRandomGeoJSONRecordList(10);
}
@@ -85,19 +111,19 @@ public void testRetrieveAndUpdateRecord() {
// null means that the object was successful
Object nothing = locationService.update(defaultRecord);
assertNull("Should return a null value", nothing);
- ModelHelperTest.waitForWrite();
+ ModelHelper.waitForWrite();
nothing = locationService.update((GeoJSONObject)feature);
assertNull("Should return a null value", nothing);
- ModelHelperTest.waitForWrite();
+ ModelHelper.waitForWrite();
IRecord retrievedRecord = (DefaultRecord)locationService.retrieve(defaultRecord);
assertTrue(DefaultRecord.class.isInstance(retrievedRecord));
- assertTrue(equals(retrievedRecord, defaultRecord));
+ assertTrue(ModelHelper.equals(retrievedRecord, defaultRecord));
retrievedRecord = (GeoJSONRecord)locationService.retrieve(feature);
assertTrue("Should be an instance of GeoJSONRecord", GeoJSONRecord.class.isInstance(retrievedRecord));
- assertTrue("The two records should be equal", equals(retrievedRecord, feature));
+ assertTrue("The two records should be equal", ModelHelper.equals(retrievedRecord, feature));
nothing = locationService.update(defaultRecordList);
assertNull("Should return a null value", nothing);
@@ -149,10 +175,10 @@ public void testNearby() throws Exception {
try {
locationService.update(defaultRecordList);
- ModelHelperTest.waitForWrite();
+ ModelHelper.waitForWrite();
locationService.update((IRecord)featureCollection);
- ModelHelperTest.waitForWrite();
+ ModelHelper.waitForWrite();
} catch (ClientProtocolException e) {
assertTrue(e.getLocalizedMessage(), false);
@@ -205,21 +231,20 @@ public void testDeleteRecord() {
Object nothing = locationService.update(defaultRecord);
assertNull("A null value should be returned", nothing);
- SimpleGeoClientTest.waitForWrite();
+ ModelHelper.waitForWrite();
IRecord r = (IRecord)locationService.retrieve(defaultRecord);
assertNotNull("The record should be retrievable", r);
- assertTrue("The records should be equal", equals(defaultRecord, r));
+ assertTrue("The records should be equal", ModelHelper.equals(defaultRecord, r));
locationService.delete(defaultRecord);
nothing = locationService.update((GeoJSONObject)feature);
assertNull("A null value should be returned", nothing);
- SimpleGeoClientTest.waitForWrite();
r = (IRecord)locationService.retrieve(feature);
assertNotNull("The record should be retrievable", r);
- assertTrue("The records should be equal", equals(feature, r));
+ assertTrue("The records should be equal", ModelHelper.equals(feature, r));
locationService.delete(feature);
@@ -354,7 +379,7 @@ public void testHistory() throws Exception{
locationService.update(defaultRecord);
}
- ModelHelperTest.waitForWrite();
+ ModelHelper.waitForWrite();
} catch (ClientProtocolException e) {
assertTrue(e.getLocalizedMessage(), false);
@@ -363,7 +388,7 @@ public void testHistory() throws Exception{
}
try {
- ModelHelperTest.waitForWrite();
+ ModelHelper.waitForWrite();
HistoryQuery query = new HistoryQuery(recordId, TestEnvironment.getLayer(), 2);
GeoJSONObject jsonObject = (GeoJSONObject)locationService.history(query);
assertNotNull(jsonObject);
@@ -409,7 +434,7 @@ public void testFutureRetrieval() {
IRecord returnedRecord = (IRecord)updateTaskOne.get();
assertNull(returnedRecord);
- SimpleGeoClientTest.waitForWrite();
+ ModelHelper.waitForWrite();
FutureTask<Object> retrieveTaskOne = (FutureTask<Object>)locationService.retrieve(defaultRecord);
assertTrue(FutureTask.class.isInstance(retrieveTaskOne));
@@ -421,12 +446,12 @@ public void testFutureRetrieval() {
returnedRecord = ((List<IRecord>)retrieveTaskOne.get()).get(0);
assertNotNull(returnedRecord);
assertTrue(DefaultRecord.class.isInstance(returnedRecord));
- assertTrue(equals(returnedRecord, defaultRecord));
+ assertTrue(ModelHelper.equals(returnedRecord, defaultRecord));
returnedRecord = (IRecord)retrieveTaskTwo.get();
assertNotNull(returnedRecord);
assertTrue(GeoJSONRecord.class.isInstance(returnedRecord));
- assertTrue(equals(returnedRecord, feature));
+ assertTrue(ModelHelper.equals(returnedRecord, feature));
} catch (ClientProtocolException e) {
assertFalse(e.getLocalizedMessage(), true);
View
17 tests/src/com/simplegeo/client/encoder/GeoJSONEncoderTest.java
@@ -6,40 +6,39 @@
import java.util.List;
-import org.json.JSONException;
+import junit.framework.TestCase;
import com.simplegeo.client.model.DefaultRecord;
import com.simplegeo.client.model.GeoJSONRecord;
-import com.simplegeo.client.model.IRecord;
-import com.simplegeo.client.test.ModelHelperTest;
+import com.simplegeo.client.utilities.ModelHelper;
/**
* @author Derek Smith
*
*/
-public class GeoJSONEncoderTest extends ModelHelperTest {
+public class GeoJSONEncoderTest extends TestCase {
public void testRecordToGeoJSON() throws Exception {
- DefaultRecord record = getRandomDefaultRecord();
+ DefaultRecord record = ModelHelper.getRandomDefaultRecord();
record.getProperties().put("name", "derek");
GeoJSONRecord jsonRecord = GeoJSONEncoder.getGeoJSONRecord(record);
assertNotNull(String.format("GeoJSON record %s should not be null", jsonRecord.toString()), jsonRecord);
- assertTrue(String.format("Record %s does not equal %s", record, jsonRecord), equals(record, jsonRecord));
+ assertTrue(String.format("Record %s does not equal %s", record, jsonRecord), ModelHelper.equals(record, jsonRecord));
jsonRecord = (GeoJSONRecord)GeoJSONEncoder.getGeoJSONRecord((DefaultRecord)null);
assertNull(String.format("GeoJSON record %s should be null", jsonRecord), jsonRecord);
}
public void testGeoJSONToRecord() throws Exception {
- GeoJSONRecord jsonRecord = getRandomGeoJSONRecord();
+ GeoJSONRecord jsonRecord = ModelHelper.getRandomGeoJSONRecord();
jsonRecord.setObjectProperty("name", "derek");
DefaultRecord record = GeoJSONEncoder.getRecord(jsonRecord);
assertNotNull(record);
- assertTrue(equals(record, jsonRecord));
+ assertTrue(ModelHelper.equals(record, jsonRecord));
record = GeoJSONEncoder.getRecord(null);
assertNull(record);
@@ -48,7 +47,7 @@ public void testGeoJSONToRecord() throws Exception {
public void testMultiGeoJSONToRecords() throws Exception {
- GeoJSONRecord bigGeoJSONRecord = getRandomGeoJSONRecordList(10);
+ GeoJSONRecord bigGeoJSONRecord = ModelHelper.getRandomGeoJSONRecordList(10);
List<DefaultRecord> defaultRecords = GeoJSONEncoder.getRecords(bigGeoJSONRecord);
View
11 tests/src/com/simplegeo/client/model/LayerTest.java
@@ -29,23 +29,24 @@
package com.simplegeo.client.model;
import java.io.IOException;
-import java.util.ArrayList;
import java.util.List;
+import junit.framework.TestCase;
+
import org.apache.http.client.ClientProtocolException;
import com.simplegeo.client.SimpleGeoClient;
import com.simplegeo.client.query.LatLonNearbyQuery;
import com.simplegeo.client.query.NearbyQuery;
import com.simplegeo.client.test.TestEnvironment;
-import com.simplegeo.client.test.ModelHelperTest;
+import com.simplegeo.client.utilities.ModelHelper;
/**
* @author Derek Smith
*
*/
-public class LayerTest extends ModelHelperTest {
+public class LayerTest extends TestCase {
private Layer testingLayer;
@@ -81,7 +82,7 @@ public void testLayerRetrieval() throws Exception {
double longitude = 27.0;
NearbyQuery query = new LatLonNearbyQuery(latitude, longitude, 1.0, testingLayer.getName());
for(int i = 0; i < 10; i++) {
- DefaultRecord record = getRandomDefaultRecord();
+ DefaultRecord record = ModelHelper.getRandomDefaultRecord();
record.setLatitude(latitude);
record.setLongitude(longitude);
testingLayer.add(record);
@@ -90,7 +91,7 @@ public void testLayerRetrieval() throws Exception {
try {
testingLayer.update();
- ModelHelperTest.waitForWrite();
+ ModelHelper.waitForWrite();
GeoJSONRecord geojson = (GeoJSONRecord)testingLayer.nearby(query);
assertNotNull(geojson);
assertTrue(geojson.getFeatures().length() >= 10);

0 comments on commit 8785c7d

Please sign in to comment.