View Javadoc

1   /*
2   This project provide access to non public fields
3   and methods as help for writing tests.
4   Copyright (C) 2007  Christof Lehmann
5   
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License
8   as published by the Free Software Foundation; either version 2
9   of the License, or (at your option) any later version.
10  
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   */
20  package net.sf.testextensions;
21  
22  import java.lang.reflect.Field;
23  
24  /**
25   * The class <code>AccessFieldImpl</code> declares the required
26   * methods for accessing a non public field.
27   *
28   * @author forge-cl
29   *
30   */
31  class AccessFieldImpl implements AccessField {
32  
33  	/**
34  	 * The file <code>inspectedObject</code> contains
35  	 * the concret instance of the class.
36  	 */
37  	private Object inspectedObject;
38  	/**
39  	 * The field <code>selectedField</code> contains
40  	 * the field, which should manipulate.
41  	 */
42  	private Field selectedField;
43  
44  	/**
45  	 *
46  	 */
47  	private AccessibilityManipulator manipulator;
48  	/**
49  	 * The constructor take over the required data and
50  	 * initialised the object.
51  	 * @param theInspectedClass the class which will inspected.
52  	 * @param theInspectedObject the object, which is an instance
53  	 * 	of the class.
54  	 * @param theSelectedField the field, which contains to the
55  	 * class and the object.
56  	 */
57  	public AccessFieldImpl(final Class theInspectedClass,
58  			final Object theInspectedObject, final Field theSelectedField) {
59  			inspectedObject = theInspectedObject;
60  			selectedField = theSelectedField;
61  			selectAccessibilityManipulator();
62  	}
63  
64  	/**
65  	 * The method <code>selectAccessibilityManipulator</code>
66  	 * select a behavior for manipulation of the
67  	 * SecurityManager.
68  	 */
69  	private void selectAccessibilityManipulator() {
70  		manipulator = AccessibilityManipulator.selectManipulator(selectedField);
71  	}
72  
73  	/**
74  	 * The method <code>getValue</code> give the value of the selected field.
75  	 *
76  	 * @return the value of the selected field.
77  	 * @throws TestExtensionException
78  	 *             shows, that an error occures during field access.
79  	 */
80  	public Object getValue() throws TestExtensionException {
81  		Object aValue = null;
82  		manipulator.before();
83  		try {
84  			aValue = selectedField.get(inspectedObject);
85  		} catch (IllegalArgumentException e) {
86  			throw new TestExtensionException(e);
87  		} catch (IllegalAccessException e) {
88  			throw new TestExtensionException(e);
89  		} finally {
90  			manipulator.after();
91  		}
92  		return aValue;
93  	}
94  
95  	/**
96  	 * The method <code>setValue</code> set the selected field to the given
97  	 * value.
98  	 *
99  	 * @param aNewValue
100 	 *            the value, which should the selected field contains after this
101 	 *            method call.
102 	 * @throws TestExtensionException
103 	 *             if the dstValue does not suite to the field or any other
104 	 *             exception occures.
105 	 */
106 	public void setValue(final Object aNewValue) throws TestExtensionException {
107 		manipulator.before();
108 		try {
109 			selectedField.set(inspectedObject, aNewValue);
110 		} catch (IllegalArgumentException e) {
111 			throw new TestExtensionException(e);
112 		} catch (IllegalAccessException e) {
113 			throw new TestExtensionException(e);
114 		} finally {
115 			manipulator.after();
116 		}
117 
118 	}
119 
120 }