001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.cli.compiler.autoconfigure; 018 019import groovy.lang.GroovyClassLoader; 020import org.codehaus.groovy.ast.AnnotationNode; 021import org.codehaus.groovy.ast.ClassHelper; 022import org.codehaus.groovy.ast.ClassNode; 023import org.codehaus.groovy.ast.expr.ClassExpression; 024import org.codehaus.groovy.classgen.GeneratorContext; 025import org.codehaus.groovy.control.CompilationFailedException; 026import org.codehaus.groovy.control.SourceUnit; 027import org.codehaus.groovy.control.customizers.ImportCustomizer; 028 029import org.springframework.boot.cli.compiler.AstUtils; 030import org.springframework.boot.cli.compiler.CompilerAutoConfiguration; 031import org.springframework.boot.cli.compiler.DependencyCustomizer; 032import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration; 033 034/** 035 * {@link CompilerAutoConfiguration} for Spring Test. 036 * 037 * @author Dave Syer 038 * @since 1.1.0 039 */ 040public class SpringTestCompilerAutoConfiguration extends CompilerAutoConfiguration { 041 042 @Override 043 public boolean matches(ClassNode classNode) { 044 return AstUtils.hasAtLeastOneAnnotation(classNode, "SpringBootTest"); 045 } 046 047 @Override 048 public void applyDependencies(DependencyCustomizer dependencies) { 049 dependencies.ifAnyMissingClasses("org.springframework.http.HttpHeaders") 050 .add("spring-boot-starter-web"); 051 } 052 053 @Override 054 public void apply(GroovyClassLoader loader, GroovyCompilerConfiguration configuration, 055 GeneratorContext generatorContext, SourceUnit source, ClassNode classNode) 056 throws CompilationFailedException { 057 if (!AstUtils.hasAtLeastOneAnnotation(classNode, "RunWith")) { 058 AnnotationNode runWith = new AnnotationNode(ClassHelper.make("RunWith")); 059 runWith.addMember("value", 060 new ClassExpression(ClassHelper.make("SpringRunner"))); 061 classNode.addAnnotation(runWith); 062 } 063 } 064 065 @Override 066 public void applyImports(ImportCustomizer imports) throws CompilationFailedException { 067 imports.addStarImports("org.junit.runner", "org.springframework.boot.test", 068 "org.springframework.boot.test.context", 069 "org.springframework.boot.test.web.client", "org.springframework.http", 070 "org.springframework.test.context.junit4", 071 "org.springframework.test.annotation").addImports( 072 "org.springframework.boot.test.context.SpringBootTest.WebEnvironment", 073 "org.springframework.boot.test.web.client.TestRestTemplate"); 074 } 075 076}