mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Add] CacheStore-api 对 CacheKey 增加新的构造方法, 补充完整的单元测试;
[Add] CacheKey 添加构造方法 '<init>(String[])'; [Add] CacheKeyTest 添加针对 CacheKey 的完整单元测试;
This commit is contained in:
parent
3c705ee16a
commit
9a3f8698d3
@ -45,6 +45,18 @@ public final class CacheKey {
|
||||
System.arraycopy(keyStrings, 0, this.key, 1, keyStrings.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供一组字符串用于组成缓存键.
|
||||
* @param keyStrings 键名组成数组.
|
||||
*/
|
||||
public CacheKey(String[] keyStrings) {
|
||||
Objects.requireNonNull(keyStrings);
|
||||
if (keyStrings.length == 0) {
|
||||
throw new IllegalArgumentException("Provide at least one element that makes up the key");
|
||||
}
|
||||
this.key = keyStrings;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取组成 Key 的字符串数组.
|
||||
* @return 返回用于组成 Key 的字符串数组.
|
||||
|
53
ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/CacheKeyTest.java
vendored
Normal file
53
ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/CacheKeyTest.java
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2020 LamGC
|
||||
*
|
||||
* ContentGrabbingJi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* ContentGrabbingJi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.lamgc.cgj.bot.cache;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CacheKeyTest {
|
||||
|
||||
@Test
|
||||
public void hashCodeAndEqualsTest() {
|
||||
CacheKey key = new CacheKey("test");
|
||||
Assert.assertEquals(key, key);
|
||||
Assert.assertNotEquals(key, null);
|
||||
Assert.assertNotEquals(key, new Object());
|
||||
Assert.assertEquals(new CacheKey("test", "key01"), new CacheKey("test", "key01"));
|
||||
Assert.assertEquals(new CacheKey("test", "key01").hashCode(), new CacheKey("test", "key01").hashCode());
|
||||
Assert.assertNotEquals(new CacheKey("test", "key01"), new CacheKey("test", "key00"));
|
||||
Assert.assertNotEquals(new CacheKey("test", "key01").hashCode(), new CacheKey("test", "key00").hashCode());
|
||||
Assert.assertEquals(new CacheKey("test", "key01").toString(), "test.key01");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildTest() {
|
||||
Assert.assertThrows(NullPointerException.class, () -> new CacheKey(null));
|
||||
Assert.assertThrows(NullPointerException.class, () -> new CacheKey(null, new String[0]));
|
||||
Assert.assertThrows(IllegalArgumentException.class, () -> new CacheKey(new String[0]));
|
||||
final String[] keys = new String[] {"test", "key01"};
|
||||
Assert.assertArrayEquals(new CacheKey(keys).getKeyArray(), keys);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void joinTest() {
|
||||
Assert.assertEquals("test.key01", new CacheKey("test", "key01").join("."));
|
||||
Assert.assertEquals("test:key01", new CacheKey("test", "key01").join(":"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user