SpringJavaConfigService { public void addBeanDefinition(File configFile, Project project, Object model) { ModelConverter converter = modelConverter.stream().filter(c -> c.getTargetModelClass().equals(model.getClass())) .findFirst() .orElseGet(() -> { if (model instanceof SpringBean) { try { return new SpringBeanModelConverter(project.getClassLoader().loadClass(((SpringBean) model).getClazz())); } catch (ClassNotFoundException | IOException e) { log.warn("Unable to access target Spring bean model type: " + ((SpringBean) model).getClazz(), e); } } return new SpringBeanModelConverter(model.getClass()); }); String javaCode; try (InputStream fis = new FileInputStream(configFile)) { javaCode = FileUtils.readToString(fis); } catch (IOException e) { throw new ApplicationRuntimeException("Failed to read/write Java config file"); } StringBuilder codeBuilder = new StringBuilder(javaCode); String codeSnippet = converter.getJavaConfig(model); Matcher classStartMatcher = Pattern.compile("^(public)? class " + configFile.getName().replaceAll("\\.java", "") + "(.*)\\{(\\s)*$", Pattern.MULTILINE).matcher(javaCode); if (classStartMatcher.find()) { codeBuilder.insert(classStartMatcher.end(), codeSnippet); } Matcher importMatcher = Pattern.compile("^import(.*);(\\s)*$", Pattern.MULTILINE).matcher(javaCode); if (importMatcher.find()) { List<Class<?>> imports = converter.getAdditionalImports(); imports.stream().map(importType -> String.format("%nimport %s;", importType.getName())).forEach(importStmt -> { if (!javaCode.contains(importStmt)) { codeBuilder.insert(importMatcher.end(), importStmt); } }); } FileUtils.writeToFile(codeBuilder.toString(), configFile); }  List<Class<?>> getConfigImports(Class<?> configFile, Project project); T getBeanDefinition(Class<?> configFile, Project project, String id, Class<T> type); List<T> getBeanDefinitions(Class<?> configFile, Project project, Class<T> type); List<String> getBeanNames(Class<?> configFile, Project project, Class<?> type); void addBeanDefinition(File configFile, Project project, Object model); void removeBeanDefinition(File configFile, Project project, String id); void removeBeanDefinitions(File configFile, Project project, Class<?> type); void updateBeanDefinition(File configFile, Project project, String id, Object model); void updateBeanDefinitions(File configFile, Project project, Class<?> type, Object model);  }
@Test public void testAddJavaConfig() throws Exception { SchemaModel xsdSchema1 = new SchemaModelBuilder().withId("xsdSchema1").withLocation("path/to/schema1.xsd").build(); SchemaModel xsdSchema2 = new SchemaModelBuilder().withId("xsdSchema2").withLocation("path/to/schema2.xsd").build(); SchemaRepositoryModel schemaRepository = new SchemaRepositoryModelBuilder() .withId("schemaRepository") .addSchema(xsdSchema1) .addSchema(xsdSchema2).build(); JmsEndpointModel endpoint = new JmsEndpointModel(); endpoint.setId("jmsEndpoint"); endpoint.setDestinationName("jms.inbound.queue"); File configFile = new ClassPathResource("config/AddBeanJavaConfig.java").getFile(); springJavaConfigService.addBeanDefinition(configFile, project, xsdSchema1); springJavaConfigService.addBeanDefinition(configFile, project, xsdSchema2); springJavaConfigService.addBeanDefinition(configFile, project, schemaRepository); springJavaConfigService.addBeanDefinition(configFile, project, endpoint); String result = FileUtils.readToString(new FileInputStream(configFile)); System.out.println(result); Assert.assertTrue(result.contains("import com.consol.citrus.dsl.endpoint.CitrusEndpoints;")); Assert.assertTrue(result.contains("import com.consol.citrus.xml.XsdSchemaRepository;")); Assert.assertTrue(result.contains("import org.springframework.core.io.ClassPathResource;")); Assert.assertTrue(result.contains("import org.springframework.xml.xsd.SimpleXsdSchema;")); Assert.assertTrue(result.contains("import com.consol.citrus.jms.endpoint.JmsEndpoint;")); Assert.assertTrue(result.contains("import com.consol.citrus.http.client.HttpClient;")); Assert.assertTrue(result.contains("public HttpClient httpClient() {")); Assert.assertTrue(result.contains("public JmsEndpoint jmsEndpoint() {")); Assert.assertTrue(result.contains("return CitrusEndpoints.jms().asynchronous()")); Assert.assertTrue(result.contains(".destination(\"jms.inbound.queue\")")); Assert.assertTrue(result.contains("public XsdSchemaRepository schemaRepository() {")); Assert.assertTrue(result.contains("public SimpleXsdSchema xsdSchema1() {")); Assert.assertTrue(result.contains("xsdSchema1.setXsd(new ClassPathResource(\"path/to/schema1.xsd\"));")); Assert.assertTrue(result.contains("public SimpleXsdSchema xsdSchema2() {")); Assert.assertTrue(result.contains("xsdSchema2.setXsd(new ClassPathResource(\"path/to/schema2.xsd\"));")); }