From ca916ff7c0ccd9bdb2e24749c5800193741d9022 Mon Sep 17 00:00:00 2001 From: leaderli <429243408@qq.com> Date: Thu, 21 Nov 2024 10:48:29 +0800 Subject: [PATCH] sonar --- .../io/leaderli/litool/core/meta/Lino.java | 65 +++++++++++++++++-- .../bean/{EnumTest.java => EnumTestBean.java} | 2 +- .../io/leaderli/litool/core/bean/Person.java | 24 +++++++ .../litool/core/bit/BitStrEnumTest.java | 2 +- .../leaderli/litool/core/io/FileUtilTest.java | 10 +++ .../leaderli/litool/core/io/TailerTest.java | 8 +-- .../leaderli/litool/core/lang/ShellTest.java | 5 +- .../adapters/MapTypeAdapterFactoryTest.java | 23 +++++++ .../leaderli/litool/core/meta/LinoTest.java | 13 +++- .../litool/core/type/ClassScannerTest.java | 2 +- ...umTest.java => PrimitiveEnumTestBean.java} | 2 +- .../litool/core/type/ReflectUtilTest.java | 4 +- .../litool/core/util/NetUtilTest.java | 7 +- ...numTest.java => OperatorEnumTestBean.java} | 2 +- pom.xml | 8 ++- 15 files changed, 157 insertions(+), 20 deletions(-) rename litool-core/src/test/java/io/leaderli/litool/core/bean/{EnumTest.java => EnumTestBean.java} (72%) create mode 100644 litool-core/src/test/java/io/leaderli/litool/core/bean/Person.java rename litool-core/src/test/java/io/leaderli/litool/core/type/{PrimitiveEnumTest.java => PrimitiveEnumTestBean.java} (99%) rename litool-runner/src/test/java/io/leaderli/litool/runner/constant/{OperatorEnumTest.java => OperatorEnumTestBean.java} (97%) diff --git a/litool-core/src/main/java/io/leaderli/litool/core/meta/Lino.java b/litool-core/src/main/java/io/leaderli/litool/core/meta/Lino.java index 07b84554..f2f5ac4a 100644 --- a/litool-core/src/main/java/io/leaderli/litool/core/meta/Lino.java +++ b/litool-core/src/main/java/io/leaderli/litool/core/meta/Lino.java @@ -93,7 +93,7 @@ static Lino ofIgnoreError(ThrowableSupplier supplier) { return new Some<>(value); } catch (Throwable e) { WhenThrowBehavior.whenThrow(ExceptionUtil.unwrapThrowable(e)); - return none(); + return new Error<>(e); } } @@ -152,6 +152,19 @@ static Lino narrow(Lino value) { } + + /** + * @return 判断是否有异常 + * @see Error + */ + boolean hasError(); + + /** + * @return 返回是否有异常 + * @see Error + */ + Throwable getError(); + /** * 这是一个泛型接口Lino,该方法为其默认方法,作用是将传入的参数filter应用于泛型T,如果满足断言条件,则返回当前对象,否则抛出IllegalStateException异常。 * @@ -549,6 +562,16 @@ public String toString() { return name() + "(" + value + ")"; } + @Override + public boolean hasError() { + return false; + } + + @Override + public Throwable getError() { + return null; + } + @Override public Lino assertTrue(Function filter, String msg) { if (filter(filter).absent()) { @@ -737,10 +760,10 @@ public Lino mapIgnoreError(ThrowableFunction mapp return of(mapper.apply(this.value)); } catch (Throwable throwable) { if (whenThrow != null) { - whenThrow.accept(throwable); + whenThrow.accept(ExceptionUtil.unwrapThrowable(throwable)); } + return new Error<>(throwable); } - return none(); } @Override @@ -766,7 +789,7 @@ public Lira toLira(LiTypeToken type) { /** * @param 值的类型 */ - final class None implements Lino { + class None implements Lino { private static final None INSTANCE = new None<>(); private None() { @@ -788,6 +811,16 @@ public String toString() { return name() + "()"; } + @Override + public boolean hasError() { + return false; + } + + @Override + public Throwable getError() { + return null; + } + @Override public Lino assertTrue(Function filter, String msg) { throw new IllegalStateException(msg); @@ -972,4 +1005,28 @@ public Lira toLira(LiTypeToken type) { return Lira.none(); } } + + final class Error extends None { + + public final Throwable throwable; + + public Error(Throwable throwable) { + this.throwable = throwable; + } + + @Override + public String name() { + return "Error"; + } + + @Override + public boolean hasError() { + return true; + } + + @Override + public Throwable getError() { + return throwable; + } + } } diff --git a/litool-core/src/test/java/io/leaderli/litool/core/bean/EnumTest.java b/litool-core/src/test/java/io/leaderli/litool/core/bean/EnumTestBean.java similarity index 72% rename from litool-core/src/test/java/io/leaderli/litool/core/bean/EnumTest.java rename to litool-core/src/test/java/io/leaderli/litool/core/bean/EnumTestBean.java index f67f06ce..8e5d3044 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/bean/EnumTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/bean/EnumTestBean.java @@ -1,6 +1,6 @@ package io.leaderli.litool.core.bean; -public class EnumTest { +public class EnumTestBean { enum State { A diff --git a/litool-core/src/test/java/io/leaderli/litool/core/bean/Person.java b/litool-core/src/test/java/io/leaderli/litool/core/bean/Person.java new file mode 100644 index 00000000..4ec6435c --- /dev/null +++ b/litool-core/src/test/java/io/leaderli/litool/core/bean/Person.java @@ -0,0 +1,24 @@ +package io.leaderli.litool.core.bean; + +public class Person { + + private String name; + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + private int age; +} diff --git a/litool-core/src/test/java/io/leaderli/litool/core/bit/BitStrEnumTest.java b/litool-core/src/test/java/io/leaderli/litool/core/bit/BitStrEnumTest.java index 94437656..e9d5cb52 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/bit/BitStrEnumTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/bit/BitStrEnumTest.java @@ -15,7 +15,7 @@ * @author leaderli * @since 2022/7/21 */ -class BitPositionEnumTest { +class BitPositionEnumTestBean { @Test diff --git a/litool-core/src/test/java/io/leaderli/litool/core/io/FileUtilTest.java b/litool-core/src/test/java/io/leaderli/litool/core/io/FileUtilTest.java index ab5633d6..6059de4a 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/io/FileUtilTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/io/FileUtilTest.java @@ -1,8 +1,18 @@ package io.leaderli.litool.core.io; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + class FileUtilTest { + @Test + void test() { + + Assertions.assertNotNull(FileUtil.getHome()); + Assertions.assertTrue(FileUtil.getHomeFile().isDirectory()); + + } diff --git a/litool-core/src/test/java/io/leaderli/litool/core/io/TailerTest.java b/litool-core/src/test/java/io/leaderli/litool/core/io/TailerTest.java index 825261e5..e1977b9d 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/io/TailerTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/io/TailerTest.java @@ -1,13 +1,13 @@ package io.leaderli.litool.core.io; import io.leaderli.litool.core.util.RandomUtil; -import io.leaderli.litool.core.util.ThreadUtil; +import org.junit.jupiter.api.Test; import java.io.File; class TailerTest { -// @Test + @Test void test() { Tailer tailer = new Tailer(new File("123.txt"), new TailerListener() { @@ -25,8 +25,8 @@ public void delay(Tailer tailer) { } } }); - ThreadUtil.start(tailer, false); +// ThreadUtil.start(tailer, false); } -} \ No newline at end of file +} diff --git a/litool-core/src/test/java/io/leaderli/litool/core/lang/ShellTest.java b/litool-core/src/test/java/io/leaderli/litool/core/lang/ShellTest.java index 12e76552..5669bab9 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/lang/ShellTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/lang/ShellTest.java @@ -1,5 +1,6 @@ package io.leaderli.litool.core.lang; +import io.leaderli.litool.core.env.OSInfo; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -17,7 +18,9 @@ class ShellTest { @Test void bash() throws ExecutionException, InterruptedException, TimeoutException { - + if (OSInfo.isWindows()) { + return; + } Future command = new Shell().bash("123"); Assertions.assertTrue(command.get().contains("not found")); Assertions.assertTrue(command.get().contains("not found")); diff --git a/litool-core/src/test/java/io/leaderli/litool/core/lang/lean/adapters/MapTypeAdapterFactoryTest.java b/litool-core/src/test/java/io/leaderli/litool/core/lang/lean/adapters/MapTypeAdapterFactoryTest.java index ee524891..0c4cec80 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/lang/lean/adapters/MapTypeAdapterFactoryTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/lang/lean/adapters/MapTypeAdapterFactoryTest.java @@ -1,5 +1,28 @@ package io.leaderli.litool.core.lang.lean.adapters; +import io.leaderli.litool.core.bean.Person; +import io.leaderli.litool.core.lang.lean.Lean; +import io.leaderli.litool.core.lang.lean.TypeAdapter; +import io.leaderli.litool.core.type.LiTypeToken; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Map; + class MapTypeAdapterFactoryTest { + @Test + void test() { + Lean lean = new Lean(); + + MapTypeAdapterFactory mapTypeAdapterFactory = new MapTypeAdapterFactory(); + TypeAdapter> mapTypeAdapter = mapTypeAdapterFactory.create(lean, new LiTypeToken>() { + }); + + Person person = new Person(); + person.setName("tom"); + Map read = mapTypeAdapter.read(person, lean); + Assertions.assertEquals("tom", read.get("name")); + Assertions.assertEquals("0", read.get("age")); + } } diff --git a/litool-core/src/test/java/io/leaderli/litool/core/meta/LinoTest.java b/litool-core/src/test/java/io/leaderli/litool/core/meta/LinoTest.java index 89474273..e4653d57 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/meta/LinoTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/meta/LinoTest.java @@ -277,8 +277,15 @@ void map() { @Test void throwable_map() { + WhenThrowBehavior.setIgnore(); + Assertions.assertFalse(Lino.none().hasError()); + Assertions.assertFalse(Lino.of(1).hasError()); + //noinspection divzero,NumericOverflow + Assertions.assertTrue(Lino.ofIgnoreError(() -> 1 / 0).hasError()); LiBox box = LiBox.none(); - Assertions.assertSame(Lino.of(0).mapIgnoreError(i -> 5 / i, null), Lino.none()); + Lino lino = Lino.of(0).mapIgnoreError(i -> 5 / i, null); + Assertions.assertTrue(lino.hasError()); + Assertions.assertInstanceOf(ArithmeticException.class, lino.getError()); Assertions.assertNull(box.value()); Lino.of(0).mapIgnoreError(i -> 5 / i, t -> box.value(2)); Assertions.assertSame(2, box.value()); @@ -287,10 +294,12 @@ void throwable_map() { StringWriter out = new StringWriter(); System.setErr(new PrintStream(out)); WhenThrowBehavior.setPrintStackTrace(); - Assertions.assertSame(Lino.of(0).mapIgnoreError(i -> 5 / i), Lino.none()); + lino = Lino.of(0).mapIgnoreError(i -> 5 / i); + Assertions.assertInstanceOf(ArithmeticException.class, lino.getError()); Assertions.assertTrue(out.get().startsWith("java.lang.ArithmeticException: / by zero")); } + @Test void toLira() { diff --git a/litool-core/src/test/java/io/leaderli/litool/core/type/ClassScannerTest.java b/litool-core/src/test/java/io/leaderli/litool/core/type/ClassScannerTest.java index 127f75ea..4e1b039f 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/type/ClassScannerTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/type/ClassScannerTest.java @@ -51,7 +51,7 @@ void getSubTypesOf() { Lira> subTypesOf = ClassScanner.getSubTypesOf(Lino.class.getPackage().getName(), Lino.class); - Assertions.assertEquals(2, subTypesOf.size()); + Assertions.assertEquals(3, subTypesOf.size()); } diff --git a/litool-core/src/test/java/io/leaderli/litool/core/type/PrimitiveEnumTest.java b/litool-core/src/test/java/io/leaderli/litool/core/type/PrimitiveEnumTestBean.java similarity index 99% rename from litool-core/src/test/java/io/leaderli/litool/core/type/PrimitiveEnumTest.java rename to litool-core/src/test/java/io/leaderli/litool/core/type/PrimitiveEnumTestBean.java index edf13da1..9f52ca78 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/type/PrimitiveEnumTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/type/PrimitiveEnumTestBean.java @@ -13,7 +13,7 @@ * @author leaderli * @since 2022/8/17 7:11 PM */ -class PrimitiveEnumTest { +class PrimitiveEnumTestBean { @Test void checkNotPrimitive() { diff --git a/litool-core/src/test/java/io/leaderli/litool/core/type/ReflectUtilTest.java b/litool-core/src/test/java/io/leaderli/litool/core/type/ReflectUtilTest.java index 47e5b8c6..f669dd4e 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/type/ReflectUtilTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/type/ReflectUtilTest.java @@ -1,6 +1,6 @@ package io.leaderli.litool.core.type; -import io.leaderli.litool.core.bean.EnumTest; +import io.leaderli.litool.core.bean.EnumTestBean; import io.leaderli.litool.core.io.StringWriter; import io.leaderli.litool.core.lang.DisposableRunnableProxy; import io.leaderli.litool.core.meta.Lino; @@ -241,7 +241,7 @@ void newInstance() throws NoSuchMethodException, InstantiationException, Illegal // enum - Class declaredClass = EnumTest.class.getDeclaredClasses()[0]; + Class declaredClass = EnumTestBean.class.getDeclaredClasses()[0]; Assertions.assertEquals("A", ReflectUtil.newInstance(declaredClass).get().toString()); } diff --git a/litool-core/src/test/java/io/leaderli/litool/core/util/NetUtilTest.java b/litool-core/src/test/java/io/leaderli/litool/core/util/NetUtilTest.java index 1bbbfc74..79cdda28 100644 --- a/litool-core/src/test/java/io/leaderli/litool/core/util/NetUtilTest.java +++ b/litool-core/src/test/java/io/leaderli/litool/core/util/NetUtilTest.java @@ -1,8 +1,10 @@ package io.leaderli.litool.core.util; +import io.leaderli.litool.core.env.OSInfo; import io.leaderli.litool.core.meta.WhenThrowBehavior; import io.leaderli.litool.core.resource.net.NetUtil; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * @author leaderli @@ -10,8 +12,11 @@ */ class NetUtilTest { - // @Test + @Test void pingable() { + if (OSInfo.isWindows()) { + return; + } WhenThrowBehavior.setIgnore(); Assertions.assertTrue(NetUtil.pingable("127.0.0.1")); Assertions.assertFalse(NetUtil.pingable("182.180.333.444")); diff --git a/litool-runner/src/test/java/io/leaderli/litool/runner/constant/OperatorEnumTest.java b/litool-runner/src/test/java/io/leaderli/litool/runner/constant/OperatorEnumTestBean.java similarity index 97% rename from litool-runner/src/test/java/io/leaderli/litool/runner/constant/OperatorEnumTest.java rename to litool-runner/src/test/java/io/leaderli/litool/runner/constant/OperatorEnumTestBean.java index 761e802a..005e9f41 100644 --- a/litool-runner/src/test/java/io/leaderli/litool/runner/constant/OperatorEnumTest.java +++ b/litool-runner/src/test/java/io/leaderli/litool/runner/constant/OperatorEnumTestBean.java @@ -8,7 +8,7 @@ * @author leaderli * @since 2022/8/13 9:53 AM */ -class OperatorEnumTest { +class OperatorEnumTestBean { @Test void test() { diff --git a/pom.xml b/pom.xml index 445934a4..51239933 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,13 @@ 2.8.9 test - + + org.jacoco + org.jacoco.agent + 0.8.7 + runtime + test +