View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import java.util.concurrent.TimeoutException;
8   import org.apache.maven.monitor.logging.DefaultLog;
9   import org.apache.maven.plugin.MojoFailureException;
10  import org.apache.maven.project.MavenProject;
11  import org.codehaus.plexus.context.DefaultContext;
12  import org.codehaus.plexus.logging.Logger;
13  import org.junit.jupiter.api.Assertions;
14  import org.junit.jupiter.api.Test;
15  
16  /**
17   * Test case for {@link CheckMojo} class.
18   * @since 0.3
19   */
20  final class CheckMojoTest {
21  
22      /**
23       * CheckMojo can skip execution if "skip" flag is set.
24       * @throws Exception If something wrong happens inside
25       */
26      @Test
27      void skipsExecutionOnSkipFlag() throws Exception {
28          final CheckMojo mojo = new CheckMojo();
29          final Logger logger = new FakeLogger();
30          mojo.setLog(new DefaultLog(logger));
31          mojo.setSkip(true);
32          mojo.execute();
33          Assertions.assertEquals("[INFO] Execution skipped", logger.toString());
34      }
35  
36      /**
37       * CheckMojo can set timeout to "forever".
38       */
39      @Test
40      void setsTimeoutToForever() {
41          final CheckMojo mojo = new CheckMojo();
42          mojo.setTimeout("forever");
43          final BlockedValidator validator = new BlockedValidator();
44          mojo.setValidatorsProvider(
45              new ValidatorsProviderMocker()
46                  .withExternalResource(validator)
47                  .mock()
48          );
49          mojo.setProject(new MavenProject());
50          mojo.setLog(new DefaultLog(new FakeLogger()));
51          new Thread(
52              () -> {
53                  try {
54                      mojo.execute();
55                  } catch (final MojoFailureException exception) {
56                      throw new IllegalStateException(exception);
57                  }
58              }
59          ).start();
60          validator.await();
61          Assertions.assertEquals(
62              1,
63              validator.count(),
64              "Without the 'await' statement above, this test would run forever"
65          );
66      }
67  
68      /**
69       * CheckMojo can set timeout to "1s".
70       */
71      @Test
72      void setsTimeoutToOneSecond() {
73          final CheckMojo mojo = new CheckMojo();
74          mojo.setTimeout("1s");
75          mojo.setValidatorsProvider(
76              new ValidatorsProviderMocker()
77                  .withExternalResource(new BlockedValidator())
78                  .mock()
79          );
80          mojo.setProject(new MavenProject());
81          mojo.setLog(new DefaultLog(new FakeLogger()));
82          Assertions.assertSame(
83              TimeoutException.class,
84              Assertions.assertThrows(
85                  IllegalStateException.class,
86                  mojo::execute,
87                  "Should throw IllegalStateException because of timeout"
88              ).getCause().getClass(),
89              "The cause is expected to be timeout"
90          );
91      }
92  
93      /**
94       * CheckMojo can validate a project using all provided validators.
95       * @throws Exception If something wrong happens inside
96       */
97      @Test
98      void validatesUsingAllProvidedValidators() throws Exception {
99          final CheckMojo mojo = new CheckMojo();
100         final FakeValidator external = new FakeValidator("somename");
101         final FakeResourceValidator rexternal = new FakeResourceValidator(
102             "other"
103         );
104         final FakeMavenValidator internal = new FakeMavenValidator();
105         mojo.setValidatorsProvider(
106             new ValidatorsProviderMocker()
107                 .withInternal(internal)
108                 .withExternal(external)
109                 .withExternalResource(rexternal)
110                 .mock()
111         );
112         mojo.setProject(new MavenProject());
113         mojo.setLog(new DefaultLog(new FakeLogger()));
114         mojo.contextualize(new DefaultContext());
115         mojo.execute();
116         Assertions.assertAll(
117             () -> Assertions.assertEquals(1, internal.count()),
118             () -> Assertions.assertEquals(1, external.count()),
119             () -> Assertions.assertEquals(1, rexternal.count())
120         );
121     }
122 }