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