1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.internal.digest.factory

File DefaultDigestFactoryTest.java

 

Code metrics

2
15
2
1
79
51
3
0.2
7.5
2
1.5

Classes

Class Line # Actions
DefaultDigestFactoryTest 36 15 0% 3 1
0.9473684494.7%
 

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.crypto.internal.digest.factory;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.crypto.BinaryStringEncoder;
26    import org.xwiki.crypto.DigestFactory;
27    import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder;
28    import org.xwiki.test.annotation.ComponentList;
29    import org.xwiki.test.mockito.MockitoComponentMockingRule;
30   
31    import static org.hamcrest.CoreMatchers.equalTo;
32    import static org.junit.Assert.assertThat;
33   
34    @ComponentList({Base64BinaryStringEncoder.class, BcMD5DigestFactory.class, BcSHA1DigestFactory.class,
35    BcSHA224DigestFactory.class, BcSHA256DigestFactory.class, BcSHA384DigestFactory.class, BcSHA512DigestFactory.class})
 
36    public class DefaultDigestFactoryTest extends AbstractDigestFactoryTestConstants
37    {
38    private static final byte[] BYTES = TEXT.getBytes();
39   
40    @Rule
41    public final MockitoComponentMockingRule<DigestFactory> mocker =
42    new MockitoComponentMockingRule<DigestFactory>(DefaultDigestFactory.class);
43   
44    private byte[] md5Digest;
45    private byte[] sha1Digest;
46    private byte[] sha224Digest;
47    private byte[] sha256Digest;
48    private byte[] sha384Digest;
49    private byte[] sha512Digest;
50   
51    private DigestFactory factory;
52   
 
53  1 toggle @Before
54    public void configure() throws Exception
55    {
56  1 factory = mocker.getComponentUnderTest();
57   
58  1 if (md5Digest == null) {
59  1 BinaryStringEncoder base64encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
60  1 md5Digest = base64encoder.decode(MD5_DIGEST);
61  1 sha1Digest = base64encoder.decode(SHA1_DIGEST);
62  1 sha224Digest = base64encoder.decode(SHA224_DIGEST);
63  1 sha256Digest = base64encoder.decode(SHA256_DIGEST);
64  1 sha384Digest = base64encoder.decode(SHA384_DIGEST);
65  1 sha512Digest = base64encoder.decode(SHA512_DIGEST);
66    }
67    }
68   
 
69  1 toggle @Test
70    public void testDefaultDigestFactory() throws Exception
71    {
72  1 assertThat(factory.getInstance(MD5_DIGEST_ALGO.getEncoded()).digest(BYTES), equalTo(md5Digest));
73  1 assertThat(factory.getInstance(SHA1_DIGEST_ALGO.getEncoded()).digest(BYTES), equalTo(sha1Digest));
74  1 assertThat(factory.getInstance(SHA224_DIGEST_ALGO.getEncoded()).digest(BYTES), equalTo(sha224Digest));
75  1 assertThat(factory.getInstance(SHA256_DIGEST_ALGO.getEncoded()).digest(BYTES), equalTo(sha256Digest));
76  1 assertThat(factory.getInstance(SHA384_DIGEST_ALGO.getEncoded()).digest(BYTES), equalTo(sha384Digest));
77  1 assertThat(factory.getInstance(SHA512_DIGEST_ALGO.getEncoded()).digest(BYTES), equalTo(sha512Digest));
78    }
79    }