Skip to content

Commit

Permalink
Add testcase for Spring Boot Starter
Browse files Browse the repository at this point in the history
  • Loading branch information
TeslaCN committed Jul 17, 2020
1 parent 01a9cab commit e3284e6
Show file tree
Hide file tree
Showing 20 changed files with 758 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.shardingsphere.elasticjob.reg.boot;

import org.apache.shardingsphere.elasticjob.reg.boot.fixture.EmbedTestingServer;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@SpringBootTest
@SpringBootApplication
@ActiveProfiles("elasticjob")
public class RegistryCenterSpringBootTest extends AbstractJUnit4SpringContextTests {

@BeforeClass
public static void init() {
EmbedTestingServer.start();
}

@Test
public void testZookeeperProperties() {
assertNotNull(applicationContext);
ZookeeperProperties zookeeperProperties = applicationContext.getBean(ZookeeperProperties.class);
assertEquals(EmbedTestingServer.getConnectionString(), zookeeperProperties.getServerLists());
assertEquals("elasticjob-registry-center-spring-boot-starter", zookeeperProperties.getNamespace());
}

@Test
public void testRegistryCenterCreation() {
assertNotNull(applicationContext);
ZookeeperRegistryCenter zookeeperRegistryCenter = applicationContext.getBean(ZookeeperRegistryCenter.class);
assertNotNull(zookeeperRegistryCenter);
zookeeperRegistryCenter.persist("/foo", "bar");
assertEquals("bar", zookeeperRegistryCenter.get("/foo"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.shardingsphere.elasticjob.reg.boot.fixture;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.curator.test.TestingServer;
import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler;

import java.io.File;
import java.io.IOException;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EmbedTestingServer {

private static final int PORT = 16181;

private static volatile TestingServer testingServer;

/**
* Get the connection string.
*
* @return connection string
*/
public static String getConnectionString() {
return "localhost:" + PORT;
}

/**
* Start the server.
*/
public static void start() {
if (null != testingServer) {
return;
}
try {
testingServer = new TestingServer(PORT, new File(String.format("target/test_zk_data/%s/", System.nanoTime())));
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
RegExceptionHandler.handleException(ex);
} finally {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
Thread.sleep(1000);
testingServer.close();
} catch (final IOException | InterruptedException ex) {
RegExceptionHandler.handleException(ex);
}
}));
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# 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.
#

elasticjob:
regCenter:
serverLists: localhost:16181
namespace: elasticjob-registry-center-spring-boot-starter
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.shardingsphere.elasticjob.tracing.boot;

import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration;
import org.apache.shardingsphere.elasticjob.tracing.boot.fixture.EmbedTestingServer;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import javax.sql.DataSource;
import java.sql.SQLException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@SpringBootTest
@SpringBootApplication
@ActiveProfiles("elasticjob")
public class TracingConfigurationSpringBootTest extends AbstractJUnit4SpringContextTests {

@BeforeClass
public static void setUp() {
EmbedTestingServer.start();
}

@Test
public void testTracingConfigurationCreation() throws SQLException {
assertNotNull(applicationContext);
TracingConfiguration tracingConfiguration = applicationContext.getBean(TracingConfiguration.class);
assertNotNull(tracingConfiguration);
assertEquals("RDB", tracingConfiguration.getType());
assertTrue(tracingConfiguration.getStorage() instanceof DataSource);
DataSource dataSource = (DataSource) tracingConfiguration.getStorage();
assertNotNull(dataSource.getConnection());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.shardingsphere.elasticjob.tracing.boot.fixture;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.curator.test.TestingServer;
import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler;

import java.io.File;
import java.io.IOException;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EmbedTestingServer {

private static final int PORT = 17181;

private static volatile TestingServer testingServer;

/**
* Get the connection string.
*
* @return connection string
*/
public static String getConnectionString() {
return "localhost:" + PORT;
}

/**
* Start the server.
*/
public static void start() {
if (null != testingServer) {
return;
}
try {
testingServer = new TestingServer(PORT, new File(String.format("target/test_zk_data/%s/", System.nanoTime())));
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
RegExceptionHandler.handleException(ex);
} finally {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
Thread.sleep(1000);
testingServer.close();
} catch (final IOException | InterruptedException ex) {
RegExceptionHandler.handleException(ex);
}
}));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# 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.
#

spring:
datasource:
url: jdbc:h2:mem:job_event_storage
driver-class-name: org.h2.Driver
username: sa
password:
elasticjob:
reg-center:
server-lists: localhost:17181
namespace: elasticjob-tracing-spring-boot-starter
tracing:
type: RDB
10 changes: 10 additions & 0 deletions elasticjob-lite/elasticjob-lite-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@
<artifactId>elasticjob-registry-center-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>

0 comments on commit e3284e6

Please sign in to comment.