testrobustvisitor.java
来自「一个关于java 的常用工具包」· Java 代码 · 共 661 行 · 第 1/2 页
JAVA
661 行
} Object[] nullArray = 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(nullArray); } 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 testApplyToIterator() { // reset the vectors. setUp(); // test in case of an exception // throw an exception at the 37th element // and check whether the end result // is equal to $origIntVector. Iterator iter = $intVector.iterator(); 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(iter); // An exception should be thrown assertTrue(false); } catch(Exception exc) { } catch(Throwable th) { assertTrue(false); } assertEquals($intVector,$origIntVector); iter = $intVector.iterator(); // 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(iter); // An exception should be thrown assertTrue(false); } catch(Exception exc) { } catch(Throwable th) { assertTrue(false); } assertEquals($intVector,$origIntVector); iter=$intVector.iterator(); // 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(iter); } catch(Exception exc) { assertTrue(false); } catch(Throwable th) { assertTrue(false); } assertTrue(! iter.hasNext()); assertEquals($intVector,$otherIntVector); Iterator nullIterator = 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(nullIterator); } 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 testApplyToEnumeration() { // reset the vectors. setUp(); // test in case of an exception // throw an exception at the 37th element // and check whether the end result // is equal to $origIntVector. Enumeration enum = $intVector.elements(); 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(enum); // An exception should be thrown assertTrue(false); } catch(Exception exc) { } catch(Throwable th) { assertTrue(false); } assertEquals($intVector,$origIntVector); enum = $intVector.elements(); // 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(enum); // An exception should be thrown assertTrue(false); } catch(Exception exc) { } catch(Throwable th) { assertTrue(false); } assertEquals($intVector,$origIntVector); enum=$intVector.elements(); // 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(enum); } catch(Exception exc) { assertTrue(false); } catch(Throwable th) { assertTrue(false); } assertTrue(! enum.hasMoreElements()); assertEquals($intVector,$otherIntVector); Enumeration nullEnumeration = 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(nullEnumeration); } 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()); // }}//############################ class MyInt { public MyInt(int integer){ setInt(integer); } public int getInt(){ return $int; } public void setInt(int integer){ $int=integer; } public boolean equals(Object other){ if(other instanceof MyInt){ return getInt() == ((MyInt) other).getInt(); } return false; } private int $int; } }/* * <copyright>Copyright (C) 1997-2001. This software is copyrighted by * the people and entities mentioned after the "@author" tags above, on * behalf of the JUTIL.ORG Project. The copyright is dated by the dates * after the "@date" tags above. All rights reserved. * This software is published under the terms of the JUTIL.ORG Software * License version 1.1 or later, a copy of which has been included with * this distribution in the LICENSE file, which can also be found at * http://org-jutil.sourceforge.net/LICENSE. This software is distributed * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the JUTIL.ORG Software License for more details. For more information, * please see http://org-jutil.sourceforge.net/</copyright> */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?