diff --git a/group02/812350401/.gitignore b/group02/812350401/.gitignore index 0a2df7bbee..8cd204f114 100644 --- a/group02/812350401/.gitignore +++ b/group02/812350401/.gitignore @@ -1,4 +1,6 @@ target/ .idea/ src/main/java/train/ -src/main/resources/ \ No newline at end of file +!src/main/resources/**/*.class +!src/main/resources/**/*.xml +src/main/resources/**/*.png diff --git a/group02/812350401/pom.xml b/group02/812350401/pom.xml index 9a637781ce..5311bd3c32 100644 --- a/group02/812350401/pom.xml +++ b/group02/812350401/pom.xml @@ -7,6 +7,7 @@ coding2017 812350401 1.0-SNAPSHOT + jar @@ -32,7 +33,20 @@ dom4j 1.6.1 + + org.jetbrains + annotations + RELEASE + + + + commons-io + commons-io + 2.5 + + + \ No newline at end of file diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/linklist/LRUPageFrame.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..b29b385d31 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/linklist/LRUPageFrame.java @@ -0,0 +1,148 @@ +package com.github.miniyk2012.coding2017.basic.linklist; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + + Node(int pageNum) { + this.pageNum = pageNum; + } + } + + private int capacity; + private int size; + + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param pageNum: page值 + * @return + */ + public void access(int pageNum) { + Node hitNode = get(pageNum); + if (null == hitNode) { + hitNode = new Node(pageNum); + addTop(hitNode); + if (size > capacity) { + delBottom(); + } + } else { + switchTop(hitNode); + } + } + + /** + * 获取值为pageNum的Node,如果没有返回null + * @param pageNum + * @return + */ + private Node get(int pageNum) { + Node currentNode = first; + while (currentNode != null) { + if (currentNode.pageNum == pageNum) return currentNode; + currentNode = currentNode.next; + } + return null; + } + + /** + * 往顶部放一个Node + * @param node + */ + private void addTop(Node node) { + size++; + if (first == null) { + first = last = node; + } else { + node.next = first; + first.prev = node; + first = node; + } + } + + /** + * 把node和顶部做交换 + * @param node + */ + private void switchTop(Node node) { + if (node == first) return; + Node preNode = node.prev; + Node nextNode = node.next; + preNode.next = nextNode; + if (nextNode != null) { + nextNode.prev = preNode; + } else { + last = preNode; + } + node.next = node.prev = null; + addTop(node); + size--; + } + + /** + * 把底部的踢掉 + */ + private void delBottom() { + size--; + if (last == first) first = null; + Node temp = last; + last = last.prev; + temp.prev = null; + last.next = null; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + /** + * 测试双向链表逆序输出 + * @return + */ + public String lastToString(){ + StringBuilder buffer = new StringBuilder(); + Node node = last; + while(node != null){ + buffer.append(node.pageNum); + + node = node.prev; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/linklist/LRUPageFrameTest.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..9f93ca8fc6 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,62 @@ +package com.github.miniyk2012.coding2017.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + Assert.assertEquals("0,7", frame.toString()); + Assert.assertEquals("7,0", frame.lastToString()); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + Assert.assertEquals("7,0,1", frame.lastToString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + Assert.assertEquals("0,1,2", frame.lastToString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + Assert.assertEquals("1,2,0", frame.lastToString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + Assert.assertEquals("1,2,0", frame.lastToString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + Assert.assertEquals("2,0,3", frame.lastToString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + Assert.assertEquals("2,3,0", frame.lastToString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + Assert.assertEquals("3,0,4", frame.lastToString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + Assert.assertEquals("3,0,4", frame.lastToString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + Assert.assertEquals("3,0,4", frame.lastToString()); + frame.access(0); + Assert.assertEquals("0,4,3", frame.toString()); + Assert.assertEquals("3,4,0", frame.lastToString()); + frame.access(7); + Assert.assertEquals("7,0,4", frame.toString()); + Assert.assertEquals("4,0,7", frame.lastToString()); + frame.access(4); + Assert.assertEquals("4,7,0", frame.toString()); + Assert.assertEquals("0,7,4", frame.lastToString()); + + LRUPageFrame frame2 = new LRUPageFrame(1); + Assert.assertEquals("", frame2.toString()); + frame2.access(7); + Assert.assertEquals("7", frame2.toString()); + frame2.access(0); + Assert.assertEquals("0", frame2.toString()); + } + +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/loader/ClassFileLoader.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..185adfb6a3 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,63 @@ +package com.github.miniyk2012.coding2017.coderising.jvm.loader; + +import org.apache.commons.io.FileUtils; +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { +// InputStream is = null; +// ByteArrayOutputStream bas = null; +// byte[] ret = null; +// try { +// is = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(className)); +// bas = new ByteArrayOutputStream(); +// byte[] buf = new byte[1024]; +// int bytesRead = 0; +// while ((bytesRead = is.read(buf)) != -1) { +// bas.write(buf, 0, bytesRead); +// } +// ret = bas.toByteArray(); +// } catch (IOException e) { +// e.printStackTrace(); +// } finally { +// try { +// if (is != null) +// is.close(); +// if (bas != null) +// bas.close(); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } +// return ret; + int lastIndexDot = className.lastIndexOf("."); + String classPath = className.substring(0, lastIndexDot).replace(".", File.separator) + + className.substring(lastIndexDot); + for (String parentPath: clzPaths) { + try { + String fullPath = parentPath + File.separator + classPath; + return FileUtils.readFileToByteArray(new File(fullPath)); + } catch (IOException e) { + continue; + } + } + return null; + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath(){ + return String.join(";", clzPaths); + } + + public static void main(String[] args) { + new ClassFileLoader().readBinaryCode(""); + } +} diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/test/ClassFileloaderTest.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..4d4586fd29 --- /dev/null +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,83 @@ +package com.github.miniyk2012.coding2017.coderising.jvm.test; + +import com.github.miniyk2012.coding2017.coderising.jvm.loader.ClassFileLoader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class ClassFileloaderTest { + + + static String path1 = ClassFileloaderTest.class.getClassLoader().getResource("struts").getPath(); + static String path2 = ClassFileloaderTest.class.getClassLoader().getResource("jvm").getPath(); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String className = "com.github.miniyk2012.coding2017.jvm.test.EmployeeV1.class"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1114, byteCodes.length); + + } + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + String className = "com.github.miniyk2012.coding2017.jvm.test.EmployeeV1.class"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + @org.jetbrains.annotations.NotNull + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i parameters) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + public static View runAction(String actionName, Map parameters) throws Exception { /* @@ -93,10 +95,9 @@ private static Map getFields() return map; } - private static void readXml() throws DocumentException { - String fileName = Thread.currentThread().getContextClassLoader().getResource("").getPath() - + "com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml"; - File aFile = new File(fileName); + private static void readXml() throws DocumentException, URISyntaxException { + URL url = Struts.class.getClassLoader().getResource("struts/struts.xml"); + File aFile = new File(url.toURI()); SAXReader xmlReader = new SAXReader(); doc = xmlReader.read(aFile); } @@ -161,13 +162,16 @@ private static void generateObject(String actionName) } - public static void main(String args[]) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException + public static void main(String args[]) throws Exception { - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - View view = runAction("login", params); - logger.info(view.toString()); +// Map params = new HashMap(); +// params.put("name","test"); +// params.put("password","1234"); +// View view = runAction("login", params); +// logger.info(view.toString()); + System.out.println(Struts.class.getResource("")); + System.out.println(Struts.class.getResource("/")); + System.out.println(Struts.class.getClassLoader().getResource("")); } } diff --git a/group02/812350401/src/main/resources/jvm/com/github/miniyk2012/coding2017/jvm/test/EmployeeV1.class b/group02/812350401/src/main/resources/jvm/com/github/miniyk2012/coding2017/jvm/test/EmployeeV1.class new file mode 100644 index 0000000000..868b919f93 Binary files /dev/null and b/group02/812350401/src/main/resources/jvm/com/github/miniyk2012/coding2017/jvm/test/EmployeeV1.class differ diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml b/group02/812350401/src/main/resources/struts/struts.xml similarity index 100% rename from group02/812350401/src/main/java/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml rename to group02/812350401/src/main/resources/struts/struts.xml diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ListTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ListTest.java index 87a8cdddb3..102a002754 100644 --- a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ListTest.java +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/basic/ListTest.java @@ -14,6 +14,7 @@ public class ListTest { @Test public void testFunctional() { + if (getClass() == ListTest.class) return; aList.add(1); aList.add(2); assertEquals(1, aList.get(0)); @@ -38,6 +39,7 @@ public void testFunctional() { @Test public void testAdd() { + if (getClass() == ListTest.class) return; for (int i=0; i<100; i++) aList.add(i); assertEquals(0, aList.get(0)); @@ -47,6 +49,7 @@ public void testAdd() { @Test public void testRemove() { + if (getClass() == ListTest.class) return; aList.add(1); aList.add(2); aList.add(3); @@ -70,6 +73,7 @@ public void testRemove() { @Test public void testSize() { + if (getClass() == ListTest.class) return; for (int i=0; i<10; i++) aList.add(i*2); assertEquals(10, aList.size()); @@ -80,6 +84,7 @@ public void testSize() { @Test public void testException() { + if (getClass() == ListTest.class) return; expectedEx.expect(Exception.class); aList.remove(1); @@ -89,6 +94,7 @@ public void testException() { @Test public void testIterator() { + if (getClass() == ListTest.class) return; Iterator it = aList.iterator(); assertEquals(false, it.hasNext()); diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java index 501326dae0..0b5276fda5 100644 --- a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/download/FileDownloaderTest.java @@ -3,9 +3,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; - import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionManager; -import com.github.miniyk2012.coding2017.coderising.download.api.DownloadListener; import com.github.miniyk2012.coding2017.coderising.download.impl.ConnectionManagerImpl; public class FileDownloaderTest { @@ -24,17 +22,12 @@ public void testDownload() { String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000"; // String url = "https://www.baidu.com/img/bd_logo.png"; - - FileDownloader downloader = new FileDownloader(url, "test.png"); + String filePath = "src/main/resources/downloads/test.png"; + FileDownloader downloader = new FileDownloader(url, filePath); ConnectionManager cm = new ConnectionManagerImpl(); downloader.setConnectionManager(cm); - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - }); + downloader.setListener(() -> downloadFinished = true); downloader.execute(); diff --git a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java index 15e569a766..f183d9a778 100644 --- a/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java +++ b/group02/812350401/src/test/java/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java @@ -13,7 +13,7 @@ public class StrutsTest { @Test - public void testLoginActionSuccess() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + public void testLoginActionSuccess() throws Exception { String actionName = "login"; @@ -29,7 +29,7 @@ public void testLoginActionSuccess() throws DocumentException, InstantiationExce } @Test - public void testLoginActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + public void testLoginActionFailed() throws Exception { String actionName = "login"; Map params = new HashMap(); params.put("name","test"); @@ -42,7 +42,7 @@ public void testLoginActionFailed() throws DocumentException, InstantiationExcep } @Test - public void testLogoutActionSuccess() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + public void testLogoutActionSuccess() throws Exception { String actionName = "logout"; @@ -58,7 +58,7 @@ public void testLogoutActionSuccess() throws DocumentException, InstantiationExc } @Test - public void testLogoutActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + public void testLogoutActionFailed() throws Exception { String actionName = "logout"; Map params = new HashMap(); params.put("name","test");