Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources

File ResourceLinkerTest.java

 

Code metrics

0
27
11
1
140
82
11
0.41
2.45
11
1

Classes

Class Line # Actions
ResourceLinkerTest 57 27 0% 11 2
0.9473684494.7%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015 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.module.resources;
35   
36    import static info.magnolia.resourceloader.dummy.DummyResourceOrigin.Differentiator.foo;
37    import static info.magnolia.resourceloader.dummy.DummyResourceOrigin.*;
38    import static org.junit.Assert.assertEquals;
39    import static org.mockito.BDDMockito.given;
40    import static org.mockito.Matchers.any;
41    import static org.mockito.Mockito.mock;
42   
43    import info.magnolia.cms.core.AggregationState;
44    import info.magnolia.objectfactory.guice.GuiceUtils;
45    import info.magnolia.resourceloader.dummy.DummyResourceOrigin;
46    import info.magnolia.templating.functions.TemplatingFunctions;
47   
48    import java.util.Date;
49   
50    import javax.inject.Provider;
51    import javax.jcr.Node;
52   
53    import org.junit.Before;
54    import org.junit.Ignore;
55    import org.junit.Test;
56   
 
57    public class ResourceLinkerTest {
58   
59    private ResourceLinker resourceLinker;
60   
 
61  8 toggle @Before
62    public void setUp() throws Exception {
63    // Ideally, we'd want to check no other methods of AggregationState.getMainContentNode() is called (and even this could ideally go away some time, see comments in ResourceLinker.linkTo()), but I haven't found a simple way to do that with mockito
64    // (defaultAnswer could help but it's verbose)
65  8 final Provider<AggregationState> aggregationStateProvider = GuiceUtils.providerForInstance(mock(AggregationState.class));
66  8 final TemplatingFunctions templatingFunctions = mock(TemplatingFunctions.class);
67  8 given(templatingFunctions.linkPrefix(any(Node.class))).willReturn("/ctxPath");
68   
69    // TODO the deps of RL and servlet are wrong (both have ResourceOrigins), aggState is there twice too, etc
70  8 resourceLinker = new ResourceLinker(new DummyResourceOrigin(foo, constant(new Date(123l)), pathsFor("/foo/bar/qux.css")),
71    GuiceUtils.providerForInstance(new ResourcesModule()),
72    aggregationStateProvider,
73    templatingFunctions);
74    }
75   
 
76  1 toggle @Test
77    public void ignoresExternalLinks() {
78  1 assertEquals("https://magnolia-cms.com", resourceLinker.linkTo("https://magnolia-cms.com", false));
79    }
80   
 
81  1 toggle @Test
82    public void ignoresDotResourcesPrefixForExistingResource() {
83  1 assertEquals("/ctxPath/.resources/foo/bar/qux.css", resourceLinker.linkTo("/.resources/foo/bar/qux.css", false));
84    }
85   
 
86  1 toggle @Test
87    public void prependsDotResourcesPrefixForExistingResourceIfNotSpecified() {
88  1 assertEquals("/ctxPath/.resources/foo/bar/qux.css", resourceLinker.linkTo("/foo/bar/qux.css", false));
89    }
90   
 
91  1 toggle @Test
92    public void ignoresNonExistingResource() {
93  1 assertEquals("/ctxPath/.resources/lol/zor.css", resourceLinker.linkTo("/.resources/lol/zor.css", false));
94  1 assertEquals("/ctxPath/lol/zor.css", resourceLinker.linkTo("/lol/zor.css", false));
95    }
96   
 
97  1 toggle @Test
98    public void farFutureFingerPrintIsAddedForKnownResources() {
99  1 assertEquals("/ctxPath/.resources/foo/bar/qux~1970-01-01-00-00-00-123~cache.css", resourceLinker.linkTo("/foo/bar/qux.css", true));
100  1 assertEquals("/ctxPath/.resources/foo/bar/qux~1970-01-01-00-00-00-123~cache.css", resourceLinker.linkTo("/.resources/foo/bar/qux.css", true));
101    }
102   
 
103  1 toggle @Test
104    public void farFutureFingerPrintCantBeAddedForUnknownResources() {
105    // Should we fake it ?
106  1 assertEquals("/ctxPath/.resources/lol/zor.css", resourceLinker.linkTo("/.resources/lol/zor.css", true));
107  1 assertEquals("/ctxPath/lol/zor.css", resourceLinker.linkTo("/lol/zor.css", true));
108    }
109   
 
110  0 toggle @Ignore("We currently do not support links without protocol even though this style of links is common to be used nowadays")
111    @Test
112    public void linkWithoutProtocol() {
113  0 assertEquals("//static.addtoany.com/menu/page.js", resourceLinker.linkTo("//static.addtoany.com/menu/page.js", false));
114    }
115   
 
116  1 toggle @Test
117    public void servletMappingIsCleanedUp() {
118  1 assertEquals("/.foo/*", newResourceLinkerWithConfiguredDownloadPath("/.foo").getServletMapping());
119  1 assertEquals("/.foo/*", newResourceLinkerWithConfiguredDownloadPath("/.foo/").getServletMapping());
120  1 assertEquals("/.foo/*", newResourceLinkerWithConfiguredDownloadPath(".foo").getServletMapping());
121  1 assertEquals("/.foo/*", newResourceLinkerWithConfiguredDownloadPath(".foo/").getServletMapping());
122  1 assertEquals("/.foo/*", newResourceLinkerWithConfiguredDownloadPath("//.foo///").getServletMapping());
123    }
124   
 
125  1 toggle @Test
126    public void downloadPathPrefixIsCleanedUp() {
127  1 assertEquals("/.foo", newResourceLinkerWithConfiguredDownloadPath("/.foo").cleanDownloadPath());
128  1 assertEquals("/.foo", newResourceLinkerWithConfiguredDownloadPath("/.foo/").cleanDownloadPath());
129  1 assertEquals("/.foo", newResourceLinkerWithConfiguredDownloadPath(".foo").cleanDownloadPath());
130  1 assertEquals("/.foo", newResourceLinkerWithConfiguredDownloadPath(".foo/").cleanDownloadPath());
131  1 assertEquals("/.foo", newResourceLinkerWithConfiguredDownloadPath("//.foo///").cleanDownloadPath());
132    }
133   
 
134  10 toggle private ResourceLinker newResourceLinkerWithConfiguredDownloadPath(String configuredDownloadPath) {
135  10 final ResourcesModule module = new ResourcesModule();
136  10 module.setDownloadPath(configuredDownloadPath);
137  10 return new ResourceLinker(new DummyResourceOrigin(), GuiceUtils.providerForInstance(module), null, null);
138    }
139   
140    }