Skip to content

Commit e441665

Browse files
committed
Fix the issues reported from the SonarCloud
Fixes #62 Signed-off-by: Zbynek Cervinka <[email protected]>
1 parent a473d47 commit e441665

File tree

45 files changed

+459
-410
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

+459
-410
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: 55 additions & 40 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,10 @@
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());
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
}
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)