1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package lib;
29 import com.sun.star.uno.XInterface;
30 
31 import java.util.Hashtable;
32 
33 
34 /**
35  * The class contains an instance of a given implementation object and
36  * auxiliary objects associated with it and required for the object testing.
37  *
38  * @see TestCase
39  */
40 
41 public final class TestEnvironment {
42     /**
43      * Contains object relations - auxiliary objects associated with the
44      * tested object and required for testing.
45      */
46     private final Hashtable relations = new Hashtable(10);
47 
48     /**
49      * An instance of the tested implementation object.
50      */
51     private final XInterface testObject;
52 
53     /**
54      * Indicates that the testObject is in invalid state and should notbe
55      * used for testing anymore.
56      */
57     private boolean disposed = false;
58 
59     /**
60      * A reference to TestCase which has created the test environment.
61      */
62     private TestCase tCase;
63 
64     /**
65      * Creates an instance of test environment with testObject.
66      *
67      * @param testObject object to test
68      *
69      * @throws java.lang.IllegalArgumentException if the testObject is
70      * <tt>null</tt>.
71      */
72     public TestEnvironment( XInterface testObject ) {
73         if (testObject == null) {
74             throw new IllegalArgumentException(
75                     "Couldn't create a test object");
76             }
77         this.testObject = testObject;
78     }
79 
80     /**
81      * @return the object to test.
82      */
83     public XInterface getTestObject() {
84         return testObject;
85     }
86 
87     /**
88      * Adds to the environment an auxiliary object required for testing.
89      *
90      * @param name a name to reference the auxiliary object
91      *
92      * @param relation the auxiliary object related to the tested one
93      */
94     public void addObjRelation( String name, Object relation) {
95         relations.put( name, relation );
96     }
97 
98     /**
99      * Returns an auxiliary object referenced by tname.
100      *
101      * @param name a name of the object relation
102      *
103      * @return the auxiliary object(object relation)
104      */
105     public Object getObjRelation( String name ) {
106         return relations.get( name );
107     }
108 
109     /**
110      * Checks if an auxiliary object has been registered with name
111      *
112      * @param name a name referencing an auxiliarx object
113      *
114      * @return <tt>true</tt> if the object has been associated, <tt>false</tt>
115      * otherwise.
116      */
117     public boolean hasObjRelation(String name) {
118         return (relations.get(name) != null) ;
119     }
120 
121     /**
122      * Sets the <code>TestCase</code> that created the environment.
123      */
124     public void setTestCase( TestCase tCase) {
125         this.tCase = tCase;
126     }
127 
128     /**
129      * @return the <code>TestCase</code> created the environment.
130      */
131     public TestCase getTestCase() {
132         return tCase;
133     }
134 
135     /**
136      * Makes the environment invalid, i.e. it should not be used for
137      * testing anymore.
138      */
139     public void dispose() {
140         disposed = true;
141     }
142 
143     /**
144      * Checks if the environment has been disposed.
145      *
146      * @return <tt>true</tt< if it has been disposed, <tt>false</tt> otherwise.
147      *
148      * @see #dispose()
149      */
150     public boolean isDisposed() {
151         return disposed;
152     }
153 }