Skip to content

Commit

Permalink
sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Nov 21, 2024
1 parent 1ef9373 commit ca916ff
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 20 deletions.
65 changes: 61 additions & 4 deletions litool-core/src/main/java/io/leaderli/litool/core/meta/Lino.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static <T> Lino<T> ofIgnoreError(ThrowableSupplier<? extends T> supplier) {
return new Some<>(value);
} catch (Throwable e) {
WhenThrowBehavior.whenThrow(ExceptionUtil.unwrapThrowable(e));
return none();
return new Error<>(e);
}
}

Expand Down Expand Up @@ -152,6 +152,19 @@ static <T> Lino<T> narrow(Lino<? extends T> value) {

}


/**
* @return 判断是否有异常
* @see Error
*/
boolean hasError();

/**
* @return 返回是否有异常
* @see Error
*/
Throwable getError();

/**
* 这是一个泛型接口Lino,该方法为其默认方法,作用是将传入的参数filter应用于泛型T,如果满足断言条件,则返回当前对象,否则抛出IllegalStateException异常。
*
Expand Down Expand Up @@ -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<T> assertTrue(Function<? super T, ?> filter, String msg) {
if (filter(filter).absent()) {
Expand Down Expand Up @@ -737,10 +760,10 @@ public <R> Lino<R> mapIgnoreError(ThrowableFunction<? super T, ? extends R> 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
Expand All @@ -766,7 +789,7 @@ public <R> Lira<R> toLira(LiTypeToken<? extends R> type) {
/**
* @param <T> 值的类型
*/
final class None<T> implements Lino<T> {
class None<T> implements Lino<T> {
private static final None<?> INSTANCE = new None<>();

private None() {
Expand All @@ -788,6 +811,16 @@ public String toString() {
return name() + "()";
}

@Override
public boolean hasError() {
return false;
}

@Override
public Throwable getError() {
return null;
}

@Override
public Lino<T> assertTrue(Function<? super T, ?> filter, String msg) {
throw new IllegalStateException(msg);
Expand Down Expand Up @@ -972,4 +1005,28 @@ public <R> Lira<R> toLira(LiTypeToken<? extends R> type) {
return Lira.none();
}
}

final class Error<T> extends None<T> {

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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.leaderli.litool.core.bean;

public class EnumTest {
public class EnumTestBean {

enum State {
A
Expand Down
24 changes: 24 additions & 0 deletions litool-core/src/test/java/io/leaderli/litool/core/bean/Person.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author leaderli
* @since 2022/7/21
*/
class BitPositionEnumTest {
class BitPositionEnumTestBean {


@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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());

}



Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -25,8 +25,8 @@ public void delay(Tailer tailer) {
}
}
});
ThreadUtil.start(tailer, false);
// ThreadUtil.start(tailer, false);

}

}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,7 +18,9 @@ class ShellTest {

@Test
void bash() throws ExecutionException, InterruptedException, TimeoutException {

if (OSInfo.isWindows()) {
return;
}
Future<String> command = new Shell().bash("123");
Assertions.assertTrue(command.get().contains("not found"));
Assertions.assertTrue(command.get().contains("not found"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Map<String, String>> mapTypeAdapter = mapTypeAdapterFactory.create(lean, new LiTypeToken<Map<String, String>>() {
});

Person person = new Person();
person.setName("tom");
Map<String, String> read = mapTypeAdapter.read(person, lean);
Assertions.assertEquals("tom", read.get("name"));
Assertions.assertEquals("0", read.get("age"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> box = LiBox.none();
Assertions.assertSame(Lino.of(0).mapIgnoreError(i -> 5 / i, null), Lino.none());
Lino<Integer> 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());
Expand All @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void getSubTypesOf() {
Lira<Class<Lino>> subTypesOf =
ClassScanner.getSubTypesOf(Lino.class.getPackage().getName(),
Lino.class);
Assertions.assertEquals(2, subTypesOf.size());
Assertions.assertEquals(3, subTypesOf.size());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author leaderli
* @since 2022/8/17 7:11 PM
*/
class PrimitiveEnumTest {
class PrimitiveEnumTestBean {
@Test
void checkNotPrimitive() {

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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
* @since 2022/6/27
*/
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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author leaderli
* @since 2022/8/13 9:53 AM
*/
class OperatorEnumTest {
class OperatorEnumTestBean {

@Test
void test() {
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@
<version>2.8.9</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.7</version>
<classifier>runtime</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit ca916ff

Please sign in to comment.