diff --git a/group11/283091182/src/com/coderising/array/ArrayUtil.java b/group11/283091182/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..93c43d9500 --- /dev/null +++ b/group11/283091182/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,303 @@ +package com.coderising.array; + +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){ + if(origin==null||origin.length==0){ + throw new RuntimeException("invalid array argument!"); + } + if(origin.length>1){ + int temp; + for(int i=0;iarray2[pos2]){ + array3[pos3]=array2[pos2]; + pos2++; + pos3++; + }else if(array1[pos1]3){ + result[pos]=2; + pos++; + } + if(max>4){ + result[pos]=3; + pos++; + } + for(int i=4;i0){ + sb.append(seperator); + } + sb.append(i); + } + return sb.toString(); + } + + private void printArray(String msg,int[] array){ + System.out.print(msg); + for(int i=0;i resultMap= new HashMap(); + public static final String CONST_ACTION = "action"; + public static final String CONST_NAME = "name"; + public static final String CONST_CLASS = "class"; + public static final String CONST_RESULT = "result"; + + private String actionName; + private String actionClass; + + public Action(){}; + + public Action(String actionName,String actionClass){ + this.actionName = actionName; + this.actionClass = actionClass; + } + + public void setActionResultJsp(String result,String dispatcherJsp){ + this.resultMap.put(result, dispatcherJsp); + } + + public String getActionResultJsp(String result){ + return this.resultMap.get(result); + } + + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public String getActionClass() { + return actionClass; + } + + public void setActionClass(String actionClass) { + this.actionClass = actionClass; + } + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append(CONST_ACTION).append(":").append(this.actionName).append(","); + sb.append(CONST_CLASS).append(":").append(this.actionClass).append(","); + sb.append("ResultMap").append(":").append(this.resultMap.toString()); + return sb.toString(); + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/LoginAction.java b/group11/283091182/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coderising/litestruts/Struts.java b/group11/283091182/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..dbe4691938 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,229 @@ +package com.coderising.litestruts; + +import java.beans.BeanInfo; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +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字段中。 + * + */ + + //0. Load Structs.xml and return a map + Map actionMap = loadActionMap(); + + //1.Find and initialize action instance to actionName provided + Action action = actionMap.get(actionName); + Object instance = initializeActionInstance(action.getActionClass(),parameters); + + //2.invoke the execute method and get the result + String result = executeAction(instance); + + //3.Extract info for view + View view = new View(); + view.setParameters(extractInfo(instance)); + + //4.set dispatcher jsp according to execution result + view.setJsp(action.getActionResultJsp(result)); + + return view; + } + + private static Map loadActionMap() { + try { + + InputStream is = Struts.class.getResourceAsStream("struts.xml"); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(is); + Element struct = document.getDocumentElement(); + + return getActions(struct); + + } catch (IOException e) { + throw new RuntimeException("Error Reading Configuration XML",e); + } catch (ParserConfigurationException e) { + throw new RuntimeException("Error Parsing Configuration XML",e); + } catch (SAXException e) { + throw new RuntimeException("Error Parsing Configuration XML",e); + } + } + + /** + * Parse the XML and construct ActionName:Action map + * + * @param Element struct, root of the struct xml + * @return Map + */ + private static Map getActions(Element struct) { + + Map map = new HashMap(); + + NodeList nl = struct.getElementsByTagName(Action.CONST_ACTION); + + for (int i = 0; i < nl.getLength(); i++) + { + Node actionNode = nl.item(i); + // Get action name and corresponding action class from property + String actionClass = getAttribute(actionNode,Action.CONST_CLASS); + String actionName = getAttribute(actionNode,Action.CONST_NAME); + + Action action = new Action(actionName, actionClass); + + // get results under action + NodeList childNodes = actionNode.getChildNodes(); + + for (int j = 0; j < childNodes.getLength(); j++) + { + Node result = childNodes.item(j); + //Only accept if Node Type is element and Node name is "result" + if ((result.getNodeType() == result.ELEMENT_NODE) + && (result.getNodeName() == Action.CONST_RESULT)) + { + String resultName = getAttribute(result,Action.CONST_NAME); + String dispatcherJsp = result.getTextContent(); + action.setActionResultJsp(resultName, dispatcherJsp); + } + } + map.put(action.getActionName(), action); + } + System.out.println(map); + return map; + } + /** + * Get property from given node + * @param node + * @param key + * @return attribute as String + */ + private static String getAttribute(Node node,String key){ + NamedNodeMap map = node.getAttributes(); + Node attriNode = map.getNamedItem(key); + if(attriNode!=null && attriNode.getNodeType()==Node.ATTRIBUTE_NODE){ + return attriNode.getNodeValue(); + } + return null; + } + + /** + * Initialize instance from given class name and parameters map + * @param actionClass + * @param parameters + * @return instance of specified class + */ + private static Object initializeActionInstance(String actionClass,Map parameters){ + try { + Class clazz= Class.forName(actionClass); + //Instantiate by calling constructor + Constructor constructor = clazz.getConstructor(); + + constructor.setAccessible(true); + Object instance = constructor.newInstance(new Object[]{}); + + //Check class propertes with instrospector + BeanInfo beanInfo = Introspector.getBeanInfo(clazz); + PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); + + for(PropertyDescriptor prop:props){ + String propName = prop.getName(); + Method propSetter = prop.getWriteMethod(); + //If there is a setter for the property and also there is a value in parameter map + //then invoke the setter method to set the values + if(propSetter!=null && parameters.containsKey(propName)) + { + propSetter.invoke(instance, parameters.get(propName)); + } + } + + return instance; + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Error initializing instance: ClassName="+actionClass,e); + } + } + + + /** + * Invoke the "execute" method from the action instance + * @param Action instance + * @return execute result as String + */ + private static String executeAction(Object instance){ + Class clazz = instance.getClass(); + try { + //exepct no argument for execute method + Method execute = clazz.getMethod("execute", new Class[0]); + return (String)execute.invoke(instance, new Object[0]); + } catch (Exception e) { + throw new RuntimeException("Error executing action,class name="+clazz.getCanonicalName()); + } + } + + + /** + * Extracting Bean info by calling the getting method in the Action instance + * @param instance + * @return map + */ + private static Map extractInfo(Object instance){ + Map map = new HashMap(); + Class clazz = instance.getClass(); + try{ + Method[] methods = clazz.getMethods(); + for(Method method:methods) + { + String methodName = method.getName(); + if(methodName.startsWith("get")&&method.getParameterTypes().length==0) + { + Object methodReturn = method.invoke(instance, new Object[0]); + //construct the properties name by getter method name,first character toLower case + String propName = methodName.replaceFirst("get", ""); + char[] propNameCharArr = propName.toCharArray(); + propNameCharArr[0]=Character.toLowerCase(propNameCharArr[0]); + + map.put(String.valueOf(propNameCharArr), methodReturn); + } + } + + }catch(Exception e){ + throw new RuntimeException("Error extracting info from Action Insance,class="+clazz.getCanonicalName(),e); + } + return map; + } + +} diff --git a/group11/283091182/src/com/coderising/litestruts/StrutsTest.java b/group11/283091182/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coderising/litestruts/View.java b/group11/283091182/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coderising/litestruts/struts.xml b/group11/283091182/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..4c6eeabbd4 --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coding/basic/ArrayListTest.java b/group11/283091182/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..7807fa831e --- /dev/null +++ b/group11/283091182/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,142 @@ +/** + * + */ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Administrator + * + */ +public class ArrayListTest { + + private ArrayList al; + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + System.out.println("SetUp"); + al= new ArrayList(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + System.out.println("TearDown"); + al = null; + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(java.lang.Object)}. + */ + @Test + public final void testAddObject() { + al.add("aaa"); + al.add("bbb"); + al.add("ccc"); + assertEquals("aaa",al.get(0)); + assertEquals("bbb",al.get(1)); + assertEquals("ccc",al.get(2)); + assertEquals(3,al.size()); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test + public final void testAddIntObject() { + al.add("aaa"); + al.add(0,"bbb"); + al.add(1,"ccc"); + assertEquals("bbb",al.get(0)); + assertEquals("ccc",al.get(1)); + assertEquals("aaa",al.get(2)); + assertEquals(3,al.size()); + } + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test(expected=IndexOutOfBoundsException.class) + public final void testAddIntObjectWithException1() { + al.add(-1, "aaa"); + } + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test(expected=IndexOutOfBoundsException.class) + public final void testAddIntObjectWithException2() { + al.add("aaa"); + al.add(1,"bbb"); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGet() { + fail("Not yet implemented"); // TODO + } + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGetWithException1() { + fail("Not yet implemented"); // TODO + } + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGetWithException2() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#remove(int)}. + */ + @Test + public final void testRemove() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#size()}. + */ + @Test + public final void testSize() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#iterator()}. + */ + @Test + public final void testIterator() { + fail("Not yet implemented"); // TODO + } + +}