1
2
3
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
17
18
19
20 final class EnvironmentTest {
21
22
23
24
25
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
41
42
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
59
60
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
78
79
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
92
93
94
95
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
121
122
123
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
141
142
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 }