Kapt3: Add Maven integration test
This commit is contained in:
committed by
Yan Zhulanow
parent
7503376731
commit
eba5baa7ff
@@ -0,0 +1,146 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.jetbrains.kotlin.examples</groupId>
|
||||
<artifactId>dagger-maven-example</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<kotlin.version>1.1-SNAPSHOT</kotlin.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<main.class>coffee.CoffeeApp</main.class>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.dagger</groupId>
|
||||
<artifactId>dagger</artifactId>
|
||||
<version>2.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<version>${kotlin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>kapt</id>
|
||||
<goals>
|
||||
<goal>kapt</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<sourceDir>src/main/kotlin</sourceDir>
|
||||
<sourceDir>src/main/java</sourceDir>
|
||||
</sourceDirs>
|
||||
<annotationProcessorPaths>
|
||||
<annotationProcessorPath>
|
||||
<groupId>com.google.dagger</groupId>
|
||||
<artifactId>dagger-compiler</artifactId>
|
||||
<version>2.9</version>
|
||||
</annotationProcessorPath>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<sourceDir>src/main/kotlin</sourceDir>
|
||||
<sourceDir>src/main/java</sourceDir>
|
||||
</sourceDirs>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-kapt</id>
|
||||
<goals>
|
||||
<goal>test-kapt</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<sourceDir>src/test/kotlin</sourceDir>
|
||||
<sourceDir>src/test/java</sourceDir>
|
||||
</sourceDirs>
|
||||
<annotationProcessorPaths>
|
||||
<annotationProcessorPath>
|
||||
<groupId>com.google.dagger</groupId>
|
||||
<artifactId>dagger-compiler</artifactId>
|
||||
<version>2.9</version>
|
||||
</annotationProcessorPath>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<sourceDir>src/test/kotlin</sourceDir>
|
||||
<sourceDir>src/test/java</sourceDir>
|
||||
<sourceDir>target/generated-sources/kapt/test</sourceDir>
|
||||
</sourceDirs>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<proc>none</proc>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
<executions>
|
||||
<!-- Replacing default-compile as it is treated specially by maven -->
|
||||
<execution>
|
||||
<id>default-compile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<!-- Replacing default-testCompile as it is treated specially by maven -->
|
||||
<execution>
|
||||
<id>default-testCompile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>java-compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>java-test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals> <goal>testCompile</goal> </goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package coffee
|
||||
|
||||
import dagger.Component
|
||||
import javax.inject.Singleton
|
||||
|
||||
object CoffeeApp {
|
||||
@Singleton
|
||||
@Component(modules = arrayOf(DripCoffeeModule::class))
|
||||
interface Coffee {
|
||||
fun maker(): CoffeeMaker
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
val coffee = DaggerCoffeeApp_Coffee.builder().build()
|
||||
coffee.maker().brew()
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package coffee
|
||||
|
||||
import dagger.Lazy
|
||||
import javax.inject.Inject
|
||||
|
||||
class CoffeeMaker @Inject constructor(
|
||||
private val heater: Lazy<Heater>,
|
||||
private val pump: Pump
|
||||
) {
|
||||
|
||||
fun brew() {
|
||||
heater.get().on()
|
||||
pump.pump()
|
||||
println(" [_]P coffee! [_]P ")
|
||||
heater.get().off()
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package coffee
|
||||
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module(includes = arrayOf(PumpModule::class))
|
||||
class DripCoffeeModule {
|
||||
@Provides @Singleton
|
||||
fun provideHeater(): Heater {
|
||||
return ElectricHeater()
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package coffee
|
||||
|
||||
open class ElectricHeater : Heater {
|
||||
override var isHot: Boolean = false
|
||||
|
||||
override fun on() {
|
||||
println("~ ~ ~ heating ~ ~ ~")
|
||||
this.isHot = true
|
||||
}
|
||||
|
||||
override fun off() {
|
||||
this.isHot = false
|
||||
}
|
||||
}
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
package coffee
|
||||
|
||||
interface Heater {
|
||||
fun on()
|
||||
fun off()
|
||||
val isHot: Boolean
|
||||
}
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
package coffee
|
||||
|
||||
interface Pump {
|
||||
fun pump()
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package coffee
|
||||
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
|
||||
@Module
|
||||
abstract class PumpModule {
|
||||
@Binds
|
||||
abstract fun providePump(pump: Thermosiphon): Pump
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package coffee
|
||||
|
||||
import javax.inject.Inject
|
||||
|
||||
class Thermosiphon @Inject
|
||||
constructor(private val heater: Heater) : Pump {
|
||||
override fun pump() {
|
||||
if (heater.isHot) {
|
||||
println("=> => pumping => =>")
|
||||
}
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package hello.tests
|
||||
|
||||
import coffee.*
|
||||
import dagger.Component
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import junit.framework.TestCase
|
||||
import javax.inject.Singleton
|
||||
|
||||
private var executed = false
|
||||
|
||||
class ExampleTest : TestCase() {
|
||||
@Singleton
|
||||
@Component(modules = arrayOf(TestCoffeeModule::class))
|
||||
interface Coffee {
|
||||
fun maker(): CoffeeMaker
|
||||
}
|
||||
|
||||
@Module(includes = arrayOf(PumpModule::class))
|
||||
class TestCoffeeModule {
|
||||
@Provides @Singleton
|
||||
fun provideHeater(): Heater {
|
||||
return object: ElectricHeater() {
|
||||
override fun on() {
|
||||
println("~ psh ~ psh ~ psh ~")
|
||||
println("(from tests)")
|
||||
executed = true
|
||||
super.on()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun testAssert() {
|
||||
val coffee = DaggerExampleTest_Coffee.builder().build()
|
||||
coffee.maker().brew()
|
||||
assert(executed)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import java.io.*;
|
||||
|
||||
File file = new File(basedir, "target/dagger-maven-example-1.0-SNAPSHOT.jar");
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
throw new FileNotFoundException("Could not find generated JAR: " + file);
|
||||
}
|
||||
Reference in New Issue
Block a user