View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.ssl.maven.plugin;
6   
7   import java.io.File;
8   import java.util.Properties;
9   import org.apache.maven.project.MavenProject;
10  import org.hamcrest.MatcherAssert;
11  import org.hamcrest.Matchers;
12  import org.junit.Test;
13  import org.junit.runner.RunWith;
14  import org.mockito.Mockito;
15  import org.mockito.junit.MockitoJUnitRunner;
16  
17  /**
18   * Test case for {@link KeygenMojo} (more detailed test is in maven invoker).
19   *
20   * @since 0.5
21   */
22  @RunWith(MockitoJUnitRunner.class)
23  public final class KeygenMojoTest {
24  
25      /**
26       * KeygenMojo can skip execution when flag is set.
27       * @throws Exception If something is wrong
28       */
29      @Test
30      @SuppressWarnings("PMD.UnitTestShouldIncludeAssert")
31      public void skipsExecutionWhenRequired() throws Exception {
32          final KeygenMojo mojo = new KeygenMojo();
33          mojo.setSkip(true);
34          mojo.execute();
35      }
36  
37      /**
38       * KeygenMojo populates cacerts even is keystore is active.
39       * @throws Exception if test have failed
40       */
41      @Test
42      @SuppressWarnings("PMD.UnitTestContainsTooManyAsserts")
43      public void populatesCacertsIdKeystoreIsActive() throws Exception {
44          final Keystore keystore = new Keystore("changeit");
45          keystore.activate(
46              new File("target/populatesCacertsIdKeystoreIsActive/keystore.jks")
47          );
48          final MavenProject project = Mockito.mock(MavenProject.class);
49          final Properties properties = new Properties();
50          Mockito.when(project.getProperties()).thenReturn(properties);
51          final KeygenMojo mojo = new KeygenMojo(
52              project, keystore,
53              new Cacerts(
54                  new File("target/populatesCacertsIdKeystoreIsActive/trust.jks")
55              )
56          );
57          System.getProperties().setProperty(Cacerts.TRUST, "trust");
58          System.getProperties().setProperty(Cacerts.TRUST_PWD, "pwd");
59          mojo.execute();
60          MatcherAssert.assertThat(
61              properties.getProperty(Cacerts.TRUST_PWD),
62              Matchers.notNullValue()
63          );
64          MatcherAssert.assertThat(
65              properties.getProperty(Cacerts.TRUST),
66              Matchers.notNullValue()
67          );
68      }
69  
70  }