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 ());
4248 private static RemoteRobot remoteRobot = null ;
4349 private static Process ideProcess ;
4450 private static IdeaVersion ideaVersion ;
@@ -65,8 +71,8 @@ public static RemoteRobot runIde(IdeaVersion ideaVersion, int port) {
6571 ideProcess = pb .start ();
6672 waitUntilIntelliJStarts (port );
6773 remoteRobot = getRemoteRobotConnection (port );
68- } catch (IOException | InterruptedException e ) {
69- e . printStackTrace ( );
74+ } catch (IOException e ) {
75+ LOGGER . log ( Level . SEVERE , e . getMessage (), e );
7076 }
7177
7278 remoteRobot .find (FlatWelcomeFrame .class , Duration .ofSeconds (10 )).clearWorkspace ();
@@ -81,7 +87,7 @@ public static RemoteRobot runIde(IdeaVersion ideaVersion, int port) {
8187 * @return instance of the RemoteRobot
8288 */
8389 public static RemoteRobot runIde (IdeaVersion ideaVersion ) {
84- return runIde (ideaVersion , defaultPort );
90+ return runIde (ideaVersion , DEFAULT_PORT );
8591 }
8692
8793 /**
@@ -121,9 +127,8 @@ public static RemoteRobot getRemoteRobot() {
121127 *
122128 * @param port port number
123129 * @return instance of the RemoteRobot
124- * @throws InterruptedException may be thrown in Thread.sleep()
125130 */
126- public static RemoteRobot getRemoteRobotConnection (int port ) throws InterruptedException {
131+ public static RemoteRobot getRemoteRobotConnection (int port ) {
127132 return step ("Create an instance of the RemoteRobot listening on port " + port , () -> {
128133 RemoteRobot remoteRobot = new RemoteRobot ("http://127.0.0.1:" + port );
129134 for (int i = 0 ; i < 60 ; i ++) {
@@ -133,7 +138,8 @@ public static RemoteRobot getRemoteRobotConnection(int port) throws InterruptedE
133138 try {
134139 Thread .sleep (1000 );
135140 } catch (InterruptedException e2 ) {
136- e2 .printStackTrace ();
141+ LOGGER .log (Level .SEVERE , e2 .getMessage (), e2 );
142+ Thread .currentThread ().interrupt ();
137143 }
138144 }
139145 }
@@ -149,7 +155,7 @@ public enum IdeaVersion {
149155 V_2020_2 ("IC-2020.2" ),
150156 V_2020_3 ("IC-2020.3" );
151157
152- final private String ideaVersionStringRepresentation ;
158+ private final String ideaVersionStringRepresentation ;
153159
154160 IdeaVersion (String ideaVersionStringRepresentation ) {
155161 this .ideaVersionStringRepresentation = ideaVersionStringRepresentation ;
@@ -168,63 +174,64 @@ public int toInt() {
168174
169175 private static void makeSureAllTermsAndConditionsAreAccepted () {
170176 String osName = System .getProperty ("os.name" ).toLowerCase ();
177+ String userHome = "user.home" ;
171178
172179 if (osName .contains ("linux" )) {
173180 step ("Copy the 'prefs.xml' file to the appropriate location" , () -> {
174181 String prefsXmlSourceLocation = "prefs.xml" ;
175- String prefsXmlDir = System .getProperty ("user.home" ) + "/.java/.userPrefs/jetbrains/_!(!!cg\" p!(}!}@\" j!(k!|w\" w!'8!b!\" p!':!e@==" ;
182+ String prefsXmlDir = System .getProperty (userHome ) + "/.java/.userPrefs/jetbrains/_!(!!cg\" p!(}!}@\" j!(k!|w\" w!'8!b!\" p!':!e@==" ;
176183 createDirectoryHierarchy (prefsXmlDir );
177184 copyFileFromJarResourceDir (prefsXmlSourceLocation , prefsXmlDir + "/prefs.xml" );
178185 });
179186
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" ;
187+ step (COPY_ACCEPTED_FILE_STEP_DESCRIPTION , () -> {
188+ String acceptedDir = System .getProperty (userHome ) + "/.local/share/JetBrains/consentOptions" ;
183189 createDirectoryHierarchy (acceptedDir );
184- copyFileFromJarResourceDir (acceptedSourceLocation , acceptedDir + "/accepted" );
190+ copyFileFromJarResourceDir (ACCEPTED_SOURCE_LOCATION , acceptedDir + "/accepted" );
185191 });
186192 } else if (osName .contains ("os x" )) {
187193 step ("Copy the 'com.apple.java.util.prefs.plist' file to the appropriate location" , () -> {
188194 String plistSourceLocation = "com.apple.java.util.prefs.plist" ;
189- String plistDir = System .getProperty ("user.home" ) + "/Library/Preferences" ;
195+ String plistDir = System .getProperty (userHome ) + "/Library/Preferences" ;
190196 copyFileFromJarResourceDir (plistSourceLocation , plistDir + "/com.apple.java.util.prefs.plist" );
191197 });
192198
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" ;
199+ step (COPY_ACCEPTED_FILE_STEP_DESCRIPTION , () -> {
200+ String acceptedDir = System .getProperty (userHome ) + "/Library/Application Support/JetBrains/consentOptions" ;
196201 createDirectoryHierarchy (acceptedDir );
197- copyFileFromJarResourceDir (acceptedSourceLocation , acceptedDir + "/accepted" );
202+ copyFileFromJarResourceDir (ACCEPTED_SOURCE_LOCATION , acceptedDir + "/accepted" );
198203
199204 // run the 'killall cfprefsd' cmd to force OS X to reload preferences files
200- ProcessBuilder pb = new ProcessBuilder ("killall" , "cfprefsd" );
205+ ProcessBuilder pb = new ProcessBuilder ("/usr/bin/ killall" , "cfprefsd" );
201206 try {
202207 Process p = pb .start ();
203208 p .waitFor ();
204209 } catch (IOException | InterruptedException e ) {
205- e .printStackTrace ();
210+ LOGGER .log (Level .SEVERE , e .getMessage (), e );
211+ Thread .currentThread ().interrupt ();
206212 }
207213 });
208214 } else if (osName .contains ("windows" )) {
209- step ("Copy the 'accepted' file to the appropriate location" , () -> {
210- String acceptedSourceLocation = "accepted" ;
215+ step (COPY_ACCEPTED_FILE_STEP_DESCRIPTION , () -> {
211216 String acceptedDir = System .getProperty ("user.home" ) + "\\ AppData\\ Roaming\\ JetBrains\\ consentOptions" ;
212217 createDirectoryHierarchy (acceptedDir );
213- copyFileFromJarResourceDir (acceptedSourceLocation , acceptedDir + "\\ accepted" );
218+ copyFileFromJarResourceDir (ACCEPTED_SOURCE_LOCATION , acceptedDir + "\\ accepted" );
214219 });
215220
216221 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'" );
222+ String registryPath = "HKCU:" + "\\ Software\\ JavaSoft\\ Prefs\\ jetbrains\\ privacy_policy" ;
223+ String powershellLocation = "C:\\ Windows\\ system32\\ WindowsPowerShell\\ v1.0\\ powershell.exe" ;
224+ ProcessBuilder pb1 = new ProcessBuilder (powershellLocation , "New-Item" , "-Path" , registryPath , "-Force" );
225+ ProcessBuilder pb2 = new ProcessBuilder (powershellLocation , "New-ItemProperty" , "-Path" , registryPath , "-Name" , "accepted_version" , "-Value" , "'2.1'" );
220226
221227 try {
222228 Process p1 = pb1 .start ();
223229 p1 .waitFor ();
224230 Process p2 = pb2 .start ();
225231 p2 .waitFor ();
226232 } catch (IOException | InterruptedException e ) {
227- e .printStackTrace ();
233+ LOGGER .log (Level .SEVERE , e .getMessage (), e );
234+ Thread .currentThread ().interrupt ();
228235 }
229236 });
230237 }
@@ -240,35 +247,43 @@ private static boolean isIntelliJUIVisible(int port) {
240247
241248 private static boolean isHostOnIpAndPortAccessible (String ip , int port ) {
242249 SocketAddress sockaddr = new InetSocketAddress (ip , port );
243- Socket socket = new Socket ();
244-
245- try {
246- socket .connect (sockaddr , 10000 );
247- } catch (IOException IOException ) {
250+ try (Socket socket = new Socket ()) {
251+ connectToHost (socket , sockaddr );
252+ } catch (IOException e ) {
248253 return false ;
249254 }
250255 return true ;
251256 }
252257
258+ private static void connectToHost (Socket socket , SocketAddress sockaddr ) throws IOException {
259+ socket .connect (sockaddr , 10000 );
260+ try {
261+ socket .close ();
262+ } catch (IOException e ) {
263+ LOGGER .log (Level .SEVERE , e .getMessage (), e );
264+ }
265+ }
266+
253267 private static void createDirectoryHierarchy (String location ) {
254268 Path path = Paths .get (location );
255269 try {
256270 Files .createDirectories (path );
257271 } catch (IOException e ) {
258- e . printStackTrace ( );
272+ LOGGER . log ( Level . SEVERE , e . getMessage (), e );
259273 }
260274 }
261275
262276 private static void copyFileFromJarResourceDir (String sourceFileLocation , String destFileLocation ) {
263277 InputStream resourceStream = UITestRunner .class .getClassLoader ().getResourceAsStream (sourceFileLocation );
264- try {
278+ try ( OutputStream outStream = new FileOutputStream ( new File ( destFileLocation ))) {
265279 byte [] buffer = new byte [resourceStream .available ()];
266- resourceStream .read (buffer );
267- File targetFile = new File (destFileLocation );
268- OutputStream outStream = new FileOutputStream (targetFile );
280+ int count = resourceStream .read (buffer );
281+ if (count == 0 ) {
282+ throw new UITestException ("Reading from buffer was unsuccessful." );
283+ }
269284 outStream .write (buffer );
270285 } catch (IOException e ) {
271- e . printStackTrace ( );
286+ LOGGER . log ( Level . SEVERE , e . getMessage (), e );
272287 }
273288 }
274289}
0 commit comments