1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.signature.internal

File MacroBlockSignatureGeneratorTest.java

 
testIncompatibleBlockSignature: Unsupported block [org.xwiki.rendering.block.WordBlock].
 

Code metrics

0
15
5
1
114
75
5
0.33
3
5
1

Classes

Class Line # Actions
MacroBlockSignatureGeneratorTest 55 15 0% 5 9
0.5555%
 

Contributing tests

This file is covered by 3 tests. .

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.rendering.signature.internal;
21   
22    import java.io.IOException;
23    import java.io.OutputStream;
24    import java.util.Collections;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.junit.rules.ExpectedException;
30    import org.mockito.invocation.InvocationOnMock;
31    import org.mockito.stubbing.Answer;
32    import org.xwiki.crypto.signer.CMSSignedDataGenerator;
33    import org.xwiki.crypto.signer.param.CMSSignedDataGeneratorParameters;
34    import org.xwiki.rendering.block.Block;
35    import org.xwiki.rendering.block.MacroBlock;
36    import org.xwiki.rendering.block.MacroMarkerBlock;
37    import org.xwiki.rendering.block.WordBlock;
38    import org.xwiki.rendering.signature.BlockSignatureGenerator;
39    import org.xwiki.test.mockito.MockitoComponentMockingRule;
40   
41    import static org.hamcrest.core.IsEqual.equalTo;
42    import static org.junit.Assert.assertThat;
43    import static org.mockito.AdditionalMatchers.or;
44    import static org.mockito.ArgumentMatchers.any;
45    import static org.mockito.ArgumentMatchers.eq;
46    import static org.mockito.Mockito.doAnswer;
47    import static org.mockito.Mockito.when;
48   
49    /**
50    * Unit test for {@link MacroBlockSignatureGenerator}.
51    *
52    * @version $Id: d18ec7f4966ba3924ac5fd4ebe3a83db21e89af1 $
53    * @since 6.1M2
54    */
 
55    public class MacroBlockSignatureGeneratorTest
56    {
57    @Rule
58    public final MockitoComponentMockingRule<BlockSignatureGenerator> mocker =
59    new MockitoComponentMockingRule<BlockSignatureGenerator>(MacroBlockSignatureGenerator.class);
60   
61    @Rule
62    public ExpectedException thrown = ExpectedException.none();
63   
64    private static final Block MACRO_BLOCK = new MacroBlock("macro", Collections.<String, String>emptyMap(), "content", false);
65    private static final Block MARKER_BLOCK = new MacroMarkerBlock("macro", Collections.<String, String>emptyMap(), "content", Collections.<Block>emptyList(), false);
66    private static final byte[] DUMPED_BLOCK = "macro\0\0content\04:wiki5:space6:source\0".getBytes();
67    private static final CMSSignedDataGeneratorParameters CMS_PARAMS = new CMSSignedDataGeneratorParameters();
68    private static final byte[] BLOCK_SIGNATURE = "Signature".getBytes();
69   
70    private BlockDumper dumper;
71    private CMSSignedDataGenerator generator;
72    private BlockSignatureGenerator signer;
73   
 
74  3 toggle @Before
75    public void setUp() throws Exception
76    {
77  3 signer = mocker.getComponentUnderTest();
78  3 dumper = mocker.getInstance(BlockDumper.class, "macro");
79  3 generator = mocker.getInstance(CMSSignedDataGenerator.class);
80   
81  3 doAnswer(new Answer() {
 
82  0 toggle public Object answer(InvocationOnMock invocation) throws IOException {
83  0 Object[] args = invocation.getArguments();
84  0 OutputStream out = (OutputStream) args[0];
85  0 out.write(DUMPED_BLOCK);
86  0 return null;
87    }
88    }).when(dumper).dump(any(OutputStream.class), or(eq(MACRO_BLOCK), eq(MARKER_BLOCK)));
89  3 when(dumper.dump(or(eq(MACRO_BLOCK), eq(MARKER_BLOCK)))).thenReturn(DUMPED_BLOCK);
90   
91  3 when(generator.generate(DUMPED_BLOCK, CMS_PARAMS)).thenReturn(BLOCK_SIGNATURE);
92    }
93   
 
94  1 toggle @Test
95    public void testMacroBlockSignature() throws Exception
96    {
97  1 assertThat(signer.generate(MACRO_BLOCK, CMS_PARAMS), equalTo(BLOCK_SIGNATURE));
98    }
99   
 
100  1 toggle @Test
101    public void testMacroMarkerBlockSignature() throws Exception
102    {
103  1 assertThat(signer.generate(MARKER_BLOCK, CMS_PARAMS), equalTo(BLOCK_SIGNATURE));
104    }
105   
 
106  0 toggle @Test
107    public void testIncompatibleBlockSignature() throws Exception
108    {
109  0 thrown.expect(IllegalArgumentException.class);
110  0 thrown.expectMessage("Unsupported block [org.xwiki.rendering.block.WordBlock].");
111   
112  0 Test failure here assertThat(signer.generate(new WordBlock("macro"), CMS_PARAMS), equalTo(BLOCK_SIGNATURE));
113    }
114    }