-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test:add unit test for seata spring starter module
- Loading branch information
1 parent
029dc91
commit 057289e
Showing
23 changed files
with
2,842 additions
and
15 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
23 changes: 23 additions & 0 deletions
23
...r/src/test/java/org/apache/seata/spring/boot/autoconfigure/AutoConfigurationBaseTest.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,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(); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
.../src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataAutoConfigurationTest.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 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(); | ||
} | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...java/org/apache/seata/spring/boot/autoconfigure/SeataDataSourceAutoConfigurationTest.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,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(); | ||
} | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
.../test/java/org/apache/seata/spring/boot/autoconfigure/SeataSagaAutoConfigurationTest.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,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; | ||
} | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...-boot-starter/src/test/java/org/apache/seata/spring/boot/autoconfigure/mock/MockBlob.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,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; | ||
} | ||
} |
Oops, something went wrong.