Skip to content

Commit

Permalink
add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfeng committed Jul 15, 2020
1 parent d4f3549 commit b57fb1d
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bom/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2799,7 +2799,7 @@
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
Expand Down
5 changes: 0 additions & 5 deletions extensions/mybatis/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal-deployment</artifactId>
Expand Down Expand Up @@ -51,7 +47,6 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import io.quarkus.builder.item.MultiBuildItem;

public final class MyBatisMapperBuildItem extends MultiBuildItem {
private final DotName dotName;
private final DotName mapperName;

public MyBatisMapperBuildItem(DotName dotName) {
this.dotName = dotName;
public MyBatisMapperBuildItem(DotName mapperName) {
this.mapperName = mapperName;
}

public DotName getDotName() {
return dotName;
public DotName getMapperName() {
return mapperName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import javax.inject.Singleton;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.javassist.util.proxy.ProxyFactory;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;
Expand All @@ -22,6 +26,8 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.configuration.ConfigurationError;
import io.quarkus.mybatis.runtime.MyBatisProducers;
import io.quarkus.mybatis.runtime.MyBatisRecorder;
Expand All @@ -39,14 +45,24 @@ FeatureBuildItem feature() {
}

@BuildStep
void addMyBatisMappers(BuildProducer<MyBatisMapperBuildItem> mappers,
BuildProducer<AdditionalBeanBuildItem> additionalBeans,
CombinedIndexBuildItem indexBuildItem) {
void runtimeInitialzed(BuildProducer<RuntimeInitializedClassBuildItem> runtimeInit) {
runtimeInit.produce(new RuntimeInitializedClassBuildItem(Log4jImpl.class.getName()));
}

@BuildStep
void reflectiveClasses(BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
ProxyFactory.class,
XMLLanguageDriver.class,
RawLanguageDriver.class));
}

@BuildStep
void addMyBatisMappers(BuildProducer<MyBatisMapperBuildItem> mappers, CombinedIndexBuildItem indexBuildItem) {
for (AnnotationInstance i : indexBuildItem.getIndex().getAnnotations(MYBATIS_MAPPER)) {
if (i.target().kind() == AnnotationTarget.Kind.CLASS) {
DotName dotName = i.target().asClass().name();
mappers.produce(new MyBatisMapperBuildItem(dotName));
additionalBeans.produce(new AdditionalBeanBuildItem(dotName.toString()));
}
}
}
Expand All @@ -63,7 +79,7 @@ SqlSessionFactoryBuildItem generateSqlSessionFactory(MyBatisRuntimeConfig myBati
List<JdbcDataSourceBuildItem> jdbcDataSourcesBuildItem,
MyBatisRecorder recorder) {
List<String> mappers = myBatisMapperBuildItems
.stream().map(m -> m.getDotName().toString()).collect(Collectors.toList());
.stream().map(m -> m.getMapperName().toString()).collect(Collectors.toList());

String dataSourceName;
if (myBatisRuntimeConfig.dataSource.isPresent()) {
Expand Down Expand Up @@ -109,11 +125,11 @@ void generateMapperBeans(MyBatisRecorder recorder,

for (MyBatisMapperBuildItem i : myBatisMapperBuildItems) {
SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem
.configure(i.getDotName())
.configure(i.getMapperName())
.scope(Singleton.class)
.setRuntimeInit()
.unremovable()
.supplier(recorder.MyBatisMapperSupplier(i.getDotName().toString(),
.supplier(recorder.MyBatisMapperSupplier(i.getMapperName().toString(),
sqlSessionManagerBuildItem.getSqlSessionManager()));
syntheticBeanBuildItemBuildProducer.produce(configurator.done());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ quarkus.datasource.db-kind=h2
quarkus.datasource.username=username-default

quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:default
quarkus.mybatis.initialSql=insert.sql
quarkus.mybatis.initial-sql=insert.sql
5 changes: 0 additions & 5 deletions extensions/mybatis/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
<description>MyBatis SQL mapper framework for Java</description>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
Expand All @@ -40,7 +36,6 @@
<artifactId>quarkus-bootstrap-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public RuntimeValue<SqlSessionFactory> createSqlSessionFactory(
try {
configuration.addMapper(Resources.classForName(mapper));
} catch (ClassNotFoundException e) {

LOG.debug("Can not find the mapper class " + mapper);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public class MyBatisRuntimeConfig {
* MyBatis environment id
*/
@ConfigItem(defaultValue = "quarkus")
public String environment = "quarkus";
public String environment;

/**
* MyBatis transaction factory
*/
@ConfigItem(defaultValue = "MANAGED")
public String transactionFactory = "MANAGED";
public String transactionFactory;

/**
* MyBatis data source
Expand All @@ -29,6 +29,6 @@ public class MyBatisRuntimeConfig {
/**
* MyBatis initial sql
*/
@ConfigItem(name = "initialSql")
@ConfigItem(name = "initial-sql")
public Optional<String> initialSql;
}
118 changes: 118 additions & 0 deletions integration-tests/mybatis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-integration-tests-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-mybatis-integration-test</artifactId>
<name>Quarkus - Integration Tests - MyBatis</name>
<description>The mybatis integration tests module</description>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mybatis</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<id>native-image</id>
<goals>
<goal>native-image</goal>
</goals>
<configuration>
<cleanupServer>true</cleanupServer>
<enableHttpsUrlHandler>true</enableHttpsUrlHandler>
<graalvmHome>${graalvmHome}</graalvmHome>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</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 io.quarkus.it.mybatis;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/mybatis")
public class MybatisResource {

@Inject
UserMapper userMapper;

@Path("/user/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public User getUser(@PathParam("id") Integer id) {
return userMapper.getUser(id);
}

@Path("/user")
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Integer createUser(@FormParam("id") Integer id, @FormParam("name") String name) {
return userMapper.createUser(id, name);
}

@Path("/user/{id}")
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public Integer removeUser(@PathParam("id") Integer id) {
return userMapper.removeUser(id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.it.mybatis;

public class User {
private Integer id;
private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.it.mybatis;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {

@Select("select * from users where id = #{id}")
User getUser(Integer id);

@Insert("insert into users (id, name) values (#{id}, #{name})")
Integer createUser(@Param("id") Integer id, @Param("name") String name);

@Delete("delete from users where id = #{id}")
Integer removeUser(Integer id);
}
Loading

0 comments on commit b57fb1d

Please sign in to comment.