File: TestCase.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (459 lines) | stat: -rw-r--r-- 18,238 bytes parent folder | download | duplicates (18)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.lang.annotation.RetentionPolicy;
import java.util.*;
import java.util.stream.Collectors;

public class TestCase {

    /**
     * The top-level classes of the test case.
     */
    public final Map<String, TestClassInfo> classes = new LinkedHashMap<>();

    /**
     * Constructs a test class info with {@code classType} as top-level class,
     * with {@code outerClassName} as name and {@code mods} as modifiers.
     *
     * @param classType a class type
     * @param outerClassName a name
     * @param mods an array of modifiers
     */
    public TestClassInfo addClassInfo(ClassType classType, String outerClassName, String...mods) {
        return addClassInfo(null, classType, outerClassName, mods);
    }

    /**
     * Constructs a test class info with {@code classType} as top-level class,
     * with {@code outerClassName} as name, {@code parent} class name
     * as parent class and {@code mods} as modifiers.
     *
     * @param classType a class type
     * @param outerClassName a name
     * @param mods an array of modifiers
     */
    public TestClassInfo addClassInfo(String parent, ClassType classType, String outerClassName, String...mods) {
        TestClassInfo clazz = new TestClassInfo(classType, outerClassName, parent, mods);
        if (classes.put(outerClassName, clazz) != null) {
            throw new IllegalArgumentException("Duplicate class name: " + outerClassName);
        }
        return clazz;
    }

    public String generateSource() {
        return classes.values().stream()
                .map(TestMemberInfo::generateSource)
                .collect(Collectors.joining("\n"));
    }

    /**
     * Returns {@code TestClassInfo} by class signature.
     * Example, {@code getTestClassInfo(&quot;Test$1Local&quot;)}
     * returns local inner class of class {@code Test}.
     *
     * @param classSignature a class signature
     * @return {@code TestClassInfo} by class signature
     */
    public TestClassInfo getTestClassInfo(String classSignature) {
        String[] cs = classSignature.split("\\$");
        if (cs.length > 0 && classes.containsKey(cs[0])) {
            // check signature corresponds to top level class
            if (cs.length == 1) {
                return classes.get(cs[0]);
            }
        } else {
            throw new IllegalArgumentException("Cannot find class : " + classSignature);
        }
        TestClassInfo current = classes.get(cs[0]);
        // find class info in the inner classes
        for (int i = 1; i < cs.length; ++i) {
            Map<String, TestClassInfo> innerClasses = current.innerClasses;
            Map<String, TestMethodInfo> methods = current.methods;
            current = innerClasses.get(cs[i]);
            // if current is null then class info does not exist or the class is local
            if (current == null) {
                if (!cs[i].isEmpty()) {
                    // the class is local, remove leading digit
                    String className = cs[i].substring(1);
                    Optional<TestClassInfo> opt = methods.values().stream()
                            .flatMap(c -> c.localClasses.values().stream())
                            .filter(c -> c.name.equals(className)).findAny();
                    if (opt.isPresent()) {
                        current = opt.get();
                        // continue analysis of local class
                        continue;
                    }
                }
                throw new IllegalArgumentException("Cannot find class : " + classSignature);
            }
        }
        return current;
    }

    /**
     * Class represents a program member.
     */
    public static abstract class TestMemberInfo {
        // next two fields are used for formatting
        protected final int indention;
        protected final ClassType containerType;
        public final List<String> mods;
        public final String name;
        public final Map<String, TestAnnotationInfo> annotations;

        TestMemberInfo(int indention, ClassType containerType, String name, String... mods) {
            this.indention = indention;
            this.containerType = containerType;
            this.mods = Arrays.asList(mods);
            this.name = name;
            this.annotations = new HashMap<>();
        }

        public abstract String generateSource();

        public boolean isAnnotated(RetentionPolicy policy) {
            return annotations.values().stream()
                    .filter(a -> a.policy == policy)
                    .findAny().isPresent();
        }

        public Set<String> getRuntimeVisibleAnnotations() {
            return getRuntimeAnnotations(RetentionPolicy.RUNTIME);
        }

        public Set<String> getRuntimeInvisibleAnnotations() {
            return getRuntimeAnnotations(RetentionPolicy.CLASS);
        }

        private Set<String> getRuntimeAnnotations(RetentionPolicy policy) {
            return annotations.values().stream()
                    .filter(e -> e.policy == policy)
                    .map(a -> a.annotationName)
                    .distinct()
                    .collect(Collectors.toSet());
        }

        /**
         * Generates source for annotations.
         *
         * @param prefix a leading text
         * @param suffix a trailing text
         * @param joining a text between annotations
         * @return source for annotations
         */
        protected String generateSourceForAnnotations(String prefix, String suffix, String joining) {
            StringBuilder sb = new StringBuilder();
            for (TestAnnotationInfo annotation : annotations.values()) {
                sb.append(prefix);
                if (annotation.isContainer) {
                    // the annotation is repeatable
                    // container consists of an array of annotations
                    TestAnnotationInfo.TestArrayElementValue containerElementValue =
                            (TestAnnotationInfo.TestArrayElementValue) annotation.elementValues.get(0).elementValue;
                    // concatenate sources of repeatable annotations
                    sb.append(containerElementValue.values.stream()
                            .map(TestAnnotationInfo.TestElementValue::toString)
                            .collect(Collectors.joining(joining)));
                } else {
                    sb.append(annotation);
                }
                sb.append(suffix);
            }
            String src = sb.toString();
            return src.trim().isEmpty() ? "" : src;

        }

        /**
         * Generates source for annotations.
         *
         * @return source for annotations
         */
        public String generateSourceForAnnotations() {
            return generateSourceForAnnotations(indention(), "\n", "\n" + indention());
        }

        /**
         * Adds annotation info to the member.
         *
         * @param anno an annotation info
         */
        public void addAnnotation(TestAnnotationInfo anno) {
            String containerName = anno.annotationName + "Container";
            TestAnnotationInfo annotation = annotations.get(anno.annotationName);
            TestAnnotationInfo containerAnnotation = annotations.get(containerName);

            if (annotation == null) {
                // if annotation is null then either it is first adding of the annotation to the member
                // or there is the container of the annotation.
                if (containerAnnotation == null) {
                    // first adding to the member
                    annotations.put(anno.annotationName, anno);
                } else {
                    // add annotation to container
                    TestAnnotationInfo.TestArrayElementValue containerElementValue =
                            ((TestAnnotationInfo.TestArrayElementValue) containerAnnotation.elementValues.get(0).elementValue);
                    containerElementValue.values.add(new TestAnnotationInfo.TestAnnotationElementValue(anno.annotationName, anno));
                }
            } else {
                // remove previously added annotation and add new container of repeatable annotation
                // which contains previously added and new annotation
                annotations.remove(anno.annotationName);
                containerAnnotation = new TestAnnotationInfo(
                        containerName,
                        anno.policy,
                        true,
                        new TestAnnotationInfo.Pair("value",
                                new TestAnnotationInfo.TestArrayElementValue(
                                        new TestAnnotationInfo.TestAnnotationElementValue(anno.annotationName, annotation),
                                        new TestAnnotationInfo.TestAnnotationElementValue(anno.annotationName, anno))));
                annotations.put(containerName, containerAnnotation);
            }
        }

        public String indention() {
            char[] a = new char[4 * indention];
            Arrays.fill(a, ' ');
            return new String(a);
        }

        public String getName() {
            return name;
        }
    }

    /**
     * The class represents a class.
     */
    public static class TestClassInfo extends TestMemberInfo {
        public final ClassType classType;
        public final String parent;
        public final Map<String, TestClassInfo> innerClasses;
        public final Map<String, TestMethodInfo> methods;
        public final Map<String, TestFieldInfo> fields;

        TestClassInfo(int indention, ClassType classType, String className, String... mods) {
            this(indention, classType, className, null, mods);
        }

        TestClassInfo(ClassType classType, String className, String parent, String... mods) {
            this(0, classType, className, parent, mods);
        }

        TestClassInfo(int indention, ClassType classType, String className, String parent, String... mods) {
            super(indention, null, className, mods);
            this.classType = classType;
            this.parent = parent;
            innerClasses = new LinkedHashMap<>();
            methods = new LinkedHashMap<>();
            fields = new LinkedHashMap<>();
        }

        /**
         * Generates source which represents the class.
         *
         * @return source which represents the class
         */
        @Override
        public String generateSource() {
            String sourceForAnnotations = generateSourceForAnnotations();
            String classModifiers = mods.stream().collect(Collectors.joining(" "));
            return sourceForAnnotations
                    + String.format("%s%s %s %s %s {%n",
                    indention(),
                    classModifiers,
                    classType.getDescription(),
                    name,
                    parent == null ? "" : "extends " + parent)
                    + classType.collectFields(fields.values())
                    + classType.collectMethods(methods.values())
                    + classType.collectInnerClasses(innerClasses.values())
                    + indention() + "}";
        }

        /**
         * Adds a new inner class to the class.
         *
         * @param classType a class type
         * @param className a class name
         * @param mods modifiers
         * @return a new added inner class to the class
         */
        public TestClassInfo addInnerClassInfo(ClassType classType, String className, String... mods) {
            TestClassInfo testClass = new TestClassInfo(indention + 1, classType, className, mods);
            if (innerClasses.put(className, testClass) != null) {
                throw new IllegalArgumentException("Duplicated class : " + className);
            }
            return testClass;
        }

        /**
         * Adds a new method to the class.
         *
         * @param methodName a method name
         * @param mods modifiers
         * @return a new inner class to the class
         */
        public TestMethodInfo addMethodInfo(String methodName, String... mods) {
            return addMethodInfo(methodName, false, mods);
        }

        /**
         * Adds a new method to the class.
         *
         * @param methodName a method name
         * @param isSynthetic if {@code true} the method is synthetic
         * @param mods modifiers
         * @return a new method added to the class
         */
        public TestMethodInfo addMethodInfo(String methodName, boolean isSynthetic, String... mods) {
            boolean isConstructor = methodName.contains("<init>");
            if (isConstructor) {
                methodName = methodName.replace("<init>", name);
            }
            TestMethodInfo testMethod = new TestMethodInfo(indention + 1, classType, methodName, isConstructor, isSynthetic, mods);
            if (methods.put(methodName, testMethod) != null) {
                throw new IllegalArgumentException("Duplicated method : " + methodName);
            }
            return testMethod;
        }

        /**
         * Adds a new field to the class.
         *
         * @param fieldName a method name
         * @param mods modifiers
         * @return a new field added to the class
         */
        public TestFieldInfo addFieldInfo(String fieldName, String... mods) {
            TestFieldInfo field = new TestFieldInfo(indention + 1, classType, fieldName, mods);
            if (fields.put(fieldName, field) != null) {
                throw new IllegalArgumentException("Duplicated field : " + fieldName);
            }
            return field;
        }

        public TestMethodInfo getTestMethodInfo(String methodName) {
            return methods.get(methodName);
        }

        public TestFieldInfo getTestFieldInfo(String fieldName) {
            return fields.get(fieldName);
        }
    }

    public static class TestMethodInfo extends TestMemberInfo {
        public final boolean isConstructor;
        public final boolean isSynthetic;
        public final Map<String, TestClassInfo> localClasses;
        public final List<TestParameterInfo> parameters;

        TestMethodInfo(int indention, ClassType containerType, String methodName,
                               boolean isConstructor, boolean isSynthetic, String... mods) {
            super(indention, containerType, methodName, mods);
            this.isSynthetic = isSynthetic;
            this.localClasses = new LinkedHashMap<>();
            this.parameters = new ArrayList<>();
            this.isConstructor = isConstructor;
        }

        public boolean isParameterAnnotated(RetentionPolicy policy) {
            return parameters.stream()
                    .filter(p -> p.isAnnotated(policy))
                    .findFirst().isPresent();
        }

        public TestParameterInfo addParameter(String type, String name) {
            TestParameterInfo testParameter = new TestParameterInfo(type, name);
            parameters.add(testParameter);
            return testParameter;
        }

        /**
         * Adds a local class to the method.
         *
         * @param className a class name
         * @param mods modifiers
         * @return a local class added to the method
         */
        public TestClassInfo addLocalClassInfo(String className, String... mods) {
            TestClassInfo testClass = new TestClassInfo(indention + 1, ClassType.CLASS, className, mods);
            if (localClasses.put(className, testClass) != null) {
                throw new IllegalArgumentException("Duplicated class : " + className);
            }
            return testClass;
        }

        @Override
        public String generateSource() {
            if (isSynthetic) {
                return "";
            }
            return generateSourceForAnnotations() +
                    containerType.methodToString(this);
        }

        @Override
        public String getName() {
            return name.replaceAll("\\(.*\\)", "");
        }
    }

    /**
     * The class represents a method parameter.
     */
    public static class TestParameterInfo extends TestMemberInfo {
        public final String type;

        TestParameterInfo(String type, String name) {
            super(0, null, name);
            this.type = type;
        }

        @Override
        public String generateSource() {
            return generateSourceForAnnotations() + type + " " + name;
        }

        public String generateSourceForAnnotations() {
            return generateSourceForAnnotations("", " ", " ");
        }
    }

    /**
     * The class represents a field.
     */
    public static class TestFieldInfo extends TestMemberInfo {

        TestFieldInfo(int indention, ClassType containerType, String fieldName, String... mods) {
            super(indention, containerType, fieldName, mods);
        }

        @Override
        public String generateSource() {
            return generateSourceForAnnotations() +
                    containerType.fieldToString(this);
        }
    }
}