testrobustvisitor.java

来自「一个关于java 的常用工具包」· Java 代码 · 共 661 行 · 第 1/2 页

JAVA
661
字号
package org.jutiltest.java.collections;import org.jutil.java.collections.RobustVisitor;import org.jutil.junit.JutilTest;import org.jutil.junit.CVSRevision;import org.jutil.java.collections.Collections;import junit.framework.*;import java.util.Vector;import java.util.Collection;import java.util.Iterator;import java.util.Enumeration;import java.util.HashSet;/*  * @path    $Source: /cvsroot/org-jutil/jutil.org/src/org/jutiltest/java/collections/TestRobustVisitor.java,v $ * @date    $Date: 2002/07/21 18:59:18 $ * @state   $State: Exp $ * @author  Marko van Dooren * @release $Name:  $ */public class TestRobustVisitor extends JutilTest {    public TestRobustVisitor(String name) {    super(name, new CVSRevision("1.16"));  }    private Vector $intVector;    private Vector $origIntVector;      private Vector $otherIntVector;    private MyInt[][] $intArray;    private MyInt[][] $origIntArray;      private MyInt[][] $otherIntArray;      public void setUp() {    $intVector = new Vector();    $origIntVector = new Vector();    $otherIntVector = new Vector();    $intArray = new MyInt[10][10];    $origIntArray = new MyInt[10][10];    $otherIntArray = new MyInt[10][10];    for(int i=0; i<100; i++) {      $intVector.add(new MyInt(i));      $origIntVector.add(new MyInt(i));      $otherIntVector.add(new MyInt(i + 1));    }    for(int i=0; i<10; i++) {      for(int j=0; j<10; j++) {        $intArray[i][j] = new MyInt(i*10 + j);        $origIntArray[i][j] = new MyInt(i*10 + j);        $otherIntArray[i][j] = new MyInt(i*10 + j + 1);      }    }      }      public void testApplyToCollection() {    // test in case of an exception    // throw an exception at the 37th element    // and check whether the end result    // is equal to $origIntVector.    Collection clone = null;    try {      clone = new HashSet($intVector);      new RobustVisitor() {        public Object visit(Object element) throws Exception {          MyInt integer = (MyInt) element;          if(integer.getInt() == 37) {            throw new Exception();          }          integer.setInt(integer.getInt() + 1);          return null;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          integer.setInt(integer.getInt() - 1);        }      }.applyTo($intVector);      // An exception should be thrown      assertTrue(false);    }    catch(Exception exc) {    }    catch(Throwable th) {      assertTrue(false);    }    assertEquals($intVector,$origIntVector);        assertTrue(Collections.identical($intVector, clone));            // now using the undo data.    try {      new RobustVisitor() {        public Object visit(Object element) throws Exception {          MyInt integer = (MyInt) element;          Integer undo = new Integer(integer.getInt());          if(integer.getInt() == 37) {            throw new Exception();          }          integer.setInt(integer.getInt() + 1);          return undo;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          Integer undoInt = (Integer) undo;          integer.setInt(undoInt.intValue());        }      }.applyTo($intVector);      // An exception should be thrown      assertTrue(false);    }    catch(Exception exc) {    }    catch(Throwable th) {      assertTrue(false);    }    assertEquals($intVector,$origIntVector);            // check the normal behaviour    try {      new RobustVisitor() {        public Object visit(Object element) throws Exception {          MyInt integer = (MyInt) element;          Integer undo = new Integer(integer.getInt());          integer.setInt(integer.getInt() + 1);          return undo;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          Integer undoInt = (Integer) undo;          integer.setInt(undoInt.intValue());        }      }.applyTo($intVector);    }    catch(Exception exc) {      assertTrue(false);    }    catch(Throwable th) {      assertTrue(false);    }    assertEquals($intVector,$otherIntVector);    Vector nullVector = null;    // check null as an argument      try {      new RobustVisitor() {        public Object visit(Object element) throws Exception {          MyInt integer = (MyInt) element;          Integer undo = new Integer(integer.getInt());          integer.setInt(integer.getInt() + 1);          return undo;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          Integer undoInt = (Integer) undo;          integer.setInt(undoInt.intValue());        }      }.applyTo(nullVector);    }    catch(Exception exc) {      assertTrue(false);    }    catch(Throwable th) {      assertTrue(false);    }    // pass an empty collection as argument    try {      new RobustVisitor() {        public Object visit(Object element) throws Exception {          MyInt integer = (MyInt) element;          Integer undo = new Integer(integer.getInt());          integer.setInt(integer.getInt() + 1);          return undo;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          Integer undoInt = (Integer) undo;          integer.setInt(undoInt.intValue());        }      }.applyTo(new HashSet());    }    catch(Exception exc) {      assertTrue(false);    }    catch(Throwable th) {      assertTrue(false);    }    Vector stupidVector=new Vector();    for(int i=0; i<100;i++) {      stupidVector.add(null);    }        try {      new RobustVisitor() {        public Object visit(Object element) throws Exception {          return null;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          Integer undoInt = (Integer) undo;          integer.setInt(undoInt.intValue());        }      }.applyTo(stupidVector);    }    catch(Exception exc) {      assertTrue(false);    }    catch(Throwable th) {      assertTrue(false);    }                //         //     for(int i=0;i<100;i++) {//       MyInt first = (MyInt) $intVector.elementAt(i);//       MyInt second = (MyInt) $origIntVector.elementAt(i);//       System.out.println(first.getInt()+" -> "+second.getInt());//     }  }  //#############################################  public void testApplyToArray() {    // test in case of an exception    // throw an exception at the 37th element    // and check whether the end result    // is equal to $origIntVector.    try {      new RobustVisitor() {        public Object visit(Object element) throws Exception {          MyInt integer = (MyInt) element;          if(integer.getInt() == 37) {            throw new Exception();          }          integer.setInt(integer.getInt() + 1);          return null;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          integer.setInt(integer.getInt() - 1);        }      }.applyTo($intArray);      // An exception should be thrown      assertTrue(false);    }    catch(Exception exc) {    }    catch(Throwable th) {      assertTrue(false);    }    for(int i=0;i<10;i++) {      for(int j=0;j<10;j++) {        assertEquals($intArray[i][j], $origIntArray[i][j]);      }    }            // now using the undo data.    try {      new RobustVisitor() {        public Object visit(Object element) throws Exception {          MyInt integer = (MyInt) element;          Integer undo = new Integer(integer.getInt());          if(integer.getInt() == 37) {            throw new Exception();          }          integer.setInt(integer.getInt() + 1);          return undo;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          Integer undoInt = (Integer) undo;          integer.setInt(undoInt.intValue());        }      }.applyTo($intArray);      // An exception should be thrown      assertTrue(false);    }    catch(Exception exc) {    }    catch(Throwable th) {      assertTrue(false);    }    for(int i=0;i<10;i++) {      for(int j=0;j<10;j++) {        assertEquals($intArray[i][j], $origIntArray[i][j]);      }    }        // check the normal behaviour    try {      new RobustVisitor() {        public Object visit(Object element) throws Exception {          MyInt integer = (MyInt) element;          Integer undo = new Integer(integer.getInt());          integer.setInt(integer.getInt() + 1);          return undo;        }                public void unvisit(Object element,Object undo) {          MyInt integer = (MyInt) element;          Integer undoInt = (Integer) undo;          integer.setInt(undoInt.intValue());        }      }.applyTo($intArray);    }    catch(Exception exc) {      assertTrue(false);    }    catch(Throwable th) {      assertTrue(false);    }    for(int i=0;i<10;i++) {      for(int j=0;j<10;j++) {        assertEquals($intArray[i][j], $otherIntArray[i][j]);      }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?