View Javadoc
1   /*
2    * Copyright (c) 2011-2025 Yegor Bugayenko
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.maven;
32  
33  import com.qulice.spi.Environment;
34  import com.qulice.spi.ResourceValidator;
35  import com.qulice.spi.Validator;
36  import org.apache.maven.plugin.logging.Log;
37  import org.apache.maven.project.MavenProject;
38  import org.codehaus.plexus.context.Context;
39  import org.junit.jupiter.api.Test;
40  import org.mockito.Mockito;
41  
42  /**
43   * Test case for {@link CheckMojo} class.
44   * @since 0.3
45   */
46  final class CheckMojoTest {
47  
48      /**
49       * CheckMojo can skip execution if "skip" flag is set.
50       * @throws Exception If something wrong happens inside
51       */
52      @Test
53      void skipsExecutionOnSkipFlag() throws Exception {
54          final CheckMojo mojo = new CheckMojo();
55          final Log log = Mockito.mock(Log.class);
56          mojo.setLog(log);
57          mojo.setSkip(true);
58          mojo.execute();
59          Mockito.verify(log).info("Execution skipped");
60      }
61  
62      /**
63       * CheckMojo can validate a project using all provided validators.
64       * @throws Exception If something wrong happens inside
65       */
66      @Test
67      void validatesUsingAllProvidedValidators() throws Exception {
68          final CheckMojo mojo = new CheckMojo();
69          final Validator external = Mockito.mock(Validator.class);
70          Mockito.when(external.name()).thenReturn("somename");
71          final ResourceValidator rexternal =
72              Mockito.mock(ResourceValidator.class);
73          Mockito.when(rexternal.name()).thenReturn("other");
74          final MavenValidator internal = Mockito.mock(MavenValidator.class);
75          final ValidatorsProvider provider = new ValidatorsProviderMocker()
76              .withInternal(internal)
77              .withExternal(external)
78              .withExternalResource(rexternal)
79              .mock();
80          mojo.setValidatorsProvider(provider);
81          final MavenProject project = Mockito.mock(MavenProject.class);
82          mojo.setProject(project);
83          mojo.setLog(Mockito.mock(Log.class));
84          final Context context = Mockito.mock(Context.class);
85          mojo.contextualize(context);
86          mojo.execute();
87          Mockito.verify(internal).validate(Mockito.any(MavenEnvironment.class));
88          Mockito.verify(external).validate(Mockito.any(Environment.class));
89          Mockito.verify(rexternal, Mockito.atLeastOnce())
90              .validate(Mockito.anyCollection());
91      }
92  
93  }