Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

Commit

Permalink
Add cache folder to InFileObjetPersisters. Customizable, tests and in…
Browse files Browse the repository at this point in the history
…heritance. Issue #103 and issue #98.
  • Loading branch information
stephanenicolas committed Jun 1, 2013
1 parent 65d085c commit d0c2991
Show file tree
Hide file tree
Showing 88 changed files with 980 additions and 514 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

//Thanks to http://stackoverflow.com/questions/2300029/servicetestcaset-getservice
@SmallTest
public class GoogleHttpClientSpiceServiceTest extends
ServiceTestCase<GoogleHttpClientSpiceTestService> {
public class GoogleHttpClientSpiceServiceTest extends ServiceTestCase<GoogleHttpClientSpiceTestService> {

private static final int REQUEST_COMPLETION_TIMEOUT = 1000;
private SpiceManager spiceManager;
Expand All @@ -38,22 +37,19 @@ protected void tearDown() throws Exception {

public void test_createRequestFactory_returns_default_factory() {
Intent startIntent = new Intent();
startIntent.setClass(getContext(),
GoogleHttpClientSpiceTestService.class);
startIntent.setClass(getContext(), GoogleHttpClientSpiceTestService.class);
startService(startIntent);
assertNotNull(getService().createRequestFactory());
}

public void test_addRequest_injects_request_factory()
throws InterruptedException {
public void test_addRequest_injects_request_factory() throws InterruptedException {
// given
spiceManager.start(getContext());
GoogleHttpClientSpiceRequestStub googleHttpClientSpiceRequest = new GoogleHttpClientSpiceRequestStub(
Weather.class);

// when
spiceManager.execute(googleHttpClientSpiceRequest,
new RequestListenerStub<Weather>());
spiceManager.execute(googleHttpClientSpiceRequest, new RequestListenerStub<Weather>());
googleHttpClientSpiceRequest.await(REQUEST_COMPLETION_TIMEOUT);

// test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.octo.android.robospice.googlehttpclient.test;

import java.io.File;

import roboguice.util.temp.Ln;
import android.app.Application;

import com.octo.android.robospice.GoogleHttpClientSpiceService;
import com.octo.android.robospice.persistence.CacheManager;
import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.googlehttpclient.json.JacksonObjectPersisterFactory;

public class GoogleHttpClientSpiceTestService extends
GoogleHttpClientSpiceService {
public class GoogleHttpClientSpiceTestService extends GoogleHttpClientSpiceService {

@Override
public CacheManager createCacheManager(Application application) {
CacheManager cacheManager = new CacheManager();
JacksonObjectPersisterFactory jacksonObjectPersisterFactory = new JacksonObjectPersisterFactory(
application);
cacheManager.addPersister(jacksonObjectPersisterFactory);
try {
JacksonObjectPersisterFactory jacksonObjectPersisterFactory = new JacksonObjectPersisterFactory(
application, new File("/"));
cacheManager.addPersister(jacksonObjectPersisterFactory);
} catch (CacheCreationException e) {
Ln.e(e);
}
return cacheManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,9 @@ public int hashCode() {
result = prime * result + (humidity == null ? 0 : humidity.hashCode());
result = prime * result + (pressure == null ? 0 : pressure.hashCode());
result = prime * result + (temp == null ? 0 : temp.hashCode());
result = prime * result
+ (temp_unit == null ? 0 : temp_unit.hashCode());
result = prime * result
+ (weather_code == null ? 0 : weather_code.hashCode());
result = prime * result
+ (weather_text == null ? 0 : weather_text.hashCode());
result = prime * result + (temp_unit == null ? 0 : temp_unit.hashCode());
result = prime * result + (weather_code == null ? 0 : weather_code.hashCode());
result = prime * result + (weather_text == null ? 0 : weather_text.hashCode());
result = prime * result + (wind == null ? 0 : wind.hashCode());
return result;
}
Expand Down Expand Up @@ -159,10 +156,8 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "CurrenWeather [humidity=" + humidity + ", pressure="
+ pressure + ", temp=" + temp + ", temp_unit=" + temp_unit
+ ", weather_code=" + weather_code + ", weather_text="
+ weather_text + ", wind=" + wind + "]";
return "CurrenWeather [humidity=" + humidity + ", pressure=" + pressure + ", temp=" + temp + ", temp_unit="
+ temp_unit + ", weather_code=" + weather_code + ", weather_text=" + weather_text + ", wind=" + wind + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public void setWind(List<Wind> wind) {

@Override
public String toString() {
return "Day [weather_code=" + weather_code + ", weather_text="
+ weather_text + ", wind=" + wind + "]";
return "Day [weather_code=" + weather_code + ", weather_text=" + weather_text + ", wind=" + wind + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,10 @@ public int hashCode() {
int result = 1;
result = prime * result + (date == null ? 0 : date.hashCode());
result = prime * result + (day == null ? 0 : day.hashCode());
result = prime * result
+ (day_max_temp == null ? 0 : day_max_temp.hashCode());
result = prime * result + (day_max_temp == null ? 0 : day_max_temp.hashCode());
result = prime * result + (night == null ? 0 : night.hashCode());
result = prime * result
+ (night_min_temp == null ? 0 : night_min_temp.hashCode());
result = prime * result
+ (temp_unit == null ? 0 : temp_unit.hashCode());
result = prime * result + (night_min_temp == null ? 0 : night_min_temp.hashCode());
result = prime * result + (temp_unit == null ? 0 : temp_unit.hashCode());
return result;
}

Expand Down Expand Up @@ -141,9 +138,8 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "Forecast [date=" + date + ", day=" + day + ", day_max_temp="
+ day_max_temp + ", night=" + night + ", night_min_temp="
+ night_min_temp + ", temp_unit=" + temp_unit + "]";
return "Forecast [date=" + date + ", day=" + day + ", day_max_temp=" + day_max_temp + ", night=" + night
+ ", night_min_temp=" + night_min_temp + ", temp_unit=" + temp_unit + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public String toString() {
sb.append("id=").append(id);
sb.append(", ").append("string=").append(string);
sb.append(", ").append("millis=").append(millis);
SimpleDateFormat dateFormatter = new SimpleDateFormat(
"MM/dd/yyyy HH:mm:ss.S");
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.S");
sb.append(", ").append("date=").append(dateFormatter.format(date));
sb.append(", ").append("even=").append(even);
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public void setForecast(List<Forecast> forecast) {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ (currenWeather == null ? 0 : currenWeather.hashCode());
result = prime * result + (currenWeather == null ? 0 : currenWeather.hashCode());
result = prime * result + (forecast == null ? 0 : forecast.hashCode());
return result;
}
Expand Down Expand Up @@ -71,8 +70,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "Weather [currenWeather=" + currenWeather + ", forecast="
+ forecast + "]";
return "Weather [currenWeather=" + currenWeather + ", forecast=" + forecast + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public int hashCode() {
int result = 1;
result = prime * result + (dir == null ? 0 : dir.hashCode());
result = prime * result + (speed == null ? 0 : speed.hashCode());
result = prime * result
+ (wind_unit == null ? 0 : wind_unit.hashCode());
result = prime * result + (wind_unit == null ? 0 : wind_unit.hashCode());
return result;
}

Expand Down Expand Up @@ -83,8 +82,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "Wind [dir=" + dir + ", speed=" + speed + ", wind_unit="
+ wind_unit + "]";
return "Wind [dir=" + dir + ", speed=" + speed + ", wind_unit=" + wind_unit + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import com.octo.android.robospice.googlehttpclient.test.model.Weather;
import com.octo.android.robospice.request.googlehttpclient.GoogleHttpClientSpiceRequest;

public class GoogleHttpClientSpiceRequestStub extends
GoogleHttpClientSpiceRequest<Weather> {
public class GoogleHttpClientSpiceRequestStub extends GoogleHttpClientSpiceRequest<Weather> {
private ReentrantLock reentrantLock = new ReentrantLock();
private Condition loadDataFromNetworkHasBeenExecuted = reentrantLock
.newCondition();
private Condition loadDataFromNetworkHasBeenExecuted = reentrantLock.newCondition();

public GoogleHttpClientSpiceRequestStub(Class<Weather> clazz) {
super(clazz);
Expand All @@ -31,8 +29,7 @@ public Weather loadDataFromNetwork() throws Exception {
public void await(long timeout) throws InterruptedException {
try {
reentrantLock.lock();
loadDataFromNetworkHasBeenExecuted.await(timeout,
TimeUnit.MILLISECONDS);
loadDataFromNetworkHasBeenExecuted.await(timeout, TimeUnit.MILLISECONDS);
} finally {
reentrantLock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public void onRequestSuccess(T arg0) {
}

protected void checkIsExectuedInUIThread() {
if (Looper.myLooper() != null
&& Looper.myLooper() == Looper.getMainLooper()) {
if (Looper.myLooper() != null && Looper.myLooper() == Looper.getMainLooper()) {
isExecutedInUIThread = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import android.app.Application;
import android.test.suitebuilder.annotation.SmallTest;

import com.octo.android.robospice.persistence.exception.CacheCreationException;

@SmallTest
public class InFileWeatherPersisterGsonTest extends
JsonObjectPersisterFactoryTest {
public class InFileWeatherPersisterGsonTest extends JsonObjectPersisterFactoryTest {

@Override
protected JsonObjectPersisterFactory getJsonObjectPersisterFactory(
Application application) {
protected JsonObjectPersisterFactory getJsonObjectPersisterFactory(Application application)
throws CacheCreationException {
return new GsonObjectPersisterFactory(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import android.app.Application;
import android.test.suitebuilder.annotation.SmallTest;

import com.octo.android.robospice.persistence.exception.CacheCreationException;

@SmallTest
public class InFileWeatherPersisterJackson2Test extends
JsonObjectPersisterFactoryTest {
public class InFileWeatherPersisterJackson2Test extends JsonObjectPersisterFactoryTest {

@Override
protected JsonObjectPersisterFactory getJsonObjectPersisterFactory(
Application application) {
protected JsonObjectPersisterFactory getJsonObjectPersisterFactory(Application application)
throws CacheCreationException {
return new Jackson2ObjectPersisterFactory(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import android.app.Application;
import android.test.suitebuilder.annotation.SmallTest;

import com.octo.android.robospice.persistence.exception.CacheCreationException;

@SmallTest
public class InFileWeatherPersisterJacksonTest extends
JsonObjectPersisterFactoryTest {
public class InFileWeatherPersisterJacksonTest extends JsonObjectPersisterFactoryTest {

@Override
protected JsonObjectPersisterFactory getJsonObjectPersisterFactory(
Application application) {
protected JsonObjectPersisterFactory getJsonObjectPersisterFactory(Application application)
throws CacheCreationException {
return new JacksonObjectPersisterFactory(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.octo.android.robospice.googlehttpclient.test.model.Weather;
import com.octo.android.robospice.googlehttpclient.test.model.WeatherResult;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.CacheCreationException;

@SmallTest
public abstract class JsonObjectPersisterFactoryTest extends InstrumentationTestCase {
Expand All @@ -28,10 +29,11 @@ protected void setUp() throws Exception {
super.setUp();
Application application = (Application) getInstrumentation().getTargetContext().getApplicationContext();
JsonObjectPersisterFactory factory = getJsonObjectPersisterFactory(application);
inFileObjectPersister = factory.createObjectPersister(WeatherResult.class);
inFileObjectPersister = factory.createInFileObjectPersister(WeatherResult.class, null);
}

protected abstract JsonObjectPersisterFactory getJsonObjectPersisterFactory(Application application);
protected abstract JsonObjectPersisterFactory getJsonObjectPersisterFactory(Application application)
throws CacheCreationException;

@Override
protected void tearDown() throws Exception {
Expand All @@ -49,7 +51,8 @@ public void test_saveDataAndReturnData() throws Exception {
WeatherResult weatherRequestStatus = buildWeather(TEST_TEMP, TEST_TEMP_UNIT);

// WHEN
WeatherResult weatherReturned = inFileObjectPersister.saveDataToCacheAndReturnData(weatherRequestStatus, "weather.json");
WeatherResult weatherReturned = inFileObjectPersister.saveDataToCacheAndReturnData(weatherRequestStatus,
"weather.json");

// THEN
assertEquals(TEST_TEMP, weatherReturned.getWeather().getCurren_weather().get(0).getTemp());
Expand All @@ -62,7 +65,8 @@ public void test_loadDataFromCache_no_expiracy() throws Exception {
inFileObjectPersister.saveDataToCacheAndReturnData(weatherRequestStatus, FILE_NAME);

// WHEN
WeatherResult weatherReturned = inFileObjectPersister.loadDataFromCache(FILE_NAME, DurationInMillis.ALWAYS_RETURNED);
WeatherResult weatherReturned = inFileObjectPersister.loadDataFromCache(FILE_NAME,
DurationInMillis.ALWAYS_RETURNED);

// THEN
assertEquals(TEST_TEMP, weatherReturned.getWeather().getCurren_weather().get(0).getTemp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ public HttpRequestFactory createRequestFactory() {
}

@Override
public void addRequest(CachedSpiceRequest<?> request,
Set<RequestListener<?>> listRequestListener) {
public void addRequest(CachedSpiceRequest<?> request, Set<RequestListener<?>> listRequestListener) {
if (request.getSpiceRequest() instanceof GoogleHttpClientSpiceRequest) {
((GoogleHttpClientSpiceRequest<?>) request.getSpiceRequest())
.setHttpRequestFactory(httpRequestFactory);
((GoogleHttpClientSpiceRequest<?>) request.getSpiceRequest()).setHttpRequestFactory(httpRequestFactory);
}
super.addRequest(request, listRequestListener);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package com.octo.android.robospice.persistence.googlehttpclient.json;

import java.io.File;
import java.util.List;

import android.app.Application;

import com.google.api.client.json.gson.GsonFactory;
import com.octo.android.robospice.persistence.exception.CacheCreationException;

/**
* Allows to serialize objects using the google http java client gson module.
* @author sni
*/
public class GsonObjectPersisterFactory extends JsonObjectPersisterFactory {

public GsonObjectPersisterFactory(Application application,
List<Class<?>> listHandledClasses) {
super(application, new GsonFactory(), listHandledClasses);
public GsonObjectPersisterFactory(Application application, File cacheFolder) throws CacheCreationException {
super(application, new GsonFactory(), cacheFolder);
}

public GsonObjectPersisterFactory(Application application) {
this(application, null);
public GsonObjectPersisterFactory(Application application, List<Class<?>> listHandledClasses, File cacheFolder)
throws CacheCreationException {
super(application, new GsonFactory(), listHandledClasses, cacheFolder);
}

public GsonObjectPersisterFactory(Application application, List<Class<?>> listHandledClasses)
throws CacheCreationException {
super(application, new GsonFactory(), listHandledClasses);
}

public GsonObjectPersisterFactory(Application application) throws CacheCreationException {
super(application, new GsonFactory());
}
}
Loading

0 comments on commit d0c2991

Please sign in to comment.