From eb7574e152ed96c6c14a8252c5996eb5d32cd864 Mon Sep 17 00:00:00 2001 From: sdnb Date: Mon, 20 Mar 2017 00:27:42 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/494800949/build.gradle | 2 + .../com/coding/week2/array/ArrayUtil.java | 217 ++++++++++++++++++ .../coding/week2/litestruts/LoginAction.java | 39 ++++ .../com/coding/week2/litestruts/Struts.java | 121 ++++++++++ .../week2/litestruts/StrutsXmlUtil.java | 92 ++++++++ .../com/coding/week2/litestruts/View.java | 23 ++ .../494800949/src/main/resources/struts.xml | 11 + .../com/coding/week2/array/ArrayUtilTest.java | 91 ++++++++ .../coding/week2/litestruts/StrutsTest.java | 36 +++ .../week2/litestruts/StrutsXmlUtilTest.java | 35 +++ 10 files changed, 667 insertions(+) create mode 100644 group24/494800949/src/main/java/com/coding/week2/array/ArrayUtil.java create mode 100644 group24/494800949/src/main/java/com/coding/week2/litestruts/LoginAction.java create mode 100644 group24/494800949/src/main/java/com/coding/week2/litestruts/Struts.java create mode 100644 group24/494800949/src/main/java/com/coding/week2/litestruts/StrutsXmlUtil.java create mode 100644 group24/494800949/src/main/java/com/coding/week2/litestruts/View.java create mode 100644 group24/494800949/src/main/resources/struts.xml create mode 100644 group24/494800949/src/test/java/com/coding/week2/array/ArrayUtilTest.java create mode 100644 group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsTest.java create mode 100644 group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsXmlUtilTest.java diff --git a/group24/494800949/build.gradle b/group24/494800949/build.gradle index e88b275214..7930ebfca5 100644 --- a/group24/494800949/build.gradle +++ b/group24/494800949/build.gradle @@ -18,6 +18,8 @@ repositories { dependencies{ compile group: 'junit', name: 'junit', version: '4.11' + compile files("lib/dom4j-1.6.1.jar") + compile files("lib/jaxen-1.1.1.jar") } gradle.projectsEvaluated { tasks.withType(JavaCompile) { diff --git a/group24/494800949/src/main/java/com/coding/week2/array/ArrayUtil.java b/group24/494800949/src/main/java/com/coding/week2/array/ArrayUtil.java new file mode 100644 index 0000000000..f454b99d94 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/array/ArrayUtil.java @@ -0,0 +1,217 @@ +package com.coding.week2.array; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int[] newArr = new int[origin.length]; + for(int i = origin.length - 1,j = 0; i >= 0; i--,j++){ + newArr[j] = origin[i]; + } + System.arraycopy(newArr, 0, origin, 0, origin.length); + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] newArr = new int[oldArray.length]; + int j = 0; + for(int i = 0; i < oldArray.length; i++){ + if(oldArray[i] != 0) + newArr[j++] = oldArray[i]; + } + int[] newArr1 = new int[j]; + System.arraycopy(newArr, 0, newArr1, 0, j); + return newArr1; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int len = array1.length > array2.length ? array1.length : array2.length; + int[] newArr = new int[len]; + for(int i = 0; i < array1.length; i++){ + for(int j : array2){ + if(array1[i] == j){ + newArr[i] = array1[i]; + } + } + } + int[] newArr2 = new int[array1.length - newArr.length]; + for(int i = 0; i < array1.length; i++){ + for(int j : newArr){ + if(array1[i] == j) + continue; + } + } + int[] newArr1 = new int[array1.length + array2.length]; + System.arraycopy(array2, 0, newArr1, 0, array2.length); + System.arraycopy(newArr, 0, newArr1, array2.length-1, newArr.length); + bubuleSort(newArr1); + return newArr1; + } + + public void bubuleSort(int[] newArr1){ + for(int i = 0; i < newArr1.length; i++){ + for(int j = 0; j < newArr1.length; j++) + if(newArr1[i] < newArr1[j]){ + int temp = newArr1[i]; + newArr1[i] = newArr1[j] ; + newArr1[j] = temp; + } + } + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArr = new int[oldArray.length + size]; + for(int i = 0; i < newArr.length; i++){ + if (i < oldArray.length){ + newArr[i] = oldArray[i]; + }else + newArr[i] = 0; + } + + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + //1 得到小于max的斐波那契数 + //2 + + if( max == 1) + return new int[]{}; + int[] arr = new int[3]; + arr[0] = 1; + arr[1] = 1; + arr[2] = arr[0] + arr[1]; + if(max == 2) + return arr; + int i = 2; + while (max > arr[i]){ + if(i+1 >= arr.length){ + int capacity = arr.length + (arr.length >> 1); + int[] newArr = new int[capacity]; + System.arraycopy(arr, 0, newArr, 0, arr.length); + arr = newArr; + } + arr[++i] = arr[i - 1] + arr[i - 2]; + if(arr[i] < 0){ + System.out.println(Arrays.toString(arr)); + throw new OutOfMemoryError(); + } + } + + return removeZero(arr); + } + + + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 2) + return new int[]{}; + int[] arr = new int[max]; + for(int i = max; i >= 2; i--){ + if(isPrime(i)){ + arr[i] = i; + } + } + return removeZero(arr); + } + + private boolean isPrime(int value){ + for(int i = 2; i < Math.sqrt(value); i++){ + if(value % i == 0) + return false; + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] ints = new int[max+1]; + for(int i = 1; i <= max; i++){ + if(isPerfectNum(i)){ + ints[i] = i; + } + } + return removeZero(ints); + } + + + private boolean isPerfectNum(int value){ + if(value == 1) + return false; + int sum = 0; + for(int i = 1; i <= Math.sqrt(value); i++){ + if(value % i == 0){ + sum += i + value / i; + } + } + return sum-value == value; + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder builder = new StringBuilder(); + for(int i = 0; i < array.length; i++){ + builder.append(array[i]); + if(i != array.length - 1) + builder.append(seperator); + } + return builder.toString(); + } + + +} diff --git a/group24/494800949/src/main/java/com/coding/week2/litestruts/LoginAction.java b/group24/494800949/src/main/java/com/coding/week2/litestruts/LoginAction.java new file mode 100644 index 0000000000..1c90d49ccb --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.week2.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group24/494800949/src/main/java/com/coding/week2/litestruts/Struts.java b/group24/494800949/src/main/java/com/coding/week2/litestruts/Struts.java new file mode 100644 index 0000000000..6448434800 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/litestruts/Struts.java @@ -0,0 +1,121 @@ +package com.coding.week2.litestruts; + +import org.dom4j.DocumentException; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + View view = new View(); + try { + //0 + //得到action类名 + String actionClassName = StrutsXmlUtil.getActionClassName(actionName); + + //得到action的结果及jsp路径 + Map jspMap = StrutsXmlUtil.getResultOfAction(actionName); + + //加载类 + Class clazz = Class.forName(actionClassName); + //实例化对象 + Object obj = clazz.newInstance(); + Method[] methods = clazz.getDeclaredMethods(); + + //1 注入action实例变量 + setFields(obj, methods, parameters); + + //2 执行action + String result = excute(obj, methods); + + + //3 获取返回的参数 + Map viewParams = buildReturnParams(obj, methods); + view.setParameters(viewParams); + + //4 设置返回的jsp + String jsp = jspMap.get(result); + view.setJsp(jsp); + + } catch (DocumentException | ClassNotFoundException + | InstantiationException | IllegalAccessException + | InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } + + private static String excute(Object obj, Method[] methods ) + throws InvocationTargetException, IllegalAccessException { + for(Method method : methods){ + String methodName = method.getName(); + if("execute".equals(methodName)){ + return (String) method.invoke(obj); + } + } + return ""; + } + + + private static void setFields(Object obj, Method[] methods, Map parameters) + throws InvocationTargetException, IllegalAccessException { + Set> entities = parameters.entrySet(); + for(Map.Entry entry : entities){ + for(Method method : methods){ + if(method.getName().equals("set" + upperFirstChar(entry.getKey()))){ + method.invoke(obj, entry.getValue()); + } + } + } + } + + private static Map buildReturnParams(Object obj, Method[] methods) throws InvocationTargetException, IllegalAccessException { + Map viewParams = new HashMap<>(); + for(Method method : methods){ + String methodName = method.getName(); + if(methodName.startsWith("get")){ + Object ret = method.invoke(obj); + String field = methodName.substring(3); + viewParams.put(lowerFirstChar(field), ret); + } + } + return viewParams; + } + private static String upperFirstChar(String str){ + char[] cs = str.toCharArray(); + cs[0] -= 32; + return new String(cs); + } + + private static String lowerFirstChar(String str){ + char[] cs = str.toCharArray(); + cs[0] += 32; + return new String(cs); + } +} diff --git a/group24/494800949/src/main/java/com/coding/week2/litestruts/StrutsXmlUtil.java b/group24/494800949/src/main/java/com/coding/week2/litestruts/StrutsXmlUtil.java new file mode 100644 index 0000000000..e4cbee7480 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/litestruts/StrutsXmlUtil.java @@ -0,0 +1,92 @@ +package com.coding.week2.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.util.*; + +/** + * Created by Administrator on 2017/3/19 0019. + */ +public class StrutsXmlUtil { + + private static final String RESOURCE_PATH = "src/main/resources"; + private static final String STRUTS_CONFIG_FILE_NAME = "struts.xml"; + + private StrutsXmlUtil() { + } + + public static Document readXml(String path) throws DocumentException { + try { + SAXReader reader = new SAXReader(); + return reader.read(new File(path)); + } catch (DocumentException e) { + e.printStackTrace(); + throw e; + } + } + + + public static Iterator actionIterator(String path) throws DocumentException { + Document doc = readXml(path); + Element root = doc.getRootElement(); + return root.elementIterator("action"); + } + + public static String getActionClassName(String actionName) throws DocumentException { + Iterator iterator = actionIterator(getPathOfStrutsConfigurtion()); + if(actionName == null || "".equals(actionName)){ + throw new IllegalArgumentException("actionName can't be empty"); + } + while (iterator.hasNext()){ + Element e =(Element) iterator.next(); + if(actionName.equals(e.attributeValue("name"))){ + return e.attributeValue("class"); + } + } + return null; + } + + + public static String getPathOfStrutsConfigurtion(){ + File file = new File(RESOURCE_PATH); + List fileList = new ArrayList<>(); + + if (file.isDirectory()){ + File[] files = file.listFiles((dir, name) -> { + return name.equals(STRUTS_CONFIG_FILE_NAME); + }); + fileList.addAll(Arrays.asList(files)); + if(fileList.size() > 1){ + throw new RuntimeException("配置文件不止一个"); + }else if(fileList.size() == 0){ + throw new RuntimeException("找不到struts配置文件"); + } + } + return fileList.get(0).getPath(); + } + + public static Map getResultOfAction(String actionName) throws DocumentException { + Iterator iterator = actionIterator(getPathOfStrutsConfigurtion()); + Map res = new HashMap<>(); + while (iterator.hasNext()){ + Element e =(Element) iterator.next(); + System.out.println(e.getName()); + if(actionName.equals(e.attributeValue("name"))){ + Iterator resItr = e.elementIterator("result"); + while (resItr.hasNext()){ + Element result = (Element)resItr.next(); + System.out.println(result.attribute("name").getValue()); + System.out.println(result.getData()); + res.put(result.attribute("name").getValue(), result.getData().toString()); + } + + } + } + return res; + } + +} diff --git a/group24/494800949/src/main/java/com/coding/week2/litestruts/View.java b/group24/494800949/src/main/java/com/coding/week2/litestruts/View.java new file mode 100644 index 0000000000..bf7bd0d6c8 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.week2.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/494800949/src/main/resources/struts.xml b/group24/494800949/src/main/resources/struts.xml new file mode 100644 index 0000000000..4133f6a2fb --- /dev/null +++ b/group24/494800949/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/494800949/src/test/java/com/coding/week2/array/ArrayUtilTest.java b/group24/494800949/src/test/java/com/coding/week2/array/ArrayUtilTest.java new file mode 100644 index 0000000000..b402701691 --- /dev/null +++ b/group24/494800949/src/test/java/com/coding/week2/array/ArrayUtilTest.java @@ -0,0 +1,91 @@ +package com.coding.week2.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by Administrator on 2017/3/19 0019. + */ +public class ArrayUtilTest { + + + private ArrayUtil arrayUtil; + + private int[] ints = new int[]{4, 9, 10, 3, 5, 0, 10, 12, 0, 9}; + + @Before + public void setup(){ + arrayUtil = new ArrayUtil(); + } + + @Test + public void testReverseArray() throws Exception { + arrayUtil.reverseArray(ints); + Assert.assertEquals(ints[0], 9); + Assert.assertEquals(ints[1], 0); + Assert.assertEquals(ints[2], 12); + Assert.assertEquals(ints[3], 10); + Assert.assertEquals(ints[4], 0); + } + + @Test + public void testRemoveZero() throws Exception { + int[] newInts = ints.clone(); + int[] newArr = arrayUtil.removeZero(newInts); + System.out.println(Arrays.toString(newArr)); + } + + @Test + public void testMerge() throws Exception { + int[] ints1 = new int[]{3, 4, 9, 20}; + int[] ints2 = new int[]{4, 6, 7, 12}; + int[] mergeArr = arrayUtil.merge(ints1, ints2); + System.out.println(Arrays.toString(mergeArr)); + } + + @Test + public void testGrow() throws Exception { + int[] newInts = arrayUtil.grow(ints, 5); + + Assert.assertEquals(newInts.length, 15); + Assert.assertEquals(newInts[14], 0); + Assert.assertEquals(newInts[13], 0); + Assert.assertEquals(newInts[12], 0); + Assert.assertEquals(newInts[11], 0); + Assert.assertEquals(newInts[10], 0); + } + + @Test + public void testFibonacci() throws Exception { + int[] ints = arrayUtil.fibonacci(100); + Assert.assertArrayEquals(ints, new int[]{1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144}); + } + + @Test + public void testGetPrimes() throws Exception { + int[] ints = arrayUtil.getPrimes(100); + System.out.println(Arrays.toString(arrayUtil.getPrimes(100))); + Assert.assertArrayEquals(ints, new int[]{2, 3, 4, 5, 7, 9, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] ints = arrayUtil.getPerfectNumbers(10000); + long start = System.currentTimeMillis(); + System.out.println(Arrays.toString(arrayUtil.getPerfectNumbers(100000))); + long end = System.currentTimeMillis(); + System.out.println(end - start); + Assert.assertArrayEquals(ints, new int[]{6, 28, 496, 8128}); + } + + @Test + public void testJoin() throws Exception { + System.out.println(arrayUtil.join(ints, "|")); + Assert.assertEquals("4+9+10+3+5+0+10+12+0+9", arrayUtil.join(ints, "+")); + Assert.assertEquals("4|9|10|3|5|0|10|12|0|9", arrayUtil.join(ints, "|")); + } + +} \ No newline at end of file diff --git a/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsTest.java b/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..bf16557d9c --- /dev/null +++ b/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsTest.java @@ -0,0 +1,36 @@ +package com.coding.week2.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} diff --git a/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsXmlUtilTest.java b/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsXmlUtilTest.java new file mode 100644 index 0000000000..bedff77fe6 --- /dev/null +++ b/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsXmlUtilTest.java @@ -0,0 +1,35 @@ +package com.coding.week2.litestruts; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; + +/** + * Created by Administrator on 2017/3/19 0019. + */ +public class StrutsXmlUtilTest { + + @Test + public void testGetClassNameOfAction() throws Exception { + String path = "src\\main\\java\\com\\coding\\week2\\litestruts\\struts.xml"; + + String actionCLass = StrutsXmlUtil.getActionClassName( "login"); + Assert.assertEquals(actionCLass, "com.coderising.litestruts.LoginAction"); + actionCLass = StrutsXmlUtil.getActionClassName("logout"); + Assert.assertEquals(actionCLass, "com.coderising.litestruts.LogoutAction"); + + } + + + @Test + public void testGetResultOfAction() throws DocumentException { + String path = "src\\main\\java\\com\\coding\\week2\\litestruts\\struts.xml"; + Map res = StrutsXmlUtil.getResultOfAction("login"); + Assert.assertNotNull(res.get("success"), "/jsp/homepage.jsp"); + Assert.assertNotNull(res.get("fail"), "/jsp/showLogin.jsp"); + } + + +} \ No newline at end of file