/
AllClassesTest.java
153 lines (132 loc) · 5.86 KB
/
AllClassesTest.java
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
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.apache.activemq.artemis.tests.unit;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import org.apache.activemq.artemis.utils.RandomUtil;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
@RunWith(value = Parameterized.class)
public class AllClassesTest {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@Parameterized.Parameters(name = "classInfo={0}")
public static Collection getParameters() {
List<Class> parameters = new ArrayList<>();
ClassLoader classLoader = AllClassesTest.class.getClassLoader();
try {
ClassPath classPath = ClassPath.from(classLoader);
ImmutableSet<ClassPath.ClassInfo> classInfos = classPath.getTopLevelClassesRecursive("org.apache.activemq.artemis");
for (ClassPath.ClassInfo classInfo : classInfos) {
if (!classInfo.getPackageName().contains("tests")) {
try {
Class loadedClass = classInfo.load();
if (!loadedClass.isEnum() && !loadedClass.isInterface() && !Modifier.isAbstract(loadedClass.getModifiers())) {
parameters.add(loadedClass);
}
} catch (Throwable loadThrowable) {
logger.debug("cannot load {} : {}", classInfo.getName(), loadThrowable);
}
}
}
parameters.sort(Comparator.comparing(Class::getName));
return parameters;
} catch (Exception e) {
logger.warn("Exception on loading all classes: {}", e.toString());
}
return parameters;
}
private Class targetClass;
public AllClassesTest(Class targetClass) {
this.targetClass = targetClass;
}
@Test
public void testToString() {
Object targetInstance = null;
try {
targetInstance = newInstance(targetClass);
} catch (Throwable t) {
logger.debug("Error creating a new instance of {}: {}", targetClass.getName(), t);
}
Assume.assumeTrue("Cannot create " + targetClass.getName(), targetInstance != null);
try {
String targetOutput = targetInstance.toString();
logger.debug("targetOutput: {}", targetOutput);
} finally {
if (targetInstance instanceof AutoCloseable) {
try {
((AutoCloseable)targetInstance).close();
} catch (Throwable t) {
logger.debug("Error closing the instance of {}: {}", targetClass.getName(), t);
}
}
}
}
private Object newInstance(Class targetClass) {
Constructor[] targetConstructors = targetClass.getDeclaredConstructors();
Arrays.sort(targetConstructors, (c1, c2) -> c2.getParameterCount() - c1.getParameterCount());
for (Constructor targetConstructor : targetConstructors) {
List<Object> initArgs = new ArrayList<>();
Parameter[] constructorParameters = targetConstructor.getParameters();
for (Parameter constructorParameter : constructorParameters) {
Object initArg;
if (constructorParameter.getType().isAssignableFrom(byte.class)) {
initArg = RandomUtil.randomByte();
} else if (constructorParameter.getType().isAssignableFrom(byte[].class)) {
initArg = RandomUtil.randomBytes();
} else if (constructorParameter.getType().isAssignableFrom(boolean.class)) {
initArg = RandomUtil.randomBoolean();
} else if (constructorParameter.getType().isAssignableFrom(char.class)) {
initArg = RandomUtil.randomChar();
} else if (constructorParameter.getType().isAssignableFrom(double.class)) {
initArg = RandomUtil.randomDouble();
} else if (constructorParameter.getType().isAssignableFrom(float.class)) {
initArg = RandomUtil.randomFloat();
} else if (constructorParameter.getType().isAssignableFrom(int.class)) {
initArg = RandomUtil.randomInt() / 1024;
} else if (constructorParameter.getType().isAssignableFrom(long.class)) {
initArg = RandomUtil.randomLong();
} else if (constructorParameter.getType().isAssignableFrom(String.class)) {
initArg = RandomUtil.randomString();
} else {
initArg = null;
}
initArgs.add(initArg);
}
try {
return targetConstructor.newInstance(initArgs.toArray());
} catch (Throwable t) {
if (logger.isDebugEnabled()) {
logger.debug("Cannot construct {}: {}", targetClass.getName(), t);
}
}
}
return null;
}
}