org.netbeans.junit.MethodOrder Maven / Gradle / Ivy
The newest version!
/*
* 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 org.netbeans.junit;
import java.lang.ref.Reference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/** Support for sorting or shuffling NbTestCase test methods.
*
* @author Jaroslav Tulach
*/
final class MethodOrder {
private static final List allDeclaredMethods = new LinkedList();
private static final String DEFAULT_METHOD_ORDER = "natural"; // NOI18N
private static Long shuffleSeed;
static void initialize() {
String orderS = findOrder();
if (!"natural".equals(orderS)) { // NOI18N
try {
// TODO: ClassLoader fields can not be accessed via reflection on JDK 12+
// see jdk.internal.reflect.Reflection#fieldFilterMap
// this won't work
Field classesF = ClassLoader.class.getDeclaredField("classes"); // NOI18N
classesF.setAccessible(true);
@SuppressWarnings("unchecked")
Collection> classes = new ArrayList>((Collection>) classesF.get(NbTestCase.class.getClassLoader()));
for (Class> c : classes) {
if (NbTestCase.class.isAssignableFrom(c) && c != NbTestCase.class) {
orderMethods(c, orderS);
}
}
} catch (Exception x) {
System.err.println("WARNING: test method ordering disabled");
x.printStackTrace();
}
}
}
static void orderMethods(Class> c, String orderS) throws Exception { // #7023180
if (orderS == null) {
orderS = findOrder();
}
if ("natural".equals(orderS)) { // NOI18N
return;
}
Method[] ms = null;
try {
Field declaredMethodsF = Class.class.getDeclaredField("declaredMethods");
declaredMethodsF.setAccessible(true);
while (ms == null) {
c.getDeclaredMethods();
ms = (Method[]) ((Reference) declaredMethodsF.get(c)).get();
}
} catch (NoSuchFieldException ex) {
// try JDK8
Field rdF = Class.class.getDeclaredField("reflectionData");
rdF.setAccessible(true);
c.getDeclaredMethods();
Reference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy