1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File VelocityRendererTest.java

 

Code metrics

8
42
12
1
162
128
16
0.38
3.5
12
1.33

Classes

Class Line # Actions
VelocityRendererTest 28 42 0% 16 4
0.935483993.5%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    package org.europa.together.application;
2   
3    import static com.google.code.beanmatchers.BeanMatchers.*;
4    import java.util.HashMap;
5    import java.util.Map;
6    import org.europa.together.JUnit5Preperator;
7    import org.europa.together.business.Logger;
8    import org.europa.together.domain.LogLevel;
9    import org.europa.together.utils.Constraints;
10    import static org.hamcrest.MatcherAssert.assertThat;
11    import org.junit.jupiter.api.AfterAll;
12    import org.junit.jupiter.api.AfterEach;
13    import static org.junit.jupiter.api.Assertions.*;
14    import org.junit.jupiter.api.Assumptions;
15    import org.junit.jupiter.api.BeforeAll;
16    import org.junit.jupiter.api.BeforeEach;
17    import org.junit.jupiter.api.Test;
18    import org.europa.together.business.TemplateRenderer;
19    import org.junit.jupiter.api.extension.ExtendWith;
20    import org.springframework.beans.factory.annotation.Autowired;
21    import org.springframework.test.context.ContextConfiguration;
22    import org.springframework.test.context.junit.jupiter.SpringExtension;
23   
24    @SuppressWarnings("unchecked")
25    @ExtendWith({JUnit5Preperator.class})
26    @ExtendWith(SpringExtension.class)
27    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
28    public class VelocityRendererTest {
29   
30    private static final Logger LOGGER
31    = new LogbackLogger(VelocityRendererTest.class);
32    private static final String FILE_PATH
33    = "org/europa/together/velocity";
34    private static final String DIRECTORY
35    = Constraints.SYSTEM_APP_DIR + "/target/test-classes/" + FILE_PATH;
36   
37    @Autowired
38    private TemplateRenderer instance;
39   
40    private Map<String, String> properties = new HashMap<>();
41   
42    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
43  1 toggle @BeforeAll
44    static void setUp() {
45  1 Assumptions.assumeTrue(true, "Assumtion failed.");
46   
47  1 LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG);
48    }
49   
 
50  1 toggle @AfterAll
51    static void tearDown() {
52    }
53   
 
54  8 toggle @BeforeEach
55    void testCaseInitialization() {
56    }
57   
 
58  8 toggle @AfterEach
59    void testCaseTermination() {
60    }
61    //</editor-fold>
62   
 
63  1 toggle @Test
64    void constructor() {
65  1 LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG);
66   
67  1 assertThat(VelocityRenderer.class, hasValidBeanConstructor());
68    }
69   
 
70  1 toggle @Test
71    void generateContent() {
72  1 LOGGER.log("TEST CASE: generateContent", LogLevel.DEBUG);
73   
74  1 assertEquals("Hello World?",
75    instance.loadContentByStringResource("Hello World?", null));
76    }
77   
 
78  1 toggle @Test
79    void generateContentWithProperties() {
80  1 LOGGER.log("TEST CASE: generateContentWithProperties", LogLevel.DEBUG);
81   
82  1 instance = new VelocityRenderer();
83  1 if (properties != null) {
84  1 properties.clear();
85    }
86  1 properties.put("property_key", "value");
87   
88  1 assertEquals("Hello World? : value",
89    instance.loadContentByStringResource("Hello World? : $property_key", properties));
90    }
91   
 
92  1 toggle @Test
93    void generateComplexContent() {
94  1 LOGGER.log("TEST CASE: generateContentWithProperties", LogLevel.DEBUG);
95   
96  1 String template = "## single line comment \n"
97    + "#set( $var = \"Velocity\" ) \n"
98    + "Hello $var World?"
99    + "#if($property_key==\"value\") : $property_key #end";
100   
101  1 instance = new VelocityRenderer();
102  1 if (properties != null) {
103  1 properties.clear();
104    }
105  1 properties.put("property_key", "value");
106   
107  1 assertEquals("Hello Velocity World? : value ",
108    instance.loadContentByStringResource(template, properties));
109    }
110   
 
111  1 toggle @Test
112    void loadContentByClasspathResource() {
113  1 LOGGER.log("TEST CASE: loadContentByClasspathResource", LogLevel.DEBUG);
114   
115  1 instance = new VelocityRenderer();
116  1 if (properties != null) {
117  1 properties.clear();
118    }
119  1 properties.put("property_key", "value");
120   
121  1 String result
122    = instance.loadContentByClasspathResource(FILE_PATH, "/TemplateWithProperties.vm", properties);
123  1 assertEquals("Hello World? : value", result);
124    }
125   
 
126  1 toggle @Test
127    void failLoadContentByClasspathResource() throws Exception {
128  1 LOGGER.log("TEST CASE: failLoadContentByClasspathResource", LogLevel.DEBUG);
129   
130  1 instance = new VelocityRenderer();
131  1 assertThrows(Exception.class, () -> {
132  1 instance.loadContentByClasspathResource(FILE_PATH, "notExist.vm", null);
133    });
134    }
135   
 
136  1 toggle @Test
137    void loadContentByFileResource() {
138  1 LOGGER.log("TEST CASE: loadContentByFileResource", LogLevel.DEBUG);
139   
140  1 instance = new VelocityRenderer();
141  1 if (properties != null) {
142  1 properties.clear();
143    }
144  1 properties.put("property_key", "value");
145   
146  1 LOGGER.log("\n PATH: " + DIRECTORY + "\n", LogLevel.ERROR);
147   
148  1 String result
149    = instance.loadContentByFileResource(DIRECTORY, "/TemplateWithProperties.vm", properties);
150  1 assertEquals("Hello World? : value", result);
151    }
152   
 
153  1 toggle @Test
154    void failLoadContentByFileResource() throws Exception {
155  1 LOGGER.log("TEST CASE: failLoadContentByFileResource", LogLevel.DEBUG);
156   
157  1 instance = new VelocityRenderer();
158  1 assertThrows(Exception.class, () -> {
159  1 instance.loadContentByFileResource(DIRECTORY, "/notExist.vm", null);
160    });
161    }
162    }