diff --git a/cms-templates/src/main/java/com/condation/cms/templates/CMSTemplateEngine.java b/cms-templates/src/main/java/com/condation/cms/templates/CMSTemplateEngine.java index 67a074837..fd5119c74 100644 --- a/cms-templates/src/main/java/com/condation/cms/templates/CMSTemplateEngine.java +++ b/cms-templates/src/main/java/com/condation/cms/templates/CMSTemplateEngine.java @@ -87,21 +87,25 @@ public Template getTemplateFromString (String templateContent) { return new DefaultTemplate(rootNode, renderer); } + private boolean useCache () { + return !configuration.isDevMode() && templateCache != null; + } + public Template getTemplate (String template) { - if (templateCache != null && templateCache.contains(template)) { + if (useCache() && templateCache.contains(template)) { return templateCache.get(template).get(); } String templateString = configuration.getTemplateLoader().load(template); if (templateString == null) { - throw new TemplateNotFoundException("template % not found".formatted(template)); + throw new TemplateNotFoundException("template %s not found".formatted(template)); } var tokenStream = lexer.tokenize(templateString); var rootNode = parser.parse(tokenStream); var temp = new DefaultTemplate(rootNode, renderer); - if (templateCache != null) { + if (useCache()) { templateCache.put(template, temp); } diff --git a/cms-templates/src/test/java/com/condation/cms/templates/CMSTemplateEngineTest.java b/cms-templates/src/test/java/com/condation/cms/templates/CMSTemplateEngineTest.java new file mode 100644 index 000000000..5c93bb8c9 --- /dev/null +++ b/cms-templates/src/test/java/com/condation/cms/templates/CMSTemplateEngineTest.java @@ -0,0 +1,80 @@ +package com.condation.cms.templates; + +/*- + * #%L + * cms-templates + * %% + * Copyright (C) 2023 - 2025 CondationCMS + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ + +import com.condation.cms.templates.exceptions.TemplateNotFoundException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +class CMSTemplateEngineTest { + + private TemplateConfiguration configuration; + private TemplateCache templateCache; + private TemplateLoader templateLoader; + private CMSTemplateEngine engine; + + @BeforeEach + void setUp() { + configuration = mock(TemplateConfiguration.class); + templateCache = mock(TemplateCache.class); + templateLoader = mock(TemplateLoader.class); + + when(configuration.isDevMode()).thenReturn(true); // Dev-Modus aktiv + when(configuration.getTemplateCache()).thenReturn(templateCache); + when(configuration.getTemplateLoader()).thenReturn(templateLoader); + + engine = new CMSTemplateEngine(configuration); + } + + @Test + void shouldNotUseCacheInDevMode() { + // Arrange + String templateName = "testTemplate"; + String templateContent = "Hello {{ name }}"; + when(templateLoader.load(templateName)).thenReturn(templateContent); + + // Act + Template result = engine.getTemplate(templateName); + + // Assert + assertThat(result).isNotNull(); + verify(templateCache, never()).contains(templateName); + verify(templateCache, never()).get(templateName); + verify(templateCache, never()).put(eq(templateName), any()); + } + + @Test + void shouldThrowExceptionIfTemplateNotFound() { + // Arrange + String templateName = "missingTemplate"; + when(templateLoader.load(templateName)).thenReturn(null); + + // Assert + assertThatThrownBy(() -> engine.getTemplate(templateName)) + .isInstanceOf(TemplateNotFoundException.class) + .hasMessageContaining("template missingTemplate not found"); + } +}