1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal.thread.context

File XWikiContextCopierTest.java

 

Code metrics

0
39
3
1
162
93
3
0.08
13
3
1

Classes

Class Line # Actions
XWikiContextCopierTest 58 39 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.mail.internal.thread.context;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.mockito.invocation.InvocationOnMock;
26    import org.mockito.stubbing.Answer;
27    import org.xwiki.component.util.DefaultParameterizedType;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30    import org.xwiki.model.reference.EntityReferenceSerializer;
31    import org.xwiki.test.mockito.MockitoComponentMockingRule;
32   
33    import com.xpn.xwiki.XWiki;
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.store.XWikiStoreInterface;
36    import com.xpn.xwiki.web.Utils;
37    import com.xpn.xwiki.web.XWikiRequest;
38    import com.xpn.xwiki.web.XWikiServletResponseStub;
39    import com.xpn.xwiki.web.XWikiURLFactory;
40    import com.xpn.xwiki.web.XWikiURLFactoryService;
41   
42    import static org.junit.Assert.assertEquals;
43    import static org.junit.Assert.assertNotNull;
44    import static org.junit.Assert.assertNotSame;
45    import static org.junit.Assert.assertNull;
46    import static org.junit.Assert.assertSame;
47    import static org.mockito.ArgumentMatchers.any;
48    import static org.mockito.ArgumentMatchers.anyInt;
49    import static org.mockito.Mockito.doAnswer;
50    import static org.mockito.Mockito.mock;
51    import static org.mockito.Mockito.when;
52   
53    /**
54    * Unit tests for {@link XWikiContextCopier}.
55    *
56    * @version $Id: 90c095b3facd59981958f5166c6c0d15b426cbb0 $
57    */
 
58    public class XWikiContextCopierTest
59    {
60    private static final String HIBSESSION = "hibsession";
61   
62    @Rule
63    public MockitoComponentMockingRule<XWikiContextCopier> mocker = new MockitoComponentMockingRule<>(
64    XWikiContextCopier.class);
65   
66    XWikiServletResponseStub originalResponse;
67   
68    XWikiRequest originalRequest;
69   
70    XWikiStoreInterface store;
71   
72    XWikiContext original;
73   
 
74  1 toggle @Before
75    public void setup() throws Exception
76    {
77  1 Utils.setComponentManager(mocker);
78   
79  1 originalResponse = new XWikiServletResponseStub();
80   
81  1 original = new XWikiContext();
82   
83    // Set some values
84  1 original.setWikiId("wiki");
85   
86  1 DocumentReference userReference = new DocumentReference("wiki", "Space", "Page");
87  1 EntityReferenceSerializer<String> serializer =
88    mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
89  1 when(serializer.serialize(userReference)).thenReturn("wiki:Space.Page");
90   
91  1 mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
92   
93  1 original.setUserReference(userReference);
94   
95    // Set the mock request
96  1 this.originalRequest = mock(XWikiRequest.class);
97  1 original.setRequest(this.originalRequest);
98  1 Copier<XWikiRequest> requestCopier =
99    mocker.getInstance(new DefaultParameterizedType(null, Copier.class, XWikiRequest.class));
100  1 when(requestCopier.copy(this.originalRequest)).thenReturn(this.originalRequest);
101   
102    // Set the stubbed response
103  1 original.setResponse(originalResponse);
104   
105    // XWiki mock
106  1 XWiki xwiki = mock(XWiki.class);
107  1 original.setWiki(xwiki);
108   
109    // Store mock
110    // Simulate the existence of a hibernate session in context
111  1 original.put(HIBSESSION,"opened session");
112  1 store = mock(XWikiStoreInterface.class);
113    // clean up will remove the session in the given context
114  1 doAnswer(new Answer<Void>() {
 
115  1 toggle public Void answer(InvocationOnMock invocation) {
116  1 XWikiContext context = (XWikiContext) invocation.getArguments()[0];
117  1 context.put(HIBSESSION, null);
118  1 return null;
119    }
120    }).when(store).cleanUp(any(XWikiContext.class));
121  1 when(xwiki.getStore()).thenReturn(store);
122   
123    // URL factory mock
124  1 XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
125   
126  1 XWikiURLFactoryService urlFactoryService = mock(XWikiURLFactoryService.class);
127  1 when(urlFactoryService.createURLFactory(anyInt(), any(XWikiContext.class))).thenReturn(
128    urlFactory);
129  1 when(xwiki.getURLFactoryService()).thenReturn(urlFactoryService);
130    }
131   
 
132  1 toggle @Test
133    public void copyContext() throws Exception
134    {
135  1 XWikiContext copy = mocker.getComponentUnderTest().copy(original);
136   
137    // Check that the response is not the same.
138  1 assertNotSame(originalResponse, copy.getResponse());
139   
140    // Check that the context values are cloned.
141  1 assertEquals(original.getUserReference(), copy.getUserReference());
142  1 assertEquals(original.getWikiId(), copy.getWikiId());
143    // No URL was present in the original context so a stub is used.
144    // Note: for some reason, comparing 2 URLs takes ages (~19 seconds) on my machine. Comparing strings instead for
145    // performance.
146  1 assertEquals("http://www.mystuburl.com/", copy.getURL().toString());
147    // Actually, all the context keys should be copied.
148  1 assertNotSame(original.entrySet(), copy.entrySet());
149  1 assertEquals(original.entrySet(), copy.entrySet());
150   
151    // Some things are not cloned.
152  1 assertSame(original.getWiki(), copy.getWiki());
153   
154    // Verify that the store session has been cleaned for both context.
155  1 assertNull(original.get(HIBSESSION));
156  1 assertNull(copy.get(HIBSESSION));
157   
158    // Check that the URLFactory is cloned.
159  1 assertNotNull(copy.getURLFactory());
160  1 assertNotSame(original.getURLFactory(), copy.getURLFactory());
161    }
162    }