From a305e0c532f30744762603429c1d5f8af6a8d75d Mon Sep 17 00:00:00 2001 From: linyimin0812 Date: Sat, 16 Dec 2023 15:10:53 +0800 Subject: [PATCH 1/7] test: add AtEnterEventSpec --- spring-profiler-api/pom.xml | 7 ++++ .../api/event/AtEnterEventSpec.groovy | 34 +++++++++++++++++++ spring-startup-cli/pom.xml | 13 +++---- 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 spring-profiler-api/src/test/java/io/github/linyimin0812/profiler/api/event/AtEnterEventSpec.groovy diff --git a/spring-profiler-api/pom.xml b/spring-profiler-api/pom.xml index 8be263f..5c1dcdc 100644 --- a/spring-profiler-api/pom.xml +++ b/spring-profiler-api/pom.xml @@ -17,6 +17,13 @@ org.picocontainer picocontainer + + + org.spockframework + spock-core + test + + diff --git a/spring-profiler-api/src/test/java/io/github/linyimin0812/profiler/api/event/AtEnterEventSpec.groovy b/spring-profiler-api/src/test/java/io/github/linyimin0812/profiler/api/event/AtEnterEventSpec.groovy new file mode 100644 index 0000000..23bc2ac --- /dev/null +++ b/spring-profiler-api/src/test/java/io/github/linyimin0812/profiler/api/event/AtEnterEventSpec.groovy @@ -0,0 +1,34 @@ +package io.github.linyimin0812.profiler.api.event + +import spock.lang.Specification + +/** + * @author linyimin + * */ +class AtEnterEventSpec extends Specification { + + def "test changeParameter"() { + given: + AtEnterEvent enterEvent = new AtEnterEvent( + 1000, + 1001, + null, + null, + null, + null, + new Object[] { '1', '2', '3' } + ) + + when: + enterEvent.changeParameter(index, value) + + then: + enterEvent.args[index] == value + + where: + index || value + 0 || 'test1' + 1 || 'test2' + 2 || 'test3' + } +} diff --git a/spring-startup-cli/pom.xml b/spring-startup-cli/pom.xml index a9cbe7d..a367200 100644 --- a/spring-startup-cli/pom.xml +++ b/spring-startup-cli/pom.xml @@ -53,12 +53,13 @@ directory-watcher 0.18.0 - - - org.spockframework - spock-core - test - + + + org.spockframework + spock-core + 2.4-M1-groovy-4.0 + test + From 7437c283d8a7e173e8c8ab419709bda46a95b6bb Mon Sep 17 00:00:00 2001 From: linyimin0812 Date: Sat, 16 Dec 2023 15:18:01 +0800 Subject: [PATCH 2/7] test: add InstrumentationHolderSpec --- .../InstrumentationHolderSpec.groovy | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 spring-profiler-common/src/test/java/io/github/linyimin0812/profiler/common/instruction/InstrumentationHolderSpec.groovy diff --git a/spring-profiler-common/src/test/java/io/github/linyimin0812/profiler/common/instruction/InstrumentationHolderSpec.groovy b/spring-profiler-common/src/test/java/io/github/linyimin0812/profiler/common/instruction/InstrumentationHolderSpec.groovy new file mode 100644 index 0000000..81a779f --- /dev/null +++ b/spring-profiler-common/src/test/java/io/github/linyimin0812/profiler/common/instruction/InstrumentationHolderSpec.groovy @@ -0,0 +1,29 @@ +package io.github.linyimin0812.profiler.common.instruction + +import spock.lang.Specification +import spock.lang.Stepwise + +import java.lang.instrument.Instrumentation + +/** + * @author linyimin + * */ +@Stepwise +class InstrumentationHolderSpec extends Specification { + + def "test setInstrumentation"() { + given: + Instrumentation instrumentation = Mock() + + when: + InstrumentationHolder.instrumentation = instrumentation + + then: + InstrumentationHolder.instrumentation != null + } + + def "test getInstrumentation"() { + expect: + InstrumentationHolder.instrumentation != null + } +} From 0f83189c7ccf8a700378340e138f1756b24bd6df Mon Sep 17 00:00:00 2001 From: linyimin0812 Date: Sun, 17 Dec 2023 01:23:09 +0800 Subject: [PATCH 3/7] test: add processor test case --- spring-async-bean-core/pom.xml | 14 ++++++ ...ncBeanPriorityLoadPostProcessorSpec.groovy | 48 +++++++++++++++++++ .../AsyncProxyBeanPostProcessorSpec.groovy | 30 ++++++++++++ .../src/test/resources/bean-context.xml | 1 + 4 files changed, 93 insertions(+) create mode 100644 spring-async-bean-core/src/test/java/io/github/linyimin0812/async/processor/AsyncBeanPriorityLoadPostProcessorSpec.groovy create mode 100644 spring-async-bean-core/src/test/java/io/github/linyimin0812/async/processor/AsyncProxyBeanPostProcessorSpec.groovy diff --git a/spring-async-bean-core/pom.xml b/spring-async-bean-core/pom.xml index 5f26df3..6c2c07e 100644 --- a/spring-async-bean-core/pom.xml +++ b/spring-async-bean-core/pom.xml @@ -49,6 +49,20 @@ 2.4-M1-groovy-4.0 test + + + net.bytebuddy + byte-buddy + 1.14.10 + test + + + + org.objenesis + objenesis + 3.3 + test + org.springframework diff --git a/spring-async-bean-core/src/test/java/io/github/linyimin0812/async/processor/AsyncBeanPriorityLoadPostProcessorSpec.groovy b/spring-async-bean-core/src/test/java/io/github/linyimin0812/async/processor/AsyncBeanPriorityLoadPostProcessorSpec.groovy new file mode 100644 index 0000000..2bfd98d --- /dev/null +++ b/spring-async-bean-core/src/test/java/io/github/linyimin0812/async/processor/AsyncBeanPriorityLoadPostProcessorSpec.groovy @@ -0,0 +1,48 @@ +package io.github.linyimin0812.async.processor + +import io.github.linyimin0812.async.config.AsyncBeanProperties +import io.github.linyimin0812.async.config.AsyncConfig +import org.springframework.beans.factory.support.DefaultListableBeanFactory +import spock.lang.Specification + +/** + * @author linyimin + * */ +class AsyncBeanPriorityLoadPostProcessorSpec extends Specification { + + def "test setBeanFactory"() { + + given: + DefaultListableBeanFactory beanFactory = Mock() + AsyncBeanProperties properties = new AsyncBeanProperties() + AsyncConfig.instance.setAsyncBeanProperties(properties) + AsyncBeanPriorityLoadPostProcessor processor = new AsyncBeanPriorityLoadPostProcessor() + + when: "测试不开启优先加载Bean的情况" + processor.setBeanFactory(beanFactory) + + then: + 0 * beanFactory.getBean(_) + + when: "测试开启优先加载,但是beanName不存在的情况" + + properties.setBeanPriorityLoadEnable(true) + beanFactory.containsBeanDefinition('testBean') >> true + + processor.setBeanFactory(beanFactory) + + then: + 0 * beanFactory.getBean(_) + + when: "测试开启优先加载且beanName存在的情况" + + properties.setBeanPriorityLoadEnable(true) + properties.setBeanNames(['testBean']) + + beanFactory.containsBeanDefinition('testBean') >> true + processor.setBeanFactory(beanFactory) + + then: + 1 * beanFactory.getBean(_) + } +} diff --git a/spring-async-bean-core/src/test/java/io/github/linyimin0812/async/processor/AsyncProxyBeanPostProcessorSpec.groovy b/spring-async-bean-core/src/test/java/io/github/linyimin0812/async/processor/AsyncProxyBeanPostProcessorSpec.groovy new file mode 100644 index 0000000..0839b75 --- /dev/null +++ b/spring-async-bean-core/src/test/java/io/github/linyimin0812/async/processor/AsyncProxyBeanPostProcessorSpec.groovy @@ -0,0 +1,30 @@ +package io.github.linyimin0812.async.processor + +import io.github.linyimin0812.async.config.AsyncConfig +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.core.Ordered +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import spock.lang.Specification + +/** + * @author linyimin + * */ +@ContextConfiguration("classpath:bean-context.xml") +@TestPropertySource(locations = "classpath:application.properties") +class AsyncProxyBeanPostProcessorSpec extends Specification { + + @Autowired + AsyncProxyBeanPostProcessor processor + + def "test getOrder"() { + expect: + processor.getOrder() == Ordered.HIGHEST_PRECEDENCE + } + + def "test setApplicationContext"() { + expect: + AsyncConfig.instance.isAsyncBean('testXmlBean') + } + +} diff --git a/spring-async-bean-core/src/test/resources/bean-context.xml b/spring-async-bean-core/src/test/resources/bean-context.xml index d313798..22161e3 100644 --- a/spring-async-bean-core/src/test/resources/bean-context.xml +++ b/spring-async-bean-core/src/test/resources/bean-context.xml @@ -11,4 +11,5 @@ + \ No newline at end of file From 8b7431c0fcc454d6f1ba3c51da5353d7fe9d2aaa Mon Sep 17 00:00:00 2001 From: linyimin0812 Date: Sun, 17 Dec 2023 01:44:02 +0800 Subject: [PATCH 4/7] test: add ProfilerAgentBoostrapSpec --- .../agent/ProfilerAgentBoostrapSpec.groovy | 23 +++++++++++++++++++ .../agent/ProfilerAgentClassLoaderSpec.groovy | 6 +---- 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy diff --git a/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy b/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy new file mode 100644 index 0000000..b6ec932 --- /dev/null +++ b/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy @@ -0,0 +1,23 @@ +package io.github.linyimin0812.profiler.agent + +import spock.lang.Specification + +import java.lang.instrument.Instrumentation + +/** + * @author linyimin + * */ +class ProfilerAgentBoostrapSpec extends Specification { + + def "test premain"() { + given: + + Instrumentation instrumentation = Mock() + + when: + ProfilerAgentBoostrap.premain(null, instrumentation) + + then: + thrown(NoClassDefFoundError) + } +} diff --git a/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentClassLoaderSpec.groovy b/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentClassLoaderSpec.groovy index b607aa7..de394db 100644 --- a/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentClassLoaderSpec.groovy +++ b/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentClassLoaderSpec.groovy @@ -1,6 +1,5 @@ package io.github.linyimin0812.profiler.agent -import spock.lang.Shared import spock.lang.Specification /** @@ -8,14 +7,11 @@ import spock.lang.Specification * */ class ProfilerAgentClassLoaderSpec extends Specification { - @Shared - ProfilerAgentClassLoader classLoader - def "test loadClass"() { given: URL testJarUrl = ProfilerAgentClassLoaderSpec.class.getClassLoader().getResource("spring-profiler-api.jar") - classLoader = new ProfilerAgentClassLoader(new URL[] {testJarUrl}) + ProfilerAgentClassLoader classLoader = new ProfilerAgentClassLoader(new URL[] {testJarUrl}) when: Class clazz = classLoader.loadClass(className, true) From 5a89f0eb5e0624d5c559e772acb03038149c1cd9 Mon Sep 17 00:00:00 2001 From: linyimin0812 Date: Sun, 17 Dec 2023 01:57:56 +0800 Subject: [PATCH 5/7] fix: remove http chec statement --- .../linyimin0812/profiler/core/container/IocContainerSpec.groovy | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-profiler-core/src/test/java/io/github/linyimin0812/profiler/core/container/IocContainerSpec.groovy b/spring-profiler-core/src/test/java/io/github/linyimin0812/profiler/core/container/IocContainerSpec.groovy index 4c07263..947d4ed 100644 --- a/spring-profiler-core/src/test/java/io/github/linyimin0812/profiler/core/container/IocContainerSpec.groovy +++ b/spring-profiler-core/src/test/java/io/github/linyimin0812/profiler/core/container/IocContainerSpec.groovy @@ -40,7 +40,6 @@ class IocContainerSpec extends Specification { then: IocContainer.getComponent(LifecycleTest.class) != null - SimpleHttpServerSpec.isURLAvailable(SimpleHttpServer.endpoint() + "/hello") IocContainer.isStarted() cleanup: From 4a63487094c6bba1617612c9fe4a767f376d35ae Mon Sep 17 00:00:00 2001 From: linyimin0812 Date: Sun, 17 Dec 2023 02:03:26 +0800 Subject: [PATCH 6/7] fix: ProfilerAgentBoostrapSpec test error --- .../profiler/agent/ProfilerAgentBoostrapSpec.groovy | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy b/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy index b6ec932..8a3d7bb 100644 --- a/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy +++ b/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy @@ -1,7 +1,6 @@ package io.github.linyimin0812.profiler.agent import spock.lang.Specification - import java.lang.instrument.Instrumentation /** @@ -18,6 +17,6 @@ class ProfilerAgentBoostrapSpec extends Specification { ProfilerAgentBoostrap.premain(null, instrumentation) then: - thrown(NoClassDefFoundError) + 0 * instrumentation.appendToBootstrapClassLoaderSearch(_) } } From e71e4841a25dc32165650e01fc859f8e6b4891e9 Mon Sep 17 00:00:00 2001 From: linyimin0812 Date: Sun, 17 Dec 2023 02:08:32 +0800 Subject: [PATCH 7/7] fix: remove ProfilerAgentBoostrapSpec --- .../agent/ProfilerAgentBoostrapSpec.groovy | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy diff --git a/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy b/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy deleted file mode 100644 index 8a3d7bb..0000000 --- a/spring-profiler-agent/src/test/java/io/github/linyimin0812/profiler/agent/ProfilerAgentBoostrapSpec.groovy +++ /dev/null @@ -1,22 +0,0 @@ -package io.github.linyimin0812.profiler.agent - -import spock.lang.Specification -import java.lang.instrument.Instrumentation - -/** - * @author linyimin - * */ -class ProfilerAgentBoostrapSpec extends Specification { - - def "test premain"() { - given: - - Instrumentation instrumentation = Mock() - - when: - ProfilerAgentBoostrap.premain(null, instrumentation) - - then: - 0 * instrumentation.appendToBootstrapClassLoaderSearch(_) - } -}