testvisitor.java

来自「一个关于java 的常用工具包」· Java 代码 · 共 331 行

JAVA
331
字号
package org.jutiltest.java.collections;import org.jutil.java.collections.Visitor;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/TestVisitor.java,v $ * @date    $Date: 2002/09/07 09:37:22 $ * @state   $State: Exp $ * @author  Marko van Dooren * @release $Name:  $ */public class TestVisitor extends JutilTest{    public TestVisitor(String name) {    super(name, new CVSRevision("1.19"));  }    private Vector $integers;  private Vector $intVector;    private Vector $doubleIntVector;      private Vector $otherIntVector;    private MyInt[][] $intArray;     private MyInt[][] $otherIntArray;    //   private MyInt[][] getIntArray() {//     return $intArray;//   }     public void setUp() {    $integers=new Vector();    for(int i = 0; i<10; i++) {      $integers.add(new Integer(i));    }        $intVector = new Vector();    $intArray = new MyInt[10][10];    for(int i = 1; i<=10; i++) {      for(int j = 1; j<=10; j++) {        $intArray[i-1][j-1]=new MyInt((i-1)*10 + j);        $intVector.add(new MyInt((i-1)*10 + j));      }    }        $otherIntVector = new Vector();    $doubleIntVector = new Vector();    $otherIntArray = new MyInt[10][10];    for(int i = 1; i<=10; i++) {      for(int j = 1; j<=10; j++) {        $otherIntArray[i-1][j-1]=new MyInt((i-1)*10 + j -1);        $otherIntVector.add(new MyInt((i-1)*10 + j -1));        $doubleIntVector.add(new MyInt(2 * ((i-1)*10 + j)));      }    }    // $otherIntArray[i][j] == $intArray[i][j] - 1;  }    public void testApplyTo() {    // Add all integerts in $integerSet to a new Vector and    // check whether both vectors are equal.    final Vector otherIntegerSet = new Vector();    Collection clone = new HashSet($integers);    new Visitor() {      public void visit(Object element) {        otherIntegerSet.add(element);      }    }.applyTo($integers);    assertEquals($integers,otherIntegerSet);    // Check whether the collection has been altered.    assertTrue(Collections.identical($integers,otherIntegerSet));        // passing null as an argument should do nothing    Collection nullCollection = null;    try {      new Visitor() {        public void visit(Object element) {          otherIntegerSet.add(element);        }      }.applyTo(nullCollection);    }    catch(Exception exc) {        assertTrue(false);    }        //pass an empty collection    try {      new Visitor() {        public void visit(Object element) {          otherIntegerSet.add(element);        }      }.applyTo(new HashSet());    }    catch(Exception exc) {        assertTrue(false);    }        //pass a collection containing only null references    Vector stupidVector=new Vector();    for(int i=0; i<100;i++) {      stupidVector.add(null);    }        try {      new Visitor() {        public void visit(Object element) {          otherIntegerSet.add(element);        }      }.applyTo(stupidVector);    }    catch(Exception exc) {        assertTrue(false);    }      }    public void testApplyToArray() {    new Visitor() {      public void visit(Object element) {          MyInt integer = (MyInt) element;        integer.setInt(integer.getInt() - 1);      }    }.applyTo($intArray);    for(int i=0;i<10;i++) {      for(int j=0;j<10;j++) {        assertEquals($intArray[i][j], $otherIntArray[i][j]);      }    }    // passing null as an argument should do nothing    Object[] nullArray = null;    try {      new Visitor() {        public void visit(Object element) {            MyInt integer = (MyInt) element;          integer.setInt(integer.getInt() - 1);        }      }.applyTo(nullArray);    }    catch(Exception exc) {        assertTrue(false);    }    // empty array    Object[][] emptyArray = new Object[13][45];    try {      new Visitor() {        public void visit(Object element) {            //NOP since elements are null        }      }.applyTo(emptyArray);    }    catch(Exception exc) {        assertTrue(false);    }        emptyArray = new Object[13][0];    try {      new Visitor() {        public void visit(Object element) {            MyInt integer = (MyInt) element;          integer.setInt(integer.getInt() - 1);        }      }.applyTo(emptyArray);    }    catch(Exception exc) {        assertTrue(false);    }      }  public void testApplyToIterator() {    Iterator iter = $intVector.iterator();    new Visitor() {      public void visit(Object element){        MyInt integer = (MyInt) element;        integer.setInt(integer.getInt() - 1);      }    }.applyTo(iter);    assertTrue(! iter.hasNext());    assertEquals($intVector,$otherIntVector);    Iterator nullIterator = null;    try {      new Visitor() {        public void visit(Object element){          MyInt integer = (MyInt) element;          integer.setInt(integer.getInt() - 1);        }      }.applyTo(nullIterator);    }    catch(Exception exc) {        assertTrue(false);    }    // pass an iterator for an empty collection.    try {      new Visitor() {        public void visit(Object element){          MyInt integer = (MyInt) element;          integer.setInt(integer.getInt() - 1);        }      }.applyTo(new HashSet().iterator());    }    catch(Exception exc) {        assertTrue(false);    }        // pass an iterator for a collection with only    // null references.    Vector stupidVector=new Vector();    for(int i=0; i<100;i++) {      stupidVector.add(null);    }      try {      new Visitor() {        public void visit(Object element){          //NOP of course        }      }.applyTo(stupidVector.iterator());    }    catch(Exception exc) {        assertTrue(false);    }              }    public void testApplyToEnumeration() {    Enumeration enum = $intVector.elements();    new Visitor() {      public void visit(Object element){        MyInt integer = (MyInt) element;        integer.setInt(integer.getInt() * 2);      }    }.applyTo(enum);        assertTrue(! enum.hasMoreElements());    assertEquals($intVector,$doubleIntVector);    // null as an argument should do nothing    try {      new Visitor() {        public void visit(Object element){          MyInt integer = (MyInt) element;          integer.setInt(integer.getInt() * 2);        }      }.applyTo($intVector.elements());            }    catch(Exception exc) {        assertTrue(false);    }    // pass an enumeration for an empty collection.    try {      new Visitor() {        public void visit(Object element){          MyInt integer = (MyInt) element;          integer.setInt(integer.getInt() - 1);        }      }.applyTo(new Vector().elements());    }    catch(Exception exc) {        assertTrue(false);    }        // pass an iterator for a collection with only    // null references.    Vector stupidVector=new Vector();    for(int i=0; i<100;i++) {      stupidVector.add(null);    }      try {      new Visitor() {        public void visit(Object element){          //NOP of course        }      }.applyTo(stupidVector.elements());    }    catch(Exception exc) {        assertTrue(false);    }                }      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;  }    public void tearDown() {    //nothing actually  }}/* * <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 + -
显示快捷键?