From bcded8c9f6de640265ad2382ceb66882ad197893 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 14 Nov 2020 16:39:58 +0800 Subject: [PATCH] =?UTF-8?q?[Add][Change]=20Common=20=E8=B0=83=E6=95=B4=20F?= =?UTF-8?q?actoryInfo=20equals=20=E5=88=A4=E6=96=AD=E7=BB=86=E8=8A=82,=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=20FactoryInfo=20=E7=9A=84=E5=85=A8?= =?UTF-8?q?=E8=A6=86=E7=9B=96=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 [Change] FactoryInfo 调整 equals 方法, 移除优先级判断以防止因更新优先级导致出现同样组件有不同版本的情况; [Add] FactoryInfoTest 添加单元测试项(100%); [Add] CacheStoreSourceNoEqualFactory, FactoryInfoTestFactory, NameNoEqualFactoryA, NameNoEqualFactoryB, TooHighPriorityFactory, TooLowPriorityFactory, UnnamedFactory 添加测试用工厂(不要把它们注册到 SPI 中!); --- .../net/lamgc/cgj/bot/cache/FactoryInfo.java | 4 +- .../lamgc/cgj/bot/cache/FactoryInfoTest.java | 86 +++++++++++++++++++ .../CacheStoreSourceNoEqualFactory.java | 57 ++++++++++++ .../cache/factory/FactoryInfoTestFactory.java | 57 ++++++++++++ .../cache/factory/NameNoEqualFactoryA.java | 57 ++++++++++++ .../cache/factory/NameNoEqualFactoryB.java | 57 ++++++++++++ .../cache/factory/TooHighPriorityFactory.java | 57 ++++++++++++ .../cache/factory/TooLowPriorityFactory.java | 57 ++++++++++++ .../cgj/bot/cache/factory/UnnamedFactory.java | 57 ++++++++++++ 9 files changed, 487 insertions(+), 2 deletions(-) create mode 100644 ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/FactoryInfoTest.java create mode 100644 ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/CacheStoreSourceNoEqualFactory.java create mode 100644 ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/FactoryInfoTestFactory.java create mode 100644 ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/NameNoEqualFactoryA.java create mode 100644 ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/NameNoEqualFactoryB.java create mode 100644 ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/TooHighPriorityFactory.java create mode 100644 ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/TooLowPriorityFactory.java create mode 100644 ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/UnnamedFactory.java diff --git a/ContentGrabbingJi-common/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java b/ContentGrabbingJi-common/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java index 09e15b0..16eff5c 100644 --- a/ContentGrabbingJi-common/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java +++ b/ContentGrabbingJi-common/src/main/java/net/lamgc/cgj/bot/cache/FactoryInfo.java @@ -84,11 +84,11 @@ public final class FactoryInfo { return false; } FactoryInfo that = (FactoryInfo) o; - return factoryName.equals(that.factoryName); + return factoryName.equals(that.factoryName) && storeSource == that.storeSource; } @Override public int hashCode() { - return Objects.hash(factoryName); + return Objects.hash(factoryName, storeSource); } } diff --git a/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/FactoryInfoTest.java b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/FactoryInfoTest.java new file mode 100644 index 0000000..dff662b --- /dev/null +++ b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/FactoryInfoTest.java @@ -0,0 +1,86 @@ +/* + * 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 net.lamgc.cgj.bot.cache.factory.*; +import org.junit.Assert; +import org.junit.Test; + +public class FactoryInfoTest { + + + @Test + public void analyticTest() { + FactoryInfo infoA = new FactoryInfo(FactoryInfoTestFactory.class); + Assert.assertEquals("test-factoryInfo", infoA.getFactoryName()); + Assert.assertEquals(6, infoA.getFactoryPriority()); + Assert.assertEquals(CacheStoreSource.MEMORY, infoA.getStoreSource()); + + FactoryInfo infoB = new FactoryInfo(FactoryInfoTestFactory.class); + + Assert.assertEquals(infoA, infoB); + Assert.assertEquals(infoA.hashCode(), infoB.hashCode()); + } + + @Test + @SuppressWarnings({"SimplifiableAssertion", "EqualsWithItself", "ConstantConditions"}) + public void equalsTest() { + FactoryInfo infoA = new FactoryInfo(FactoryInfoTestFactory.class); + FactoryInfo infoB = new FactoryInfo(FactoryInfoTestFactory.class); + + Assert.assertTrue(infoA.equals(infoA)); + Assert.assertTrue(infoA.equals(infoB)); + + Assert.assertFalse(infoA.equals(null)); + Assert.assertFalse(infoA.equals(new Object())); + + Assert.assertFalse( + new FactoryInfo(NameNoEqualFactoryA.class) + .equals(new FactoryInfo(NameNoEqualFactoryB.class))); + + Assert.assertFalse( + new FactoryInfo(NameNoEqualFactoryA.class) + .equals(new FactoryInfo(CacheStoreSourceNoEqualFactory.class))); + + } + + @Test(expected = IllegalArgumentException.class) + public void noAnnotationTest() { + new FactoryInfo(NoAnnotationFactory.class); + } + + @Test(expected = IllegalArgumentException.class) + public void unnamedTest() { + new FactoryInfo(UnnamedFactory.class); + } + + @Test + public void priorityCheckTest() { + FactoryInfo toHighPriorityFactoryInfo = new FactoryInfo(TooHighPriorityFactory.class); + Assert.assertEquals(FactoryPriority.PRIORITY_HIGHEST, + toHighPriorityFactoryInfo.getFactoryPriority()); + + FactoryInfo toLowPriorityFactoryInfo = new FactoryInfo(TooLowPriorityFactory.class); + Assert.assertEquals(FactoryPriority.PRIORITY_LOWEST, + toLowPriorityFactoryInfo.getFactoryPriority()); + + Assert.assertNotEquals(toHighPriorityFactoryInfo, toLowPriorityFactoryInfo); + + } + +} diff --git a/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/CacheStoreSourceNoEqualFactory.java b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/CacheStoreSourceNoEqualFactory.java new file mode 100644 index 0000000..1d08a64 --- /dev/null +++ b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/CacheStoreSourceNoEqualFactory.java @@ -0,0 +1,57 @@ +/* + * 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.factory; + +import net.lamgc.cgj.bot.cache.*; +import net.lamgc.cgj.bot.cache.convert.StringConverter; +import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; + +import java.io.File; + +@Factory(name = "test-NameNoEqualA", source = CacheStoreSource.REMOTE) +public class CacheStoreSourceNoEqualFactory implements CacheStoreFactory { + @Override + public void initial(File dataDirectory) { + + } + + @Override + public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public ListCacheStore newListCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public SetCacheStore newSetCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public MapCacheStore newMapCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public boolean canGetCacheStore() { + return false; + } +} diff --git a/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/FactoryInfoTestFactory.java b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/FactoryInfoTestFactory.java new file mode 100644 index 0000000..1ddabc8 --- /dev/null +++ b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/FactoryInfoTestFactory.java @@ -0,0 +1,57 @@ +/* + * 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.factory; + +import net.lamgc.cgj.bot.cache.*; +import net.lamgc.cgj.bot.cache.convert.StringConverter; +import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; + +import java.io.File; + +@Factory(name = "test-factoryInfo", source = CacheStoreSource.MEMORY, priority = 6) +public class FactoryInfoTestFactory implements CacheStoreFactory { + @Override + public void initial(File dataDirectory) { + + } + + @Override + public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public ListCacheStore newListCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public SetCacheStore newSetCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public MapCacheStore newMapCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public boolean canGetCacheStore() { + return false; + } +} diff --git a/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/NameNoEqualFactoryA.java b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/NameNoEqualFactoryA.java new file mode 100644 index 0000000..2fc41c5 --- /dev/null +++ b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/NameNoEqualFactoryA.java @@ -0,0 +1,57 @@ +/* + * 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.factory; + +import net.lamgc.cgj.bot.cache.*; +import net.lamgc.cgj.bot.cache.convert.StringConverter; +import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; + +import java.io.File; + +@Factory(name = "test-NameNoEqualA") +public class NameNoEqualFactoryA implements CacheStoreFactory { + @Override + public void initial(File dataDirectory) { + + } + + @Override + public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public ListCacheStore newListCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public SetCacheStore newSetCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public MapCacheStore newMapCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public boolean canGetCacheStore() { + return false; + } +} diff --git a/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/NameNoEqualFactoryB.java b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/NameNoEqualFactoryB.java new file mode 100644 index 0000000..6103b24 --- /dev/null +++ b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/NameNoEqualFactoryB.java @@ -0,0 +1,57 @@ +/* + * 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.factory; + +import net.lamgc.cgj.bot.cache.*; +import net.lamgc.cgj.bot.cache.convert.StringConverter; +import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; + +import java.io.File; + +@Factory(name = "test-NameNoEqualB") +public class NameNoEqualFactoryB implements CacheStoreFactory { + @Override + public void initial(File dataDirectory) { + + } + + @Override + public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public ListCacheStore newListCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public SetCacheStore newSetCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public MapCacheStore newMapCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public boolean canGetCacheStore() { + return false; + } +} diff --git a/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/TooHighPriorityFactory.java b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/TooHighPriorityFactory.java new file mode 100644 index 0000000..b3529a1 --- /dev/null +++ b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/TooHighPriorityFactory.java @@ -0,0 +1,57 @@ +/* + * 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.factory; + +import net.lamgc.cgj.bot.cache.*; +import net.lamgc.cgj.bot.cache.convert.StringConverter; +import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; + +import java.io.File; + +@Factory(name = "test-factoryInfo-tooHighPriority", priority = 15) +public class TooHighPriorityFactory implements CacheStoreFactory { + @Override + public void initial(File dataDirectory) { + + } + + @Override + public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public ListCacheStore newListCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public SetCacheStore newSetCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public MapCacheStore newMapCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public boolean canGetCacheStore() { + return false; + } +} diff --git a/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/TooLowPriorityFactory.java b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/TooLowPriorityFactory.java new file mode 100644 index 0000000..674008a --- /dev/null +++ b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/TooLowPriorityFactory.java @@ -0,0 +1,57 @@ +/* + * 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.factory; + +import net.lamgc.cgj.bot.cache.*; +import net.lamgc.cgj.bot.cache.convert.StringConverter; +import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; + +import java.io.File; + +@Factory(name = "test-factoryInfo-tooLowPriority", priority = -5) +public class TooLowPriorityFactory implements CacheStoreFactory { + @Override + public void initial(File dataDirectory) { + + } + + @Override + public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public ListCacheStore newListCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public SetCacheStore newSetCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public MapCacheStore newMapCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public boolean canGetCacheStore() { + return false; + } +} diff --git a/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/UnnamedFactory.java b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/UnnamedFactory.java new file mode 100644 index 0000000..b564b3e --- /dev/null +++ b/ContentGrabbingJi-common/src/test/java/net/lamgc/cgj/bot/cache/factory/UnnamedFactory.java @@ -0,0 +1,57 @@ +/* + * 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.factory; + +import net.lamgc.cgj.bot.cache.*; +import net.lamgc.cgj.bot.cache.convert.StringConverter; +import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; + +import java.io.File; + +@Factory(name = "") +public class UnnamedFactory implements CacheStoreFactory { + @Override + public void initial(File dataDirectory) { + + } + + @Override + public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public ListCacheStore newListCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public SetCacheStore newSetCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public MapCacheStore newMapCacheStore(String identify, StringConverter converter) throws GetCacheStoreException { + return null; + } + + @Override + public boolean canGetCacheStore() { + return false; + } +}