use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.
the class ValuesTest method testValueEquality.
@Test
public void testValueEquality() {
GeoPoint geoPoint1 = new GeoPoint(1, 0);
GeoPoint geoPoint2 = new GeoPoint(0, 2);
Timestamp timestamp1 = new Timestamp(date1);
Timestamp timestamp2 = new Timestamp(date2);
new EqualsTester().addEqualityGroup(wrap(true), wrap(true)).addEqualityGroup(wrap(false), wrap(false)).addEqualityGroup(wrap((Object) null), wrap(Values.NULL_VALUE)).addEqualityGroup(wrap(0.0 / 0.0), wrap(Double.longBitsToDouble(0x7ff8000000000000L)), wrap(Values.NAN_VALUE)).addEqualityGroup(wrap(-0.0)).addEqualityGroup(wrap(0.0)).addEqualityGroup(wrap(1), wrap(1)).addEqualityGroup(wrap(1.0), wrap(1.0)).addEqualityGroup(wrap(1.1), wrap(1.1)).addEqualityGroup(wrap(blob(0, 1, 2)), wrap(blob(0, 1, 2))).addEqualityGroup(wrap(blob(0, 1))).addEqualityGroup(wrap("string"), wrap("string")).addEqualityGroup(wrap("strin")).addEqualityGroup(wrap("e\u0301b")).addEqualityGroup(wrap("\u00e9a")).addEqualityGroup(wrap(date1), wrap(timestamp1)).addEqualityGroup(wrap(timestamp2)).addEqualityGroup(wrap(ServerTimestamps.valueOf(new Timestamp(date1), null)), wrap(ServerTimestamps.valueOf(new Timestamp(date1), null))).addEqualityGroup(wrap(ServerTimestamps.valueOf(new Timestamp(date2), null))).addEqualityGroup(wrap(geoPoint1), wrap(new GeoPoint(1, 0))).addEqualityGroup(wrap(geoPoint2)).addEqualityGroup(wrap(ref("coll/doc1")), wrap(wrapRef(dbId("project"), key("coll/doc1")))).addEqualityGroup(wrap(wrapRef(dbId("project", "bar"), key("coll/doc2")))).addEqualityGroup(wrap(wrapRef(dbId("project", "baz"), key("coll/doc2")))).addEqualityGroup(wrap(Arrays.asList("foo", "bar")), wrap(Arrays.asList("foo", "bar"))).addEqualityGroup(wrap(Arrays.asList("foo", "bar", "baz"))).addEqualityGroup(wrap(Arrays.asList("foo"))).addEqualityGroup(wrap(map("bar", 1, "foo", 2)), wrap(map("foo", 2, "bar", 1))).addEqualityGroup(wrap(map("bar", 2, "foo", 1))).addEqualityGroup(wrap(map("bar", 1))).addEqualityGroup(wrap(map("foo", 1))).testEquals();
}
use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.
the class MutationTest method testAppliesLocalServerTimestampTransformsToDocuments.
@Test
public void testAppliesLocalServerTimestampTransformsToDocuments() {
Map<String, Object> data = map("foo", map("bar", "bar-value"), "baz", "baz-value");
MutableDocument transformedDoc = doc("collection/key", 1, data);
Timestamp timestamp = Timestamp.now();
Mutation transform = patchMutation("collection/key", map("foo.bar", FieldValue.serverTimestamp()));
transform.applyToLocalView(transformedDoc, /* previousMask= */
null, timestamp);
// Server timestamps aren't parsed, so we manually insert it.
ObjectValue expectedData = wrapObject(map("foo", map("bar", "<server-timestamp>"), "baz", "baz-value"));
Value fieldValue = ServerTimestamps.valueOf(timestamp, wrap("bar-value"));
expectedData.set(field("foo.bar"), fieldValue);
MutableDocument expectedDoc = doc("collection/key", 1, expectedData).setHasLocalMutations();
assertEquals(expectedDoc, transformedDoc);
}
use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.
the class SQLiteTargetCacheTest method testMetadataPersistedAcrossRestarts.
@Test
public void testMetadataPersistedAcrossRestarts() {
String name = "test-targetCache-restarts";
SQLitePersistence db1 = PersistenceTestHelpers.createSQLitePersistence(name);
TargetCache targetCache1 = db1.getTargetCache();
assertEquals(0, targetCache1.getHighestListenSequenceNumber());
long originalSequenceNumber = 1234;
int targetId = 5;
SnapshotVersion snapshotVersion = new SnapshotVersion(new Timestamp(1, 2));
Query query = query("rooms");
TargetData targetData = new TargetData(query.toTarget(), targetId, originalSequenceNumber, QueryPurpose.LISTEN);
db1.runTransaction("add query data", () -> {
targetCache1.addTargetData(targetData);
targetCache1.setLastRemoteSnapshotVersion(snapshotVersion);
});
db1.shutdown();
SQLitePersistence db2 = PersistenceTestHelpers.createSQLitePersistence(name);
db2.runTransaction("verify sequence number", () -> {
long newSequenceNumber = db2.getReferenceDelegate().getCurrentSequenceNumber();
assertTrue(newSequenceNumber > originalSequenceNumber);
});
TargetCache targetCache2 = db2.getTargetCache();
assertEquals(targetId, targetCache2.getHighestTargetId());
assertEquals(snapshotVersion, targetCache2.getLastRemoteSnapshotVersion());
assertEquals(1, targetCache2.getTargetCount());
db2.shutdown();
}
use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.
the class DocumentSnapshot method getDate.
/**
* Returns the value of the field as a Date.
*
* @param field The path to the field.
* @param serverTimestampBehavior Configures the behavior for server timestamps that have not yet
* been set to their final value.
* @throws RuntimeException if the value is not a Date.
* @return The value of the field
*/
@Nullable
public Date getDate(@NonNull String field, @NonNull ServerTimestampBehavior serverTimestampBehavior) {
checkNotNull(field, "Provided field path must not be null.");
checkNotNull(serverTimestampBehavior, "Provided serverTimestampBehavior value must not be null.");
@Nullable Timestamp timestamp = getTimestamp(field, serverTimestampBehavior);
return timestamp != null ? timestamp.toDate() : null;
}
use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.
the class BundleSerializer method decodeTimestamp.
private Timestamp decodeTimestamp(String timestamp) {
try {
int dayOffset = timestamp.indexOf('T');
if (dayOffset == -1) {
throw new IllegalArgumentException("Invalid timestamp: " + timestamp);
}
int timezoneOffsetPosition = timestamp.indexOf('Z', dayOffset);
if (timezoneOffsetPosition == -1) {
timezoneOffsetPosition = timestamp.indexOf('+', dayOffset);
}
if (timezoneOffsetPosition == -1) {
timezoneOffsetPosition = timestamp.indexOf('-', dayOffset);
}
if (timezoneOffsetPosition == -1) {
throw new IllegalArgumentException("Invalid timestamp: Missing valid timezone offset: " + timestamp);
}
// Parse seconds and nanos.
String timeValue = timestamp.substring(0, timezoneOffsetPosition);
String secondValue = timeValue;
String nanoValue = "";
int pointPosition = timeValue.indexOf('.');
if (pointPosition != -1) {
secondValue = timeValue.substring(0, pointPosition);
nanoValue = timeValue.substring(pointPosition + 1);
}
Date date = timestampFormat.parse(secondValue);
long seconds = date.getTime() / MILLIS_PER_SECOND;
int nanos = nanoValue.isEmpty() ? 0 : parseNanos(nanoValue);
// Parse timezone offsets.
if (timestamp.charAt(timezoneOffsetPosition) == 'Z') {
if (timestamp.length() != timezoneOffsetPosition + 1) {
throw new IllegalArgumentException("Invalid timestamp: Invalid trailing data \"" + timestamp.substring(timezoneOffsetPosition) + "\"");
}
} else {
String offsetValue = timestamp.substring(timezoneOffsetPosition + 1);
long offset = decodeTimezoneOffset(offsetValue);
if (timestamp.charAt(timezoneOffsetPosition) == '+') {
seconds -= offset;
} else {
seconds += offset;
}
}
return new Timestamp(seconds, nanos);
} catch (ParseException e) {
throw new IllegalArgumentException("Failed to parse timestamp", e);
}
}
Aggregations