View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.spi;
6   
7   import java.io.File;
8   import java.nio.charset.StandardCharsets;
9   import java.util.ArrayList;
10  import java.util.Collection;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link Environment}.
17   *
18   * @since 0.3
19   */
20  final class EnvironmentTest {
21  
22      /**
23       * Environment interface can be mocked/instantiated with Mocker.
24       *
25       * @throws Exception If something wrong happens inside.
26       */
27      @Test
28      void canBeInstantiatedWithMocker() throws Exception {
29          final Environment env = new Environment.Mock();
30          MatcherAssert.assertThat(
31              "Basedir, tempdir and outdir should all exist",
32              env.basedir().exists()
33                  && env.tempdir().exists()
34                  && env.outdir().exists(),
35              Matchers.is(true)
36          );
37      }
38  
39      /**
40       * EnvironmentMocker can create file.
41       *
42       * @throws Exception If something wrong happens inside.
43       */
44      @Test
45      void writesFileContentToTheDesignatedLocation() throws Exception {
46          final String name = "src/main/java/Main.java";
47          MatcherAssert.assertThat(
48              "File should be created in basedir from string value",
49              new File(
50                  new Environment.Mock().withFile(name, "class Main {}").basedir(),
51                  name
52              ).exists(),
53              Matchers.is(true)
54          );
55      }
56  
57      /**
58       * EnvironmentMocker can write bytearray too.
59       *
60       * @throws Exception If something wrong happens inside.
61       */
62      @Test
63      void writesByteArrayToTheDesignatedLocation() throws Exception {
64          final String name = "src/main/java/Foo.java";
65          MatcherAssert.assertThat(
66              "File should be created in basedir from bytes",
67              new File(
68                  new Environment.Mock()
69                      .withFile(name, "class Foo {}".getBytes(StandardCharsets.UTF_8)).basedir(),
70                  name
71              ).exists(),
72              Matchers.is(true)
73          );
74      }
75  
76      /**
77       * EnvironmentMocker can set classpath for the mock.
78       *
79       * @throws Exception If something wrong happens inside.
80       */
81      @Test
82      void setsClasspathOnTheMock() throws Exception {
83          MatcherAssert.assertThat(
84              "Classpath should be not empty",
85              new Environment.Mock().classpath().size(),
86              Matchers.greaterThan(0)
87          );
88      }
89  
90      /**
91       * Environment.files() should skip binary files so validators never see
92       * them (see <a href="https://github.com/yegor256/qulice/issues/1264">
93       * issue #1264</a>).
94       *
95       * @throws Exception If something wrong happens inside.
96       */
97      @Test
98      void skipsBinaryFilesWhenListing() throws Exception {
99          final String image = "src/main/resources/pixel.png";
100         final String source = "src/main/java/Foo.java";
101         final Environment env = new Environment.Mock()
102             .withFile(source, "class Foo {}".concat(String.valueOf('\n'))).withFile(
103                 image,
104                 new byte[] {
105                     (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
106                     0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
107                 }
108             );
109         MatcherAssert.assertThat(
110             "Binary files cannot leak into the list of files to validate",
111             env.files("*.*"),
112             Matchers.allOf(
113                 Matchers.hasItem(new File(env.basedir(), source)),
114                 Matchers.not(Matchers.hasItem(new File(env.basedir(), image)))
115             )
116         );
117     }
118 
119     /**
120      * Many mocks should all initialize without failure, even when the system
121      * temp directory already contains many leftover mock directories
122      * (see <a href="https://github.com/yegor256/qulice/issues/691">issue
123      * #691</a>).
124      */
125     @Test
126     void createsManyMocksWithoutFailure() {
127         final int count = 200;
128         final Collection<File> bases = new ArrayList<>(count);
129         for (int idx = 0; idx < count; ++idx) {
130             bases.add(new Environment.Mock().basedir());
131         }
132         MatcherAssert.assertThat(
133             "All mocks must have distinct and existing basedirs",
134             bases.stream().filter(File::isDirectory).distinct().count(),
135             Matchers.equalTo((long) count)
136         );
137     }
138 
139     /**
140      * EnvironmentMocker can mock params.
141      *
142      * @throws Exception If something wrong happens inside.
143      */
144     @Test
145     void configuresParametersInMock() throws Exception {
146         final String name = "alpha";
147         final String value = "some complex value";
148         MatcherAssert.assertThat(
149             "Environment variable should be set",
150             new Environment.Mock().withParam(name, value).param(name, ""),
151             Matchers.equalTo(value)
152         );
153     }
154 }