Clover icon

Magnolia Resources templating Module 2.6.1

  1. Project Clover database Thu Feb 15 2018 14:24:59 CET
  2. Package info.magnolia.modules.resources.templating

File ResourcesTemplatingFunctionsTest.java

 

Code metrics

0
30
11
1
218
126
11
0.37
2.73
11
1

Classes

Class Line # Actions
ResourcesTemplatingFunctionsTest 59 30 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2016-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.modules.resources.templating;
35   
36   
37    import static org.hamcrest.MatcherAssert.assertThat;
38    import static org.hamcrest.Matchers.*;
39    import static org.mockito.Mockito.*;
40   
41    import info.magnolia.context.Context;
42    import info.magnolia.context.WebContext;
43    import info.magnolia.module.resources.ResourceLinker;
44    import info.magnolia.module.resources.ResourcesModule;
45    import info.magnolia.objectfactory.guice.GuiceUtils;
46    import info.magnolia.resourceloader.dummy.DummyResourceOrigin;
47   
48    import java.util.Arrays;
49    import java.util.List;
50   
51    import javax.inject.Provider;
52   
53    import org.junit.Before;
54    import org.junit.Test;
55   
56    /**
57    * A class to test {@link ResourcesTemplatingFunctions}.
58    */
 
59    public class ResourcesTemplatingFunctionsTest {
60   
61    private ResourcesTemplatingFunctions resfn;
62   
 
63  8 toggle @Before
64    public void setUp() throws Exception {
65   
66    /*
67    * A dummy ResourceOrigin which has 2 modules.
68    * - foobar-module with 2 css files, 2 js files and 2 files with different suffix.
69    * - module2 with one css and one js file.
70    */
71  8 DummyResourceOrigin dummyResourceOrigin = new DummyResourceOrigin(
72    "/foobar-module",
73    "/foobar-module/webresources",
74    "/foobar-module/webresources/css",
75    "/foobar-module/webresources/css/a.css",
76    "/foobar-module/webresources/css/b.css",
77    "/foobar-module/webresources/css/wrong.txt",
78    "/foobar-module/webresources/js",
79    "/foobar-module/webresources/js/a.js",
80    "/foobar-module/webresources/js/b.js",
81    "/foobar-module/webresources/js/bad.jsp",
82    "/module2",
83    "/module2/file.js",
84    "/module2/file.css"
85    );
86   
87  8 WebContext context = mock(WebContext.class);
88  8 final Provider<Context> contextProvider = GuiceUtils.providerForInstance(context);
89  8 doReturn("/ctxPath").when(context).getContextPath();
90  8 ResourceLinker resourceLinker = new ResourceLinker(dummyResourceOrigin,
91    GuiceUtils.providerForInstance(new ResourcesModule()), contextProvider) {
92   
 
93  4 toggle @Override
94    protected String fingerPrintFor(info.magnolia.resourceloader.Resource resource) {
95  4 return "1968-01-24-18-35-00-000";
96    }
97   
98    };
99   
100  8 resfn = new ResourcesTemplatingFunctions(dummyResourceOrigin, resourceLinker);
101    }
102   
 
103  1 toggle @Test
104    public void cssOnePattern() {
105    // when
106  1 String foobarCssLinks = resfn.css("/foobar-module/webresources/css/.*css");
107  1 String allCssLinks = resfn.css(".*.css");
108  1 String txtLinks = resfn.css(".*.txt");
109   
110    // then
111  1 assertThat(splitResFnResult(foobarCssLinks), containsInAnyOrder(
112    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>",
113    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/b.css\"/>"
114    ));
115  1 assertThat(splitResFnResult(allCssLinks), containsInAnyOrder(
116    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>",
117    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/b.css\"/>",
118    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/module2/file.css\"/>"
119    ));
120  1 assertThat(splitResFnResult(txtLinks), containsInAnyOrder(
121    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/wrong.txt\"/>"
122    ));
123    }
124   
 
125  1 toggle @Test
126    public void onePatternNoMatch() {
127    // when
128  1 String noMatch = resfn.css("/tarzan.*.css");
129    // then
130  1 assertThat(noMatch, isEmptyOrNullString());
131    }
132   
 
133  1 toggle @Test
134    public void cssPatternList() {
135    // when
136  1 String cssFrom2Modules = resfn.css(Arrays.asList("/foobar.*.css", "/module2.*.css"));
137   
138    // then
139  1 assertThat(splitResFnResult(cssFrom2Modules), containsInAnyOrder(
140    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>",
141    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/b.css\"/>",
142    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/module2/file.css\"/>"
143    ));
144    }
145   
 
146  1 toggle @Test
147    public void cssWithOtherAttributes() {
148    // when
149  1 String linkWithAttributeSingleQuoted = resfn.css("/foobar-module.*a.css", "media='all'");
150  1 String linkWithAttributeDoubleQuoted = resfn.css("/foobar-module.*a.css", "media=\"all\"");
151   
152    // then
153  1 assertThat(splitResFnResult(linkWithAttributeSingleQuoted), containsInAnyOrder(
154    "<link rel=\"stylesheet\" type=\"text/css\" media='all' href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>"
155    ));
156  1 assertThat(splitResFnResult(linkWithAttributeDoubleQuoted), containsInAnyOrder(
157    "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>"
158    ));
159    }
160   
 
161  1 toggle @Test
162    public void cachedCss() {
163    // when
164  1 String foobarCssLinks = resfn.cachedCss("/foobar-module/webresources/css/.*css");
165   
166    // then
167  1 assertThat(splitResFnResult(foobarCssLinks), containsInAnyOrder(
168    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a~1968-01-24-18-35-00-000~cache.css\"/>",
169    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/b~1968-01-24-18-35-00-000~cache.css\"/>"
170    ));
171    }
172   
 
173  1 toggle @Test
174    public void jsOnePattern() {
175    // when
176  1 String fooBarJsLinks = resfn.js("/foobar.*.js");
177   
178    // then
179  1 assertThat(splitResFnResult(fooBarJsLinks), containsInAnyOrder(
180    "<script src=\"/ctxPath/.resources/foobar-module/webresources/js/a.js\"></script>",
181    "<script src=\"/ctxPath/.resources/foobar-module/webresources/js/b.js\"></script>"
182    ));
183    }
184   
 
185  1 toggle @Test
186    public void jsPatternList() {
187    // when
188  1 String jsFrom2Modules = resfn.js(Arrays.asList("/foobar.*.js", "/module2.*.js"));
189   
190    // then
191  1 assertThat(splitResFnResult(jsFrom2Modules), containsInAnyOrder(
192    "<script src=\"/ctxPath/.resources/foobar-module/webresources/js/a.js\"></script>",
193    "<script src=\"/ctxPath/.resources/foobar-module/webresources/js/b.js\"></script>",
194    "<script src=\"/ctxPath/.resources/module2/file.js\"></script>"
195    ));
196   
197    }
198   
 
199  1 toggle @Test
200    public void cachedJs() {
201    // when
202  1 String fooBarJsLinksCached = resfn.cachedJs("/foobar.*.js");
203   
204    // then
205  1 assertThat(splitResFnResult(fooBarJsLinksCached), containsInAnyOrder(
206    "<script src=\"/ctxPath/.resources/foobar-module/webresources/js/a~1968-01-24-18-35-00-000~cache.js\"></script>",
207    "<script src=\"/ctxPath/.resources/foobar-module/webresources/js/b~1968-01-24-18-35-00-000~cache.js\"></script>"
208    ));
209    }
210   
211    /**
212    * This method splits the result of a resfn method into a list which contains the links to css or js files in order to use hamcrest matcher which expects a list.
213    */
 
214  10 toggle private List<String> splitResFnResult(String fullResfnResult) {
215  10 return Arrays.asList(fullResfnResult.split("\\n"));
216    }
217   
218    }