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.workbench

File ResourcesContainerTest.java

 

Code metrics

0
73
10
1
412
271
10
0.14
7.3
10
1
25.2% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ResourcesContainerTest 68 73 25.2% 10 0
1.0100%
 

Contributing tests

This file is covered by 15 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.workbench;
35   
36    import static info.magnolia.resourceloader.dummy.DummyResourceOrigin.Differentiator.*;
37    import static info.magnolia.test.hamcrest.ExceptionMatcher.instanceOf;
38    import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException;
39    import static java.util.Collections.EMPTY_LIST;
40    import static org.hamcrest.Matchers.*;
41    import static org.junit.Assert.*;
42   
43    import info.magnolia.context.SystemContext;
44    import info.magnolia.jcr.util.NodeTypes;
45    import info.magnolia.resourceloader.ResourceOrigin;
46    import info.magnolia.resourceloader.dummy.DummyResourceOrigin;
47    import info.magnolia.resourceloader.jcr.JcrResourceOriginFactory;
48    import info.magnolia.resourceloader.layered.LayeredResourceOrigin;
49    import info.magnolia.resourceloader.layered.LayeredResourceOriginFactory;
50    import info.magnolia.test.hamcrest.Execution;
51    import info.magnolia.test.mock.MockContext;
52    import info.magnolia.test.mock.jcr.MockSession;
53   
54    import java.util.Collection;
55    import java.util.Collections;
56    import java.util.List;
57   
58    import org.junit.Test;
59   
60    import com.google.inject.util.Providers;
61    import com.vaadin.data.Item;
62   
63    /**
64    * Tests for the {@link ResourcesContainer}.
65    * <p>
66    * Note that this container is implemented with collapsible strategy in mind, hence test cases may not represent the origin itself, but rather the current state of a partially visible tree.
67    */
 
68    public class ResourcesContainerTest {
69   
 
70  1 toggle @Test
71    public void cacheMechanismWorks() {
72    // GIVEN
73  1 DummyResourceOrigin origin = new DummyResourceOrigin(
74    "/apples",
75    "/bananas",
76    "/cherries",
77    "/durians",
78    "/eggplants"
79    );
80  1 ResourcesContainer container = new ResourcesContainer(origin, EMPTY_LIST);
81   
82    // WHEN
83  1 final Item apples = container.getItem("/apples");
84  1 final Item cachedApples = container.getItem("/apples");
85   
86  1 final Item bananas = container.getItem("/bananas");
87  1 final Item cachedBananas = container.getItem("/bananas");
88   
89  1 container.refresh();
90   
91  1 final Item refreshedApples = container.getItem("/apples");
92  1 final Item refreshedBananas = container.getItem("/apples");
93   
94    // THEN
95  1 assertThat(apples, sameInstance(cachedApples));
96  1 assertThat(bananas, sameInstance(cachedBananas));
97  1 assertThat(apples, not(sameInstance(refreshedApples)));
98  1 assertThat(bananas, not(sameInstance(refreshedBananas)));
99    }
100   
101    // CONTAINER.INDEXED
102   
 
103    toggle @Test
104    public void getItemIdsWithRangeIncludesLastEntryAndDoesntGoOutOfBounds() {
105    // GIVEN
106    DummyResourceOrigin origin = new DummyResourceOrigin(
107    "/apples",
108    "/bananas",
109    "/cherries",
110    "/durians",
111    "/eggplants"
112    );
113    ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
114   
115    // WHEN
116    List<String> itemIds = container.getItemIds(3, 3);
117   
118    // THEN - no IndexOutOfBoundsException, correctly goes to the end of the list (underlying subList's high endpoint is exclusive).
119    assertThat(itemIds, contains(
120    "/durians",
121    "/eggplants"
122    ));
123    }
124   
125    // CONTAINER.HIERARCHICAL
126   
 
127    toggle @Test
128    public void getParentReturnsNullForContainerRoots() {
129    // GIVEN — we don't have the origin's actual root in the container, only 1st level children are considered roots
130    DummyResourceOrigin origin = new DummyResourceOrigin(
131    "/apples",
132    "/apples/a1"
133    );
134    ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
135   
136    // WHEN/THEN
137    assertNull(container.getParent("/"));
138    assertNull(container.getParent("/apples"));
139    assertNotNull(container.getParent("/apples/a1"));
140    }
141   
 
142    toggle @Test
143    public void getParentDoesntReturnNullIndefinitelyForNullInput() {
144    // GIVEN — simulates a case where the tree can fall into an infinite loop
145    // see com.vaadin.ui.TreeTable.AbstractStrategy.getDepth(Object)
146    DummyResourceOrigin origin = new DummyResourceOrigin();
147    final ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
148   
149    // WHEN/THEN
150    assertThat(new Execution() {
 
151    toggle @Override
152    public void evaluate() throws Exception {
153    container.getParent(null);
154    }
155    }, throwsAnException(instanceOf(IllegalArgumentException.class).withMessage("The provided itemId is null.")));
156    }
157   
 
158  1 toggle @Test
159    public void rootItemIdsDoesntIncludeOriginRoot() {
160    // GIVEN — we don't have the origin's actual root in the container, only 1st level children are considered roots
161  1 DummyResourceOrigin origin = new DummyResourceOrigin("/apples");
162  1 ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
163   
164    // WHEN
165  1 Collection<String> itemIds = container.rootItemIds();
166   
167    // THEN
168  1 assertThat(itemIds, not(hasItem(("/"))));
169    }
170   
 
171    toggle @Test
172    public void isRootIsOnlyTrueForContainerRoots() {
173    // GIVEN — we don't have the origin's actual root in the container, only 1st level children are considered roots
174    DummyResourceOrigin origin = new DummyResourceOrigin(
175    "/apples",
176    "/apples/a1"
177    );
178    ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
179   
180    // WHEN/THEN
181    assertFalse(container.isRoot("/"));
182    assertTrue(container.isRoot("/apples"));
183    assertFalse(container.isRoot("/apples/a1"));
184    }
185   
186    // COLLAPSIBLE
187   
 
188  1 toggle @Test
189    public void setExpandedLazilyLoadsAdditionalResourcesFromOrigin() {
190    // GIVEN
191  1 final DummyResourceOrigin origin = new DummyResourceOrigin(
192    "/apples",
193    "/apples/a1",
194    "/apples/a1/a11",
195    "/apples/a2",
196    "/bananas",
197    "/bananas/b1"
198    );
199  1 ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
200  1 assertThat(container.getItemIds(), contains(
201    "/apples",
202    "/bananas"
203    ));
204  1 String directory = "/apples";
205   
206    // WHEN
207  1 container.setCollapsed(directory, false);
208   
209    // THEN
210  1 assertThat(container.getItemIds(), contains(
211    "/apples",
212    "/apples/a1",
213    "/apples/a2",
214    "/bananas"
215    ));
216    }
217   
 
218  1 toggle @Test
219    public void setCollapsedFreesResourcePathsRecursively() {
220    // GIVEN
221  1 DummyResourceOrigin origin = new DummyResourceOrigin(
222    "/apples",
223    "/apples/a1",
224    "/apples/a1/a11",
225    "/apples/a2",
226    "/bananas"
227    );
228  1 ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
229  1 container.setCollapsed("/apples", false);
230  1 container.setCollapsed("/apples/a1", false);
231  1 assertThat(container.getItemIds(), contains(
232    "/apples",
233    "/apples/a1",
234    "/apples/a1/a11",
235    "/apples/a2",
236    "/bananas"
237    ));
238   
239    // WHEN
240  1 container.setCollapsed("/apples", true);
241   
242    // THEN
243  1 assertThat(container.getItemIds(), contains(
244    "/apples",
245    "/bananas"
246    ));
247    }
248   
 
249  1 toggle @Test
250    public void setExpandedReExpandsElementsThatWereOnceExpanded() {
251    // GIVEN
252  1 DummyResourceOrigin origin = new DummyResourceOrigin(
253    "/apples",
254    "/apples/a1",
255    "/apples/a1/a11",
256    "/apples/a2",
257    "/bananas"
258    );
259  1 ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
260  1 container.setCollapsed("/apples", false);
261  1 container.setCollapsed("/apples/a1", false);
262  1 container.setCollapsed("/apples", true);
263  1 assertThat(container.getItemIds(), contains(
264    "/apples",
265    "/bananas"
266    ));
267   
268    // WHEN
269  1 container.setCollapsed("/apples", false);
270   
271    // THEN
272  1 assertThat(container.getItemIds(), contains(
273    "/apples",
274    "/apples/a1",
275    "/apples/a1/a11",
276    "/apples/a2",
277    "/bananas"
278    ));
279    }
280   
 
281  1 toggle @Test
282    public void setExpandedTwiceDoesntDuplicatePaths() {
283    // GIVEN
284  1 DummyResourceOrigin origin = new DummyResourceOrigin(
285    "/apples",
286    "/apples/a1"
287    );
288  1 ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
289  1 container.setCollapsed("/apples", false);
290  1 assertThat(container.getItemIds(), contains(
291    "/apples",
292    "/apples/a1"
293    ));
294   
295    // WHEN
296  1 container.setCollapsed("/apples", false);
297   
298    // THEN
299  1 assertThat(container.getItemIds(), contains(
300    "/apples",
301    "/apples/a1"
302    ));
303    }
304   
 
305  1 toggle @Test
306    public void setCollapsedDoesntGoOutOfBoundsWhenCollapsingLastRootId() {
307    // GIVEN
308  1 DummyResourceOrigin origin = new DummyResourceOrigin(
309    "/apples",
310    "/bananas",
311    "/bananas/b1",
312    "/bananas/b2"
313    );
314  1 ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
315  1 container.setCollapsed("/bananas", false);
316   
317    // WHEN
318  1 container.setCollapsed("/bananas", true);
319   
320    // THEN
321  1 assertThat(container.getItemIds(), contains(
322    "/apples",
323    "/bananas"
324    ));
325    }
326   
 
327  1 toggle @Test
328    public void setCollapsedSupportsExpandingAndCollapsingItemsWithoutChildren() {
329    // GIVEN - simulates a case where origin could have changed in the background, we mostly expect it not to crash
330  1 DummyResourceOrigin origin = new DummyResourceOrigin(
331    "/apples",
332    "/bananas"
333    );
334  1 ResourcesContainer container = new ResourcesContainer(origin, Collections.EMPTY_LIST);
335   
336    // WHEN
337  1 container.setCollapsed("/bananas", false);
338  1 container.setCollapsed("/bananas", true);
339   
340    // THEN
341  1 assertThat(container.getItemIds(), contains(
342    "/apples",
343    "/bananas"
344    ));
345    }
346   
 
347    toggle @Test
348    public void isOverriding() {
349    // GIVEN
350    LayeredResourceOrigin origin = new LayeredResourceOriginFactory().create("layered", new ResourceOrigin[]{
351    new DummyResourceOrigin(foo, "/a", "/c"),
352    new DummyResourceOrigin(bar, "/a", "/b"),
353    });
354   
355    // WHEN/THEN
356    assertThat(ResourcesContainer.isOverriding(origin.getByPath("/a")), is(true));
357    assertThat(ResourcesContainer.isOverriding(origin.getByPath("/b")), is(false));
358    assertThat(ResourcesContainer.isOverriding(origin.getByPath("/c")), is(false));
359    }
360   
 
361  1 toggle @Test
362    public void setCollapsedExpandsParentsAndPreservesCorrectIndices() throws Exception {
363    // GIVEN
364  1 LayeredResourceOrigin origin = new LayeredResourceOriginFactory().create("layered", new ResourceOrigin[]{
365    new DummyResourceOrigin(foo,
366    "/a",
367    "/a/b",
368    "/a/b/c",
369    "/d",
370    "/d/e")
371    });
372   
373  1 final ResourcesContainer container = new ResourcesContainer(origin, Collections.<String>emptyList());
374  1 assertThat(container.isCollapsed("/a/b"), is(true));
375  1 assertThat(container.isCollapsed("/a"), is(true));
376   
377    // WHEN
378  1 container.setCollapsed("/a/b/c", false);
379   
380    // THEN
381  1 assertThat(container.isCollapsed("/a/b/c"), is(false));
382  1 assertThat(container.isCollapsed("/a/b"), is(false));
383  1 assertThat(container.isCollapsed("/a"), is(false));
384   
385  1 assertThat(container.indexOfId("/a/b/c"), equalTo(2));
386    }
387   
 
388  1 toggle @Test
389    public void onlyPureJcrFoldersAreEditable() throws Exception {
390    // GIVEN
391  1 final MockSession session = new MockSession("resources");
392  1 session.getRootNode().addNode("a", NodeTypes.Folder.NAME);
393  1 session.getRootNode().addNode("b", NodeTypes.Folder.NAME);
394   
395  1 final MockContext ctx = new MockContext();
396  1 ctx.addSession("resources", session);
397   
398  1 LayeredResourceOrigin origin = new LayeredResourceOriginFactory().create("layered", new ResourceOrigin[]{
399    new DummyResourceOrigin(foo, "/a", "/c"),
400    new JcrResourceOriginFactory(Providers.<SystemContext>of(ctx)).create("jcr")
401    });
402  1 final ResourcesContainer container = new ResourcesContainer(origin, Collections.<String>emptyList());
403   
404    // THEN
405    // Folders that aren't related to JCR are not considered editable
406  1 assertThat((Boolean) container.getItem("/c").getItemProperty(ResourcesContainer.EDITABLE_PROPERTY_ID).getValue(), is(false));
407    // Overridden folders are not considered editable
408  1 assertThat((Boolean) container.getItem("/a").getItemProperty(ResourcesContainer.EDITABLE_PROPERTY_ID).getValue(), is(false));
409    // Jcr-only folders are considered editable
410  1 assertThat((Boolean) container.getItem("/b").getItemProperty(ResourcesContainer.EDITABLE_PROPERTY_ID).getValue(), is(true));
411    }
412    }