88 * Contributors:
99 * Red Hat, Inc. - initial API and implementation
1010 ******************************************************************************/
11- package com .redhat .devtools .intellij .commonUiTestLibrary ;
11+ package com .redhat .devtools .intellij .commonuitest ;
1212
1313import com .intellij .remoterobot .RemoteRobot ;
1414import com .intellij .remoterobot .stepsProcessing .StepLogger ;
1515import com .intellij .remoterobot .stepsProcessing .StepWorker ;
1616import com .intellij .remoterobot .utils .WaitForConditionTimeoutException ;
17- import com .redhat .devtools .intellij .commonUiTestLibrary .fixtures .dialogs .FlatWelcomeFrame ;
17+ import com .redhat .devtools .intellij .commonuitest .exceptions .UITestException ;
18+ import com .redhat .devtools .intellij .commonuitest .fixtures .dialogs .FlatWelcomeFrame ;
1819
1920import java .io .File ;
2021import java .io .FileOutputStream ;
2829import java .nio .file .Path ;
2930import java .nio .file .Paths ;
3031import java .time .Duration ;
32+ import java .util .logging .Level ;
33+ import java .util .logging .Logger ;
3134
3235import static com .intellij .remoterobot .stepsProcessing .StepWorkerKt .step ;
3336import static com .intellij .remoterobot .utils .RepeatUtilsKt .waitFor ;
38413942 */
4043public class UITestRunner {
41- private static final int defaultPort = 8580 ;
44+ private static final int DEFAULT_PORT = 8580 ;
45+ private static final String ACCEPTED_SOURCE_LOCATION = "accepted" ;
46+ private static final String COPY_ACCEPTED_FILE_STEP_DESCRIPTION = "Copy the 'accepted' file to the appropriate location" ;
47+ private static final Logger LOGGER = Logger .getLogger (UITestRunner .class .getName ());
48+ private static final String OS_NAME = System .getProperty ("os.name" ).toLowerCase ();
49+ private static final String USER_HOME = System .getProperty ("user.home" );
4250 private static RemoteRobot remoteRobot = null ;
4351 private static Process ideProcess ;
4452 private static IdeaVersion ideaVersion ;
@@ -57,16 +65,15 @@ public static RemoteRobot runIde(IdeaVersion ideaVersion, int port) {
5765 UITestRunner .ideaVersion = ideaVersion ;
5866 makeSureAllTermsAndConditionsAreAccepted ();
5967
60- String osName = System .getProperty ("os.name" ).toLowerCase ();
61- String fileExtension = osName .contains ("windows" ) ? ".bat" : "" ;
68+ String fileExtension = OS_NAME .contains ("windows" ) ? ".bat" : "" ;
6269 ProcessBuilder pb = new ProcessBuilder ("." + File .separator + "gradlew" + fileExtension , "runIdeForUiTests" , "-PideaVersion=" + ideaVersion .toString (), "-Drobot-server.port=" + port );
6370
6471 try {
6572 ideProcess = pb .start ();
6673 waitUntilIntelliJStarts (port );
6774 remoteRobot = getRemoteRobotConnection (port );
68- } catch (IOException | InterruptedException e ) {
69- e . printStackTrace ( );
75+ } catch (IOException e ) {
76+ LOGGER . log ( Level . SEVERE , e . getMessage (), e );
7077 }
7178
7279 remoteRobot .find (FlatWelcomeFrame .class , Duration .ofSeconds (10 )).clearWorkspace ();
@@ -81,7 +88,7 @@ public static RemoteRobot runIde(IdeaVersion ideaVersion, int port) {
8188 * @return instance of the RemoteRobot
8289 */
8390 public static RemoteRobot runIde (IdeaVersion ideaVersion ) {
84- return runIde (ideaVersion , defaultPort );
91+ return runIde (ideaVersion , DEFAULT_PORT );
8592 }
8693
8794 /**
@@ -121,9 +128,8 @@ public static RemoteRobot getRemoteRobot() {
121128 *
122129 * @param port port number
123130 * @return instance of the RemoteRobot
124- * @throws InterruptedException may be thrown in Thread.sleep()
125131 */
126- public static RemoteRobot getRemoteRobotConnection (int port ) throws InterruptedException {
132+ public static RemoteRobot getRemoteRobotConnection (int port ) {
127133 return step ("Create an instance of the RemoteRobot listening on port " + port , () -> {
128134 RemoteRobot remoteRobot = new RemoteRobot ("http://127.0.0.1:" + port );
129135 for (int i = 0 ; i < 60 ; i ++) {
@@ -133,7 +139,8 @@ public static RemoteRobot getRemoteRobotConnection(int port) throws InterruptedE
133139 try {
134140 Thread .sleep (1000 );
135141 } catch (InterruptedException e2 ) {
136- e2 .printStackTrace ();
142+ LOGGER .log (Level .SEVERE , e2 .getMessage (), e2 );
143+ Thread .currentThread ().interrupt ();
137144 }
138145 }
139146 }
@@ -149,7 +156,7 @@ public enum IdeaVersion {
149156 V_2020_2 ("IC-2020.2" ),
150157 V_2020_3 ("IC-2020.3" );
151158
152- final private String ideaVersionStringRepresentation ;
159+ private final String ideaVersionStringRepresentation ;
153160
154161 IdeaVersion (String ideaVersionStringRepresentation ) {
155162 this .ideaVersionStringRepresentation = ideaVersionStringRepresentation ;
@@ -167,64 +174,62 @@ public int toInt() {
167174 }
168175
169176 private static void makeSureAllTermsAndConditionsAreAccepted () {
170- String osName = System .getProperty ("os.name" ).toLowerCase ();
171-
172- if (osName .contains ("linux" )) {
177+ if (OS_NAME .contains ("linux" )) {
173178 step ("Copy the 'prefs.xml' file to the appropriate location" , () -> {
174179 String prefsXmlSourceLocation = "prefs.xml" ;
175- String prefsXmlDir = System . getProperty ( "user.home" ) + "/.java/.userPrefs/jetbrains/_!(!!cg\" p!(}!}@\" j!(k!|w\" w!'8!b!\" p!':!e@==" ;
180+ String prefsXmlDir = USER_HOME + "/.java/.userPrefs/jetbrains/_!(!!cg\" p!(}!}@\" j!(k!|w\" w!'8!b!\" p!':!e@==" ;
176181 createDirectoryHierarchy (prefsXmlDir );
177182 copyFileFromJarResourceDir (prefsXmlSourceLocation , prefsXmlDir + "/prefs.xml" );
178183 });
179184
180- step ("Copy the 'accepted' file to the appropriate location" , () -> {
181- String acceptedSourceLocation = "accepted" ;
182- String acceptedDir = System .getProperty ("user.home" ) + "/.local/share/JetBrains/consentOptions" ;
185+ step (COPY_ACCEPTED_FILE_STEP_DESCRIPTION , () -> {
186+ String acceptedDir = USER_HOME + "/.local/share/JetBrains/consentOptions" ;
183187 createDirectoryHierarchy (acceptedDir );
184- copyFileFromJarResourceDir (acceptedSourceLocation , acceptedDir + "/accepted" );
188+ copyFileFromJarResourceDir (ACCEPTED_SOURCE_LOCATION , acceptedDir + "/accepted" );
185189 });
186- } else if (osName .contains ("os x" )) {
190+ } else if (OS_NAME .contains ("os x" )) {
187191 step ("Copy the 'com.apple.java.util.prefs.plist' file to the appropriate location" , () -> {
188192 String plistSourceLocation = "com.apple.java.util.prefs.plist" ;
189- String plistDir = System . getProperty ( "user.home" ) + "/Library/Preferences" ;
193+ String plistDir = USER_HOME + "/Library/Preferences" ;
190194 copyFileFromJarResourceDir (plistSourceLocation , plistDir + "/com.apple.java.util.prefs.plist" );
191195 });
192196
193- step ("Copy the 'accepted' file to the appropriate location" , () -> {
194- String acceptedSourceLocation = "accepted" ;
195- String acceptedDir = System .getProperty ("user.home" ) + "/Library/Application Support/JetBrains/consentOptions" ;
197+ step (COPY_ACCEPTED_FILE_STEP_DESCRIPTION , () -> {
198+ String acceptedDir = USER_HOME + "/Library/Application Support/JetBrains/consentOptions" ;
196199 createDirectoryHierarchy (acceptedDir );
197- copyFileFromJarResourceDir (acceptedSourceLocation , acceptedDir + "/accepted" );
200+ copyFileFromJarResourceDir (ACCEPTED_SOURCE_LOCATION , acceptedDir + "/accepted" );
198201
199202 // run the 'killall cfprefsd' cmd to force OS X to reload preferences files
200- ProcessBuilder pb = new ProcessBuilder ("killall" , "cfprefsd" );
203+ ProcessBuilder pb = new ProcessBuilder ("/usr/bin/ killall" , "cfprefsd" );
201204 try {
202205 Process p = pb .start ();
203206 p .waitFor ();
204207 } catch (IOException | InterruptedException e ) {
205- e .printStackTrace ();
208+ LOGGER .log (Level .SEVERE , e .getMessage (), e );
209+ Thread .currentThread ().interrupt ();
206210 }
207211 });
208- } else if (osName .contains ("windows" )) {
209- step ("Copy the 'accepted' file to the appropriate location" , () -> {
210- String acceptedSourceLocation = "accepted" ;
211- String acceptedDir = System .getProperty ("user.home" ) + "\\ AppData\\ Roaming\\ JetBrains\\ consentOptions" ;
212+ } else if (OS_NAME .contains ("windows" )) {
213+ step (COPY_ACCEPTED_FILE_STEP_DESCRIPTION , () -> {
214+ String acceptedDir = USER_HOME + "\\ AppData\\ Roaming\\ JetBrains\\ consentOptions" ;
212215 createDirectoryHierarchy (acceptedDir );
213- copyFileFromJarResourceDir (acceptedSourceLocation , acceptedDir + "\\ accepted" );
216+ copyFileFromJarResourceDir (ACCEPTED_SOURCE_LOCATION , acceptedDir + "\\ accepted" );
214217 });
215218
216219 step ("Create appropriate registry entries" , () -> {
217- String registryPath = "HKCU:\\ Software\\ JavaSoft\\ Prefs\\ jetbrains\\ privacy_policy" ;
218- ProcessBuilder pb1 = new ProcessBuilder ("powershell.exe" , "New-Item" , "-Path" , registryPath , "-Force" );
219- ProcessBuilder pb2 = new ProcessBuilder ("powershell.exe" , "New-ItemProperty" , "-Path" , registryPath , "-Name" , "accepted_version" , "-Value" , "'2.1'" );
220+ String registryPath = "HKCU:" + "\\ Software\\ JavaSoft\\ Prefs\\ jetbrains\\ privacy_policy" ;
221+ String powershellLocation = "C:\\ Windows\\ system32\\ WindowsPowerShell\\ v1.0\\ powershell.exe" ;
222+ ProcessBuilder pb1 = new ProcessBuilder (powershellLocation , "New-Item" , "-Path" , registryPath , "-Force" );
223+ ProcessBuilder pb2 = new ProcessBuilder (powershellLocation , "New-ItemProperty" , "-Path" , registryPath , "-Name" , "accepted_version" , "-Value" , "'2.1'" );
220224
221225 try {
222226 Process p1 = pb1 .start ();
223227 p1 .waitFor ();
224228 Process p2 = pb2 .start ();
225229 p2 .waitFor ();
226230 } catch (IOException | InterruptedException e ) {
227- e .printStackTrace ();
231+ LOGGER .log (Level .SEVERE , e .getMessage (), e );
232+ Thread .currentThread ().interrupt ();
228233 }
229234 });
230235 }
@@ -240,35 +245,43 @@ private static boolean isIntelliJUIVisible(int port) {
240245
241246 private static boolean isHostOnIpAndPortAccessible (String ip , int port ) {
242247 SocketAddress sockaddr = new InetSocketAddress (ip , port );
243- Socket socket = new Socket ();
244-
245- try {
246- socket .connect (sockaddr , 10000 );
247- } catch (IOException IOException ) {
248+ try (Socket socket = new Socket ()) {
249+ connectToHost (socket , sockaddr );
250+ } catch (IOException e ) {
248251 return false ;
249252 }
250253 return true ;
251254 }
252255
256+ private static void connectToHost (Socket socket , SocketAddress sockaddr ) throws IOException {
257+ socket .connect (sockaddr , 10000 );
258+ try {
259+ socket .close ();
260+ } catch (IOException e ) {
261+ LOGGER .log (Level .SEVERE , e .getMessage (), e );
262+ }
263+ }
264+
253265 private static void createDirectoryHierarchy (String location ) {
254266 Path path = Paths .get (location );
255267 try {
256268 Files .createDirectories (path );
257269 } catch (IOException e ) {
258- e . printStackTrace ( );
270+ LOGGER . log ( Level . SEVERE , e . getMessage (), e );
259271 }
260272 }
261273
262274 private static void copyFileFromJarResourceDir (String sourceFileLocation , String destFileLocation ) {
263275 InputStream resourceStream = UITestRunner .class .getClassLoader ().getResourceAsStream (sourceFileLocation );
264- try {
276+ try ( OutputStream outStream = new FileOutputStream ( new File ( destFileLocation ))) {
265277 byte [] buffer = new byte [resourceStream .available ()];
266- resourceStream .read (buffer );
267- File targetFile = new File (destFileLocation );
268- OutputStream outStream = new FileOutputStream (targetFile );
278+ int count = resourceStream .read (buffer );
279+ if (count == 0 ) {
280+ throw new UITestException ("Reading from buffer was unsuccessful." );
281+ }
269282 outStream .write (buffer );
270283 } catch (IOException e ) {
271- e . printStackTrace ( );
284+ LOGGER . log ( Level . SEVERE , e . getMessage (), e );
272285 }
273286 }
274287}
0 commit comments