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.spi;
32  
33  import java.io.File;
34  import org.hamcrest.MatcherAssert;
35  import org.hamcrest.Matchers;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Test case for {@link Environment}.
40   * @since 0.3
41   */
42  final class EnvironmentTest {
43  
44      /**
45       * Environment interface can be mocked/instantiated with Mocker.
46       * @throws Exception If something wrong happens inside.
47       */
48      @Test
49      void canBeInstantiatedWithMocker() throws Exception {
50          final Environment env = new Environment.Mock();
51          MatcherAssert.assertThat(
52              "Basedir should exist", env.basedir().exists(), Matchers.is(true)
53          );
54          MatcherAssert.assertThat(
55              "Tempdir should exist", env.tempdir().exists(), Matchers.is(true)
56          );
57          MatcherAssert.assertThat(
58              "Outdir should exist", env.outdir().exists(), Matchers.is(true)
59          );
60      }
61  
62      /**
63       * EnvironmentMocker can create file.
64       * @throws Exception If something wrong happens inside.
65       */
66      @Test
67      void writesFileContentToTheDesignatedLocation() throws Exception {
68          final String name = "src/main/java/Main.java";
69          final String content = "class Main {}";
70          final Environment env = new Environment.Mock()
71              .withFile(name, content);
72          final File file = new File(env.basedir(), name);
73          MatcherAssert.assertThat(
74              "File should be created in basedir from string value",
75              file.exists(), Matchers.is(true)
76          );
77      }
78  
79      /**
80       * EnvironmentMocker can write bytearray too.
81       * @throws Exception If something wrong happens inside.
82       */
83      @Test
84      void writesByteArrayToTheDesignatedLocation() throws Exception {
85          final String name = "src/main/java/Foo.java";
86          final byte[] bytes = "class Foo {}".getBytes();
87          final Environment env = new Environment.Mock()
88              .withFile(name, bytes);
89          final File file = new File(env.basedir(), name);
90          MatcherAssert.assertThat(
91              "File should be created in basedir from bytes",
92              file.exists(), Matchers.is(true)
93          );
94      }
95  
96      /**
97       * EnvironmentMocker can set classpath for the mock.
98       * @throws Exception If something wrong happens inside.
99       */
100     @Test
101     void setsClasspathOnTheMock() throws Exception {
102         final Environment env = new Environment.Mock();
103         MatcherAssert.assertThat(
104             "Classpath should be not empty",
105             env.classpath().size(),
106             Matchers.greaterThan(0)
107         );
108     }
109 
110     /**
111      * EnvironmentMocker can mock params.
112      * @throws Exception If something wrong happens inside.
113      */
114     @Test
115     void configuresParametersInMock() throws Exception {
116         final String name = "alpha";
117         final String value = "some complex value";
118         final Environment env = new Environment.Mock()
119             .withParam(name, value);
120         MatcherAssert.assertThat(
121             "Environment variable should be set",
122             env.param(name, ""), Matchers.equalTo(value)
123         );
124     }
125 
126 }