From 83cb18e26d74575ba9e19da5ceb8b555a643ec44 Mon Sep 17 00:00:00 2001 From: zhoubofeng <598808350@qq.com> Date: Sun, 5 Mar 2017 17:21:11 +0800 Subject: [PATCH 1/2] 20170305work ArrayUtil,leteStruts --- .../src/org/comm/util/StringUtil.java | 30 ++ .../src/org/learning/container/ArrayUtil.java | 320 ++++++++++++++++++ .../src/org/lite/struts/LoginAction.java | 39 +++ .../src/org/lite/struts/ReadXML.java | 100 ++++++ .../src/org/lite/struts/Struts.java | 99 ++++++ .../2017project/src/org/lite/struts/View.java | 23 ++ .../src/org/lite/struts/struts.xml | 11 + .../org/learning/container/TestArrayUtil.java | 78 +++++ .../test/org/lite/struts/TestStruts.java | 38 +++ 9 files changed, 738 insertions(+) create mode 100644 group14/598808350/2017project/src/org/comm/util/StringUtil.java create mode 100644 group14/598808350/2017project/src/org/learning/container/ArrayUtil.java create mode 100644 group14/598808350/2017project/src/org/lite/struts/LoginAction.java create mode 100644 group14/598808350/2017project/src/org/lite/struts/ReadXML.java create mode 100644 group14/598808350/2017project/src/org/lite/struts/Struts.java create mode 100644 group14/598808350/2017project/src/org/lite/struts/View.java create mode 100644 group14/598808350/2017project/src/org/lite/struts/struts.xml create mode 100644 group14/598808350/2017project/test/org/learning/container/TestArrayUtil.java create mode 100644 group14/598808350/2017project/test/org/lite/struts/TestStruts.java diff --git a/group14/598808350/2017project/src/org/comm/util/StringUtil.java b/group14/598808350/2017project/src/org/comm/util/StringUtil.java new file mode 100644 index 0000000000..9c7b802252 --- /dev/null +++ b/group14/598808350/2017project/src/org/comm/util/StringUtil.java @@ -0,0 +1,30 @@ +package org.comm.util; + +import java.util.Arrays; + +public class StringUtil { + + public static void printStr(Object obj){ + System.out.print(obj); + } + + public static void printlnStr(Object obj){ + System.out.println(obj.toString()); + } + public static void printArr(int[] arr){ + printlnStr(Arrays.toString(arr)); + } + public static boolean isEmpty(Object str){ + boolean flag = false; + if(str == null || "".equals(str)) flag = true; + return flag; + } + public static String objToStr(Object obj){ + if(isEmpty(obj)){ + return ""; + }else{ + return obj.toString(); + } + } + +} diff --git a/group14/598808350/2017project/src/org/learning/container/ArrayUtil.java b/group14/598808350/2017project/src/org/learning/container/ArrayUtil.java new file mode 100644 index 0000000000..63d9cd1a45 --- /dev/null +++ b/group14/598808350/2017project/src/org/learning/container/ArrayUtil.java @@ -0,0 +1,320 @@ +package org.learning.container; + +import java.util.Arrays; + +import org.comm.util.StringUtil; + +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 + * + */ + public static int [] reverseArray(int [] sourceArray){ + int length = sourceArray.length; + int [] newArray = new int[length]; + for(int i=length;i>0;i--){ + newArray[length -i ] = sourceArray[i-1]; + } + return newArray; + + } + + /** + * 现在有如下一个数组, int [] oldArray = {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){ + int oldLength = oldArray.length; + int [] newArray = new int[oldLength]; + int index = 0; + for(int i=0;i 0){ + newArray[index] = oldArray[i]; + index ++ ; + } + } + int [] dest = new int[index]; + System.arraycopy(newArray, 0, dest, 0, index); + return dest; + } + /** + * 冒泡排序 + * @param array1 + * @return + */ + public static int[] sort(int [] arr){ + //int[] a1 = new int[]{5, 3, 7,8,1,3,42,2,6}; + + for(int i=0;ib){ + arr[i]=b; + arr[j]=a; + }else if(a == b){ + arr[j]=0; + } + } + } + + return arr; + } + + public static int[] replace(int[] sourceArr,int[] resultArr,int index){ + System.arraycopy(sourceArr, 0, resultArr, 0, index); + System.arraycopy(sourceArr, index+1, resultArr, index, sourceArr.length-(index+1)); + return resultArr; + } + + /** + * 给定两个已经排序好的整形数组, 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 [] newArr = new int[array1.length+array2.length]; + //1整合数据 + int array1L = removeZero(array1).length; + int array2L = removeZero(array2).length; + int [] array3 = new int[array1L+array2L]; + System.arraycopy(array1, 0, array3, 0, array1L); + System.arraycopy(array2, 0, array3, array1L, array2L); + //2排序 + sort(array3); + //3 去重 + for(int i=0;i=max){break;} + //printStr(c+","); + result[rIndex] = c; + rIndex +=1; + }else if(aIndex == 3){ + aIndex = 0; + bIndex = 3; + a = b+c; + if(a>=max){break;} + //printStr(c+","); + result[rIndex] = a; + rIndex +=1; + }else if(bIndex == 3){ + cIndex = 3; + bIndex = 0; + b = c+a; + if(b>=max){break;} + //printStr(c+","); + result[rIndex] = b; + rIndex +=1; + } + } + + + return removeZero(result); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + int [] result = new int[max]; + result[0] =2; + result[1] =3; + result[2] =5; + result[3] =7; + int index = 4; + for(int i = 8;i params = new HashMap(); + params.put("name", "test"); + params.put("pwd", "1234"); + runAction(actionName,params); + } + + public static View runAction(String actionName, Map parameters) { + HashMap strutsMap = ReadXML.readXml(actionName); + View view = null; + + if(!strutsMap.isEmpty()){ + String className = (String)strutsMap.get("ClassName"); + + try { + Class cls = Class.forName(className); + try { + Object obj = cls.newInstance(); + Field nameF = cls.getDeclaredField("name"); + nameF.setAccessible(true); + nameF.set(obj, StringUtil.objToStr(parameters.get("name"))); + + Field pwdF = cls.getDeclaredField("password"); + pwdF.setAccessible(true); + pwdF.set(obj, StringUtil.objToStr(parameters.get("pwd"))); + + try { + Method method = cls.getMethod("execute"); + String result = (String)method.invoke(obj); + Field messageF = cls.getDeclaredField("message"); + messageF.setAccessible(true); + String msg = (String)messageF.get(obj); + String pageUrl = (String)strutsMap.get(result+"URL"); + view = new View(); + view.setJsp(pageUrl); + HashMap map = new HashMap(); + map.put("msg", msg); + map.put("result", result); + view.setParameters(map); + + StringUtil.printlnStr("result:"+result); + StringUtil.printlnStr("msg:"+msg); + StringUtil.printlnStr("pageUrl:"+pageUrl); + } catch (NoSuchMethodException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + }catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + + + } catch (InstantiationException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (IllegalAccessException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + }catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchFieldException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + }else{ + StringUtil.printlnStr("Action is not found"); + } + + + return view; + } + +} diff --git a/group14/598808350/2017project/src/org/lite/struts/View.java b/group14/598808350/2017project/src/org/lite/struts/View.java new file mode 100644 index 0000000000..bb6a56a8c1 --- /dev/null +++ b/group14/598808350/2017project/src/org/lite/struts/View.java @@ -0,0 +1,23 @@ +package org.lite.struts; + +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/group14/598808350/2017project/src/org/lite/struts/struts.xml b/group14/598808350/2017project/src/org/lite/struts/struts.xml new file mode 100644 index 0000000000..b9bf410c73 --- /dev/null +++ b/group14/598808350/2017project/src/org/lite/struts/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/group14/598808350/2017project/test/org/learning/container/TestArrayUtil.java b/group14/598808350/2017project/test/org/learning/container/TestArrayUtil.java new file mode 100644 index 0000000000..8ac30e9dd7 --- /dev/null +++ b/group14/598808350/2017project/test/org/learning/container/TestArrayUtil.java @@ -0,0 +1,78 @@ +package org.learning.container; + +import junit.framework.TestCase; + +import org.junit.Assert; + +public class TestArrayUtil extends TestCase{ + + private ArrayUtil au = null; + + @Override + public void setUp(){ + //首先执行 + //生成实例 + au = new ArrayUtil(); + + } + public void testReverseArray(){ + int [] array1 = {7,9,30,3}; + int[] result1 = au.reverseArray(array1); + int[] exp1 = {3,30,9,7}; + Assert.assertArrayEquals(exp1, result1); + + } + public void testRemoveZero(){ + int[] oldArray = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArray = au.removeZero(oldArray); + int[] exp1 = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + Assert.assertArrayEquals(exp1, newArray); + } + public void testMerge(){ + int[] array1 = {3, 5, 7,8}; + int[] array2 = {4, 5, 6,7}; + int[] newArray = au.merge(array1, array2); + int[] exp1 = {3,4,5,6,7,8}; + Assert.assertArrayEquals(exp1, newArray); + } + + public void testGrow(){ + int[] array1 = {3, 5, 7,8}; + int[] newArray = au.grow(array1, 1); + int[] exp1= {3, 5, 7,8,0}; + Assert.assertArrayEquals(exp1, newArray); + } + + public void testFibonacci(){ + int max = 22; + int[] newArray = au.fibonacci(max); + int[] exp1 = {1,1,2,3,5,8,13,21}; + Assert.assertArrayEquals(exp1, newArray); + } + public void testGetPrimes(){ + int max =23; + int[] newArray = au.getPrimes(max); + int[] exp1 = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(exp1, newArray); + } + public void testGetPerfectNumbers(){ + int max = 497; + int[] newArray = au.getPerfectNumbers(max); + int[] exp1 = {6, 28, 496}; + Assert.assertArrayEquals(exp1, newArray); + } + public void testJoin(){ + //join(int[] array, String seperator) + int[] array = {3,8,9}; + String seperator = "-"; + String result = au.join(array, seperator); + String exp1 = "3-8-9"; + Assert.assertEquals("相等", exp1, result); + } + + + @Override + public void tearDown(){ + //最后执行 + } +} diff --git a/group14/598808350/2017project/test/org/lite/struts/TestStruts.java b/group14/598808350/2017project/test/org/lite/struts/TestStruts.java new file mode 100644 index 0000000000..16ccc711e0 --- /dev/null +++ b/group14/598808350/2017project/test/org/lite/struts/TestStruts.java @@ -0,0 +1,38 @@ +package org.lite.struts; + +import java.util.HashMap; + +import org.junit.Assert; + +import junit.framework.TestCase; + +public class TestStruts extends TestCase{ + + private Struts struts = null; + + @Override + public void setUp(){ + struts = new Struts(); + } + public void testRunAction(){ + String actionName = "login"; + HashMap params = new HashMap(); + params.put("name", "test"); + params.put("pwd", "1234"); + View view = struts.runAction(actionName, params); + String jsp = view.getJsp(); + String exp1 = "/jsp/homepage.jsp"; + Assert.assertEquals("登录成功", exp1, jsp); + + params = new HashMap(); + params = new HashMap(); + params.put("name", "test"); + params.put("pwd", "12341"); + view = struts.runAction(actionName, params); + jsp = view.getJsp(); + exp1 = "/jsp/showLogin.jsp"; + Assert.assertEquals("密码错误,登录失败", exp1, jsp); + + + } +} From b6a4abfc6a55c9fb5dad703924cad1f41d878a7c Mon Sep 17 00:00:00 2001 From: zhoubofeng <598808350@qq.com> Date: Sun, 5 Mar 2017 19:57:24 +0800 Subject: [PATCH 2/2] junit testall junit testall --- .../test/org/test/all/TestAll.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 group14/598808350/2017project/test/org/test/all/TestAll.java diff --git a/group14/598808350/2017project/test/org/test/all/TestAll.java b/group14/598808350/2017project/test/org/test/all/TestAll.java new file mode 100644 index 0000000000..265bdb4c64 --- /dev/null +++ b/group14/598808350/2017project/test/org/test/all/TestAll.java @@ -0,0 +1,24 @@ +package org.test.all; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.learning.container.TestArrayUtil; +import org.lite.struts.TestStruts; + +/** + * 将多个测试用例加到一个运行方法中运行。 + * @author z + * + */ +public class TestAll extends TestCase{ + + public static Test suite(){ + TestSuite suite = new TestSuite(); + suite.addTestSuite(TestArrayUtil.class); + suite.addTestSuite(TestStruts.class); + return suite; + } + +}