11/*
2- * Copyright © 2018 IBM Corp. All rights reserved.
2+ * Copyright © 2018, 2019 IBM Corp. All rights reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55 * except in compliance with the License. You may obtain a copy of the License at
2222import org .junit .jupiter .api .extension .Extension ;
2323import org .junit .jupiter .api .extension .ExtensionContext ;
2424
25+ import java .util .ArrayList ;
26+ import java .util .Arrays ;
27+ import java .util .Collections ;
28+ import java .util .List ;
29+
2530public class MultiExtension implements Extension , AfterAllCallback , AfterEachCallback ,
2631 AfterTestExecutionCallback , BeforeAllCallback , BeforeEachCallback ,
2732 BeforeTestExecutionCallback {
2833
29- private final Extension [] extensions ;
34+ private final List <Extension > extensions ;
35+ private final List <Extension > reversedExtensions ;
3036
3137 public MultiExtension (Extension ... extensions ) {
32- this .extensions = extensions ;
38+ this .extensions = Arrays .asList (extensions );
39+ // After tests we need to call extensions in the reverse order
40+ this .reversedExtensions = new ArrayList <>(this .extensions .size ());
41+ this .reversedExtensions .addAll (this .extensions );
42+ Collections .reverse (reversedExtensions );
3343 }
3444
3545 @ Override
3646 public void afterAll (ExtensionContext extensionContext ) throws Exception {
37- for (Extension extension : extensions ) {
47+ for (Extension extension : reversedExtensions ) {
3848 if (extension instanceof AfterAllCallback ) {
3949 ((AfterAllCallback ) extension ).afterAll (extensionContext );
4050 }
@@ -43,7 +53,7 @@ public void afterAll(ExtensionContext extensionContext) throws Exception {
4353
4454 @ Override
4555 public void afterEach (ExtensionContext extensionContext ) throws Exception {
46- for (Extension extension : extensions ) {
56+ for (Extension extension : reversedExtensions ) {
4757 if (extension instanceof AfterEachCallback ) {
4858 ((AfterEachCallback ) extension ).afterEach (extensionContext );
4959 }
@@ -52,7 +62,7 @@ public void afterEach(ExtensionContext extensionContext) throws Exception {
5262
5363 @ Override
5464 public void afterTestExecution (ExtensionContext extensionContext ) throws Exception {
55- for (Extension extension : extensions ) {
65+ for (Extension extension : reversedExtensions ) {
5666 if (extension instanceof AfterTestExecutionCallback ) {
5767 ((AfterTestExecutionCallback ) extension ).afterTestExecution (extensionContext );
5868 }
0 commit comments