Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java: cleanup loader and tests #441

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions camel-k-loader-groovy/pom.xml
Original file line number Diff line number Diff line change
@@ -76,12 +76,6 @@
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
@@ -119,23 +113,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
18 changes: 18 additions & 0 deletions camel-k-loader-java/pom.xml
Original file line number Diff line number Diff line change
@@ -109,6 +109,24 @@

<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
<configuration>
<invokeDynamic>true</invokeDynamic>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.camel.k.loader.java


import org.apache.camel.k.loader.java.model.EmployeeDTO
import org.apache.camel.k.loader.java.support.TestRuntime
import org.apache.camel.model.ProcessDefinition
import org.apache.camel.model.SetBodyDefinition
import org.apache.camel.model.ToDefinition
import spock.lang.AutoCleanup
import spock.lang.Specification

class JavaSourceLoaderTest extends Specification {
@AutoCleanup
def runtime = new TestRuntime()

def "load routes with nested class"() {
when:
runtime.loadRoutes("classpath:MyRoutesWithNestedClass.java")
then:
with(runtime.context.routeDefinitions) {
it.size() == 1

it[0].outputs[0] instanceof SetBodyDefinition
it[0].outputs[1] instanceof ProcessDefinition
it[0].outputs[2] instanceof ToDefinition

it[0].input.endpointUri == 'timer:tick'
}
}

def "load routes with nested type"() {
when:
runtime.loadRoutes("classpath:MyRoutesWithNestedTypes.java")
runtime.context.applicationContextClassLoader.loadClass('MyRoutesWithNestedTypes$MyModel')
then:
noExceptionThrown()
}

def "load routes with rest configuration"() {
when:
runtime.loadRoutes("classpath:MyRoutesWithRestConfiguration.java")
then:
runtime.context.restConfiguration.component == 'restlet'
}

def "load routes with model"() {
when:
runtime.loadRoutes("classpath:MyRoutesWithModel.java")
then:
runtime.context.restDefinitions.any {
it.verbs.first().outType == EmployeeDTO.class.name
}
}

def "load configuration"() {
when:
runtime.loadRoutes("classpath:MyRoutesConfig.java")
then:
runtime.configurations.size() == 1
}


def "load"(location) {
expect:
runtime.loadRoutes(location)

with(runtime.context.routeDefinitions) {
it[0].input.endpointUri ==~ /timer:.*tick/
it[0].outputs[0] instanceof ToDefinition
}
where:
location << [
"classpath:MyRoutes.java",
"classpath:MyRoutesWithNameOverride.java?name=MyRoutes.java",
"classpath:MyRoutesWithPackage.java",
"classpath:MyRoutesWithPackageAndComment.java",
"classpath:MyRoutesWithPackageAndLineComment.java",
"classpath:MyRoutesWithEndpointDsl.java"
]

}
}
Original file line number Diff line number Diff line change
@@ -14,34 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.k.loader.java.model;
package org.apache.camel.k.loader.java.model

public class EmployeeDTO {
public int id;
public String name;
public String org;
import groovy.transform.CompileStatic

public int getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getOrg() {
return org;
}

public void setOrg(String org) {
this.org = org;
}
@CompileStatic
class EmployeeDTO {
int id
String name
String org
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.camel.k.loader.java.support

import org.apache.camel.CamelContext
import org.apache.camel.RoutesBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.CompositeClassloader
import org.apache.camel.k.Runtime
import org.apache.camel.model.ModelCamelContext

import static org.apache.camel.k.listener.RoutesConfigurer.forRoutes

class TestRuntime implements Runtime, AutoCloseable {
final ModelCamelContext context
final List<RoutesBuilder> builders
final List<Object> configurations

TestRuntime() {
this.context = new DefaultCamelContext()
this.context.setApplicationContextClassLoader(new CompositeClassloader())
this.builders = []
this.configurations = []
}

@Override
CamelContext getCamelContext() {
return this.context
}

@Override
void addRoutes(RoutesBuilder builder) {
this.builders << builder
this.context.addRoutes(builder)
}

@Override
void addConfiguration(Object configuration) {
this.configurations.add(configuration)
}

void loadRoutes(String... routes) {
routes.each {
forRoutes(it).accept(Phase.ConfigureRoutes, this)
}
}

void start() {
context.start()
}

@Override
void stop() {
context.stop()
}

@Override
void close() {
stop()
}
}
Loading