Skip to content

Commit 889aa88

Browse files
committed
Add unit tests for successor
1 parent a0e6283 commit 889aa88

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

firebase-database/src/androidTest/java/com/google/firebase/database/QueryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ public void onCancelled(DatabaseError error) {}
771771
}
772772

773773
@Test
774-
public void setLimitEnsureChildRemovedAndChildAddedHitWhenLimitIsHitFromFrontWithStartAfter()
774+
public void setLimitEnsureChildRemovedAndChildAddedHitWhenDataEntersLimitWithStartAfter()
775775
throws DatabaseException, TestFailure, ExecutionException, TimeoutException,
776776
InterruptedException {
777777
DatabaseReference ref = IntegrationTestHelpers.getRandomNode();

firebase-database/src/main/java/com/google/firebase/database/core/utilities/PushIdGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static final String successor(String key) {
6969
Validation.validateNullableKey(key);
7070
Integer num = tryParseInt(key);
7171
if (num != null) {
72-
if (Long.valueOf(num) + 1L > Integer.MAX_VALUE) {
72+
if (num + 1L > Integer.MAX_VALUE) {
7373
// See https://firebase.google.com/docs/database/web/lists-of-data#data-order
7474
return String.valueOf(MIN_PUSH_CHAR);
7575
}
@@ -86,7 +86,6 @@ public static final String successor(String key) {
8686
}
8787

8888
int i = next.length() - 1;
89-
9089
while (i >= 0 && next.charAt(i) == MAX_PUSH_CHAR) {
9190
i--;
9291
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.database;
16+
17+
import static com.google.firebase.database.snapshot.ChildKey.MAX_KEY_NAME;
18+
import static org.junit.Assert.assertEquals;
19+
20+
import com.google.firebase.database.core.utilities.PushIdGenerator;
21+
import org.codehaus.plexus.util.StringUtils;
22+
import org.junit.Test;
23+
import org.robolectric.RobolectricTestRunner;
24+
import org.robolectric.annotation.Config;
25+
26+
@org.junit.runner.RunWith(RobolectricTestRunner.class)
27+
@Config(manifest = Config.NONE)
28+
public class PushIdGeneratorTest {
29+
30+
private static final char MIN_PUSH_CHAR = '-';
31+
32+
private static final char MAX_PUSH_CHAR = 'z';
33+
34+
private static final int MAX_KEY_LEN = 786;
35+
36+
@Test
37+
public void testSuccessorSpecialValue() {
38+
assertEquals(
39+
PushIdGenerator.successor(String.valueOf(Integer.MAX_VALUE)),
40+
Character.toString(MIN_PUSH_CHAR));
41+
assertEquals(
42+
PushIdGenerator.successor(
43+
StringUtils.repeat(Character.toString(MAX_PUSH_CHAR), MAX_KEY_LEN)),
44+
MAX_KEY_NAME);
45+
}
46+
47+
@Test
48+
public void testSuccessorBasic() {
49+
assertEquals(PushIdGenerator.successor("abc"), "abc" + MIN_PUSH_CHAR);
50+
assertEquals(
51+
PushIdGenerator.successor(
52+
"abc"
53+
+ StringUtils.repeat(
54+
Character.toString(MAX_PUSH_CHAR), MAX_KEY_LEN - "abc".length())),
55+
"abd");
56+
assertEquals(
57+
PushIdGenerator.successor("abc" + MIN_PUSH_CHAR), "abc" + MIN_PUSH_CHAR + MIN_PUSH_CHAR);
58+
}
59+
}

0 commit comments

Comments
 (0)