From 9a3f8698d3d289d789af6126e44c24141cd952cb Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 5 Sep 2020 13:31:00 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20CacheStore-api=20=E5=AF=B9=20CacheKey?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E6=96=B0=E7=9A=84=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E6=96=B9=E6=B3=95,=20=E8=A1=A5=E5=85=85=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] CacheKey 添加构造方法 '(String[])'; [Add] CacheKeyTest 添加针对 CacheKey 的完整单元测试; --- .../net/lamgc/cgj/bot/cache/CacheKey.java | 12 +++++ .../net/lamgc/cgj/bot/cache/CacheKeyTest.java | 53 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/CacheKeyTest.java diff --git a/ContentGrabbingJi-CacheStore-api/src/main/java/net/lamgc/cgj/bot/cache/CacheKey.java b/ContentGrabbingJi-CacheStore-api/src/main/java/net/lamgc/cgj/bot/cache/CacheKey.java index a4c06a6..ecab7a0 100644 --- a/ContentGrabbingJi-CacheStore-api/src/main/java/net/lamgc/cgj/bot/cache/CacheKey.java +++ b/ContentGrabbingJi-CacheStore-api/src/main/java/net/lamgc/cgj/bot/cache/CacheKey.java @@ -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 的字符串数组. diff --git a/ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/CacheKeyTest.java b/ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/CacheKeyTest.java new file mode 100644 index 0000000..a50b1f3 --- /dev/null +++ b/ContentGrabbingJi-CacheStore-api/src/test/java/net/lamgc/cgj/bot/cache/CacheKeyTest.java @@ -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 . + */ + +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(":")); + } + +}