Skip to content

Introduce code formatting + git hooks #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
name: Continuous Testing

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'temurin'
- uses: gradle/gradle-build-action@v2
- run: gradle wrapper
- run: ./gradlew test
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: "11"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to 11 for the newest version of spotless, but source and target compilation is still set to 8

distribution: "temurin"
- uses: gradle/gradle-build-action@v2
- run: gradle wrapper
- run: ./gradlew check
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repos:
- repo: local
hooks:
- id: spotless
name: spotless
language: system
entry: ./gradlew spotlessApply
pass_filenames: false
always_run: true
- id: git-diff
name: git-diff
language: system
entry: git --no-pager diff --color --exit-code
always_run: true
4 changes: 4 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugins:
- prettier-plugin-java
- prettier-plugin-properties
tabWidth: 4
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# CHANGELOG

**v0.1.0:**
- Initial implementation of [the spec](https://github.com/sqids/sqids-spec)
- Packaging & cleanup

- Initial implementation of [the spec](https://github.com/sqids/sqids-spec)
- Packaging & cleanup
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
# [Sqids Java](https://sqids.org/java)

[Sqids](https://sqids.org/java) (*pronounced "squids"*) is a small library that lets you **generate unique IDs from
[Sqids](https://sqids.org/java) (_pronounced "squids"_) is a small library that lets you **generate unique IDs from
numbers**. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker
database lookups.

Features:

- **Encode multiple numbers** - generate short IDs from one or several non-negative numbers
- **Quick decoding** - easily decode IDs back into numbers
- **Unique IDs** - generate unique IDs by shuffling the alphabet once
- **ID padding** - provide minimum length to make IDs more uniform
- **URL safe** - auto-generated IDs do not contain common profanity
- **Randomized output** - Sequential input provides nonconsecutive IDs
- **Many implementations** - Support for [40+ programming languages](https://sqids.org/)
- **Encode multiple numbers** - generate short IDs from one or several non-negative numbers
- **Quick decoding** - easily decode IDs back into numbers
- **Unique IDs** - generate unique IDs by shuffling the alphabet once
- **ID padding** - provide minimum length to make IDs more uniform
- **URL safe** - auto-generated IDs do not contain common profanity
- **Randomized output** - Sequential input provides nonconsecutive IDs
- **Many implementations** - Support for [40+ programming languages](https://sqids.org/)

## 🧰 Use-cases

Good for:

- Generating IDs for public URLs (eg: link shortening)
- Generating IDs for internal systems (eg: event tracking)
- Decoding for quicker database lookups (eg: by primary keys)
- Generating IDs for public URLs (eg: link shortening)
- Generating IDs for internal systems (eg: event tracking)
- Decoding for quicker database lookups (eg: by primary keys)

Not good for:

- Sensitive data (this is not an encryption library)
- User IDs (can be decoded revealing user count)

- Sensitive data (this is not an encryption library)
- User IDs (can be decoded revealing user count)

## System Requirements
Java 8 or higher is required.

Java 8 or higher is required.

## 🚀 Getting started

Download and Import Sqids via:

```java
import org.sqids.Sqids;

```

## 👩‍💻 Examples
@@ -55,7 +55,7 @@ List<Long> numbers=sqids.decode(id); // [1, 2, 3]
> important to your design that IDs are canonical, you have to manually re-encode decoded numbers and check that the
> generated ID matches.
Enforce a *minimum* length for IDs:
Enforce a _minimum_ length for IDs:

```java
Sqids sqids=Sqids.builder()
@@ -87,4 +87,4 @@ List<Long> numbers=sqids.decode(id); // [1, 2, 3]

## 📝 License

[MIT](LICENSE)
[MIT](LICENSE)
51 changes: 40 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
plugins {
id 'com.diffplug.spotless' version '6.23.1'
id 'com.github.node-gradle.node' version '7.0.1'
id 'java-library'
id 'maven-publish'
id 'signing'
@@ -7,7 +9,7 @@ plugins {
group = 'org.sqids'
version = '0.1.0-SNAPSHOT'

String rootArtifactiId = 'sqids'
String rootArtifactId = 'sqids'
String projectUrl = 'https://sqids.org/java'

repositories {
@@ -17,7 +19,7 @@ repositories {
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

test {
@@ -34,20 +36,19 @@ java {

publishing {
publications {

mavenJava(MavenPublication) {
groupId = group
artifactId = rootArtifactiId
artifactId = rootArtifactId
version = version
from components.java
pom {
name = 'Sqids'
description = 'Generate short YouTube-looking IDs from numbers.'
url = projectUrl
properties = [
"parent.groupId": "org.sonatype.oss",
"parent.artifactId": "oss-parent",
"parent.version": "7"
'parent.groupId': 'org.sonatype.oss',
'parent.artifactId': 'oss-parent',
'parent.version': '7'
]
licenses {
license {
@@ -72,12 +73,12 @@ publishing {
}
repositories {
maven {
def releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
def snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
String releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
String snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username "${System.getenv("SONATYPE_USERNAME")}"
password "${System.getenv("SONATYPE_PASSWORD")}"
username System.getenv('SONATYPE_USERNAME')
password System.getenv('SONATYPE_PASSWORD')
}
}
}
@@ -86,3 +87,31 @@ publishing {
sign publishing.publications.mavenJava
}
}

node {
download = true
}

String nodeSetup = 'nodeSetup'
String npmSetup = 'npmSetup'

spotless {
format 'prettier', {
target '**/*.java', '**/*.properties', '**/*.md', '**/*.yml', '**/*.yaml'
targetExclude 'bin/**/*', 'gradle/**/*'

boolean isWindows = System.getProperty('os.name').toLowerCase().contains('windows')
String nodeExec = isWindows ? '/node.exe' : '/bin/node'
String npmExec = isWindows ? '/npm.cmd' : '/bin/npm'

prettier(['prettier': '3.1.0', 'prettier-plugin-java': '2.2.0', 'prettier-plugin-properties': '0.3.0'])
.npmInstallCache()
.npmExecutable("${tasks.named(npmSetup).get().npmDir.get()}${npmExec}")
.nodeExecutable("${tasks.named(nodeSetup).get().nodeDir.get()}${nodeExec}")
.configFile('.prettierrc.yaml')
}
}

tasks.named('spotlessPrettier').configure { task ->
task.dependsOn(nodeSetup, npmSetup)
}
1,215 changes: 632 additions & 583 deletions src/main/java/org/sqids/Sqids.java

Large diffs are not rendered by default.

36 changes: 16 additions & 20 deletions src/test/java/org/sqids/AlphabetTests.java
Original file line number Diff line number Diff line change
@@ -2,16 +2,14 @@

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AlphabetTests {

@Test
public void simpleAlphabet() {
Sqids sqids = Sqids.builder()
.alphabet("0123456789abcdef")
.build();
Sqids sqids = Sqids.builder().alphabet("0123456789abcdef").build();
List<Long> numbers = Arrays.asList(1L, 2L, 3L);
String id = "489158";
Assertions.assertEquals(sqids.encode(numbers), id);
@@ -20,41 +18,39 @@ public void simpleAlphabet() {

@Test
public void shortAlphabet() {
Sqids sqids = Sqids.builder()
.alphabet("abc")
.build();
Sqids sqids = Sqids.builder().alphabet("abc").build();
List<Long> numbers = Arrays.asList(1L, 2L, 3L);
Assertions.assertEquals(sqids.decode(sqids.encode(numbers)), numbers);
}

@Test
public void specialCharsAlphabet() {
Sqids sqids = Sqids.builder()
.alphabet(".\\?")
.build();
Sqids sqids = Sqids.builder().alphabet(".\\?").build();
List<Long> numbers = Arrays.asList(1L, 2L, 3L);
Assertions.assertEquals(sqids.decode(sqids.encode(numbers)), numbers);
}

@Test
public void multibyteCharacters() {
Assertions.assertThrows(IllegalArgumentException.class, () -> Sqids.builder()
.alphabet("ë1092")
.build());
Assertions.assertThrows(
IllegalArgumentException.class,
() -> Sqids.builder().alphabet("ë1092").build()
);
}

@Test
public void repeatingAlphabetCharacters() {
Assertions.assertThrows(IllegalArgumentException.class, () -> Sqids.builder()
.alphabet("aabcdefg")
.build());
Assertions.assertThrows(
IllegalArgumentException.class,
() -> Sqids.builder().alphabet("aabcdefg").build()
);
}

@Test
public void tooShortOfAnAlphabet() {
Assertions.assertThrows(IllegalArgumentException.class, () -> Sqids.builder()
.alphabet("ab")
.build());
Assertions.assertThrows(
IllegalArgumentException.class,
() -> Sqids.builder().alphabet("ab").build()
);
}

}
77 changes: 44 additions & 33 deletions src/test/java/org/sqids/BlockListTests.java
Original file line number Diff line number Diff line change
@@ -3,35 +3,33 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class BlockListTests {

@Test
public void blockList() {
Sqids sqids = Sqids.builder()
.build();
Sqids sqids = Sqids.builder().build();
List<Long> numbers = Arrays.asList(4572721L);
Assertions.assertEquals(sqids.decode("aho1e"), numbers);
Assertions.assertEquals(sqids.encode(numbers), "JExTR");
}

@Test
public void emptyBlockList() {
Sqids sqids = Sqids.builder()
.blockList(new HashSet<>())
.build();
Sqids sqids = Sqids.builder().blockList(new HashSet<>()).build();
List<Long> numbers = Arrays.asList(4572721L);
Assertions.assertEquals(sqids.decode("aho1e"), numbers);
Assertions.assertEquals(sqids.encode(numbers), "aho1e");
}

@Test
public void nonEmptyBlockList() {
Sqids sqids = Sqids.builder()
.blockList(new HashSet<>(Arrays.asList("ArUO")))
.build();
Sqids sqids = Sqids
.builder()
.blockList(new HashSet<>(Arrays.asList("ArUO")))
.build();
List<Long> numbers = Arrays.asList(4572721L);
Assertions.assertEquals(sqids.decode("aho1e"), numbers);
Assertions.assertEquals(sqids.encode(numbers), "aho1e");
@@ -44,31 +42,41 @@ public void nonEmptyBlockList() {

@Test
public void encodeBlockList() {
Sqids sqids = Sqids.builder()
.blockList(new HashSet<>(Arrays.asList(
Sqids sqids = Sqids
.builder()
.blockList(
new HashSet<>(
Arrays.asList(
"JSwXFaosAN", // normal result of 1st encoding, let's block that word on purpose
"OCjV9JK64o", // result of 2nd encoding
"rBHf", // result of 3rd encoding is `4rBHfOiqd3`, let's block a substring
"79SM", // result of 4th encoding is `dyhgw479SM`, let's block the postfix
"7tE6" // result of 4th encoding is `7tE6jdAHLe`, let's block the prefix
)))
.build();
)
)
)
.build();
List<Long> numbers = Arrays.asList(1000000L, 2000000L);
Assertions.assertEquals(sqids.encode(numbers), "1aYeB7bRUt");
Assertions.assertEquals(sqids.decode("1aYeB7bRUt"), numbers);
}

@Test
public void decodeBlockList() {
Sqids sqids = Sqids.builder()
.blockList(new HashSet<>(Arrays.asList(
Sqids sqids = Sqids
.builder()
.blockList(
new HashSet<>(
Arrays.asList(
"86Rf07",
"se8ojk",
"ARsz1p",
"Q8AI49",
"5sQRZO"
)))
.build();
)
)
)
.build();
List<Long> numbers = Arrays.asList(1L, 2L, 3L);
Assertions.assertEquals(sqids.decode("86Rf07"), numbers);
Assertions.assertEquals(sqids.decode("se8ojk"), numbers);
@@ -79,34 +87,37 @@ public void decodeBlockList() {

@Test
public void shortBlockList() {
Sqids sqids = Sqids.builder()
.blockList(new HashSet<>(Arrays.asList("pnd")))
.build();
Sqids sqids = Sqids
.builder()
.blockList(new HashSet<>(Arrays.asList("pnd")))
.build();
List<Long> numbers = Arrays.asList(1000L);
Assertions.assertEquals(sqids.decode(sqids.encode(numbers)), numbers);
}

@Test
public void lowercaseBlockList() {
Sqids sqids = Sqids.builder()
.alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
.blockList(new HashSet<>(Arrays.asList("sxnzkl")))
.build();
Sqids sqids = Sqids
.builder()
.alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
.blockList(new HashSet<>(Arrays.asList("sxnzkl")))
.build();
List<Long> numbers = Arrays.asList(1L, 2L, 3L);
Assertions.assertEquals(sqids.encode(numbers), "IBSHOZ");
Assertions.assertEquals(sqids.decode("IBSHOZ"), numbers);
}

@Test
public void maxBlockList() {
Sqids sqids = Sqids.builder()
.alphabet("abc")
.minLength(3)
.blockList(new HashSet<>(Arrays.asList(
"cab",
"abc",
"bca")))
.build();
Assertions.assertThrows(RuntimeException.class, () -> sqids.encode(Arrays.asList(0L)));
Sqids sqids = Sqids
.builder()
.alphabet("abc")
.minLength(3)
.blockList(new HashSet<>(Arrays.asList("cab", "abc", "bca")))
.build();
Assertions.assertThrows(
RuntimeException.class,
() -> sqids.encode(Arrays.asList(0L))
);
}
}
142 changes: 122 additions & 20 deletions src/test/java/org/sqids/EncodeTests.java
Original file line number Diff line number Diff line change
@@ -5,11 +5,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class EncodeTests {

private final Sqids sqids = Sqids.builder().build();

@Test
@@ -23,17 +23,18 @@ public void simple() {
@Test
public void differentInputs() {
List<Long> numbers = Arrays.asList(
0L,
0L,
0L,
1L,
2L,
3L,
100L,
1000L,
100000L,
1000000L,
Long.MAX_VALUE);
0L,
0L,
0L,
1L,
2L,
3L,
100L,
1000L,
100000L,
1000000L,
Long.MAX_VALUE
);
Assertions.assertEquals(sqids.decode(sqids.encode(numbers)), numbers);
}

@@ -86,12 +87,107 @@ public void incrementalNumbers() {
@Test
public void multiInput() {
List<Long> numbers = Arrays.asList(
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L,
57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L,
75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L,
93L, 94L, 95L, 96L, 97L, 98L, 99L);
0L,
1L,
2L,
3L,
4L,
5L,
6L,
7L,
8L,
9L,
10L,
11L,
12L,
13L,
14L,
15L,
16L,
17L,
18L,
19L,
20L,
21L,
22L,
23L,
24L,
25L,
26L,
27L,
28L,
29L,
30L,
31L,
32L,
33L,
34L,
35L,
36L,
37L,
38L,
39L,
40L,
41L,
42L,
43L,
44L,
45L,
46L,
47L,
48L,
49L,
50L,
51L,
52L,
53L,
54L,
55L,
56L,
57L,
58L,
59L,
60L,
61L,
62L,
63L,
64L,
65L,
66L,
67L,
68L,
69L,
70L,
71L,
72L,
73L,
74L,
75L,
76L,
77L,
78L,
79L,
80L,
81L,
82L,
83L,
84L,
85L,
86L,
87L,
88L,
89L,
90L,
91L,
92L,
93L,
94L,
95L,
96L,
97L,
98L,
99L
);
Assertions.assertEquals(sqids.decode(sqids.encode(numbers)), numbers);
}

@@ -115,7 +211,13 @@ public void decodeInvalidCharacter() {

@Test
public void encodeOutOfRangeNumbers() {
Assertions.assertThrows(RuntimeException.class, () -> sqids.encode(Arrays.asList(-1L)));
Assertions.assertThrows(RuntimeException.class, () -> sqids.encode(Arrays.asList(Long.MAX_VALUE + 1)));
Assertions.assertThrows(
RuntimeException.class,
() -> sqids.encode(Arrays.asList(-1L))
);
Assertions.assertThrows(
RuntimeException.class,
() -> sqids.encode(Arrays.asList(Long.MAX_VALUE + 1))
);
}
}
123 changes: 81 additions & 42 deletions src/test/java/org/sqids/MinLengthTests.java
Original file line number Diff line number Diff line change
@@ -5,20 +5,19 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class MinLengthTests {

private final int minLength = Sqids.Builder.DEFAULT_ALPHABET.length();

@Test
public void simple() {
Sqids sqids = Sqids.builder()
.minLength(minLength)
.build();
Sqids sqids = Sqids.builder().minLength(minLength).build();
List<Long> numbers = Arrays.asList(1L, 2L, 3L);
String id = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM";
String id =
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM";
Assertions.assertEquals(sqids.encode(numbers), id);
Assertions.assertEquals(sqids.decode(id), numbers);
}
@@ -36,16 +35,26 @@ public void incremental() {
put(11, "86Rf07xd4zB");
put(12, "86Rf07xd4zBm");
put(13, "86Rf07xd4zBmi");
put(minLength + 0, "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM");
put(minLength + 1, "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMy");
put(minLength + 2, "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf");
put(minLength + 3, "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf1");
put(
minLength + 0,
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM"
);
put(
minLength + 1,
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMy"
);
put(
minLength + 2,
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf"
);
put(
minLength + 3,
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf1"
);
}
};
for (Integer minLength : ids.keySet()) {
Sqids sqids = Sqids.builder()
.minLength(minLength)
.build();
Sqids sqids = Sqids.builder().minLength(minLength).build();
String id = ids.get(minLength);
Assertions.assertEquals(sqids.encode(numbers), id);
Assertions.assertEquals(sqids.decode(id), numbers);
@@ -54,21 +63,49 @@ public void incremental() {

@Test
public void incrementalNumbers() {
Sqids sqids = Sqids.builder()
.minLength(minLength)
.build();
Sqids sqids = Sqids.builder().minLength(minLength).build();
Map<String, List<Long>> ids = new HashMap<String, List<Long>>() {
{
put("SvIzsqYMyQwI3GWgJAe17URxX8V924Co0DaTZLtFjHriEn5bPhcSkfmvOslpBu", Arrays.asList(0L, 0L));
put("n3qafPOLKdfHpuNw3M61r95svbeJGk7aAEgYn4WlSjXURmF8IDqZBy0CT2VxQc", Arrays.asList(0L, 1L));
put("tryFJbWcFMiYPg8sASm51uIV93GXTnvRzyfLleh06CpodJD42B7OraKtkQNxUZ", Arrays.asList(0L, 2L));
put("eg6ql0A3XmvPoCzMlB6DraNGcWSIy5VR8iYup2Qk4tjZFKe1hbwfgHdUTsnLqE", Arrays.asList(0L, 3L));
put("rSCFlp0rB2inEljaRdxKt7FkIbODSf8wYgTsZM1HL9JzN35cyoqueUvVWCm4hX", Arrays.asList(0L, 4L));
put("sR8xjC8WQkOwo74PnglH1YFdTI0eaf56RGVSitzbjuZ3shNUXBrqLxEJyAmKv2", Arrays.asList(0L, 5L));
put("uY2MYFqCLpgx5XQcjdtZK286AwWV7IBGEfuS9yTmbJvkzoUPeYRHr4iDs3naN0", Arrays.asList(0L, 6L));
put("74dID7X28VLQhBlnGmjZrec5wTA1fqpWtK4YkaoEIM9SRNiC3gUJH0OFvsPDdy", Arrays.asList(0L, 7L));
put("30WXpesPhgKiEI5RHTY7xbB1GnytJvXOl2p0AcUjdF6waZDo9Qk8VLzMuWrqCS", Arrays.asList(0L, 8L));
put("moxr3HqLAK0GsTND6jowfZz3SUx7cQ8aC54Pl1RbIvFXmEJuBMYVeW9yrdOtin", Arrays.asList(0L, 9L));
put(
"SvIzsqYMyQwI3GWgJAe17URxX8V924Co0DaTZLtFjHriEn5bPhcSkfmvOslpBu",
Arrays.asList(0L, 0L)
);
put(
"n3qafPOLKdfHpuNw3M61r95svbeJGk7aAEgYn4WlSjXURmF8IDqZBy0CT2VxQc",
Arrays.asList(0L, 1L)
);
put(
"tryFJbWcFMiYPg8sASm51uIV93GXTnvRzyfLleh06CpodJD42B7OraKtkQNxUZ",
Arrays.asList(0L, 2L)
);
put(
"eg6ql0A3XmvPoCzMlB6DraNGcWSIy5VR8iYup2Qk4tjZFKe1hbwfgHdUTsnLqE",
Arrays.asList(0L, 3L)
);
put(
"rSCFlp0rB2inEljaRdxKt7FkIbODSf8wYgTsZM1HL9JzN35cyoqueUvVWCm4hX",
Arrays.asList(0L, 4L)
);
put(
"sR8xjC8WQkOwo74PnglH1YFdTI0eaf56RGVSitzbjuZ3shNUXBrqLxEJyAmKv2",
Arrays.asList(0L, 5L)
);
put(
"uY2MYFqCLpgx5XQcjdtZK286AwWV7IBGEfuS9yTmbJvkzoUPeYRHr4iDs3naN0",
Arrays.asList(0L, 6L)
);
put(
"74dID7X28VLQhBlnGmjZrec5wTA1fqpWtK4YkaoEIM9SRNiC3gUJH0OFvsPDdy",
Arrays.asList(0L, 7L)
);
put(
"30WXpesPhgKiEI5RHTY7xbB1GnytJvXOl2p0AcUjdF6waZDo9Qk8VLzMuWrqCS",
Arrays.asList(0L, 8L)
);
put(
"moxr3HqLAK0GsTND6jowfZz3SUx7cQ8aC54Pl1RbIvFXmEJuBMYVeW9yrdOtin",
Arrays.asList(0L, 9L)
);
}
};
for (String id : ids.keySet()) {
@@ -81,18 +118,18 @@ public void incrementalNumbers() {
@Test
public void minLengths() {
List<Integer> minLengths = Arrays.asList(0, 1, 5, 10, minLength);
List<List<Long>> numbers = new ArrayList<List<Long>>() {{
add(Arrays.asList(0L));
add(Arrays.asList(0L, 0L, 0L, 0L, 0L));
add(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L));
add(Arrays.asList(100L, 200L, 300L));
add(Arrays.asList(1000L, 2000L, 30000L));
add(Arrays.asList((long) Integer.MAX_VALUE));
}};
List<List<Long>> numbers = new ArrayList<List<Long>>() {
{
add(Arrays.asList(0L));
add(Arrays.asList(0L, 0L, 0L, 0L, 0L));
add(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L));
add(Arrays.asList(100L, 200L, 300L));
add(Arrays.asList(1000L, 2000L, 30000L));
add(Arrays.asList((long) Integer.MAX_VALUE));
}
};
for (Integer minLength : minLengths) {
Sqids sqids = Sqids.builder()
.minLength(minLength)
.build();
Sqids sqids = Sqids.builder().minLength(minLength).build();
for (List<Long> number : numbers) {
String id = sqids.encode(number);
Assertions.assertTrue(id.length() >= minLength);
@@ -104,11 +141,13 @@ public void minLengths() {
@Test
public void encodeOutOfRangeNumbers() {
int minLengthLimit = 255;
Assertions.assertThrows(RuntimeException.class, () -> Sqids.builder()
.minLength(-1)
.build());
Assertions.assertThrows(RuntimeException.class, () -> Sqids.builder()
.minLength(minLengthLimit + 1)
.build());
Assertions.assertThrows(
RuntimeException.class,
() -> Sqids.builder().minLength(-1).build()
);
Assertions.assertThrows(
RuntimeException.class,
() -> Sqids.builder().minLength(minLengthLimit + 1).build()
);
}
}