Skip to content

Commit

Permalink
Merge pull request #131 from linyimin0812/test/20231215_test_coverage_1
Browse files Browse the repository at this point in the history
test: add test cases
  • Loading branch information
linyimin0812 authored Dec 16, 2023
2 parents d3b5309 + e71e484 commit 899f416
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 12 deletions.
14 changes: 14 additions & 0 deletions spring-async-bean-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
<version>2.4-M1-groovy-4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.10</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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(_)
}
}
Original file line number Diff line number Diff line change
@@ -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')
}

}
1 change: 1 addition & 0 deletions spring-async-bean-core/src/test/resources/bean-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
<context:component-scan base-package="io.github.linyimin0812.async.springbeans" />
<bean id="testXmlBean" init-method="init" class="io.github.linyimin0812.async.springbeans.TestXmlBean" />
<bean id="asyncTaskExecutionListener" class="io.github.linyimin0812.async.listener.AsyncTaskExecutionListener" />
<bean id="asyncProxyBeanPostProcessor" class="io.github.linyimin0812.async.processor.AsyncProxyBeanPostProcessor" />
</beans>
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package io.github.linyimin0812.profiler.agent

import spock.lang.Shared
import spock.lang.Specification

/**
* @author linyimin
* */
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)
Expand Down
7 changes: 7 additions & 0 deletions spring-profiler-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
<groupId>org.picocontainer</groupId>
<artifactId>picocontainer</artifactId>
</dependency>

<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class IocContainerSpec extends Specification {

then:
IocContainer.getComponent(LifecycleTest.class) != null
SimpleHttpServerSpec.isURLAvailable(SimpleHttpServer.endpoint() + "/hello")
IocContainer.isStarted()

cleanup:
Expand Down
13 changes: 7 additions & 6 deletions spring-startup-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@
<artifactId>directory-watcher</artifactId>
<version>0.18.0</version>
</dependency>

<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.4-M1-groovy-4.0</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
Expand Down

0 comments on commit 899f416

Please sign in to comment.