|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.dsl.jbang.core.common; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 29 | + |
| 30 | +class TemplateHelperTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void testSimpleInterpolation() throws IOException { |
| 34 | + Map<String, Object> model = new HashMap<>(); |
| 35 | + model.put("Name", "MyRoute"); |
| 36 | + model.put("Code", "from(\"timer:tick\").log(\"Hello\");"); |
| 37 | + |
| 38 | + String result = TemplateHelper.processTemplate("code-java.ftl", model); |
| 39 | + |
| 40 | + assertTrue(result.contains("class MyRoute"), "Should contain class name"); |
| 41 | + assertTrue(result.contains("from(\"timer:tick\").log(\"Hello\");"), "Should contain code"); |
| 42 | + assertFalse(result.contains("[="), "Should not contain unresolved interpolations"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testLicenseHeaderStripped() throws IOException { |
| 47 | + Map<String, Object> model = new HashMap<>(); |
| 48 | + model.put("Name", "Test"); |
| 49 | + model.put("Code", "// test"); |
| 50 | + |
| 51 | + String result = TemplateHelper.processTemplate("code-java.ftl", model); |
| 52 | + |
| 53 | + assertFalse(result.contains("<#--"), "License header should be stripped"); |
| 54 | + assertFalse(result.contains("Licensed to the Apache"), "License text should be stripped"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testConditionalRendering() throws IOException { |
| 59 | + // main.ftl has [#if PackageName??] conditional |
| 60 | + Map<String, Object> withPackage = new HashMap<>(); |
| 61 | + withPackage.put("PackageName", "package com.example;"); |
| 62 | + withPackage.put("MainClassname", "MyApp"); |
| 63 | + |
| 64 | + String result = TemplateHelper.processTemplate("main.ftl", withPackage); |
| 65 | + assertTrue(result.contains("package com.example;"), "Should contain package declaration"); |
| 66 | + assertTrue(result.contains("class MyApp"), "Should contain class name"); |
| 67 | + |
| 68 | + // Without package |
| 69 | + Map<String, Object> withoutPackage = new HashMap<>(); |
| 70 | + withoutPackage.put("MainClassname", "MyApp"); |
| 71 | + |
| 72 | + result = TemplateHelper.processTemplate("main.ftl", withoutPackage); |
| 73 | + assertFalse(result.contains("package "), "Should not contain package declaration"); |
| 74 | + assertTrue(result.contains("class MyApp"), "Should contain class name"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testListRendering() throws IOException { |
| 79 | + Map<String, Object> model = new HashMap<>(); |
| 80 | + model.put("GroupId", "com.example"); |
| 81 | + model.put("ArtifactId", "my-app"); |
| 82 | + model.put("Version", "1.0.0"); |
| 83 | + model.put("SpringBootVersion", "3.4.0"); |
| 84 | + model.put("JavaVersion", "21"); |
| 85 | + model.put("CamelSpringBootVersion", "4.10.0"); |
| 86 | + model.put("ProjectBuildOutputTimestamp", "2024-01-01T00:00:00Z"); |
| 87 | + model.put("BuildProperties", ""); |
| 88 | + model.put("Repositories", List.of()); |
| 89 | + |
| 90 | + List<Map<String, Object>> deps = List.of( |
| 91 | + Map.of("groupId", "org.apache.camel.springboot", "artifactId", "camel-timer-starter", |
| 92 | + "isLib", false, "isKameletsUtils", false)); |
| 93 | + |
| 94 | + model.put("Dependencies", deps); |
| 95 | + |
| 96 | + String result = TemplateHelper.processTemplate("spring-boot-pom.ftl", model); |
| 97 | + assertTrue(result.contains("<groupId>com.example</groupId>"), "Should contain groupId"); |
| 98 | + assertTrue(result.contains("<artifactId>camel-timer-starter</artifactId>"), |
| 99 | + "Should contain dependency artifactId"); |
| 100 | + // Maven ${...} expressions should pass through unmodified |
| 101 | + assertTrue(result.contains("${project.basedir}") || !result.contains("isLib"), |
| 102 | + "Maven expressions should not be interpreted by FreeMarker"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testMavenExpressionsPreserved() throws IOException { |
| 107 | + Map<String, Object> model = new HashMap<>(); |
| 108 | + model.put("ArtifactId", "my-app"); |
| 109 | + model.put("Version", "1.0.0"); |
| 110 | + model.put("AppJar", "my-app-1.0.0.jar"); |
| 111 | + |
| 112 | + String result = TemplateHelper.processTemplate("Dockerfile21.ftl", model); |
| 113 | + assertTrue(result.contains("my-app"), "Should contain artifact id"); |
| 114 | + assertFalse(result.contains("[="), "Should not contain unresolved interpolations"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void testMissingTemplateThrowsIOException() { |
| 119 | + Map<String, Object> model = new HashMap<>(); |
| 120 | + assertThrows(IOException.class, () -> TemplateHelper.processTemplate("nonexistent.ftl", model)); |
| 121 | + } |
| 122 | +} |
0 commit comments