fix code to replace join

This commit is contained in:
Looly 2021-06-18 01:48:02 +08:00
parent 40a14e1508
commit 7baac80d32
9 changed files with 330 additions and 186 deletions

View File

@ -7,59 +7,81 @@ import java.util.NoSuchElementException;
/**
* 数组Iterator对象
* @author Looly
*
* @param <E> 元素类型
* @author Looly
* @since 4.1.1
*/
public class ArrayIter<E> implements Iterator<E>, Iterable<E>, Serializable{
public class ArrayIter<E> implements Iterator<E>, Iterable<E>, Serializable {
private static final long serialVersionUID = 1L;
/** 数组 */
/**
* 数组
*/
private final Object array;
/** 起始位置 */
/**
* 起始位置
*/
private int startIndex;
/** 结束位置 */
/**
* 结束位置
*/
private int endIndex;
/** 当前位置 */
/**
* 当前位置
*/
private int index;
/**
* 构造
*
* @param array 数组
* @throws IllegalArgumentException array对象不为数组抛出此异常
* @throws NullPointerException array对象为null
* @throws NullPointerException array对象为null
*/
public ArrayIter(final Object array) {
public ArrayIter(E[] array) {
this((Object) array);
}
/**
* 构造
*
* @param array 数组
* @throws IllegalArgumentException array对象不为数组抛出此异常
* @throws NullPointerException array对象为null
*/
public ArrayIter(Object array) {
this(array, 0);
}
/**
* 构造
* @param array 数组
*
* @param array 数组
* @param startIndex 起始位置当起始位置小于0或者大于结束位置置为0
* @throws IllegalArgumentException array对象不为数组抛出此异常
* @throws NullPointerException array对象为null
* @throws NullPointerException array对象为null
*/
public ArrayIter(final Object array, final int startIndex) {
public ArrayIter(Object array, int startIndex) {
this(array, startIndex, -1);
}
/**
* 构造
* @param array 数组
*
* @param array 数组
* @param startIndex 起始位置当起始位置小于0或者大于结束位置置为0
* @param endIndex 结束位置当结束位置小于0或者大于数组长度置为数组长度
* @param endIndex 结束位置当结束位置小于0或者大于数组长度置为数组长度
* @throws IllegalArgumentException array对象不为数组抛出此异常
* @throws NullPointerException array对象为null
* @throws NullPointerException array对象为null
*/
public ArrayIter(final Object array, final int startIndex, final int endIndex) {
this.endIndex = Array.getLength(array);
if(endIndex > 0 && endIndex < this.endIndex){
if (endIndex > 0 && endIndex < this.endIndex) {
this.endIndex = endIndex;
}
if(startIndex >=0 && startIndex < this.endIndex){
if (startIndex >= 0 && startIndex < this.endIndex) {
this.startIndex = startIndex;
}
this.array = array;
@ -77,11 +99,12 @@ public class ArrayIter<E> implements Iterator<E>, Iterable<E>, Serializable{
if (hasNext() == false) {
throw new NoSuchElementException();
}
return (E)Array.get(array, index++);
return (E) Array.get(array, index++);
}
/**
* 不允许操作数组元素
*
* @throws UnsupportedOperationException always
*/
@Override
@ -91,6 +114,7 @@ public class ArrayIter<E> implements Iterator<E>, Iterable<E>, Serializable{
// Properties
// -----------------------------------------------------------------------
/**
* 获得原始数组对象
*

View File

@ -6,10 +6,8 @@ import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrJoiner;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import java.util.ArrayList;
import java.util.Collection;
@ -246,7 +244,7 @@ public class IterUtil {
* @return 连接后的字符串
*/
public static <T> String join(Iterator<T> iterator, CharSequence conjunction) {
return join(iterator, conjunction, null, null);
return StrJoiner.of(conjunction).append(iterator).toString();
}
/**
@ -262,17 +260,11 @@ public class IterUtil {
* @since 4.0.10
*/
public static <T> String join(Iterator<T> iterator, CharSequence conjunction, String prefix, String suffix) {
return join(iterator, conjunction, (item) -> {
if (ArrayUtil.isArray(item)) {
return ArrayUtil.join(ArrayUtil.wrap(item), conjunction, prefix, suffix);
} else if (item instanceof Iterable) {
return CollUtil.join((Iterable<?>) item, conjunction, prefix, suffix);
} else if (item instanceof Iterator) {
return join((Iterator<?>) item, conjunction, prefix, suffix);
} else {
return StrUtil.wrap(String.valueOf(item), prefix, suffix);
}
});
return StrJoiner.of(conjunction, prefix, suffix)
// 每个元素都添加前后缀
.setWrapElement(true)
.append(iterator)
.toString();
}
/**
@ -291,7 +283,7 @@ public class IterUtil {
return null;
}
return new StrJoiner(conjunction).append(iterator, func).toString();
return StrJoiner.of(conjunction).append(iterator, func).toString();
}
/**

View File

@ -1,7 +1,5 @@
package cn.hutool.core.io.resource;
import java.net.URL;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ClassUtil;
@ -9,6 +7,8 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import java.net.URL;
/**
* ClassPath单一资源访问类<br>
* 传入路径path必须为相对路径如果传入绝对路径Linux路径会去掉开头的/而Windows路径会直接报错<br>

View File

@ -22,11 +22,38 @@ public class StrJoiner implements Appendable {
private CharSequence delimiter;
private CharSequence prefix;
private CharSequence suffix;
/**
* appendable中是否包含内容
*/
// 前缀和后缀是否包装每个元素true表示包装每个元素false包装整个字符串
private boolean wrapElement;
// null元素处理逻辑
private NullMode nullMode = NullMode.NULL_STRING;
// 当结果为空时默认返回的拼接结果
private String emptyResult = StrUtil.EMPTY;
// appendable中是否包含内容用于判断增加内容时是否首先加入分隔符
private boolean hasContent;
/**
* 使用指定分隔符创建{@link StrJoiner}
*
* @param delimiter 分隔符
* @return {@link StrJoiner}
*/
public static StrJoiner of(CharSequence delimiter) {
return new StrJoiner(delimiter);
}
/**
* 使用指定分隔符创建{@link StrJoiner}
*
* @param delimiter 分隔符
* @param prefix 前缀
* @param suffix 后缀
* @return {@link StrJoiner}
*/
public static StrJoiner of(CharSequence delimiter, CharSequence prefix, CharSequence suffix) {
return new StrJoiner(delimiter, prefix, suffix);
}
/**
* 构造
*
@ -49,9 +76,9 @@ public class StrJoiner implements Appendable {
/**
* 构造
*
* @param delimiter 分隔符{@code null}表示无连接符直接拼接
* @param prefix 前缀
* @param suffix 后缀
* @param delimiter 分隔符{@code null}表示无连接符直接拼接
* @param prefix 前缀
* @param suffix 后缀
*/
public StrJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) {
this(null, delimiter, prefix, suffix);
@ -62,18 +89,14 @@ public class StrJoiner implements Appendable {
*
* @param appendable 字符串追加器拼接的字符串都将加入到此{@code null}使用默认{@link StringBuilder}
* @param delimiter 分隔符{@code null}表示无连接符直接拼接
* @param prefix 前缀
* @param suffix 后缀
* @param prefix 前缀
* @param suffix 后缀
*/
public StrJoiner(Appendable appendable, CharSequence delimiter,
CharSequence prefix, CharSequence suffix) {
if (null != appendable) {
this.appendable = appendable;
final String initStr = appendable.toString();
if (StrUtil.isNotEmpty(initStr) && false == StrUtil.endWith(initStr, delimiter)) {
// 用户传入的Appendable中已经存在内容且末尾不是分隔符
this.hasContent = true;
}
checkHasContent(appendable);
}
this.delimiter = delimiter;
@ -115,34 +138,98 @@ public class StrJoiner implements Appendable {
}
/**
* 追加{@link Iterator}中的元素到拼接器中
* 设置前缀和后缀是否包装每个元素
*
* @param <T> 元素类型
* @param iterable 元素列表
* @param wrapElement true表示包装每个元素false包装整个字符串
* @return this
*/
public <T> StrJoiner append(Iterable<T> iterable) {
return append(IterUtil.getIter(iterable));
public StrJoiner setWrapElement(boolean wrapElement) {
this.wrapElement = wrapElement;
return this;
}
/**
* 设置{@code null}元素处理逻辑
*
* @param nullMode 逻辑枚举可选忽略转换为""或转换为null字符串
* @return this
*/
public StrJoiner setNullMode(NullMode nullMode) {
this.nullMode = nullMode;
return this;
}
/**
* 设置当没有任何元素加入时默认返回的字符串默认""
*
* @param emptyResult 默认字符串
* @return this
*/
public StrJoiner setEmptyResult(String emptyResult) {
this.emptyResult = emptyResult;
return this;
}
/**
* 追加对象到拼接器中
*
* @param <T> 元素类型
* @param obj 对象支持数组集合等
* @return this
*/
public <T> StrJoiner append(Object obj) {
if (null == obj) {
append((CharSequence) null);
} else if (ArrayUtil.isArray(obj)) {
append(new ArrayIter<>(obj));
} else if (obj instanceof Iterator) {
append((Iterator<?>) obj);
} else if (obj instanceof Iterable) {
append(((Iterable<?>) obj).iterator());
} else {
append(String.valueOf(obj));
}
return this;
}
/**
* 追加数组中的元素到拼接器中
*
* @param <T> 元素类型
* @param array 元素数组
* @return this
*/
public <T> StrJoiner append(T[] array) {
if(null == array){
return this;
}
return append(new ArrayIter<>(array));
}
/**
* 追加{@link Iterator}中的元素到拼接器中
*
* @param <T> 元素类型
* @param iterator 元素列表
* @param <T> 元素类型
* @param iterator 元素列表
* @return this
*/
public <T> StrJoiner append(Iterator<T> iterator) {
return append(iterator, (t)->{
if(ArrayUtil.isArray(t)){
return new StrJoiner(this.delimiter).append((Iterator<?>) new ArrayIter<>(t)).toString();
} else if(t instanceof Iterator){
return new StrJoiner(this.delimiter).append((Iterator<?>)t).toString();
} else if(t instanceof Iterable){
return new StrJoiner(this.delimiter).append((Iterable<?>)t).toString();
}
return String.valueOf(t);
});
if(null == iterator){
return this;
}
return append(iterator, (t) -> StrJoiner.of(this.delimiter).append(t).toString());
}
/**
* 追加数组中的元素到拼接器中
*
* @param <T> 元素类型
* @param array 元素数组
* @param toStrFunc 元素对象转换为字符串的函数
* @return this
*/
public <T> StrJoiner append(T[] array, Function<T, ? extends CharSequence> toStrFunc) {
return append((Iterator<T>) new ArrayIter<>(array), toStrFunc);
}
/**
@ -176,8 +263,26 @@ public class StrJoiner implements Appendable {
@Override
public StrJoiner append(CharSequence csq) {
if (null == csq) {
switch (this.nullMode) {
case IGNORE:
return this;
case TO_EMPTY:
csq = StrUtil.EMPTY;
break;
case NULL_STRING:
csq = StrUtil.NULL;
}
}
try {
prepare().append(csq);
final Appendable appendable = prepare();
if (wrapElement && StrUtil.isNotEmpty(this.prefix)) {
appendable.append(prefix);
}
appendable.append(csq);
if (wrapElement && StrUtil.isNotEmpty(this.suffix)) {
appendable.append(suffix);
}
} catch (IOException e) {
throw new IORuntimeException(e);
}
@ -185,35 +290,46 @@ public class StrJoiner implements Appendable {
}
@Override
public StrJoiner append(CharSequence csq, int start, int end) {
try {
prepare().append(csq, start, end);
} catch (IOException e) {
throw new IORuntimeException(e);
}
return this;
public StrJoiner append(CharSequence csq, int startInclude, int endExclude) {
return append(StrUtil.sub(csq, startInclude, endExclude));
}
@Override
public StrJoiner append(char c) {
try {
prepare().append(c);
} catch (IOException e) {
throw new IORuntimeException(e);
}
return this;
return append(String.valueOf(c));
}
@Override
public String toString() {
if (StrUtil.isNotEmpty(this.suffix)) {
if(null == this.appendable){
return emptyResult;
}
if (false == wrapElement && StrUtil.isNotEmpty(this.suffix)) {
try {
appendable.append(this.suffix);
this.appendable.append(this.suffix);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
return appendable.toString();
return this.appendable.toString();
}
/**
* {@code null}处理的模式
*/
public enum NullMode {
/**
* 忽略{@code null}即null元素不加入拼接的字符串
*/
IGNORE,
/**
* {@code null}转为""
*/
TO_EMPTY,
/**
* {@code null}转为null字符串
*/
NULL_STRING
}
/**
@ -226,12 +342,33 @@ public class StrJoiner implements Appendable {
if (hasContent) {
this.appendable.append(delimiter);
} else {
this.appendable = new StringBuilder();
if (StrUtil.isNotEmpty(this.prefix)) {
if (null == this.appendable) {
this.appendable = new StringBuilder();
}
if (false == wrapElement && StrUtil.isNotEmpty(this.prefix)) {
this.appendable.append(this.prefix);
}
this.hasContent = true;
}
return this.appendable;
}
/**
* 检查用户传入的{@link Appendable} 是否已经存在内容而且不能以分隔符结尾
*
* @param appendable {@link Appendable}
*/
private void checkHasContent(Appendable appendable) {
if (appendable instanceof CharSequence) {
final CharSequence charSequence = (CharSequence) appendable;
if (charSequence.length() > 0 && StrUtil.endWith(charSequence, delimiter)) {
this.hasContent = true;
}
} else {
final String initStr = appendable.toString();
if (StrUtil.isNotEmpty(initStr) && false == StrUtil.endWith(initStr, delimiter)) {
this.hasContent = true;
}
}
}
}

View File

@ -2,7 +2,6 @@ package cn.hutool.core.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert;
@ -10,6 +9,7 @@ import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.Matcher;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrJoiner;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
@ -165,7 +165,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
@SuppressWarnings("unchecked")
public static <T> T firstMatch(Matcher<T> matcher, T... array) {
final int index = matchIndex(matcher, array);
if(index < 0){
if (index < 0) {
return null;
}
@ -184,8 +184,8 @@ public class ArrayUtil extends PrimitiveArrayUtil {
@SuppressWarnings("unchecked")
public static <T> int matchIndex(Matcher<T> matcher, T... array) {
if (isNotEmpty(array)) {
for(int i = 0; i < array.length; i++){
if(matcher.match(array[i])){
for (int i = 0; i < array.length; i++) {
if (matcher.match(array[i])) {
return i;
}
}
@ -621,7 +621,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.2.1
*/
public static <T> T[] filter(T[] array, Filter<T> filter) {
if(null == array || null == filter){
if (null == array || null == filter) {
return array;
}
return edit(array, t -> filter.accept(t) ? t : null);
@ -737,7 +737,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.0.7
*/
public static <T> int indexOf(T[] array, Object value) {
return matchIndex((obj)-> ObjectUtil.equal(value, obj), array);
return matchIndex((obj) -> ObjectUtil.equal(value, obj), array);
}
/**
@ -1114,38 +1114,24 @@ public class ArrayUtil extends PrimitiveArrayUtil {
/**
* conjunction 为分隔符将数组转换为字符串
*
* @param <T> 被处理的集合
* @param array 数组
* @param conjunction 分隔符
* @param prefix 每个元素添加的前缀null表示不添加
* @param suffix 每个元素添加的后缀null表示不添加
* @param <T> 被处理的集合
* @param array 数组
* @param delimiter 分隔符
* @param prefix 每个元素添加的前缀null表示不添加
* @param suffix 每个元素添加的后缀null表示不添加
* @return 连接后的字符串
* @since 4.0.10
*/
public static <T> String join(T[] array, CharSequence conjunction, String prefix, String suffix) {
public static <T> String join(T[] array, CharSequence delimiter, String prefix, String suffix) {
if (null == array) {
return null;
}
final StringBuilder sb = new StringBuilder();
boolean isFirst = true;
for (T item : array) {
if (isFirst) {
isFirst = false;
} else {
sb.append(conjunction);
}
if (ArrayUtil.isArray(item)) {
sb.append(join(ArrayUtil.wrap(item), conjunction, prefix, suffix));
} else if (item instanceof Iterable<?>) {
sb.append(CollUtil.join((Iterable<?>) item, conjunction, prefix, suffix));
} else if (item instanceof Iterator<?>) {
sb.append(IterUtil.join((Iterator<?>) item, conjunction, prefix, suffix));
} else {
sb.append(StrUtil.wrap(StrUtil.toString(item), prefix, suffix));
}
}
return sb.toString();
return StrJoiner.of(delimiter, prefix, suffix)
// 每个元素都添加前后缀
.setWrapElement(true)
.append(array)
.toString();
}
/**
@ -1159,26 +1145,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 5.3.3
*/
public static <T> String join(T[] array, CharSequence conjunction, Editor<T> editor) {
if (null == array) {
return null;
}
final StringBuilder sb = new StringBuilder();
boolean isFirst = true;
for (T item : array) {
if (isFirst) {
isFirst = false;
} else {
sb.append(conjunction);
}
if (null != editor) {
item = editor.edit(item);
}
if (null != item) {
sb.append(StrUtil.toString(item));
}
}
return sb.toString();
return StrJoiner.of(conjunction).append(array, (t)-> String.valueOf(editor.edit(t))).toString();
}
/**
@ -1189,39 +1156,14 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @return 连接后的字符串
*/
public static String join(Object array, CharSequence conjunction) {
if(null == array){
throw new NullPointerException("Array must be not null!");
if (null == array) {
return null;
}
if (false == isArray(array)) {
throw new IllegalArgumentException(StrUtil.format("[{}] is not a Array!", array.getClass()));
}
final Class<?> componentType = array.getClass().getComponentType();
if (componentType.isPrimitive()) {
final String componentTypeName = componentType.getName();
switch (componentTypeName) {
case "long":
return join((long[]) array, conjunction);
case "int":
return join((int[]) array, conjunction);
case "short":
return join((short[]) array, conjunction);
case "char":
return join((char[]) array, conjunction);
case "byte":
return join((byte[]) array, conjunction);
case "boolean":
return join((boolean[]) array, conjunction);
case "float":
return join((float[]) array, conjunction);
case "double":
return join((double[]) array, conjunction);
default:
throw new UtilException("Unknown primitive type: [{}]", componentTypeName);
}
} else {
return join((Object[]) array, conjunction);
}
return StrJoiner.of(conjunction).append(array).toString();
}
/**

View File

@ -1593,8 +1593,9 @@ public class PrimitiveArrayUtil {
*
* @param array 数组
* @param conjunction 分隔符
* @return 连接后的字符串
* @deprecated 请使用 {@link ArrayUtil#join(Object, CharSequence)}
*/
@Deprecated
public static String join(int[] array, CharSequence conjunction) {
if (null == array) {
return null;
@ -1618,8 +1619,9 @@ public class PrimitiveArrayUtil {
*
* @param array 数组
* @param conjunction 分隔符
* @return 连接后的字符串
* @deprecated 请使用 {@link ArrayUtil#join(Object, CharSequence)}
*/
@Deprecated
public static String join(long[] array, CharSequence conjunction) {
if (null == array) {
return null;
@ -1643,8 +1645,9 @@ public class PrimitiveArrayUtil {
*
* @param array 数组
* @param conjunction 分隔符
* @return 连接后的字符串
* @deprecated 请使用 {@link ArrayUtil#join(Object, CharSequence)}
*/
@Deprecated
public static String join(short[] array, CharSequence conjunction) {
if (null == array) {
return null;
@ -1668,8 +1671,9 @@ public class PrimitiveArrayUtil {
*
* @param array 数组
* @param conjunction 分隔符
* @return 连接后的字符串
* @deprecated 请使用 {@link ArrayUtil#join(Object, CharSequence)}
*/
@Deprecated
public static String join(char[] array, CharSequence conjunction) {
if (null == array) {
return null;
@ -1693,8 +1697,9 @@ public class PrimitiveArrayUtil {
*
* @param array 数组
* @param conjunction 分隔符
* @return 连接后的字符串
* @deprecated 请使用 {@link ArrayUtil#join(Object, CharSequence)}
*/
@Deprecated
public static String join(byte[] array, CharSequence conjunction) {
if (null == array) {
return null;
@ -1719,7 +1724,9 @@ public class PrimitiveArrayUtil {
* @param array 数组
* @param conjunction 分隔符
* @return 连接后的字符串
* @deprecated 请使用 {@link ArrayUtil#join(Object, CharSequence)}
*/
@Deprecated
public static String join(boolean[] array, CharSequence conjunction) {
if (null == array) {
return null;
@ -1744,7 +1751,9 @@ public class PrimitiveArrayUtil {
* @param array 数组
* @param conjunction 分隔符
* @return 连接后的字符串
* @deprecated 请使用 {@link ArrayUtil#join(Object, CharSequence)}
*/
@Deprecated
public static String join(float[] array, CharSequence conjunction) {
if (null == array) {
return null;
@ -1769,7 +1778,9 @@ public class PrimitiveArrayUtil {
* @param array 数组
* @param conjunction 分隔符
* @return 连接后的字符串
* @deprecated 请使用 {@link ArrayUtil#join(Object, CharSequence)}
*/
@Deprecated
public static String join(double[] array, CharSequence conjunction) {
if (null == array) {
return null;

View File

@ -32,6 +32,7 @@ public class IterUtilTest {
String join1 = IterUtil.join(list1.iterator(), ":");
Assert.assertEquals("1:2:3:4", join1);
// 包装每个节点
ArrayList<String> list2 = CollUtil.newArrayList("1", "2", "3", "4");
String join2 = IterUtil.join(list2.iterator(), ":", "\"", "\"");
Assert.assertEquals("\"1\":\"2\":\"3\":\"4\"", join2);
@ -44,6 +45,13 @@ public class IterUtilTest {
Assert.assertEquals("1:2:3:4", join);
}
@Test
public void joinWithNullTest() {
ArrayList<String> list = CollUtil.newArrayList("1", null, "3", "4");
String join = IterUtil.join(list.iterator(), ":", String::valueOf);
Assert.assertEquals("1:null:3:4", join);
}
@Test
public void testToListMap() {
Map<String, List<String>> expectedMap = new HashMap<>();

View File

@ -1,58 +1,58 @@
package cn.hutool.core.io;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.Properties;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
/**
* ClassPath资源读取测试
* @author Looly
*
* @author Looly
*/
public class ClassPathResourceTest {
@Test
public void readStringTest() throws IOException{
public void readStringTest() {
ClassPathResource resource = new ClassPathResource("test.properties");
String content = resource.readUtf8Str();
Assert.assertTrue(StrUtil.isNotEmpty(content));
}
@Test
public void readStringTest2() throws IOException{
public void readStringTest2() {
// 读取classpath根目录测试
ClassPathResource resource = new ClassPathResource("/");
String content = resource.readUtf8Str();
Assert.assertTrue(StrUtil.isNotEmpty(content));
}
@Test
public void readTest() throws IOException{
public void readTest() throws IOException {
ClassPathResource resource = new ClassPathResource("test.properties");
Properties properties = new Properties();
properties.load(resource.getStream());
Assert.assertEquals("1", properties.get("a"));
Assert.assertEquals("2", properties.get("b"));
}
@Test
public void readFromJarTest() throws IOException{
public void readFromJarTest() {
//测试读取junit的jar包下的LICENSE-junit.txt文件
final ClassPathResource resource = new ClassPathResource("LICENSE-junit.txt");
String result = resource.readUtf8Str();
Assert.assertNotNull(result);
//二次读取测试用于测试关闭流对再次读取的影响
result = resource.readUtf8Str();
Assert.assertNotNull(result);
}
@Test
public void getAbsTest() {
final ClassPathResource resource = new ClassPathResource("LICENSE-junit.txt");

View File

@ -0,0 +1,30 @@
package cn.hutool.core.text;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class StrJoinerTest {
@Test
public void joinIntArrayTest(){
int[] a = {1,2,3,4,5};
final StrJoiner append = StrJoiner.of(",").append(a);
Assert.assertEquals("1,2,3,4,5", append.toString());
}
@Test
public void joinEmptyTest(){
List<String> list = new ArrayList<>();
final StrJoiner append = StrJoiner.of(",").append(list);
Assert.assertEquals("", append.toString());
}
@Test
public void noJoinTest(){
final StrJoiner append = StrJoiner.of(",");
Assert.assertEquals("", append.toString());
}
}