瀏覽代碼

add test case

zhou-hao 7 年之前
父節點
當前提交
1fb532fb17

+ 6 - 3
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/AopUtils.java

@@ -27,7 +27,10 @@ import java.lang.reflect.Method;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
-public class AopUtils {
+public final class AopUtils {
+
+    private AopUtils() {
+    }
 
     public static <T extends Annotation> T findMethodAnnotation(Class targetClass, Method method, Class<T> annClass) {
         Method m = method;
@@ -59,7 +62,7 @@ public class AopUtils {
         return findAnnotation(targetClass, m, annClass);
     }
 
-    public static final String getMethodBody(JoinPoint pjp) {
+    public static String getMethodBody(JoinPoint pjp) {
         StringBuilder methodName = new StringBuilder(pjp.getSignature().getName()).append("(");
         MethodSignature signature = (MethodSignature) pjp.getSignature();
         String[] names = signature.getParameterNames();
@@ -73,7 +76,7 @@ public class AopUtils {
         return methodName.append(")").toString();
     }
 
-    public static final Map<String, Object> getArgsMap(JoinPoint pjp) {
+    public static Map<String, Object> getArgsMap(JoinPoint pjp) {
         MethodSignature signature = (MethodSignature) pjp.getSignature();
         Map<String, Object> args = new LinkedHashMap<>();
         String names[] = signature.getParameterNames();

+ 0 - 2
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/ApplicationContextHolder.java

@@ -5,8 +5,6 @@ import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Component;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
  */
 @Component

+ 4 - 6
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/RegexUtils.java

@@ -6,12 +6,10 @@ import java.util.List;
 import java.util.Set;
 
 /**
- * TODO 完成注释
- *
  * @author zhouhao
  */
 public class RegexUtils {
-    static Set<Character> SPECIAL_WORDS = new HashSet<>(Arrays.asList('\\', '$', '(', ')', '*', '+', '.', '[', ']', '?', '^', '{', '}', '|'));
+    private static Set<Character> SPECIAL_WORDS = new HashSet<>(Arrays.asList('\\', '$', '(', ')', '*', '+', '.', '[', ']', '?', '^', '{', '}', '|'));
 
     public static String escape(String regex) {
         if (regex == null || regex.isEmpty()) {
@@ -19,11 +17,11 @@ public class RegexUtils {
         }
         char[] chars = regex.toCharArray();
         StringBuilder builder = new StringBuilder();
-        for (int i = 0; i < chars.length; i++) {
-            if (SPECIAL_WORDS.contains(chars[i])) {
+        for (char aChar : chars) {
+            if (SPECIAL_WORDS.contains(aChar)) {
                 builder.append('\\');
             }
-            builder.append(chars[i]);
+            builder.append(aChar);
         }
         return builder.toString();
     }

+ 4 - 2
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/Sqls.java

@@ -30,8 +30,10 @@ public class Sqls {
                             tmp.add(s1);
                     }
                 });
-        sqlList.add(String.join("\n", tmp.toArray(new String[tmp.size()])));
-        tmp.clear();
+        if (!tmp.isEmpty()) {
+            sqlList.add(String.join("\n", tmp.toArray(new String[tmp.size()])));
+            tmp.clear();
+        }
         return sqlList;
     }
 }

+ 6 - 2
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/ThreadLocalUtils.java

@@ -37,13 +37,17 @@ import java.util.function.Supplier;
  * @since 2.0
  */
 @SuppressWarnings("unchecked")
-public class ThreadLocalUtils {
+public final class ThreadLocalUtils {
+
+    private ThreadLocalUtils() {
+    }
+
     private static final ThreadLocal<Map<String, Object>> local = ThreadLocal.withInitial(HashMap::new);
 
     /**
      * @return threadLocal中的全部值
      */
-    public static Map<String, Object> getAll(){
+    public static Map<String, Object> getAll() {
         return local.get();
     }
 

+ 21 - 0
hsweb-commons/hsweb-commons-utils/src/test/java/org/hswebframework/web/ExpressionUtilsTests.java

@@ -0,0 +1,21 @@
+package org.hswebframework.web;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Collections;
+
+import static org.junit.Assert.*;
+
+public class ExpressionUtilsTests {
+
+    @Test
+    public void testAnalytical() throws Exception {
+        String result = ExpressionUtils.analytical("test${1+2}", "spel");
+
+        Assert.assertEquals(result, "test3");
+
+        result = ExpressionUtils.analytical("test${#param}", Collections.singletonMap("param", "3"), "spel");
+        Assert.assertEquals(result, "test3");
+    }
+}

+ 29 - 0
hsweb-commons/hsweb-commons-utils/src/test/java/org/hswebframework/web/ListsTests.java

@@ -0,0 +1,29 @@
+package org.hswebframework.web;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.LinkedList;
+
+import static org.junit.Assert.*;
+
+public class ListsTests {
+
+    @Test
+    public void testCreate() {
+
+        assertEquals(Lists.buildList(2).add(1)
+                .get().get(0), (Integer) 2);
+
+
+        assertEquals(Lists.buildList(new ArrayList<>()).add(2,1)
+                .get().get(0), 2);
+
+        assertEquals(Lists.buildList(ArrayList::new)
+                .add(2,1)
+                .get()
+                .get(0), 2);
+
+    }
+}

+ 27 - 0
hsweb-commons/hsweb-commons-utils/src/test/java/org/hswebframework/web/MapsTests.java

@@ -0,0 +1,27 @@
+package org.hswebframework.web;
+
+import org.junit.Test;
+
+import java.util.HashMap;
+
+import static org.junit.Assert.*;
+
+public class MapsTests {
+
+    @Test
+    public void testCreateMap() {
+        assertEquals(Maps.buildMap()
+                .put("1", 1)
+                .get().get("1"), 1);
+
+        assertEquals(Maps.buildMap(new HashMap<>())
+                .put("1", 1)
+                .get().get("1"), 1);
+
+        assertEquals(Maps.buildMap(HashMap::new)
+                .put("1", 1)
+                .get().get("1"), 1);
+
+    }
+
+}

+ 18 - 0
hsweb-commons/hsweb-commons-utils/src/test/java/org/hswebframework/web/RegexUtilsTests.java

@@ -0,0 +1,18 @@
+package org.hswebframework.web;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.*;
+
+public class RegexUtilsTests {
+
+    @Test
+    public void test() {
+        Arrays.asList('\\', '$', '(', ')', '*', '+', '.', '[', ']', '?', '^', '{', '}', '|')
+                .forEach((s) -> assertEquals(RegexUtils.escape(String.valueOf(s)), "\\" + s));
+
+    }
+}

+ 33 - 0
hsweb-commons/hsweb-commons-utils/src/test/java/org/hswebframework/web/SqlsTests.java

@@ -0,0 +1,33 @@
+package org.hswebframework.web;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+public class SqlsTests {
+
+    @Test
+    public void testParse() {
+        String sql = "select 1;\ndelete from user;";
+
+        List<String> strings = Sqls.parse(sql);
+        System.out.println(strings);
+        Assert.assertTrue(strings.size() == 2);
+
+        Assert.assertTrue("select 1".equals(strings.get(0)));
+        Assert.assertTrue("delete from user".equals(strings.get(1)));
+
+
+        sql = "select 1;\ndelete from user;\nselect * from user \nwhere name = 1 \n or name =2";
+
+       strings = Sqls.parse(sql);
+        System.out.println(strings);
+        Assert.assertTrue(strings.size() == 3);
+        Assert.assertEquals(strings.get(2),"select * from user \nwhere name = 1 \n or name =2");
+
+
+    }
+}

+ 36 - 0
hsweb-commons/hsweb-commons-utils/src/test/java/org/hswebframework/web/ThreadLocalUtilsTests.java

@@ -0,0 +1,36 @@
+package org.hswebframework.web;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class ThreadLocalUtilsTests {
+
+    @Test
+    public void testAll() {
+        ThreadLocalUtils.put("test", "1");
+
+        Assert.assertEquals(ThreadLocalUtils.get("test"), "1");
+
+        ThreadLocalUtils.get("test2", () -> "2");
+
+        Assert.assertEquals(ThreadLocalUtils.get("test2"), "2");
+
+        Assert.assertEquals(ThreadLocalUtils.getAndRemove("test2"), "2");
+
+        Assert.assertTrue(ThreadLocalUtils.get("test2") == null);
+
+        ThreadLocalUtils.remove("test");
+
+        Assert.assertTrue(ThreadLocalUtils.get("test") == null);
+
+        ThreadLocalUtils.put("test", "1");
+        ThreadLocalUtils.put("test2", "2");
+
+        Assert.assertTrue(ThreadLocalUtils.getAll().size()==2);
+        ThreadLocalUtils.clear();
+
+        Assert.assertTrue(ThreadLocalUtils.getAll().size()==0);
+    }
+}