@@ -309,12 +309,14 @@ void testAddMethod() {
309
309
@ Test
310
310
void testAddMethod2 () {
311
311
String template =
312
- "@Bean\n " +
313
- "IntegrationFlow http_routeFlow() {\n " +
314
- "return IntegrationFlows.from(Http.inboundChannelAdapter(\" /test\" )).handle((p, h) -> p)\n " +
315
- ".log(LoggingHandler.Level.INFO)\n " +
316
- ".get();\n " +
317
- "}\n " ;
312
+ """
313
+ @Bean
314
+ IntegrationFlow http_routeFlow() {
315
+ return IntegrationFlows.from(Http.inboundChannelAdapter("/test")).handle((p, h) -> p)
316
+ .log(LoggingHandler.Level.INFO)
317
+ .get();
318
+ }
319
+ """ ;
318
320
319
321
Set <String > requiredImports = Set .of ("org.springframework.integration.transformer.ObjectToStringTransformer" ,
320
322
"org.springframework.context.annotation.Configuration" ,
@@ -326,40 +328,40 @@ void testAddMethod2() {
326
328
"org.springframework.integration.http.dsl.Http" );
327
329
328
330
ProjectContext context = TestProjectContext .buildProjectContext ()
329
- .withBuildFileHavingDependencies ("org.springframework.boot:spring-boot-starter-integration:2.5.5" ,
330
- "org.springframework.boot:spring-boot-starter-web:2.5.5" )
331
- .withJavaSource ("src/main/java/Config.java" , "public class Config {}" )
331
+ .withBuildFileHavingDependencies (
332
+ "org.springframework.boot:spring-boot-starter-integration:2.5.5" ,
333
+ "org.springframework.boot:spring-boot-starter-web:2.5.5"
334
+ )
335
+ .withJavaSource ("src/main/java" , "public class Config {}" )
332
336
.build ();
333
337
334
- long before = System .currentTimeMillis ();
335
-
336
- context .getBuildFile ().addDependency (Dependency .builder ()
338
+ context .getApplicationModules ().getRootModule ().getBuildFile ().addDependency (Dependency .builder ()
337
339
.groupId ("org.springframework.integration" )
338
340
.artifactId ("spring-integration-http" )
339
341
.version ("5.4.4" )
340
342
.build ());
341
343
342
- long timeSpent = System .currentTimeMillis () - before ;
343
- System .out .println ("Adding a dependency took: " + (timeSpent /1000 ) + " sec." );
344
-
345
- Type type = context .getProjectJavaSources ().list ().get (0 ).getTypes ().get (0 );
344
+ JavaSource javaSource = context .getProjectJavaSources ().findJavaSourceDeclaringType ("Config" ).get ();
345
+ Type type = javaSource .getTypes ().get (0 );
346
346
type .addMethod (template , requiredImports );
347
347
348
- assertThat (context .getProjectJavaSources ().list ().get (0 ).print ()).isEqualTo (
349
- "import org.springframework.context.annotation.Bean;\n " +
350
- "import org.springframework.integration.dsl.IntegrationFlow;\n " +
351
- "import org.springframework.integration.dsl.IntegrationFlows;\n " +
352
- "import org.springframework.integration.handler.LoggingHandler;\n " +
353
- "import org.springframework.integration.http.dsl.Http;\n " +
354
- "\n " +
355
- "public class Config {\n " +
356
- " @Bean\n " +
357
- " IntegrationFlow http_routeFlow() {\n " +
358
- " return IntegrationFlows.from(Http.inboundChannelAdapter(\" /test\" )).handle((p, h) -> p)\n " +
359
- " .log(LoggingHandler.Level.INFO)\n " +
360
- " .get();\n " +
361
- " }\n " +
362
- "}"
348
+ assertThat (javaSource .print ()).isEqualTo (
349
+ """
350
+ import org.springframework.context.annotation.Bean;
351
+ import org.springframework.integration.dsl.IntegrationFlow;
352
+ import org.springframework.integration.dsl.IntegrationFlows;
353
+ import org.springframework.integration.handler.LoggingHandler;
354
+ import org.springframework.integration.http.dsl.Http;
355
+
356
+ public class Config {
357
+ @Bean
358
+ IntegrationFlow http_routeFlow() {
359
+ return IntegrationFlows.from(Http.inboundChannelAdapter("/test")).handle((p, h) -> p)
360
+ .log(LoggingHandler.Level.INFO)
361
+ .get();
362
+ }
363
+ }
364
+ """
363
365
);
364
366
}
365
367
0 commit comments