File: getBeanInfo2.java

package info (click to toggle)
mauve 20161030-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 44,628 kB
  • ctags: 35,425
  • sloc: java: 336,555; sh: 2,834; xml: 208; makefile: 72
file content (176 lines) | stat: -rw-r--r-- 6,124 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Tags: JDK1.2
// Uses: getBeanInfoTestClass

//Copyright (C) 2004  Robert Schuster <theBohemian@gmx.net>

//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version. 
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING.  If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA.  */

package gnu.testlet.java.beans.Introspector;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.lang.reflect.Method;

/** This tests the <strong>method</strong> retrieving mechanism of the
 * Introspector class.
 * 
 * <p>See {@link getBeanInfoTestClass} for details on what is
 * expected.</p>
 * 
 * <p>The method test goes like this:</p>
 * <p>There is a test class having a number of well-known methods. All
 * <code>public</code> methods should be available in a
 * <code>MethodDescriptor</code> array. There are two methods
 * which are not <code>public</code> and these should not be
 * available in the array.</p>
 * 
 * @author Robert Schuster <theBohemian@gmx.net>
 */
public class getBeanInfo2 implements Testlet
{

    public void test(TestHarness harness)
    {
        propertyTest(harness);
    }

    void propertyTest(TestHarness harness)
    {
        BeanInfo bi = null;
        Class testClass = getBeanInfoTestClass.class;

        Method[] expectedMethods = null;
        Method[] unexpectedMethods = null;

        try
        {
            // these are the method that have to show up
            expectedMethods =
                new Method[] {
                    testClass.getMethod(
                        "setCorrectProperty",
                        new Class[] { Integer.TYPE }),
                        
                    testClass.getMethod("getCorrectProperty", new Class[0]),
                    
                    testClass.getMethod(
                        "getCorrectReadOnlyProperty",
                        new Class[0]),
                        
                    testClass.getMethod(
                        "setCorrectWriteOnlyProperty",
                        new Class[] { Integer.TYPE }),
                        
                    testClass.getMethod(
                        "setSomeStaticValue",
                        new Class[] { Integer.TYPE }),
                        
                    testClass.getMethod("getSomeStaticValue", new Class[0]),
                    };

            /* these are the method that have not to show up - it is neccessary to use
             * getDeclaredMethod() here because the wanted methods are not public.
             */
            unexpectedMethods =
                new Method[] {
                    testClass.getDeclaredMethod(
                        "setSomeValue",
                        new Class[] { Integer.TYPE }),
                        
                    testClass.getDeclaredMethod("getSomeValue", new Class[0]),
                    };
        }
        catch (NoSuchMethodException nsme)
        {
            // if that happens check the test class (compiler?)
            harness.fail("TEST_PREPARATION_NOSUCHMETHODEXCEPTION");
        }

        // checks wether all expected methods have been retrieved successfully
        for (int i = 0; i < expectedMethods.length; i++)
        {
            harness.check(
                expectedMethods[i] != null,
                "TEST_PREPARATION_METHOD_INSTANCE_AVAILABLE");
        }

        // checks wether all unexpected methods have been retrieved successfully
        for (int i = 0; i < unexpectedMethods.length; i++)
        {
            harness.check(
                unexpectedMethods[i] != null,
                "TEST_PREPARATION_METHOD_INSTANCE_AVAILABLE");
        }

        // retrieves the BeanInfo instance of the test class
        try
        {
            bi = Introspector.getBeanInfo(testClass, testClass.getSuperclass());
        }
        catch (IntrospectionException ie)
        {
            harness.fail("TEST_PREPARATION_INTROSPECTION_EXCEPTION");
        }

        harness.check(bi != null, "TEST_PREPARATION_VALID_BEANINFO");

        MethodDescriptor[] methodDescriptors = bi.getMethodDescriptors();

        // Since we only introspected the test class the number of method descriptors must match
        // the number of expected methods.
        harness.check(
            methodDescriptors.length,
            expectedMethods.length,
            "MATCH_EXPECTED_METHOD_COUNT");

        for (int i = 0; i < expectedMethods.length; i++)
        {
            harness.check(
                containsMethod(methodDescriptors, expectedMethods[i]),
                "EXPECTED_METHOD_AVAILABLE: " + expectedMethods[i].getName());
        }

        for (int i = 0; i < unexpectedMethods.length; i++)
        {
            harness.check(
                !containsMethod(methodDescriptors, unexpectedMethods[i]),
                "UNEXPECTED_METHOD_UNAVAILABE: "
                    + unexpectedMethods[i].getName());
        }

    }

    /** Returns whether a given method array contains a certain method.
     * 
     * @param methods The array of method to be examined.
     * @param key The method which is looked up.
     * @return Whether the key method is in the array.
     */
    boolean containsMethod(MethodDescriptor[] methodDescriptors, Method key)
    {
        for (int i = 0; i < methodDescriptors.length; i++)
        {
            if (key.equals(methodDescriptors[i].getMethod()))
                return true;
        }

        return false;
    }

}