Skip to content

Commit 00ec01d

Browse files
committed
- build finish unit test effor all modules are over 80%
1 parent eaf3d25 commit 00ec01d

16 files changed

Lines changed: 1860 additions & 124 deletions

File tree

modules/jooby-freemarker/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@
4343
<artifactId>jooby-test</artifactId>
4444
<scope>test</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.mockito</groupId>
48+
<artifactId>mockito-core</artifactId>
49+
<scope>test</scope>
50+
</dependency>
4651
</dependencies>
4752
</project>

modules/jooby-freemarker/src/test/java/io/jooby/freemarker/FreemarkerModuleTest.java

Lines changed: 199 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,40 @@
77

88
import static java.util.Collections.singletonList;
99
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
import static org.mockito.ArgumentMatchers.any;
13+
import static org.mockito.ArgumentMatchers.anyString;
14+
import static org.mockito.ArgumentMatchers.eq;
15+
import static org.mockito.Mockito.mock;
16+
import static org.mockito.Mockito.never;
17+
import static org.mockito.Mockito.verify;
18+
import static org.mockito.Mockito.when;
1019

20+
import java.io.IOException;
1121
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
1224
import java.time.DayOfWeek;
1325
import java.time.LocalDate;
1426
import java.time.ZoneId;
1527
import java.time.temporal.TemporalAdjusters;
16-
import java.util.Arrays;
17-
import java.util.Date;
18-
import java.util.List;
19-
import java.util.Locale;
28+
import java.util.*;
2029

30+
import org.junit.jupiter.api.BeforeEach;
2131
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.io.TempDir;
2233

34+
import com.typesafe.config.Config;
2335
import com.typesafe.config.ConfigFactory;
36+
import com.typesafe.config.ConfigObject;
2437
import com.typesafe.config.ConfigValueFactory;
38+
import freemarker.cache.ClassTemplateLoader;
39+
import freemarker.cache.FileTemplateLoader;
40+
import freemarker.cache.StringTemplateLoader;
41+
import freemarker.core.XMLOutputFormat;
2542
import freemarker.template.Configuration;
26-
import io.jooby.Environment;
27-
import io.jooby.Jooby;
28-
import io.jooby.ModelAndView;
43+
import io.jooby.*;
2944
import io.jooby.test.MockContext;
3045

3146
public class FreemarkerModuleTest {
@@ -60,6 +75,183 @@ public String getLastname() {
6075
}
6176
}
6277

78+
private Jooby app;
79+
private Environment env;
80+
private ServiceRegistry registry;
81+
private Config config;
82+
83+
@BeforeEach
84+
void setUp() {
85+
app = mock(Jooby.class);
86+
env = mock(Environment.class);
87+
registry = mock(ServiceRegistry.class);
88+
config = mock(Config.class);
89+
90+
when(app.getEnvironment()).thenReturn(env);
91+
when(app.getServices()).thenReturn(registry);
92+
when(env.getConfig()).thenReturn(config);
93+
when(config.hasPath("freemarker")).thenReturn(false);
94+
when(env.isActive("dev", "test")).thenReturn(false);
95+
when(env.getProperty(eq(TemplateEngine.TEMPLATE_PATH), anyString()))
96+
.thenAnswer(inv -> inv.getArgument(1)); // Return default path
97+
when(env.getClassLoader()).thenReturn(getClass().getClassLoader());
98+
}
99+
100+
// --- CONSTRUCTOR & INSTALLATION TESTS ---
101+
102+
@Test
103+
void testInstallWithExistingConfiguration() {
104+
Configuration customConfig = new Configuration(Configuration.VERSION_2_3_32);
105+
FreemarkerModule module = new FreemarkerModule(customConfig);
106+
107+
module.install(app);
108+
109+
verify(app).encoder(any());
110+
verify(registry).put(Configuration.class, customConfig);
111+
// Since configuration was explicitly provided, it shouldn't query the environment for a new one
112+
verify(env, never()).getConfig();
113+
}
114+
115+
@Test
116+
void testInstallWithDefaultPath() {
117+
FreemarkerModule module = new FreemarkerModule();
118+
119+
module.install(app);
120+
121+
verify(app).encoder(any());
122+
verify(registry).put(eq(Configuration.class), any(Configuration.class));
123+
}
124+
125+
@Test
126+
void testInstallWithStringPath() {
127+
FreemarkerModule module = new FreemarkerModule("custom_views");
128+
129+
module.install(app);
130+
131+
verify(app).encoder(any());
132+
verify(registry).put(eq(Configuration.class), any(Configuration.class));
133+
}
134+
135+
@Test
136+
void testInstallWithNioPath(@TempDir Path tempDir) {
137+
FreemarkerModule module = new FreemarkerModule(tempDir);
138+
139+
module.install(app);
140+
141+
verify(app).encoder(any());
142+
verify(registry).put(eq(Configuration.class), any(Configuration.class));
143+
}
144+
145+
// --- BUILDER TESTS ---
146+
147+
@Test
148+
void testBuilderWithCustomTemplateLoader() {
149+
StringTemplateLoader stringLoader = new StringTemplateLoader();
150+
Configuration conf = FreemarkerModule.create().setTemplateLoader(stringLoader).build(env);
151+
152+
assertEquals(stringLoader, conf.getTemplateLoader());
153+
}
154+
155+
@Test
156+
void testBuilderWithSettings() {
157+
Configuration conf =
158+
FreemarkerModule.create().setSetting("tag_syntax", "square_bracket").build(env);
159+
160+
assertEquals(Configuration.SQUARE_BRACKET_TAG_SYNTAX, conf.getTagSyntax());
161+
}
162+
163+
@Test
164+
void testBuilderWithOutputFormat() {
165+
Configuration conf =
166+
FreemarkerModule.create().setOutputFormat(XMLOutputFormat.INSTANCE).build(env);
167+
168+
assertEquals(XMLOutputFormat.INSTANCE, conf.getOutputFormat());
169+
}
170+
171+
@Test
172+
void testBuilderWithConfigMap() {
173+
when(config.hasPath("freemarker")).thenReturn(true);
174+
175+
Config freemarkerConfig = mock(Config.class);
176+
ConfigObject root = mock(ConfigObject.class);
177+
178+
when(config.getConfig("freemarker")).thenReturn(freemarkerConfig);
179+
when(freemarkerConfig.root()).thenReturn(root);
180+
181+
Map<String, Object> settingsMap = new HashMap<>();
182+
settingsMap.put("locale", "en_US");
183+
settingsMap.put("number_format", "0.00");
184+
when(root.unwrapped()).thenReturn(settingsMap);
185+
186+
Configuration conf = FreemarkerModule.create().build(env);
187+
188+
assertEquals(java.util.Locale.US, conf.getLocale());
189+
assertEquals("0.00", conf.getNumberFormat());
190+
}
191+
192+
@Test
193+
void testBuilderCacheStorageInDevMode() {
194+
when(env.isActive("dev", "test")).thenReturn(true);
195+
196+
Configuration conf = FreemarkerModule.create().build(env);
197+
198+
assertEquals("freemarker.cache.NullCacheStorage", conf.getCacheStorage().getClass().getName());
199+
}
200+
201+
@Test
202+
void testBuilderCacheStorageInProdMode() {
203+
when(env.isActive("dev", "test")).thenReturn(false);
204+
205+
Configuration conf = FreemarkerModule.create().build(env);
206+
207+
// prod mode defaults to soft cache
208+
assertEquals("freemarker.cache.MruCacheStorage", conf.getCacheStorage().getClass().getName());
209+
}
210+
211+
// --- DEFAULT TEMPLATE LOADER RESOLUTION TESTS ---
212+
213+
@Test
214+
void testDefaultTemplateLoaderFileSystemFallback(@TempDir Path tempDir) {
215+
Configuration conf = FreemarkerModule.create().setTemplatesPath(tempDir).build(env);
216+
217+
// Because the temp directory exists on the file system, it should map to FileTemplateLoader
218+
assertTrue(conf.getTemplateLoader() instanceof FileTemplateLoader);
219+
}
220+
221+
@Test
222+
void testDefaultTemplateLoaderClasspathFallback() {
223+
Configuration conf =
224+
FreemarkerModule.create()
225+
.setTemplatesPath("this_path_does_not_exist_on_file_system")
226+
.build(env);
227+
228+
// Because the path doesn't exist on the file system, it should fallback to ClassTemplateLoader
229+
assertTrue(conf.getTemplateLoader() instanceof ClassTemplateLoader);
230+
}
231+
232+
// --- EXCEPTION HANDLING TESTS ---
233+
234+
@Test
235+
void testBuilderThrowsTemplateExceptionViaSneakyThrows() {
236+
FreemarkerModule.Builder builder =
237+
FreemarkerModule.create().setSetting("this_is_an_invalid_freemarker_setting_key", "value");
238+
239+
// Setting an invalid freemarker config key causes setSettings to throw TemplateException
240+
Exception ex = assertThrows(Exception.class, () -> builder.build(env));
241+
assertTrue(ex instanceof freemarker.core.Configurable.UnknownSettingException);
242+
}
243+
244+
@Test
245+
void testDefaultTemplateLoaderThrowsExceptionViaSneakyThrows(@TempDir Path tempDir)
246+
throws IOException {
247+
// Create a FILE, not a directory
248+
Path tempFile = Files.createTempFile(tempDir, "dummy", ".ftl");
249+
250+
FreemarkerModule.Builder builder = FreemarkerModule.create().setTemplatesPath(tempFile);
251+
252+
assertThrows(IOException.class, () -> builder.build(env));
253+
}
254+
63255
@Test
64256
public void render() throws Exception {
65257
Configuration freemarker =

modules/jooby-handlebars/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<artifactId>jooby-test</artifactId>
4848
<scope>test</scope>
4949
</dependency>
50+
<dependency>
51+
<groupId>org.mockito</groupId>
52+
<artifactId>mockito-core</artifactId>
53+
<scope>test</scope>
54+
</dependency>
5055
</dependencies>
5156

5257
</project>

0 commit comments

Comments
 (0)