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 org.hamcrest.MatcherAssert;
9   import org.hamcrest.Matchers;
10  import org.junit.jupiter.api.Test;
11  
12  /**
13   * Test case for {@link Environment}.
14   * @since 0.3
15   */
16  final class EnvironmentTest {
17  
18      /**
19       * Environment interface can be mocked/instantiated with Mocker.
20       * @throws Exception If something wrong happens inside.
21       */
22      @Test
23      void canBeInstantiatedWithMocker() throws Exception {
24          final Environment env = new Environment.Mock();
25          MatcherAssert.assertThat(
26              "Basedir should exist", env.basedir().exists(), Matchers.is(true)
27          );
28          MatcherAssert.assertThat(
29              "Tempdir should exist", env.tempdir().exists(), Matchers.is(true)
30          );
31          MatcherAssert.assertThat(
32              "Outdir should exist", env.outdir().exists(), Matchers.is(true)
33          );
34      }
35  
36      /**
37       * EnvironmentMocker can create file.
38       * @throws Exception If something wrong happens inside.
39       */
40      @Test
41      void writesFileContentToTheDesignatedLocation() throws Exception {
42          final String name = "src/main/java/Main.java";
43          final String content = "class Main {}";
44          final Environment env = new Environment.Mock()
45              .withFile(name, content);
46          final File file = new File(env.basedir(), name);
47          MatcherAssert.assertThat(
48              "File should be created in basedir from string value",
49              file.exists(), Matchers.is(true)
50          );
51      }
52  
53      /**
54       * EnvironmentMocker can write bytearray too.
55       * @throws Exception If something wrong happens inside.
56       */
57      @Test
58      void writesByteArrayToTheDesignatedLocation() throws Exception {
59          final String name = "src/main/java/Foo.java";
60          final byte[] bytes = "class Foo {}".getBytes();
61          final Environment env = new Environment.Mock()
62              .withFile(name, bytes);
63          final File file = new File(env.basedir(), name);
64          MatcherAssert.assertThat(
65              "File should be created in basedir from bytes",
66              file.exists(), Matchers.is(true)
67          );
68      }
69  
70      /**
71       * EnvironmentMocker can set classpath for the mock.
72       * @throws Exception If something wrong happens inside.
73       */
74      @Test
75      void setsClasspathOnTheMock() throws Exception {
76          final Environment env = new Environment.Mock();
77          MatcherAssert.assertThat(
78              "Classpath should be not empty",
79              env.classpath().size(),
80              Matchers.greaterThan(0)
81          );
82      }
83  
84      /**
85       * EnvironmentMocker can mock params.
86       * @throws Exception If something wrong happens inside.
87       */
88      @Test
89      void configuresParametersInMock() throws Exception {
90          final String name = "alpha";
91          final String value = "some complex value";
92          final Environment env = new Environment.Mock()
93              .withParam(name, value);
94          MatcherAssert.assertThat(
95              "Environment variable should be set",
96              env.param(name, ""), Matchers.equalTo(value)
97          );
98      }
99  
100 }