Clover icon

Magnolia Resources App Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:17:22 CET
  2. Package info.magnolia.resources.app.availability

File IsJcrOnlyResourceRuleTest.java

 

Code metrics

0
30
5
1
143
71
5
0.17
6
5
1

Classes

Class Line # Actions
IsJcrOnlyResourceRuleTest 58 30 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 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.resources.app.availability;
35   
36    import static info.magnolia.resourceloader.dummy.DummyResourceOrigin.Differentiator.qux;
37    import static org.hamcrest.MatcherAssert.assertThat;
38    import static org.hamcrest.core.Is.is;
39   
40    import info.magnolia.context.SystemContext;
41    import info.magnolia.jcr.util.NodeTypes;
42    import info.magnolia.resourceloader.ResourceOrigin;
43    import info.magnolia.resourceloader.dummy.DummyResourceOrigin;
44    import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
45    import info.magnolia.resourceloader.jcr.JcrResourceOriginFactory;
46    import info.magnolia.resourceloader.layered.LayeredResourceOrigin;
47    import info.magnolia.resourceloader.layered.LayeredResourceOriginFactory;
48    import info.magnolia.test.mock.MockContext;
49    import info.magnolia.test.mock.jcr.MockSession;
50   
51    import javax.jcr.Node;
52   
53    import org.junit.Before;
54    import org.junit.Test;
55   
56    import com.google.inject.util.Providers;
57   
 
58    public class IsJcrOnlyResourceRuleTest {
59   
60    private IsJcrOnlyResourceRule isJcrOnlyResourceRule;
61   
62    private JcrResourceOrigin jcrResourceOrigin;
63    private IsJcrOnlyResourceRuleDefinition ruleDefinition;
64    private LayeredResourceOrigin layeredResourceOrigin;
65   
 
66  4 toggle @Before
67    public void setUp() throws Exception {
68  4 final MockSession session = new MockSession("resources");
69  4 final MockContext ctx = new MockContext();
70  4 ctx.addSession("resources", session);
71   
72  4 final Node folderA = session.getRootNode().addNode("a", NodeTypes.Folder.NAME);
73  4 folderA.addNode("foo.txt", NodeTypes.Content.NAME);
74  4 folderA.addNode("qux.txt", NodeTypes.Content.NAME);
75   
76  4 session.getRootNode().addNode("b", NodeTypes.Folder.NAME).addNode("bar.txt", NodeTypes.Content.NAME);
77   
78  4 DummyResourceOrigin dummyResourceOrigin = new DummyResourceOrigin(qux, "/a", "/a/foo.txt", "/c", "/c/baz.txt");
79  4 this.jcrResourceOrigin = new JcrResourceOriginFactory(Providers.<SystemContext>of(ctx)).create("jcr");
80  4 this.layeredResourceOrigin = new LayeredResourceOriginFactory().create("layered", new ResourceOrigin[]{
81    dummyResourceOrigin,
82    jcrResourceOrigin,
83    });
84   
85  4 this.ruleDefinition = new IsJcrOnlyResourceRuleDefinition();
86    }
87   
 
88  1 toggle @Test
89    public void nonLayeredJcrResourcePassesRule() throws Exception {
90    // GIVEN
91  1 isJcrOnlyResourceRule = new IsJcrOnlyResourceRule(jcrResourceOrigin, ruleDefinition);
92   
93    // WHEN
94  1 boolean barIsPassing = isJcrOnlyResourceRule.isAvailableForItem("/b/bar.txt");
95  1 boolean fooIsPassing = isJcrOnlyResourceRule.isAvailableForItem("/a/foo.txt");
96   
97    // THEN
98  1 assertThat(barIsPassing, is(true));
99  1 assertThat(fooIsPassing, is(true));
100    }
101   
 
102  1 toggle @Test
103    public void jcrOnlyResourceFromLayeredOriginPassesRule() throws Exception {
104    // GIVEN
105  1 isJcrOnlyResourceRule = new IsJcrOnlyResourceRule(layeredResourceOrigin, ruleDefinition);
106   
107    // WHEN
108  1 boolean barIsPassing = isJcrOnlyResourceRule.isAvailableForItem("/b/bar.txt");
109   
110    // THEN
111  1 assertThat(barIsPassing, is(true));
112    }
113   
 
114  1 toggle @Test
115    public void overriddenResourceDoesNotPassRule() throws Exception {
116    // GIVEN
117  1 isJcrOnlyResourceRule = new IsJcrOnlyResourceRule(layeredResourceOrigin, ruleDefinition);
118   
119    // WHEN
120  1 boolean fooIsPassing = isJcrOnlyResourceRule.isAvailableForItem("/a/foo.txt");
121   
122    // THEN
123  1 assertThat(fooIsPassing, is(false));
124    }
125   
 
126  1 toggle @Test
127    public void turningNegationOnInDefinitionInvertsResult() throws Exception {
128    // GIVEN
129  1 isJcrOnlyResourceRule = new IsJcrOnlyResourceRule(layeredResourceOrigin, ruleDefinition);
130  1 final IsJcrOnlyResourceRuleDefinition negatedRuleDefinition = new IsJcrOnlyResourceRuleDefinition();
131  1 negatedRuleDefinition.setNegateResult(true);
132   
133  1 final IsJcrOnlyResourceRule negatedRule = new IsJcrOnlyResourceRule(layeredResourceOrigin, negatedRuleDefinition);
134   
135    // WHEN
136  1 boolean barIsPassingStraightRule = isJcrOnlyResourceRule.isAvailableForItem("/b/bar.txt");
137  1 boolean barIsPassingNegatedRule = negatedRule.isAvailableForItem("/b/bar.txt");
138   
139    // THEN
140  1 assertThat(barIsPassingStraightRule, is(true));
141  1 assertThat(barIsPassingNegatedRule, is(false));
142    }
143    }