[Add] CacheStore-api 对 CacheKey 增加新的构造方法, 补充完整的单元测试;

[Add] CacheKey 添加构造方法 '<init>(String[])';
[Add] CacheKeyTest 添加针对 CacheKey 的完整单元测试;
This commit is contained in:
LamGC 2020-09-05 13:31:00 +08:00
parent 3c705ee16a
commit 9a3f8698d3
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 65 additions and 0 deletions

View File

@ -45,6 +45,18 @@ public final class CacheKey {
System.arraycopy(keyStrings, 0, this.key, 1, keyStrings.length); 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 的字符串数组. * 获取组成 Key 的字符串数组.
* @return 返回用于组成 Key 的字符串数组. * @return 返回用于组成 Key 的字符串数组.

View 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(":"));
}
}