Skip to content

Commit 4b46729

Browse files
authored
Merge pull request #64 from zcervink/62
Fix the issues reported from the SonarCloud
2 parents a473d47 + 8dfae82 commit 4b46729

File tree

45 files changed

+465
-418
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+465
-418
lines changed

.github/workflows/mac.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will build a Java project with Gradle
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
33

4-
name: Java CI with Gradle - maccOS
4+
name: Java CI with Gradle - macOS
55

66
on:
77
push:

src/main/java/com/redhat/devtools/intellij/commonUiTestLibrary/utils/labels/ButtonLabels.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/java/com/redhat/devtools/intellij/commonUiTestLibrary/UITestRunner.java renamed to src/main/java/com/redhat/devtools/intellij/commonuitest/UITestRunner.java

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
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

1313
import com.intellij.remoterobot.RemoteRobot;
1414
import com.intellij.remoterobot.stepsProcessing.StepLogger;
1515
import com.intellij.remoterobot.stepsProcessing.StepWorker;
1616
import 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

1920
import java.io.File;
2021
import java.io.FileOutputStream;
@@ -28,6 +29,8 @@
2829
import java.nio.file.Path;
2930
import java.nio.file.Paths;
3031
import java.time.Duration;
32+
import java.util.logging.Level;
33+
import java.util.logging.Logger;
3134

3235
import static com.intellij.remoterobot.stepsProcessing.StepWorkerKt.step;
3336
import static com.intellij.remoterobot.utils.RepeatUtilsKt.waitFor;
@@ -38,7 +41,12 @@
3841
3942
*/
4043
public 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
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Contributors:
99
* Red Hat, Inc. - initial API and implementation
1010
******************************************************************************/
11-
package com.redhat.devtools.intellij.commonUiTestLibrary.exceptions;
11+
package com.redhat.devtools.intellij.commonuitest.exceptions;
1212

1313
/**
1414
* IntelliJ UI test library runtime exception
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Contributors:
99
* Red Hat, Inc. - initial API and implementation
1010
******************************************************************************/
11-
package com.redhat.devtools.intellij.commonUiTestLibrary.fixtures.dialogs;
11+
package com.redhat.devtools.intellij.commonuitest.fixtures.dialogs;
1212

1313
import com.intellij.remoterobot.RemoteRobot;
1414
import com.intellij.remoterobot.data.RemoteComponent;
@@ -21,8 +21,8 @@
2121
import com.intellij.remoterobot.fixtures.JPopupMenuFixture;
2222
import com.intellij.remoterobot.utils.UtilsKt;
2323
import com.intellij.remoterobot.utils.WaitForConditionTimeoutException;
24-
import com.redhat.devtools.intellij.commonUiTestLibrary.UITestRunner;
25-
import com.redhat.devtools.intellij.commonUiTestLibrary.fixtures.dialogs.errors.IdeFatalErrorsDialog;
24+
import com.redhat.devtools.intellij.commonuitest.UITestRunner;
25+
import com.redhat.devtools.intellij.commonuitest.fixtures.dialogs.errors.IdeFatalErrorsDialog;
2626
import org.apache.commons.io.FileUtils;
2727
import org.jetbrains.annotations.NotNull;
2828

@@ -32,6 +32,8 @@
3232
import java.nio.file.Paths;
3333
import java.time.Duration;
3434
import java.util.List;
35+
import java.util.logging.Level;
36+
import java.util.logging.Logger;
3537

3638
import static com.intellij.remoterobot.search.locators.Locators.byXpath;
3739

@@ -43,6 +45,7 @@
4345
@DefaultXpath(by = "FlatWelcomeFrame type", xpath = "//div[@class='FlatWelcomeFrame']")
4446
@FixtureName(name = "Welcome To IntelliJ IDEA Dialog")
4547
public class FlatWelcomeFrame extends CommonContainerFixture {
48+
private static final Logger LOGGER = Logger.getLogger(FlatWelcomeFrame.class.getName());
4649
private UITestRunner.IdeaVersion intelliJVersion;
4750

4851
public FlatWelcomeFrame(@NotNull RemoteRobot remoteRobot, @NotNull RemoteComponent remoteComponent) {
@@ -98,7 +101,7 @@ public void clearWorkspace() {
98101
Files.createDirectory(Paths.get(pathToDirToMakeEmpty));
99102
}
100103
} catch (IOException e) {
101-
e.printStackTrace();
104+
LOGGER.log(Level.SEVERE, e.getMessage(), e);
102105
}
103106
}
104107

@@ -109,7 +112,7 @@ public void clearExceptions() {
109112
try {
110113
ideErrorsIcon().click();
111114
} catch (WaitForConditionTimeoutException e) {
112-
e.printStackTrace();
115+
LOGGER.log(Level.SEVERE, e.getMessage(), e);
113116
return;
114117
}
115118

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
* Contributors:
99
* Red Hat, Inc. - initial API and implementation
1010
******************************************************************************/
11-
package com.redhat.devtools.intellij.commonUiTestLibrary.fixtures.dialogs.errors;
11+
package com.redhat.devtools.intellij.commonuitest.fixtures.dialogs.errors;
1212

1313
import com.intellij.remoterobot.RemoteRobot;
1414
import com.intellij.remoterobot.data.RemoteComponent;
1515
import com.intellij.remoterobot.fixtures.CommonContainerFixture;
1616
import com.intellij.remoterobot.fixtures.DefaultXpath;
1717
import com.intellij.remoterobot.fixtures.FixtureName;
18-
import com.redhat.devtools.intellij.commonUiTestLibrary.utils.labels.ButtonLabels;
18+
import com.redhat.devtools.intellij.commonuitest.utils.labels.ButtonLabels;
1919
import org.jetbrains.annotations.NotNull;
2020

2121
/**
@@ -34,6 +34,6 @@ public IdeFatalErrorsDialog(@NotNull RemoteRobot remoteRobot, @NotNull RemoteCom
3434
* Click on the 'Clear all' button
3535
*/
3636
public void clearAll() {
37-
button(ButtonLabels.clearAllLabel).click();
37+
button(ButtonLabels.CLEAR_ALL_LABEL).click();
3838
}
3939
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
* Contributors:
99
* Red Hat, Inc. - initial API and implementation
1010
******************************************************************************/
11-
package com.redhat.devtools.intellij.commonUiTestLibrary.fixtures.dialogs.information;
11+
package com.redhat.devtools.intellij.commonuitest.fixtures.dialogs.information;
1212

1313
import com.intellij.remoterobot.RemoteRobot;
1414
import com.intellij.remoterobot.data.RemoteComponent;
1515
import com.intellij.remoterobot.fixtures.CommonContainerFixture;
1616
import com.intellij.remoterobot.fixtures.DefaultXpath;
1717
import com.intellij.remoterobot.fixtures.FixtureName;
18-
import com.redhat.devtools.intellij.commonUiTestLibrary.utils.labels.ButtonLabels;
18+
import com.redhat.devtools.intellij.commonuitest.utils.labels.ButtonLabels;
1919
import org.jetbrains.annotations.NotNull;
2020

2121
/**
@@ -34,6 +34,6 @@ public ProjectStructureDialog(@NotNull RemoteRobot remoteRobot, @NotNull RemoteC
3434
* Cancel the 'Project Structure'
3535
*/
3636
public void cancel() {
37-
button(ButtonLabels.cancelLabel).click();
37+
button(ButtonLabels.CANCEL_LABEL).click();
3838
}
3939
}

0 commit comments

Comments
 (0)