| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.wiki.workspacesmigrator.internal; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
import java.io.InputStream; |
| 24 |
|
import java.util.ArrayList; |
| 25 |
|
import java.util.List; |
| 26 |
|
|
| 27 |
|
import javax.inject.Provider; |
| 28 |
|
|
| 29 |
|
import org.junit.Before; |
| 30 |
|
import org.junit.Rule; |
| 31 |
|
import org.junit.Test; |
| 32 |
|
import org.xwiki.model.reference.DocumentReference; |
| 33 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 34 |
|
|
| 35 |
|
import com.xpn.xwiki.XWikiContext; |
| 36 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
| 37 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 38 |
|
|
| 39 |
|
import static org.mockito.ArgumentMatchers.any; |
| 40 |
|
import static org.mockito.ArgumentMatchers.eq; |
| 41 |
|
import static org.mockito.Mockito.mock; |
| 42 |
|
import static org.mockito.Mockito.times; |
| 43 |
|
import static org.mockito.Mockito.verify; |
| 44 |
|
import static org.mockito.Mockito.when; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@link |
| 48 |
|
|
| 49 |
|
@since |
| 50 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (49) |
Complexity: 5 |
Complexity Density: 0.11 |
|
| 51 |
|
public class DefaultDocumentRestorerFromAttachedXARTest |
| 52 |
|
{ |
| 53 |
|
@Rule |
| 54 |
|
public MockitoComponentMockingRule<DefaultDocumentRestorerFromAttachedXAR> mocker = |
| 55 |
|
new MockitoComponentMockingRule(DefaultDocumentRestorerFromAttachedXAR.class); |
| 56 |
|
|
| 57 |
|
private Provider<XWikiContext> xcontextProvider; |
| 58 |
|
|
| 59 |
|
private XWikiContext xcontext; |
| 60 |
|
|
| 61 |
|
private com.xpn.xwiki.XWiki xwiki; |
| 62 |
|
|
| 63 |
|
private XWikiDocument docToRestore1; |
| 64 |
|
|
| 65 |
|
private XWikiDocument docToRestore2; |
| 66 |
|
|
| 67 |
|
private List<DocumentReference> docsToRestore; |
| 68 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
|
| 69 |
4 |
@Before... |
| 70 |
|
public void setUp() throws Exception |
| 71 |
|
{ |
| 72 |
4 |
xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
| 73 |
4 |
xcontext = mock(XWikiContext.class); |
| 74 |
4 |
when(xcontextProvider.get()).thenReturn(xcontext); |
| 75 |
4 |
xwiki = mock(com.xpn.xwiki.XWiki.class); |
| 76 |
4 |
when(xcontext.getWiki()).thenReturn(xwiki); |
| 77 |
|
|
| 78 |
|
|
| 79 |
4 |
docToRestore1 = mock(XWikiDocument.class); |
| 80 |
4 |
DocumentReference docToRestoreRef1 = new DocumentReference("workspace", "XWiki", "AdminRegistrationSheet"); |
| 81 |
4 |
when(xwiki.getDocument(eq(docToRestoreRef1), any(XWikiContext.class))).thenReturn(docToRestore1); |
| 82 |
4 |
docToRestore2 = mock(XWikiDocument.class); |
| 83 |
4 |
DocumentReference docToRestoreRef2 = new DocumentReference("workspace", "XWiki", "RegistrationHelp"); |
| 84 |
4 |
when(xwiki.getDocument(eq(docToRestoreRef2), any(XWikiContext.class))).thenReturn(docToRestore2); |
| 85 |
4 |
docsToRestore = new ArrayList<DocumentReference>(); |
| 86 |
4 |
docsToRestore.add(docToRestoreRef1); |
| 87 |
4 |
docsToRestore.add(docToRestoreRef2); |
| 88 |
|
} |
| 89 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
| 90 |
1 |
@Test... |
| 91 |
|
public void restoreDocumentFromAttachedXAR() throws Exception |
| 92 |
|
{ |
| 93 |
|
|
| 94 |
1 |
XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class); |
| 95 |
1 |
when(xwiki.getDocument(eq(new DocumentReference("mainWiki", "WorkspaceManager", "Install")), |
| 96 |
|
any(XWikiContext.class))).thenReturn(workspaceInstallDoc); |
| 97 |
1 |
XWikiAttachment xeXar = mock(XWikiAttachment.class); |
| 98 |
1 |
when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(xeXar); |
| 99 |
1 |
when(xeXar.getContentInputStream(any(XWikiContext.class))).thenReturn( |
| 100 |
|
getClass().getResourceAsStream("/test-restore-documents.xar")); |
| 101 |
|
|
| 102 |
|
|
| 103 |
1 |
mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", |
| 104 |
|
"WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore); |
| 105 |
|
|
| 106 |
|
|
| 107 |
1 |
verify(docToRestore1).fromXML(any(InputStream.class)); |
| 108 |
1 |
verify(xwiki, times(1)).saveDocument(docToRestore1, xcontext); |
| 109 |
1 |
verify(docToRestore2).fromXML(any(InputStream.class)); |
| 110 |
1 |
verify(xwiki, times(1)).saveDocument(docToRestore2, xcontext); |
| 111 |
|
} |
| 112 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 113 |
1 |
@Test... |
| 114 |
|
public void errorWhenNoDocument() throws Exception |
| 115 |
|
{ |
| 116 |
|
|
| 117 |
1 |
XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class); |
| 118 |
1 |
DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install"); |
| 119 |
1 |
when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc); |
| 120 |
1 |
when(workspaceInstallDoc.isNew()).thenReturn(true); |
| 121 |
|
|
| 122 |
|
|
| 123 |
1 |
mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", |
| 124 |
|
"WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore); |
| 125 |
|
|
| 126 |
|
|
| 127 |
1 |
verify(mocker.getMockedLogger()).warn("[{}] does not exist", workspaceInstallDocRef); |
| 128 |
|
} |
| 129 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 130 |
1 |
@Test... |
| 131 |
|
public void errorWhenNoAttachment() throws Exception |
| 132 |
|
{ |
| 133 |
|
|
| 134 |
1 |
XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class); |
| 135 |
1 |
DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install"); |
| 136 |
1 |
when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc); |
| 137 |
1 |
when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(null); |
| 138 |
|
|
| 139 |
|
|
| 140 |
1 |
mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", |
| 141 |
|
"WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore); |
| 142 |
|
|
| 143 |
|
|
| 144 |
1 |
verify(mocker.getMockedLogger()).warn("[{}] has no attachment named [{}].", workspaceInstallDocRef, |
| 145 |
|
"workspace-template.xar"); |
| 146 |
|
} |
| 147 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 148 |
1 |
@Test... |
| 149 |
|
public void errorZipInvalid() throws Exception |
| 150 |
|
{ |
| 151 |
|
|
| 152 |
1 |
XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class); |
| 153 |
1 |
DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install"); |
| 154 |
1 |
when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc); |
| 155 |
1 |
XWikiAttachment xeXar = mock(XWikiAttachment.class); |
| 156 |
1 |
when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(xeXar); |
| 157 |
1 |
when(xeXar.getContentInputStream(any(XWikiContext.class))).thenReturn( |
| 158 |
|
getClass().getResourceAsStream("/invalid-xar.xar")); |
| 159 |
|
|
| 160 |
|
|
| 161 |
1 |
mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", |
| 162 |
|
"WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore); |
| 163 |
|
|
| 164 |
|
|
| 165 |
1 |
verify(mocker.getMockedLogger()).error(eq("Error during the decompression of [{}]."), |
| 166 |
|
eq("workspace-template.xar"), any(IOException.class)); |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
} |