Skip to content

Commit

Permalink
test:add unit test for seata spring starter module
Browse files Browse the repository at this point in the history
  • Loading branch information
renliangyu857 committed Dec 29, 2024
1 parent 029dc91 commit 057289e
Show file tree
Hide file tree
Showing 23 changed files with 2,842 additions and 15 deletions.
17 changes: 17 additions & 0 deletions seata-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@
<artifactId>rocketmq-client</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-mock-server</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.apache.seata.spring.boot.autoconfigure;

import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.mockserver.MockServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class AutoConfigurationBaseTest {
public static final int MOCK_SERVER_PORT = 8099;

@BeforeAll
public static void before() {
ConfigurationFactory.reload();
MockServer.start(MOCK_SERVER_PORT);
}

@AfterAll
public static void after() {
MockServer.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
*/
package org.apache.seata.spring.boot.autoconfigure;

import org.apache.seata.spring.boot.autoconfigure.properties.SeataProperties;
import org.apache.seata.spring.boot.autoconfigure.properties.SpringCloudAlibabaConfiguration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


Expand All @@ -35,18 +32,6 @@ public static void initContext() {
}


@Bean
public SeataProperties seataProperties() {
SeataProperties seataProperties = new SeataProperties();
seataProperties.setApplicationId("test-id");
return seataProperties;
}

@Bean
public SpringCloudAlibabaConfiguration springCloudAlibabaConfiguration() {
return new SpringCloudAlibabaConfiguration();
}

@AfterAll
public static void closeContext() {
context.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.apache.seata.spring.boot.autoconfigure;


import org.apache.seata.spring.annotation.GlobalTransactionScanner;
import org.apache.seata.spring.boot.autoconfigure.properties.SeataProperties;
import org.apache.seata.spring.boot.autoconfigure.properties.SpringCloudAlibabaConfiguration;
import org.apache.seata.spring.boot.autoconfigure.provider.SpringApplicationContextProvider;
import org.apache.seata.tm.api.FailureHandler;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;

import static org.apache.seata.common.Constants.BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SeataAutoConfigurationTest.TestConfig.class, SeataAutoConfiguration.class})
@Import({SeataProperties.class,SpringCloudAlibabaConfiguration.class})
public class SeataAutoConfigurationTest extends AutoConfigurationBaseTest{

@Autowired
private GlobalTransactionScanner scanner;

@Autowired
private FailureHandler failureHandler;

@Test
void testSeataAutoConfiguration() {
assertNotNull(failureHandler);
assertNotNull(scanner);
assertEquals(scanner.getTxServiceGroup(), "default_tx_group");
}

@Configuration
static class TestConfig {

@Bean(BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER)
@ConditionalOnMissingBean(name = {BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER})
public org.apache.seata.spring.boot.autoconfigure.provider.SpringApplicationContextProvider springApplicationContextProvider() {
return new SpringApplicationContextProvider();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.apache.seata.spring.boot.autoconfigure;

import org.apache.seata.spring.annotation.datasource.SeataAutoDataSourceProxyCreator;
import org.apache.seata.spring.boot.autoconfigure.mock.MockDataSource;
import org.apache.seata.spring.boot.autoconfigure.properties.SeataProperties;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SeataDataSourceAutoConfigurationTest.TestConfig.class,SeataDataSourceAutoConfiguration.class})
public class SeataDataSourceAutoConfigurationTest extends AutoConfigurationBaseTest{

@Autowired
private SeataAutoDataSourceProxyCreator seataAutoDataSourceProxyCreator;

@Test
public void assertDataSourceProxyNotNull() {
assertNotNull(seataAutoDataSourceProxyCreator);
}

@Configuration
@SpringBootApplication
static class TestConfig {
@Bean
public DataSource mockDataSource() {
return new MockDataSource();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.apache.seata.spring.boot.autoconfigure;

import org.apache.seata.saga.engine.StateMachineConfig;
import org.apache.seata.saga.engine.StateMachineEngine;
import org.apache.seata.saga.engine.config.DbStateMachineConfig;
import org.apache.seata.spring.boot.autoconfigure.mock.MockDataSource;
import org.apache.seata.spring.boot.autoconfigure.properties.SagaAsyncThreadPoolProperties;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SeataSagaAutoConfigurationTest.TestConfig.class, SeataSagaAutoConfiguration.class})
public class SeataSagaAutoConfigurationTest extends AutoConfigurationBaseTest {

@Autowired
private StateMachineConfig stateMachineConfig;

@Autowired
private StateMachineEngine stateMachineEngine;

@Resource(name = "seataSagaRejectedExecutionHandler")
private RejectedExecutionHandler rejectedExecutionHandler;

@Autowired
private ThreadPoolExecutor threadPoolExecutor;


@Test
public void assertDbStateMachineConfigNotNull() {
assertNotNull(stateMachineConfig);
Assertions.assertEquals(((DbStateMachineConfig) stateMachineConfig).getApplicationId(), "test");
}

@Test
public void assertStateMachineEngine() {
assertNotNull(stateMachineEngine);
}

@Test
public void testSagaAsyncThreadPoolExecutorConfiguration() {
assertNotNull(rejectedExecutionHandler);
assertNotNull(threadPoolExecutor);
Assertions.assertEquals(threadPoolExecutor.getCorePoolSize(), 1);
Assertions.assertEquals(threadPoolExecutor.getMaximumPoolSize(), 20);
}

@Configuration
static class TestConfig {
@Bean
public DataSource mockDataSource() {
return new MockDataSource();
}

@Bean
public SagaAsyncThreadPoolProperties mockSagaAsyncThreadPoolProperties() {
SagaAsyncThreadPoolProperties sagaAsyncThreadPoolProperties = new SagaAsyncThreadPoolProperties();
sagaAsyncThreadPoolProperties.setCorePoolSize(1);
sagaAsyncThreadPoolProperties.setMaxPoolSize(20);
sagaAsyncThreadPoolProperties.setKeepAliveTime(60);
return sagaAsyncThreadPoolProperties;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.apache.seata.spring.boot.autoconfigure.mock;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;


public class MockBlob implements Blob {

public MockBlob() {
}

@Override
public long length() throws SQLException {
return 0;
}

@Override
public byte[] getBytes(long pos, int length) throws SQLException {
return new byte[0];
}

@Override
public InputStream getBinaryStream() throws SQLException {
return null;
}

@Override
public long position(byte[] pattern, long start) throws SQLException {
return 0;
}

@Override
public long position(Blob pattern, long start) throws SQLException {
return 0;
}

@Override
public int setBytes(long pos, byte[] bytes) throws SQLException {
return 0;
}

@Override
public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
return 0;
}

@Override
public OutputStream setBinaryStream(long pos) throws SQLException {
return null;
}

@Override
public void truncate(long len) throws SQLException {

}

@Override
public void free() throws SQLException {

}

@Override
public InputStream getBinaryStream(long pos, long length) throws SQLException {
return null;
}
}
Loading

0 comments on commit 057289e

Please sign in to comment.