001/* Test for SharedParameter
002
003 Copyright (c) 2013 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
027 */
028
029package ptolemy.actor.parameters.test.junit;
030
031import static org.junit.Assert.assertTrue;
032
033import java.lang.reflect.Field;
034import java.util.Collection;
035
036import ptolemy.actor.CompositeActor;
037import ptolemy.actor.Initializable;
038import ptolemy.actor.parameters.SharedParameter;
039import ptolemy.kernel.util.NamedObj;
040import ptolemy.kernel.util.Workspace;
041
042///////////////////////////////////////////////////////////////////
043//// SharedParameterJUnitTest
044/**
045 * Tests for SharedParameter.
046 * <pre>
047 * (cd $PTII/ptolemy/actor/parameters/test/junit; java -classpath ${PTII}:${PTII}/lib/ptjacl.jar:${PTII}/lib/junit-4.8.2.jar:${PTII}/lib/JUnitParams-0.3.0.jar org.junit.runner.JUnitCore ptolemy.actor.parameters.test.junit.SharedParameterJUnitTest)
048 * </pre>
049 * @author Christopher Brooks
050 * @version $Id$
051 * @since Ptolemy II 10.0
052 * @Pt.ProposedRating Green (cxh)
053 * @Pt.AcceptedRating Green (cxh)
054 */
055public class SharedParameterJUnitTest {
056    /** Instantiate a CompositeActor with a SharedParameter and then
057     *  clone it.
058     *  @exception Exception If there is a problem cloning the
059     *  CompositeActor or accessing the _initializable field.
060     */
061    @org.junit.Test
062    public void run() throws Exception {
063
064        // - When a model contains an Initializable entity
065        // (e.g. Director or Actor) that contains Initializable
066        // Attributes/Parameters, each such entity typically
067        // maintains a collection ..._initializables.
068
069        Workspace workspace = new Workspace("myWorkspace");
070        CompositeActor compositeActor = new CompositeActor(workspace);
071        compositeActor.setName("compositeActor");
072
073        // - At construction time (e.g. during parsing), such attributes
074        // register themselves as Initializable with their containing
075        // entity, and are then stored in that collection.
076
077        // - The only way to be removed from there, is when a call
078        // ....setContainer() is done with null or another container.
079
080        // Findbugs: avoid a dead local store here.
081        /* SharedParameter sharedParameter = */new SharedParameter(
082                compositeActor, "sharedParameter", null, "4.5");
083
084        Collection<Initializable> initializables = _getInitializableField(
085                compositeActor);
086
087        // - During a clone(), the entities are not deep-cloned. The
088        // result is that a cloned Director/Actor (i.e. during the
089        // execution of its base NamedObj.clone(Workspace)), ends up
090        // with a reference to the same _initializables collection as
091        // the original instance.
092
093        // NOTE: Shouldn't this be clone(workspace)?
094        Workspace clonedWorkspace = new Workspace("clonedWorkspace");
095
096        CompositeActor clonedCompositeActor = (CompositeActor) compositeActor
097                .clone(clonedWorkspace);
098
099        Collection<Initializable> clonedInitializables = _getInitializableField(
100                clonedCompositeActor);
101
102        assertTrue(initializables.size() == clonedInitializables.size());
103
104        for (Initializable initializable : initializables) {
105            NamedObj initializableContainer = ((NamedObj) initializable)
106                    .getContainer();
107            for (Initializable clonedInitializable : clonedInitializables) {
108                NamedObj clonedInitializableContainer = ((NamedObj) clonedInitializable)
109                        .getContainer();
110
111                // First, check for equals.
112                if (initializable.equals(clonedInitializable)) {
113                    System.out.println("Error!.  An initializable: "
114                            + ((NamedObj) initializable).getFullName()
115                            + "(contained in "
116                            + initializableContainer.getFullName()
117                            + ", with workspace: "
118                            + initializableContainer.workspace().getName()
119                            + ") is equal to an initializable in the clone: "
120                            + ((NamedObj) clonedInitializable).getFullName());
121                }
122                assertTrue(!initializable.equals(clonedInitializable));
123
124                // Then, check for the same container.
125                if (initializableContainer
126                        .equals(clonedInitializableContainer)) {
127                    System.out.println("Error!.  The container of "
128                            + ((NamedObj) initializable).getFullName() + " is "
129                            + initializableContainer.getFullName()
130                            + ", which is the same as the container of "
131                            + ((NamedObj) clonedInitializable).getFullName());
132                }
133                assertTrue(!initializableContainer
134                        .equals(clonedInitializableContainer));
135            }
136        }
137
138        // Further in the NamedObj.clone(Workspace), all attributes
139        // are cloned and are then set to their new container,
140        // i.e. end up in the _initializables collection which is
141        // still pointing to the original one.
142
143        // The result is that the original model's entities are gathering
144        // references to all the cloned Initializable parameters (e.g. a
145        // SharedParameter), and so also to the containers.  => the cloned
146        // models can never be garbage-collected after their execution...
147
148        // In Ptolemy v7, the cloned entities even hang-on to this
149        // "shared" initializables collection.
150
151        // - On the trunk I noticed that the initializables collection
152        // is set to null during the clone. But this is too late,
153        // i.e. after the NameObj.clone() returns...
154
155        // I think that the current organisation of clone() and
156        // clone(Workspace) in NamedObj doesn't allow to intercept at
157        // the right moment for this situation?
158
159        // Probably similar issues could occur with other cases of
160        // state maintained by entities, outside of their parameters.
161    }
162
163    /** Given a CompositeActor, return the value of the protected _initializable field.
164     *  @param compositeActor The composite actor.
165     *  @return The value of the protected _initializable field.
166     *  @exception Exception If the field cannot be accessed.
167     */
168    private Collection<Initializable> _getInitializableField(
169            CompositeActor compositeActor) throws Exception {
170        Field[] fields = compositeActor.getClass().getDeclaredFields();
171        for (int i = 0; i < fields.length; i++) {
172            fields[i].setAccessible(true);
173            if (fields[i].getName().equals("_initializables")) {
174                Collection<Initializable> initializables = (Collection<Initializable>) fields[i]
175                        .get(compositeActor);
176                System.out.println(
177                        "fields: " + fields[i].getName() + ": " + initializables
178                                + " workspace: " + compositeActor.workspace());
179                return initializables;
180            }
181        }
182        return null;
183    }
184}