mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-19 09:43:29 +08:00
refactor: always use the collection method in core module
This commit is contained in:
@@ -3,7 +3,7 @@ package com.alibaba.testable.agent.handler;
|
||||
import com.alibaba.testable.agent.handler.test.JUnit4Framework;
|
||||
import com.alibaba.testable.agent.handler.test.JUnit5Framework;
|
||||
import com.alibaba.testable.agent.util.ClassUtil;
|
||||
import com.alibaba.testable.agent.util.CollectionUtil;
|
||||
import com.alibaba.testable.core.util.CollectionUtil;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
@@ -139,7 +139,7 @@ public class OmniClassHandler extends BaseClassHandler {
|
||||
}
|
||||
|
||||
private List<LocalVariableNode> createLocalVariables(ClassNode cn, LabelNode start, LabelNode end) {
|
||||
return CollectionUtil.listOf(
|
||||
return CollectionUtil.mutableListOf(
|
||||
new LocalVariableNode(THIS_REF, ClassUtil.toByteCodeClassName(cn.name), null, start, end, 0),
|
||||
new LocalVariableNode(IGNORE, ClassUtil.toByteCodeClassName(VOID_TYPE), null, start, end, 1)
|
||||
);
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.alibaba.testable.agent.handler.test;
|
||||
|
||||
import com.alibaba.testable.agent.model.TestCaseMethodType;
|
||||
import com.alibaba.testable.agent.util.ClassUtil;
|
||||
import com.alibaba.testable.agent.util.CollectionUtil;
|
||||
import com.alibaba.testable.core.util.CollectionUtil;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.alibaba.testable.agent.handler.test;
|
||||
|
||||
import com.alibaba.testable.agent.model.TestCaseMethodType;
|
||||
import com.alibaba.testable.agent.util.CollectionUtil;
|
||||
import com.alibaba.testable.core.util.CollectionUtil;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.alibaba.testable.agent.util;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author flin
|
||||
*/
|
||||
public class CollectionUtil {
|
||||
|
||||
/**
|
||||
* Check two collection has any equaled item
|
||||
* @param collectionLeft the first collection
|
||||
* @param collectionRight the second collection
|
||||
* @return found or not
|
||||
*/
|
||||
public static boolean containsAny(Collection<?> collectionLeft, Collection<?> collectionRight) {
|
||||
for (Object o : collectionLeft) {
|
||||
for (Object i : collectionRight) {
|
||||
if (o.equals(i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a list of item
|
||||
* @param items elements to add
|
||||
* @param <T> type of element
|
||||
* @return a ArrayList of provided elements
|
||||
*/
|
||||
public static <T> List<T> listOf(T... items) {
|
||||
List<T> list = new ArrayList<T>(items.length);
|
||||
Collections.addAll(list, items);
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package com.alibaba.testable.agent.util;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
|
||||
import static com.alibaba.testable.agent.util.CollectionUtil.listOf;
|
||||
import static com.alibaba.testable.core.util.CollectionUtil.listOf;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AnnotationUtilTest {
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.alibaba.testable.agent.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CollectionUtilTest {
|
||||
|
||||
@Test
|
||||
void should_check_collection_contains_any_element() {
|
||||
assertTrue(CollectionUtil.containsAny(
|
||||
CollectionUtil.listOf("a", "b"), CollectionUtil.listOf("b", "c")
|
||||
));
|
||||
assertFalse(CollectionUtil.containsAny(
|
||||
CollectionUtil.listOf("a", "b"), CollectionUtil.listOf("c", "d")
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,24 +7,41 @@ import java.util.*;
|
||||
public class CollectionUtil {
|
||||
|
||||
/**
|
||||
* Get slice of args[pos, args.length]
|
||||
* Get slice of args[startPos, args.length]
|
||||
* @param args original item array
|
||||
* @param startPos index of the first item to keep
|
||||
* @return a new array with sliced items
|
||||
*/
|
||||
public static Object[] slice(Object[] args, int pos) {
|
||||
int size = args.length - pos;
|
||||
public static Object[] slice(Object[] args, int startPos) {
|
||||
return slice(args, startPos, args.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get slice of args[startPos, endPos]
|
||||
* @param args original item array
|
||||
* @param startPos index of the first item to keep
|
||||
* @param endPos index of the last item to keep
|
||||
* @return a new array with sliced items
|
||||
*/
|
||||
public static Object[] slice(Object[] args, int startPos, int endPos) {
|
||||
int size = endPos - startPos + 1;
|
||||
if (size <= 0) {
|
||||
return new Object[0];
|
||||
}
|
||||
Object[] slicedArgs = new Object[size];
|
||||
System.arraycopy(args, pos, slicedArgs, 0, size);
|
||||
System.arraycopy(args, startPos, slicedArgs, 0, size);
|
||||
return slicedArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Join a collection to string
|
||||
* Join a collection into string
|
||||
* @param collection many items with proper toString() method
|
||||
* @param joinSymbol splitter of echo items
|
||||
* @return a joined string
|
||||
*/
|
||||
public static String join(Collection<?> collection, String joinSymbol) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(Iterator<?> i = collection.iterator(); i.hasNext(); sb.append((String)i.next())) {
|
||||
for(Iterator<?> i = collection.iterator(); i.hasNext(); sb.append(i.next().toString())) {
|
||||
if (sb.length() != 0) {
|
||||
sb.append(joinSymbol);
|
||||
}
|
||||
@@ -34,6 +51,9 @@ public class CollectionUtil {
|
||||
|
||||
/**
|
||||
* Check whether target exist in collection
|
||||
* @param collection many items to find from
|
||||
* @param target an item to be found
|
||||
* @return whether target exist in collection
|
||||
*/
|
||||
public static <T> boolean contains(T[] collection, T target) {
|
||||
for (T item : collection) {
|
||||
@@ -44,8 +64,27 @@ public class CollectionUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check two collection has any equaled item
|
||||
* @param collectionLeft the first collection
|
||||
* @param collectionRight the second collection
|
||||
* @return whether any equaled item found
|
||||
*/
|
||||
public static boolean containsAny(Collection<?> collectionLeft, Collection<?> collectionRight) {
|
||||
for (Object o : collectionLeft) {
|
||||
for (Object i : collectionRight) {
|
||||
if (o.equals(i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an array
|
||||
* @param items elements to add
|
||||
* @return array of the provided items
|
||||
*/
|
||||
public static <T> T[] arrayOf(T... items) {
|
||||
return items;
|
||||
@@ -53,13 +92,28 @@ public class CollectionUtil {
|
||||
|
||||
/**
|
||||
* Create a list
|
||||
* @param items elements to add
|
||||
* @return list of the provided items
|
||||
*/
|
||||
public static <T> List<T> listOf(T... items) {
|
||||
return Arrays.asList(items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a list of item
|
||||
* @param items elements to add
|
||||
* @return mutable list of the provided items
|
||||
*/
|
||||
public static <T> List<T> mutableListOf(T... items) {
|
||||
List<T> list = new ArrayList<T>(items.length);
|
||||
Collections.addAll(list, items);
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a set
|
||||
* @param items elements to add
|
||||
* @return set of the provided items
|
||||
*/
|
||||
public static <T> Set<T> setOf(T... items) {
|
||||
return new HashSet<T>(Arrays.asList(items));
|
||||
@@ -67,6 +121,8 @@ public class CollectionUtil {
|
||||
|
||||
/**
|
||||
* Create a map
|
||||
* @param pair elements to add
|
||||
* @return map of the provided items
|
||||
*/
|
||||
public static <K, V> Map<K, V> mapOf(Pair<K, V>... pair) {
|
||||
return mapOf(new HashMap<K, V>(pair.length), pair);
|
||||
@@ -74,6 +130,8 @@ public class CollectionUtil {
|
||||
|
||||
/**
|
||||
* Create an ordered map
|
||||
* @param pair elements to add
|
||||
* @return ordered map of the provided items
|
||||
*/
|
||||
public static <K, V> Map<K, V> orderMapOf(Pair<K, V>... pair) {
|
||||
return mapOf(new LinkedHashMap<K, V>(pair.length), pair);
|
||||
|
||||
@@ -3,16 +3,31 @@ package com.alibaba.testable.core.util;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.alibaba.testable.core.tool.PrivateAccessor.invokeStatic;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CollectionUtilTest {
|
||||
|
||||
@Test
|
||||
void should_slice_array() {
|
||||
Object[] args = new Object[]{"1", "2", "3"};
|
||||
Object[] slicedArgs = invokeStatic(CollectionUtil.class, "slice", args, 1);
|
||||
Object[] args = new Object[]{"1", "2", "3", "4"};
|
||||
Object[] slicedArgs = CollectionUtil.slice(args, 1);
|
||||
assertEquals(3, slicedArgs.length);
|
||||
assertEquals("2", slicedArgs[0]);
|
||||
assertEquals("3", slicedArgs[1]);
|
||||
assertEquals("4", slicedArgs[2]);
|
||||
slicedArgs = CollectionUtil.slice(args, 1, 2);
|
||||
assertEquals(2, slicedArgs.length);
|
||||
assertEquals("2", slicedArgs[0]);
|
||||
assertEquals("3", slicedArgs[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_check_collection_contains_any_element() {
|
||||
assertTrue(CollectionUtil.containsAny(
|
||||
CollectionUtil.listOf("a", "b"), CollectionUtil.listOf("b", "c")
|
||||
));
|
||||
assertFalse(CollectionUtil.containsAny(
|
||||
CollectionUtil.listOf("a", "b"), CollectionUtil.listOf("c", "d")
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user