4
4
package com .oracle .weblogic .imagetool .util ;
5
5
6
6
import java .io .IOException ;
7
+ import java .nio .file .Files ;
7
8
import java .nio .file .Path ;
8
9
import java .nio .file .Paths ;
9
10
import java .util .ArrayList ;
11
+ import java .util .Arrays ;
10
12
import java .util .List ;
11
13
import java .util .logging .Level ;
12
14
13
15
import com .oracle .weblogic .imagetool .api .model .CachedFile ;
14
16
import com .oracle .weblogic .imagetool .logging .LoggingFacade ;
15
17
import com .oracle .weblogic .imagetool .logging .LoggingFactory ;
18
+ import org .junit .jupiter .api .DisplayName ;
16
19
import org .junit .jupiter .api .Tag ;
17
20
import org .junit .jupiter .api .Test ;
21
+ import org .junit .jupiter .api .extension .ExtendWith ;
22
+ import org .junit .jupiter .api .io .TempDir ;
23
+ import uk .org .webcompere .systemstubs .environment .EnvironmentVariables ;
24
+ import uk .org .webcompere .systemstubs .jupiter .SystemStub ;
25
+ import uk .org .webcompere .systemstubs .jupiter .SystemStubsExtension ;
26
+ import uk .org .webcompere .systemstubs .properties .SystemProperties ;
18
27
19
28
import static org .junit .jupiter .api .Assertions .assertEquals ;
29
+ import static org .junit .jupiter .api .Assertions .assertNull ;
30
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
20
31
import static org .junit .jupiter .api .Assertions .assertTrue ;
21
32
22
33
@ Tag ("unit" )
34
+ @ ExtendWith (SystemStubsExtension .class )
23
35
class UtilsTest {
24
36
37
+ @ SystemStub
38
+ private EnvironmentVariables environment ;
39
+
40
+ @ SystemStub
41
+ private SystemProperties overrideProperties ;
42
+
25
43
@ Test
26
44
void compareVersions () {
27
45
assertEquals (0 , Utils .compareVersions ("12.2.1.3.0" , "12.2.1.3.0" ));
@@ -106,4 +124,96 @@ void oracleHomeFromResponseFile() throws Exception {
106
124
logger .setLevel (oldLevel );
107
125
}
108
126
}
127
+
128
+ @ Test
129
+ @ DisplayName ("Default working directory to User's home dir" )
130
+ void getBuildWorkingDir1 () throws IOException {
131
+ String expected = System .getProperty ("user.home" );
132
+ assertEquals (expected , Utils .getBuildWorkingDir ());
133
+ }
134
+
135
+ @ Test
136
+ @ DisplayName ("Override working directory with system property" )
137
+ void getBuildWorkingDir2 (@ TempDir Path tempDir ) throws IOException {
138
+ // provide existing directory as input to WLSIMG_BLDDIR should succeed
139
+ String expected = tempDir .toString ();
140
+ overrideProperties .set ("WLSIMG_BLDDIR" , expected );
141
+ assertEquals (expected , Utils .getBuildWorkingDir ());
142
+
143
+ // Create a read-only directory as input for WLSIMG_BLDDIR
144
+ Path unwritableDir = tempDir .resolve ("unwritable" );
145
+ Files .createDirectory (unwritableDir );
146
+ if (!unwritableDir .toFile ().setReadOnly ()) {
147
+ throw new IOException ("Unable to mark test directory as read-only" );
148
+ }
149
+ // read-only directory as input for working directory should throw an exception
150
+ overrideProperties .set ("WLSIMG_BLDDIR" , unwritableDir .toString ());
151
+ assertThrows (IOException .class , Utils ::getBuildWorkingDir );
152
+ }
153
+
154
+ @ Test
155
+ @ DisplayName ("Override working directory with invalid directory" )
156
+ void getBuildWorkingDir3 () {
157
+ String expected = "/this/does/not/exist" ;
158
+ overrideProperties .set ("WLSIMG_BLDDIR" , expected );
159
+ assertThrows (IOException .class , Utils ::getBuildWorkingDir );
160
+ }
161
+
162
+ @ Test
163
+ @ DisplayName ("Override working directory with invalid file" )
164
+ void getBuildWorkingDir4 (@ TempDir Path tempDir ) throws IOException {
165
+ Path tempFile = tempDir .resolve ("getBuildWorkingDir4.txt" );
166
+ List <String > lines = Arrays .asList ("a" , "b" );
167
+ Files .write (tempFile , lines );
168
+
169
+ String expected = tempFile .toString ();
170
+ overrideProperties .set ("WLSIMG_BLDDIR" , expected );
171
+ // The override for WLSIMG_BUILDDIR must be a directory, NOT a file
172
+ assertThrows (IOException .class , Utils ::getBuildWorkingDir );
173
+ }
174
+
175
+ @ Test
176
+ void findProxyUrlWithCommandLine () {
177
+ // Always default to the proxy set by the user on the tool's command line
178
+ String expected = "http://some.proxy-host.com" ;
179
+ assertEquals (expected , Utils .findProxyUrl (expected , "http" ));
180
+ assertEquals (expected , Utils .findProxyUrl (expected , "https" ));
181
+ }
182
+
183
+ /**
184
+ * If not specified by the user, the environment variable value should be returned
185
+ * for http_proxy and https_proxy.
186
+ */
187
+ @ Test
188
+ void findProxyUrlWithEnvironment () {
189
+ // No ENV variable set (filed issue on SystemStub)
190
+ environment .set ("http_proxy" , null );
191
+ environment .set ("HTTP_PROXY" , null );
192
+ assertNull (Utils .findProxyUrl ("" , "http" ));
193
+
194
+ String expected = "http://env.proxy-host.com" ;
195
+ // ENV for http_proxy and https_proxy are set
196
+ environment .set ("http_proxy" , expected );
197
+ environment .set ("https_proxy" , expected );
198
+ assertEquals (expected , Utils .findProxyUrl ("" , "http" ));
199
+ assertEquals (expected , Utils .findProxyUrl ("" , "https" ));
200
+ }
201
+
202
+ @ Test
203
+ void findProxyUrlForNoProxy () {
204
+ // No ENV variable set (filed issue on SystemStub)
205
+ environment .set ("no_proxy" , null );
206
+ environment .set ("NO_PROXY" , null );
207
+ assertNull (Utils .findProxyUrl ("" , "none" ));
208
+
209
+ String expected = ".host.com,.anotherhost.com,.and.another.com" ;
210
+ // | (bar) should be replaced by , (comma) for http.nonProxyHosts system property
211
+ String withBars = expected .replace ("," , "|" );
212
+ overrideProperties .set ("http.nonProxyHosts" , withBars );
213
+ assertEquals (expected , Utils .findProxyUrl ("" , "none" ));
214
+
215
+ // ENV for no_proxy is set
216
+ environment .set ("no_proxy" , expected );
217
+ assertEquals (expected , Utils .findProxyUrl ("" , "none" ));
218
+ }
109
219
}
0 commit comments