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.Arrays;
8   import java.util.concurrent.TimeoutException;
9   import org.apache.maven.monitor.logging.DefaultLog;
10  import org.apache.maven.plugin.MojoFailureException;
11  import org.apache.maven.project.MavenProject;
12  import org.codehaus.plexus.context.DefaultContext;
13  import org.codehaus.plexus.logging.Logger;
14  import org.junit.jupiter.api.Assertions;
15  import org.junit.jupiter.api.Test;
16  
17  /**
18   * Test case for {@link CheckMojo} class.
19   *
20   * @since 0.3
21   */
22  final class CheckMojoTest {
23  
24      /**
25       * CheckMojo can skip execution if "skip" flag is set.
26       *
27       * @throws Exception If something wrong happens inside
28       */
29      @Test
30      void skipsExecutionOnSkipFlag() throws Exception {
31          final CheckMojo mojo = new CheckMojo();
32          final Logger logger = new FakeLogger();
33          mojo.setLog(new DefaultLog(logger));
34          mojo.setSkip(true);
35          mojo.execute();
36          Assertions.assertEquals("[INFO] Execution skipped", logger.toString());
37      }
38  
39      /**
40       * CheckMojo can set timeout to "forever".
41       */
42      @Test
43      void setsTimeoutToForever() {
44          final CheckMojo mojo = new CheckMojo();
45          mojo.setTimeout("forever");
46          final BlockedValidator validator = new BlockedValidator();
47          mojo.setValidatorsProvider(
48              new ValidatorsProviderMocker()
49                  .withExternalResource(validator)
50                  .mock()
51          );
52          mojo.setProject(new MavenProject());
53          mojo.setLog(new DefaultLog(new FakeLogger()));
54          new Thread(
55              () -> {
56                  try {
57                      mojo.execute();
58                  } catch (final MojoFailureException exception) {
59                      throw new IllegalStateException(exception);
60                  }
61              }
62          ).start();
63          validator.await();
64          Assertions.assertEquals(
65              1,
66              validator.count(),
67              "Without the 'await' statement above, this test would run forever"
68          );
69      }
70  
71      /**
72       * CheckMojo can set timeout to "1s".
73       */
74      @Test
75      void setsTimeoutToOneSecond() {
76          final CheckMojo mojo = new CheckMojo();
77          mojo.setTimeout("1s");
78          mojo.setValidatorsProvider(
79              new ValidatorsProviderMocker()
80                  .withExternalResource(new BlockedValidator())
81                  .mock()
82          );
83          mojo.setProject(new MavenProject());
84          mojo.setLog(new DefaultLog(new FakeLogger()));
85          Assertions.assertSame(
86              TimeoutException.class,
87              Assertions.assertThrows(
88                  IllegalStateException.class,
89                  mojo::execute,
90                  "Should throw IllegalStateException because of timeout"
91              ).getCause().getClass(),
92              "The cause is expected to be timeout"
93          );
94      }
95  
96      /**
97       * CheckMojo can validate a project using all provided validators.
98       *
99       * @throws Exception If something wrong happens inside
100      */
101     @Test
102     void validatesUsingAllProvidedValidators() throws Exception {
103         final CheckMojo mojo = new CheckMojo();
104         final FakeValidator external = new FakeValidator("somename");
105         final FakeResourceValidator rexternal = new FakeResourceValidator(
106             "other"
107         );
108         final FakeMavenValidator internal = new FakeMavenValidator();
109         mojo.setValidatorsProvider(
110             new ValidatorsProviderMocker()
111                 .withInternal(internal)
112                 .withExternal(external)
113                 .withExternalResource(rexternal)
114                 .mock()
115         );
116         mojo.setProject(new MavenProject());
117         mojo.setLog(new DefaultLog(new FakeLogger()));
118         mojo.contextualize(new DefaultContext());
119         mojo.execute();
120         Assertions.assertAll(
121             () -> Assertions.assertEquals(1, internal.count()),
122             () -> Assertions.assertEquals(1, external.count()),
123             () -> Assertions.assertEquals(1, rexternal.count())
124         );
125     }
126 
127     /**
128      * CheckMojo can hand the ErrorProne flags of the project to validators.
129      *
130      * @throws Exception If something wrong happens inside
131      */
132     @Test
133     void passesErrorProneFlagsToValidators() throws Exception {
134         final CheckMojo mojo = new CheckMojo();
135         final RecordingValidator external =
136             new RecordingValidator("qulice.errorprone");
137         mojo.setValidatorsProvider(
138             new ValidatorsProviderMocker().withExternal(external).mock()
139         );
140         mojo.setProject(new MavenProject());
141         mojo.setLog(new DefaultLog(new FakeLogger()));
142         mojo.contextualize(new DefaultContext());
143         mojo.setErrorprone(
144             Arrays.asList("-Xep:UnicodeInCode:OFF", "-Xep:UnusedVariable:OFF")
145         );
146         mojo.execute();
147         Assertions.assertEquals(
148             "-Xep:UnicodeInCode:OFF -Xep:UnusedVariable:OFF",
149             external.seen(),
150             "The flags configured by the project must reach the validators"
151         );
152     }
153 }