| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 55 |
|
|
| 56 |
|
@version |
| 57 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (42) |
Complexity: 3 |
Complexity Density: 0.08 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 1 |
Complexity Density: 0.04 |
|
| 74 |
1 |
@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 |
|
|
| 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 |
|
|
| 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 |
|
|
| 103 |
1 |
original.setResponse(originalResponse); |
| 104 |
|
|
| 105 |
|
|
| 106 |
1 |
XWiki xwiki = mock(XWiki.class); |
| 107 |
1 |
original.setWiki(xwiki); |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
1 |
original.put(HIBSESSION,"opened session"); |
| 112 |
1 |
store = mock(XWikiStoreInterface.class); |
| 113 |
|
|
| 114 |
1 |
doAnswer(new Answer<Void>() { |
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 115 |
1 |
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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 132 |
1 |
@Test... |
| 133 |
|
public void copyContext() throws Exception |
| 134 |
|
{ |
| 135 |
1 |
XWikiContext copy = mocker.getComponentUnderTest().copy(original); |
| 136 |
|
|
| 137 |
|
|
| 138 |
1 |
assertNotSame(originalResponse, copy.getResponse()); |
| 139 |
|
|
| 140 |
|
|
| 141 |
1 |
assertEquals(original.getUserReference(), copy.getUserReference()); |
| 142 |
1 |
assertEquals(original.getWikiId(), copy.getWikiId()); |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
1 |
assertEquals("http://www.mystuburl.com/", copy.getURL().toString()); |
| 147 |
|
|
| 148 |
1 |
assertNotSame(original.entrySet(), copy.entrySet()); |
| 149 |
1 |
assertEquals(original.entrySet(), copy.entrySet()); |
| 150 |
|
|
| 151 |
|
|
| 152 |
1 |
assertSame(original.getWiki(), copy.getWiki()); |
| 153 |
|
|
| 154 |
|
|
| 155 |
1 |
assertNull(original.get(HIBSESSION)); |
| 156 |
1 |
assertNull(copy.get(HIBSESSION)); |
| 157 |
|
|
| 158 |
|
|
| 159 |
1 |
assertNotNull(copy.getURLFactory()); |
| 160 |
1 |
assertNotSame(original.getURLFactory(), copy.getURLFactory()); |
| 161 |
|
} |
| 162 |
|
} |