public UploaderSink(Context context, String path) throws DataSinkException { this(context, uriFromString(path)); }
private void setUploadingStatus(boolean enabled) { Log.i(TAG, "Setting uploading to " + enabled); if(enabled) { String path = getPreferenceString(R.string.uploading_path_key); if(!UploaderSink.validatePath(path)) { String error = "Target URL in preferences not valid " + "-- not starting uploading a trace"; Log.w(TAG, error); SharedPreferences.Editor editor = getPreferences().edit(); editor.putBoolean(getString(R.string.uploading_checkbox_key), false); editor.commit(); } else { if(mUploader != null) { stopUploading(); } try { mUploader = new UploaderSink(getContext(), path); } catch(DataSinkException e) { Log.w(TAG, "Unable to add uploader sink", e); return; } getVehicleManager().addSink(mUploader); } } else { stopUploading(); } }
@Before public void setUp() throws IOException, DataSinkException { FakeHttp.getFakeHttpLayer().interceptHttpRequests(false); FakeHttp.setDefaultHttpResponse(200, "dummy"); sink = new UploaderSink(RuntimeEnvironment.application, "http://localhost"); gson = new Gson(); }
public boolean onPreferenceChange(Preference preference, Object newValue) { String path = (String) newValue; if(!UploaderSink.validatePath(path)) { String error = "Invalid target URL \"" + path + "\" -- must be an absolute URL " + "with http:// prefix"; Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show(); Log.w(TAG, error); mUploadingPreference.setChecked(false); } updateSummary(preference, newValue); return true; } };
@After public void tearDown() throws IOException, DataSinkException { sink.stop(); }
@Test public void testUploadBatch() throws DataSinkException, IOException { TestUtils.pause(50); for(int i = 0; i < 25; i++) { sink.receive(message); } TestUtils.pause(1000); assertTrue(FakeHttp.httpRequestWasMade()); Type listType = new TypeToken<List<SimpleVehicleMessage>>() {}.getType(); ArrayList<SimpleVehicleMessage> messages = new ArrayList<>(); HttpPost request; while((request = (HttpPost) FakeHttp.getNextSentHttpRequest()) != null) { InputStream payload = request.getEntity().getContent(); int length = payload.available(); byte[] buffer = new byte[length]; payload.read(buffer); messages.addAll((List<SimpleVehicleMessage>)gson.fromJson(new String(buffer), listType)); } assertThat(messages, hasSize(25)); for(SimpleVehicleMessage deserializedMessage : messages) { assertThat(message, equalTo((VehicleMessage) deserializedMessage)); } } }
/** * Returns true if the path is not null and if it is a valid URI. * * @param path a URI to validate * @return true if path is a valid URI. * */ public static boolean validatePath(String path) { if(path == null) { Log.w(TAG, "Uploading path not set"); return false; } try { uriFromString(path); return true; } catch(DataSinkException e) { return false; } }