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

File SignedMacroBlockReferenceResolverTest.java

 

Code metrics

0
16
4
1
102
65
4
0.25
4
4
1

Classes

Class Line # Actions
SignedMacroBlockReferenceResolverTest 52 16 0% 4 0
1.0100%
 

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.model.reference.internal;
21   
22    import java.io.ByteArrayOutputStream;
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.xwiki.crypto.BinaryStringEncoder;
30    import org.xwiki.crypto.Digest;
31    import org.xwiki.crypto.DigestFactory;
32    import org.xwiki.model.reference.BlockReference;
33    import org.xwiki.model.reference.BlockReferenceResolver;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.rendering.block.Block;
36    import org.xwiki.rendering.block.XDOM;
37    import org.xwiki.rendering.signature.internal.BlockDumper;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39   
40    import static org.hamcrest.CoreMatchers.equalTo;
41    import static org.junit.Assert.assertThat;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.verify;
44    import static org.mockito.Mockito.when;
45   
46    /**
47    * Unit test for {@link SignedMacroBlockReferenceResolver}.
48    *
49    * @version $Id: ff547ab679146baf3d716c0271600d2a22ce3c5d $
50    * @since 6.1M2
51    */
 
52    public class SignedMacroBlockReferenceResolverTest
53    {
54    @Rule
55    public final MockitoComponentMockingRule<BlockReferenceResolver<Block>> mocker =
56    new MockitoComponentMockingRule<BlockReferenceResolver<Block>>(SignedMacroBlockReferenceResolver.class);
57   
58    private static final byte[] DIGEST_BLOCK = "digest".getBytes();
59    private static final Block BLOCK = new XDOM(Collections.<Block>emptyList());
60    private static final String ENCODED = "abcd";
61   
62    private BlockDumper dumper;
63    private OutputStream stream;
64   
 
65  3 toggle @Before
66    public void setUp() throws Exception
67    {
68  3 DigestFactory digestFactory = mocker.getInstance(DigestFactory.class, "SHA-1");
69  3 BinaryStringEncoder encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
70  3 dumper = mocker.getInstance(BlockDumper.class, "macro");
71  3 Digest digest = mock(Digest.class);
72  3 stream = new ByteArrayOutputStream();
73   
74  3 when(digest.getOutputStream()).thenReturn(stream);
75  3 when(digest.digest()).thenReturn(DIGEST_BLOCK);
76  3 when(digestFactory.getInstance()).thenReturn(digest);
77  3 when(encoder.encode(DIGEST_BLOCK)).thenReturn(ENCODED);
78    }
79   
 
80  1 toggle @Test
81    public void testResolveWithoutParent() throws Exception
82    {
83  1 assertThat(mocker.getComponentUnderTest().resolve(BLOCK), equalTo(new BlockReference(ENCODED)));
84  1 verify(dumper).dump(stream, BLOCK);
85    }
86   
 
87  1 toggle @Test
88    public void testResolveWithParent() throws Exception
89    {
90  1 DocumentReference parent = new DocumentReference("wiki","space","name");
91   
92  1 assertThat(mocker.getComponentUnderTest().resolve(BLOCK, parent), equalTo(new BlockReference(ENCODED, parent)));
93  1 verify(dumper).dump(stream, BLOCK);
94    }
95   
 
96  1 toggle @Test
97    public void testResolveWithNullParent() throws Exception
98    {
99  1 assertThat(mocker.getComponentUnderTest().resolve(BLOCK, new Object[] { null }), equalTo(new BlockReference(ENCODED)));
100  1 verify(dumper).dump(stream, BLOCK);
101    }
102    }