1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package lib;
20 import com.sun.star.uno.XInterface;
21 
22 import java.util.HashMap;
23 
24 
25 /**
26  * The class contains an instance of a given implementation object and
27  * auxiliary objects associated with it and required for the object testing.
28  *
29  * @see TestCase
30  */
31 
32 public final class TestEnvironment {
33     /**
34      * Contains object relations - auxiliary objects associated with the
35      * tested object and required for testing.
36      */
37     private final HashMap<String, Object> relations = new HashMap<String, Object>(10);
38 
39     /**
40      * An instance of the tested implementation object.
41      */
42     private final XInterface testObject;
43 
44     /**
45      * Indicates that the testObject is in invalid state and should not be
46      * used for testing anymore.
47      */
48     private boolean disposed = false;
49 
50     /**
51      * A reference to TestCase which has created the test environment.
52      */
53     private TestCase tCase;
54 
55     /**
56      * Creates an instance of test environment with testObject.
57      *
58      * @param testObject object to test
59      *
60      * @throws java.lang.IllegalArgumentException if the testObject is
61      * <tt>null</tt>.
62      */
TestEnvironment( XInterface testObject )63     public TestEnvironment( XInterface testObject ) {
64         if (testObject == null) {
65             throw new IllegalArgumentException(
66                     "Couldn't create a test object");
67             }
68         this.testObject = testObject;
69     }
70 
71     /**
72      * @return the object to test.
73      */
getTestObject()74     public XInterface getTestObject() {
75         return testObject;
76     }
77 
78     /**
79      * Adds to the environment an auxiliary object required for testing.
80      *
81      * @param name a name to reference the auxiliary object
82      *
83      * @param relation the auxiliary object related to the tested one
84      */
addObjRelation( String name, Object relation)85     public void addObjRelation( String name, Object relation) {
86         relations.put( name, relation );
87     }
88 
89     /**
90      * Returns an auxiliary object referenced by tname.
91      *
92      * @param name a name of the object relation
93      *
94      * @return the auxiliary object(object relation)
95      */
getObjRelation( String name )96     public Object getObjRelation( String name ) {
97         return relations.get( name );
98     }
99 
100     /**
101      * Sets the <code>TestCase</code> that created the environment.
102      */
setTestCase( TestCase tCase)103     public void setTestCase( TestCase tCase) {
104         this.tCase = tCase;
105     }
106 
107     /**
108      * @return the <code>TestCase</code> created the environment.
109      */
getTestCase()110     public TestCase getTestCase() {
111         return tCase;
112     }
113 
114     /**
115      * Makes the environment invalid, i.e. it should not be used for
116      * testing anymore.
117      */
dispose()118     public void dispose() {
119         disposed = true;
120     }
121 
122     /**
123      * Checks if the environment has been disposed.
124      *
125      * @return <tt>true</tt< if it has been disposed, <tt>false</tt> otherwise.
126      *
127      * @see #dispose()
128      */
isDisposed()129     public boolean isDisposed() {
130         return disposed;
131     }
132 }
133