|
| 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