001/* A test for IterateOverArray.clone(Workspace) 002 003 Copyright (c) 2012-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 */ 027package ptolemy.actor.lib.hoc.test; 028 029import java.lang.reflect.Field; 030import java.util.List; 031 032import ptolemy.actor.TypedCompositeActor; 033import ptolemy.actor.lib.hoc.IterateOverArray; 034import ptolemy.kernel.util.Workspace; 035 036/** 037 * Test for clone(Workspace) of IterateOverArray. 038 * To run: 039 * java -classpath $PTII ptolemy.actor.lib.hoc.test.IterateOverArrayCloneTest 040 */ 041public class IterateOverArrayCloneTest { 042 /** Check the clone(Workspace) method of the IterateOverArray class. 043 * Instantiate an IterateOverArray actor and get the Workspace 044 * of the inner IterateDirector. Then clone the actor into a 045 * new Workspace and get the Workspace of the inner IterateDirector 046 * of the *clone*. The Workspaces should be different. 047 * <p>To run:</p> 048 * <code> 049 * java -classpath $PTII ptolemy.actor.lib.hoc.test.IterateOverArrayCloneTest 050 * </code> 051 * 052 * @param args Ignored 053 * @exception Throwable If there is a problem with the test. 054 */ 055 public static void main(String args[]) throws Throwable { 056 // Create an IterateOverArray 057 Workspace workspace = new Workspace("masterWorkspace"); 058 TypedCompositeActor container = new TypedCompositeActor(workspace); 059 IterateOverArray iterateOverArray = new IterateOverArray(container, 060 "iterateOverArray"); 061 062 // IterateOverArray has an inner class called IterateDirector. 063 Class iterateDirectorClass = Class.forName( 064 "ptolemy.actor.lib.hoc.IterateOverArray$IterateDirector"); 065 List iterateDirectors = iterateOverArray 066 .attributeList(iterateDirectorClass); 067 Object iterateDirector = iterateDirectors.get(0); 068 069 // Get the this$0 field, which refers to the outer class. 070 Field thisZeroField = iterateDirectorClass.getDeclaredField("this$0"); 071 thisZeroField.setAccessible(true); 072 073 // Get the outer object and then the Workspace 074 IterateOverArray outerIterateOverArray = (IterateOverArray) thisZeroField 075 .get(iterateDirectorClass.cast(iterateDirector)); 076 Workspace outerIterateOverArrayWorkspace = outerIterateOverArray 077 .workspace(); 078 System.out.println("The workspace of the outer class is " 079 + outerIterateOverArrayWorkspace.getName()); 080 081 //////////// 082 // Call clone(Workspace) and get the outer object and the workspace. 083 Workspace cloneWorkspace = new Workspace("cloneWorkspace"); 084 085 IterateOverArray clonedIterateOverArray = (IterateOverArray) iterateOverArray 086 .clone(cloneWorkspace); 087 List clonedIterateDirectors = clonedIterateOverArray 088 .attributeList(iterateDirectorClass); 089 Object clonedIterateDirector = clonedIterateDirectors.get(0); 090 091 // Get the outer object and then the Workspace 092 IterateOverArray clonedOuterIterateOverArray = (IterateOverArray) thisZeroField 093 .get(iterateDirectorClass.cast(clonedIterateDirector)); 094 Workspace clonedOuterIterateOverArrayWorkspace = clonedOuterIterateOverArray 095 .workspace(); 096 System.out.println("The workspace of the outer class of the clone is " 097 + clonedOuterIterateOverArrayWorkspace.getName()); 098 099 if (outerIterateOverArray.equals(clonedOuterIterateOverArray)) { 100 System.err.println( 101 "Error! the outer IterateOverArray objects are equal?"); 102 } else { 103 System.err.println( 104 "Passed! the outer IterateOverArray objects are not equal!"); 105 } 106 107 if (outerIterateOverArrayWorkspace 108 .equals(clonedOuterIterateOverArrayWorkspace)) { 109 System.err.println("Error! the workspaces are equal?"); 110 System.exit(1); 111 } else { 112 System.err.println("Passed! the workspaces are not equal!"); 113 } 114 115 } 116}