44package com .oracle .weblogic .imagetool .util ;
55
66import java .io .IOException ;
7+ import java .nio .file .Files ;
78import java .nio .file .Path ;
89import java .nio .file .Paths ;
910import java .util .ArrayList ;
11+ import java .util .Arrays ;
1012import java .util .List ;
1113import java .util .logging .Level ;
1214
1315import com .oracle .weblogic .imagetool .api .model .CachedFile ;
1416import com .oracle .weblogic .imagetool .logging .LoggingFacade ;
1517import com .oracle .weblogic .imagetool .logging .LoggingFactory ;
18+ import org .junit .jupiter .api .DisplayName ;
1619import org .junit .jupiter .api .Tag ;
1720import 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 ;
1827
1928import 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 ;
2031import static org .junit .jupiter .api .Assertions .assertTrue ;
2132
2233@ Tag ("unit" )
34+ @ ExtendWith (SystemStubsExtension .class )
2335class UtilsTest {
2436
37+ @ SystemStub
38+ private EnvironmentVariables environment ;
39+
40+ @ SystemStub
41+ private SystemProperties overrideProperties ;
42+
2543 @ Test
2644 void compareVersions () {
2745 assertEquals (0 , Utils .compareVersions ("12.2.1.3.0" , "12.2.1.3.0" ));
@@ -106,4 +124,96 @@ void oracleHomeFromResponseFile() throws Exception {
106124 logger .setLevel (oldLevel );
107125 }
108126 }
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+ }
109219}
0 commit comments