-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#8965] Add SharedTestLifeCycle to simplify plugin integration tests
- Loading branch information
Showing
8 changed files
with
268 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestBeforeAllInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.navercorp.pinpoint.test.plugin.shared; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Properties; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* @author emeroad | ||
*/ | ||
public class SharedTestBeforeAllInvoker { | ||
private final Class<?> testClazz; | ||
|
||
public SharedTestBeforeAllInvoker(Class<?> testClazz) { | ||
this.testClazz = Objects.requireNonNull(testClazz, "testClazz"); | ||
} | ||
|
||
|
||
List<Method> getMethods(Class<?> testClazz, Predicate<Method> predicate) { | ||
Method[] methods = testClazz.getMethods(); | ||
Stream<Method> stream = Arrays.stream(methods); | ||
return stream.filter(predicate) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
boolean beforeAllFilter(Method method) { | ||
if (method.getAnnotation(SharedTestBeforeAllResult.class) == null) { | ||
return false; | ||
} | ||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
if (parameterTypes.length != 1) { | ||
return false; | ||
} | ||
return Properties.class.isAssignableFrom(parameterTypes[0]); | ||
} | ||
|
||
public void invoke(Properties properties) throws Throwable { | ||
List<Method> methods = getMethods(testClazz, this::beforeAllFilter); | ||
for (Method method : methods) { | ||
method.invoke(testClazz, properties); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestBeforeAllResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2021 NAVER Corp. | ||
* | ||
* Licensed 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 com.navercorp.pinpoint.test.plugin.shared; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author emeroad | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface SharedTestBeforeAllResult { | ||
} |
11 changes: 11 additions & 0 deletions
11
test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestLifeCycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.navercorp.pinpoint.test.plugin.shared; | ||
|
||
import java.util.Properties; | ||
|
||
/** | ||
* @author emeroad | ||
*/ | ||
public interface SharedTestLifeCycle { | ||
Properties beforeAll(); | ||
void afterAll(); | ||
} |
31 changes: 31 additions & 0 deletions
31
test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestLifeCycleClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2021 NAVER Corp. | ||
* | ||
* Licensed 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 com.navercorp.pinpoint.test.plugin.shared; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author emeroad | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface SharedTestLifeCycleClass { | ||
Class<? extends SharedTestLifeCycle> value(); | ||
} |
48 changes: 48 additions & 0 deletions
48
test/src/main/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestLifeCycleWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.navercorp.pinpoint.test.plugin.shared; | ||
|
||
import java.util.Objects; | ||
import java.util.Properties; | ||
|
||
/** | ||
* @author emeroad | ||
*/ | ||
public class SharedTestLifeCycleWrapper { | ||
private final SharedTestLifeCycle sharedTestLifecycle; | ||
private Properties lifeCycleResult; | ||
|
||
private static Class<? extends SharedTestLifeCycle> getVersionTestLifeCycle(final Class<?> testClazz) { | ||
SharedTestLifeCycleClass sharedTestLifeCycleClass = testClazz.getAnnotation(SharedTestLifeCycleClass.class); | ||
if (sharedTestLifeCycleClass == null) { | ||
return null; | ||
} | ||
return sharedTestLifeCycleClass.value(); | ||
} | ||
|
||
public static SharedTestLifeCycleWrapper newVersionTestLifeCycleWrapper(final Class<?> testClazz) { | ||
Class<? extends SharedTestLifeCycle> versionTestClazz = getVersionTestLifeCycle(testClazz); | ||
if (versionTestClazz == null) { | ||
return null; | ||
} | ||
try { | ||
return new SharedTestLifeCycleWrapper(versionTestClazz.newInstance()); | ||
} catch (InstantiationException | IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public SharedTestLifeCycleWrapper(SharedTestLifeCycle sharedTestLifecycle) { | ||
this.sharedTestLifecycle = Objects.requireNonNull(sharedTestLifecycle, "versionTestLifecycle"); | ||
} | ||
|
||
public Properties getLifeCycleResult() { | ||
return lifeCycleResult; | ||
} | ||
|
||
public void beforeAll() { | ||
lifeCycleResult = sharedTestLifecycle.beforeAll(); | ||
} | ||
|
||
public void afterAll() { | ||
sharedTestLifecycle.afterAll(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...c/test/java/com/navercorp/pinpoint/test/plugin/shared/SharedTestBeforeAllInvokerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.navercorp.pinpoint.test.plugin.shared; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
public class SharedTestBeforeAllInvokerTest { | ||
|
||
@Test | ||
public void invoke() throws Throwable { | ||
SharedTestBeforeAllInvoker mock = new SharedTestBeforeAllInvoker(TestClass.class); | ||
|
||
List<Method> methods = mock.getMethods(TestClass.class, mock::beforeAllFilter); | ||
Assert.assertEquals(1, methods.size()); | ||
Assert.assertEquals("setBeforeAllResult", methods.get(0).getName()); | ||
|
||
mock.invoke(new Properties()); | ||
Assert.assertNotNull(TestClass.properties); | ||
} | ||
|
||
@Test | ||
public void invoke_extends() throws Throwable { | ||
SharedTestBeforeAllInvoker mock = new SharedTestBeforeAllInvoker(ChildTestClass.class); | ||
|
||
List<Method> methods = mock.getMethods(ChildTestClass.class, mock::beforeAllFilter); | ||
Assert.assertEquals(1, methods.size()); | ||
Assert.assertEquals("setBeforeAllResult", methods.get(0).getName()); | ||
|
||
mock.invoke(new Properties()); | ||
Assert.assertNotNull(ChildTestClass.properties); | ||
} | ||
|
||
public static class TestClass { | ||
static Properties properties; | ||
|
||
@SharedTestBeforeAllResult | ||
public static void setBeforeAllResult(Properties properties) { | ||
TestClass.properties = properties; | ||
} | ||
|
||
public static void fake(Properties p) { | ||
|
||
} | ||
} | ||
|
||
public static class ChildTestClass extends TestClass { | ||
|
||
} | ||
} |