diff --git a/group05/1094051862/test01/.classpath b/group05/1094051862/test01/.classpath index 04cc82dc42..9794cd8084 100644 --- a/group05/1094051862/test01/.classpath +++ b/group05/1094051862/test01/.classpath @@ -3,5 +3,6 @@ + diff --git a/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java b/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..80c2bf7c32 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,235 @@ +package com.coderising.array; + +import org.junit.experimental.max.MaxCore; + +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 static void reverseArray(int[] origin) { + if (origin.length < 2) + return; + int temp; + for (int i = 0; i < origin.length >> 1; i++) { + temp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = temp; + } + } + + /** + * 现在有如下的一个数组: 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 static int[] removeZero(int[] oldArray) { + if (oldArray == null || oldArray.length == 0) + return null; + int zeros = 0;// 数组中0元素的个数 + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeros++; + } + int[] newArr = new int[oldArray.length - zeros]; + for (int i = 0, j = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArr[j] = oldArray[i]; + j++; + } + } + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, 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 static int[] merge(int[] array1, int[] array2) { + int[] mergedArr = new int[array1.length + array2.length]; + int temp; + int i = 0, j = 0, index = 0, size = 0; + while (i < array1.length && j < array2.length) { + //两个数组都没遍历到最后一个元素 + if (i != array1.length - 1 && j != array2.length - 1) { + if (array1[i] < array2[j]) { + temp = array1[i++]; + } else if (array1[i] > array2[j]) { + temp = array2[j++]; + } else { + //遇到相等元素,存放任意一个就实现去重了 + temp = array1[i++]; + j++; + } + mergedArr[index++] = temp; + size++; + //array1遍历到最后一个元素 + } else if (i == array1.length - 1 && j != array2.length - 1) { + if (array1[i] < array2[j]) { + temp = array1[i]; + mergedArr[index++] = temp; + size++; + //将array2的剩余元素复制到mergedArr中 + System.arraycopy(array2, j, mergedArr, index, array2.length - j); + size += array2.length - j; + break; + } else if (array1[i] > array2[j]) { + temp = array2[j++]; + size++; + } else { + System.arraycopy(array2, j, mergedArr, index, array2.length - j); + size += array2.length - j; + break; + } + //array2遍历到最后一个元素 + } else if (i != array1.length - 1 && j == array2.length - 1) { + if (array1[i] > array2[j]) { + temp = array2[j]; + mergedArr[index++] = temp; + size++; + //将array1的剩余元素复制到mergedArr中 + System.arraycopy(array1, i, mergedArr, index, array1.length - i); + size += array1.length - i; + break; + } else if (array1[i] < array2[j]) { + temp = array2[i++]; + size++; + } else { + System.arraycopy(array1, i, mergedArr, index, array1.length - i); + size += array1.length - i; + break; + } + } + } + //构造新数组,去除mergedArr中尾部若干0元素 + int[] result = new int[size]; + System.arraycopy(mergedArr, 0, result, 0, size); + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] arr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, arr, 0, oldArray.length); + return arr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max < 2) return null; + int[] a = new int[max]; + a[0] = 1; + a[1] = 1; + int size = 2; + for (int i = 2;; i++) { + a[i] = a[i-1] + a[i-2]; + if (a[i] > max) break; + size ++; + } + int[] fibonacci = new int[size]; + System.arraycopy(a, 0, fibonacci, 0, size); + return fibonacci; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + int[] a = new int[max]; + int size = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + a[size++] = i; + } + } + int[] primes = new int[size]; + System.arraycopy(a, 0, primes, 0, size); + return primes; + } + private static boolean isPrime(int i) { + for (int j = 2; j*j <= i; j++) { + if (i % j == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] a = new int[max]; + int size = 0; + for (int i = 6; i < max; i++) { + if (isPerfectNumber(i)) { + a[size++] = i; + } + } + int[] perfectNumbers = new int[size]; + System.arraycopy(a, 0, perfectNumbers, 0, size); + return perfectNumbers; + } + private static boolean isPerfectNumber(int i) { + int sum = 0; + for (int j = 1; j <= i >> 1; j++) { + if (i % j == 0) { + sum += j; + } + } + if (i == sum) return true; + else return false; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i); + sb.append(seperator); + } + return sb.substring(0, sb.lastIndexOf(seperator)); + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java b/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..ae75b41379 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,91 @@ +package com.coderising.array; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import sun.misc.Perf.GetPerfAction; + +public class ArrayUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() { + int[] a = {7,9,30,3,5}; + int[] b = a.clone(); + ArrayUtil.reverseArray(a); + for (int i = 0; i < b.length; i ++) { + Assert.assertEquals(b[i], a[b.length - 1 - i]); + } + + } + + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] result = {1,3,4,5,6,6,5,4,7,6,7,5}; + int[] removeZero = ArrayUtil.removeZero(oldArr); + assertArrayEquals(result, removeZero); + } + + @Test + public void testMerge() { + int[] a1 = {3,5,7,8}; + int[] a2 = {4,5,6,7}; + int[] a3 = {3,4,5,6,7,8}; + int[] merge = ArrayUtil.merge(a1, a2); + assertArrayEquals(a3, merge); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int size = 3; + int[] growedArr = {2,3,6,0,0,0}; + assertArrayEquals(growedArr, ArrayUtil.grow(oldArray, size)); + } + + @Test + public void testFibonacci() { + int[] fibonacci = ArrayUtil.fibonacci(15); + for (int i : fibonacci) { + System.out.println(i); + } + } + + @Test + public void testGetPrimes() { + int[] primes = ArrayUtil.getPrimes(3); + for (int i : primes) { + System.out.println(i); + } + } + + @Test + public void testGetPerfectNumbers() { + int[] perfectNumbers = ArrayUtil.getPerfectNumbers(10000); + int[] expecteds = {6,28,496,8128}; + assertArrayEquals(expecteds, perfectNumbers); + for (int i : perfectNumbers) { + System.out.println(i); + } + } + + @Test + public void testJoin() { + int[] arr = {3,4,5}; + String join = ArrayUtil.join(arr, "-"); + assertEquals("3-4-5", join); + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/litestruts/LoginAction.java b/group05/1094051862/test01/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.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/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java b/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..c9e9f50631 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,181 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.parsers.ParserConfigurationException; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; +import org.omg.PortableInterceptor.ObjectIdHelper; +import org.xml.sax.SAXException; + +import com.coding.basic.ArrayList; +import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; + +public class Struts { + @Test + 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字段中。 + */ + + // DOM4J方式解析xml + // 读取文件 转换成Document + View view = new View(); + if (actionName == null || actionName.isEmpty()) { + return view; + } + try { + //获取action标签元素 + Element actionEle = getActionElement(actionName); + + String qualifiedName = actionEle.attributeValue("class"); + Class clazz = Class.forName(qualifiedName); + Object instanceOfAction = getParameteredInstance(parameters, clazz); + Method executeMethod = clazz.getMethod("execute"); + + //调用execute方法,改变action实例,一定要在执行getResultMap方法之前执行 + String executeResult = (String) executeMethod.invoke(instanceOfAction); + Method[] methods = clazz.getMethods(); + Map resultMap = getResultMap(instanceOfAction, methods); + view.setParameters(resultMap); + + List resultEles = actionEle.elements("result"); + String jspPath = getJspPath(executeResult, resultEles); + view.setJsp(jspPath); + } catch (ClassNotFoundException e1) { + e1.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } + + private static String getJspPath(String result, List resultEles) { + for (int i = 0; i < resultEles.size(); i++) { + String text = resultEles.get(i).attributeValue("name"); + if (text.equals(result)) { + String jspString = resultEles.get(i).getText(); + return jspString; + } + } + return null; + } + + private static Map getResultMap(Object obj, Method[] methodsOfObj) + throws IllegalAccessException, InvocationTargetException { + Map resultMap = new HashMap(); + Method m; + for (int i = 0; i < methodsOfObj.length; i++) { + if (methodsOfObj[i].getName().startsWith("get")) { + m = methodsOfObj[i]; + String methodName = getMethodNameInLowerCase(m); + Object result = m.invoke(obj); + resultMap.put(methodName.substring(3), result); + } + } + return resultMap; + } + + private static String getMethodNameInLowerCase(Method m) { + String methodName = m.getName(); + StringBuilder sbr = new StringBuilder(methodName); + sbr.setCharAt(3, Character.toLowerCase(sbr.charAt(3))); + methodName = sbr.toString(); + return methodName; + } + + private static Object getParameteredInstance(Map parameters, + Class clazz) { + Object obj = null; + try { + obj = clazz.newInstance(); + Map methodsAndParams = new HashMap(); + Set keySet = parameters.keySet(); + for (String k : keySet) { + String methodName = getMethodNameByProperty(k); + methodsAndParams.put(methodName, parameters.get(k)); + Method setter = clazz.getMethod(methodName, parameters.get(k) + .getClass()); + setter.invoke(obj, parameters.get(k)); + } + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return obj; + } + + private static String getMethodNameByProperty(String k) { + StringBuilder sb = new StringBuilder(k); + sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); + sb = sb.insert(0, "set"); + String method = sb.toString();// 获得方法名 + return method; + } + + @SuppressWarnings("unchecked") + private static Element getActionElement(String actionName) throws DocumentException { + SAXReader reader = new SAXReader(); + Document document = reader.read(new File( + "src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + List actNodes = root.elements("action"); + Element e = null; + String className; + for (int i = 0; i < actNodes.size(); i++) { + className = actNodes.get(i).attributeValue("name"); + if (className != null && className.equals(actionName)) { + e = actNodes.get(i); + break; + } + } + return e; + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/litestruts/StrutsTest.java b/group05/1094051862/test01/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +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/group05/1094051862/test01/src/com/coderising/litestruts/View.java b/group05/1094051862/test01/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.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/group05/1094051862/test01/src/com/coderising/litestruts/struts.xml b/group05/1094051862/test01/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..c017732676 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group05/1094051862/test01/src/com/coding/basic/.gitignore b/group05/1094051862/test01/src/com/coding/basic/.gitignore deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/group05/1094051862/test01/src/com/coding/basic/ArrayList.java b/group05/1094051862/test01/src/com/coding/basic/ArrayList.java index af757a217e..60fe8b9150 100644 --- a/group05/1094051862/test01/src/com/coding/basic/ArrayList.java +++ b/group05/1094051862/test01/src/com/coding/basic/ArrayList.java @@ -8,7 +8,7 @@ public class ArrayList implements List { private Object[] elementData = new Object[10]; - private int increaseSize = 3; + private int increaseSize = 10; private void increaseArray() { Object[] newData = Arrays.copyOf(elementData, elementData.length + increaseSize); elementData = newData;