summaryrefslogtreecommitdiffstats
path: root/support/src/test/java/tests/util/CallVerificationStack.java
blob: 6c1881bfe4bdb7e45f2938b8df32b21f9e9edcf6 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tests.util;

import java.util.Stack;

/**
 * A stack to store the parameters of a call, as well as the call stack.
 *
 */
public class CallVerificationStack extends Stack<Object> {

    /*
     * --------------------------------------------------------------------
     * Class variables
     * --------------------------------------------------------------------
     */

    private static final long serialVersionUID = 1L;

    // the singleton
    private static final CallVerificationStack _instance = new CallVerificationStack();

    /*
     * --------------------------------------------------------------------
     * Instance variables
     * --------------------------------------------------------------------
     */

    // the call stack, store StackTraceElement
    private final Stack<StackTraceElement> callStack = new Stack<StackTraceElement>();

    /*
     * -------------------------------------------------------------------
     * Constructors
     * -------------------------------------------------------------------
     */

    /**
     * Can't be instantiated.
     */
    private CallVerificationStack() {
        // empty
    }

    /*
     * -------------------------------------------------------------------
     * Methods
     * -------------------------------------------------------------------
     */

    /**
     * Gets the singleton instance.
     *
     * @return the singleton instance
     */
    public static CallVerificationStack getInstance() {
        return _instance;
    }

    /**
     * Pushes the call stack.
     */
    private void pushCallStack() {
        StackTraceElement[] eles = (new Throwable()).getStackTrace();
        int i;
        for (i = 1; i < eles.length; i++) {
            if (!eles[i].getClassName().equals(this.getClass().getName())) {
                break;
            }
        }
        this.callStack.push(eles[i]);
    }

    /**
     * Gets the "current" calling class name.
     *
     * @return the "current" calling class name
     */
    public String getCurrentSourceClass() {
        return this.callStack.peek().getClassName();
    }

    /**
     * Gets the "current" calling method name.
     *
     * @return the "current" calling method name
     */
    public String getCurrentSourceMethod() {
        return this.callStack.peek().getMethodName();
    }

    /**
     * Clear the parameter stack and the call stack.
     *
     */
    @Override
    public void clear() {
        this.callStack.clear();
        super.clear();
    }

    @Override
    public Object push(Object o) {
        pushCallStack();
        return super.push(o);
    }

    /**
     * Pushes a boolean onto the top of this stack.
     *
     * @param val
     *            the value to push
     */
    public void push(boolean val) {
        this.push(new BaseTypeWrapper(val));
    }

    /**
     * Pushes a char onto the top of this stack.
     *
     * @param val
     *            the value to push
     */
    public void push(char val) {
        this.push(new BaseTypeWrapper(val));
    }

    /**
     * Pushes a double onto the top of this stack.
     *
     * @param val
     *            the value to push
     */
    public void push(double val) {
        this.push(new BaseTypeWrapper(val));
    }

    /**
     * Pushes a float onto the top of this stack.
     *
     * @param val
     *            the value to push
     */
    public void push(float val) {
        this.push(new BaseTypeWrapper(val));
    }

    /**
     * Pushes an int onto the top of this stack.
     *
     * @param val
     *            the value to push
     */
    public void push(int val) {
        this.push(new BaseTypeWrapper(val));
    }

    /**
     * Pushes a long onto the top of this stack.
     *
     * @param val
     *            the value to push
     */
    public void push(long val) {
        this.push(new BaseTypeWrapper(val));
    }

    /**
     * Pushes a short onto the top of this stack.
     *
     * @param val
     *            the value to push
     */
    public void push(short val) {
        this.push(new BaseTypeWrapper(val));
    }

    /**
     * Pop an object.
     *
     * @return the object
     */
    @Override
    public Object pop() {
        this.callStack.pop();
        return super.pop();
    }

    /**
     * Pop a boolean.
     *
     * @return the value
     */
    public boolean popBoolean() {
        BaseTypeWrapper wrapper = (BaseTypeWrapper) this.pop();
        Boolean value = (Boolean) wrapper.getValue();
        return value.booleanValue();
    }

    /**
     * Pop a char.
     *
     * @return the value
     */
    public char popChar() {
        BaseTypeWrapper wrapper = (BaseTypeWrapper) this.pop();
        Character value = (Character) wrapper.getValue();
        return value.charValue();
    }

    /**
     * Pop a double.
     *
     * @return the value
     */
    public double popDouble() {
        BaseTypeWrapper wrapper = (BaseTypeWrapper) this.pop();
        Double value = (Double) wrapper.getValue();
        return value.doubleValue();
    }

    /**
     * Pop a float.
     *
     * @return the value
     */
    public float popFloat() {
        BaseTypeWrapper wrapper = (BaseTypeWrapper) this.pop();
        Float value = (Float) wrapper.getValue();
        return value.floatValue();
    }

    /**
     * Pop a int.
     *
     * @return the value
     */
    public int popInt() {
        BaseTypeWrapper wrapper = (BaseTypeWrapper) this.pop();
        Integer value = (Integer) wrapper.getValue();
        return value.intValue();
    }

    /**
     * Pop a long.
     *
     * @return the value
     */
    public long popLong() {
        BaseTypeWrapper wrapper = (BaseTypeWrapper) this.pop();
        Long value = (Long) wrapper.getValue();
        return value.longValue();
    }

    /**
     * Pop a short.
     *
     * @return the value
     */
    public short popShort() {
        BaseTypeWrapper wrapper = (BaseTypeWrapper) this.pop();
        Short value = (Short) wrapper.getValue();
        return value.shortValue();
    }

    /*
     * Wrapper of base types.
     */
    class BaseTypeWrapper {

        // the internal value
        private Object value;

        /*
         * Constructs a wrapper object for the base type <code> boolean </code> .
         */
        public BaseTypeWrapper(boolean val) {
            this.value = new Boolean(val);
        }

        /*
         * Constructs a wrapper object for the base type <code> c </code> .
         */
        public BaseTypeWrapper(byte val) {
            this.value = new Byte(val);
        }

        /*
         * Constructs a wrapper object for the base type <code> char </code> .
         */
        public BaseTypeWrapper(char val) {
            this.value = new Character(val);
        }

        /*
         * Constructs a wrapper object for the base type <code> double </code> .
         */
        public BaseTypeWrapper(double val) {
            this.value = new Double(val);
        }

        /*
         * Constructs a wrapper object for the base type <code> float </code> .
         */
        public BaseTypeWrapper(float val) {
            this.value = new Float(val);
        }

        /*
         * Constructs a wrapper object for the base type <code> int </code> .
         */
        public BaseTypeWrapper(int val) {
            this.value = new Integer(val);
        }

        /*
         * Constructs a wrapper object for the base type <code> long </code> .
         */
        public BaseTypeWrapper(long val) {
            this.value = new Long(val);
        }

        /*
         * Constructs a wrapper object for the base type <code> short </code> .
         */
        public BaseTypeWrapper(short val) {
            this.value = new Short(val);
        }

        /*
         * Gets the internal value.
         */
        public Object getValue() {
            return this.value;
        }
    }
}