Skip to content

Commit 561bcb9

Browse files
authored
Merge pull request #5371 from jdi-testing/5370_add_style_exception
Add new style rule for RuntimeException
2 parents 22ea95c + ec24d2d commit 561bcb9

File tree

10 files changed

+27
-16
lines changed

10 files changed

+27
-16
lines changed

config/checkstyle/checkstyle.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,11 @@
352352
<property name="id" value="MatcherBooleanWithoutMessage_Error" />
353353
<property name="fileExtensions" value="java"/>
354354
</module>
355+
<module name="RegexpSingleline">
356+
<property name="severity" value="error" />
357+
<property name="format" value="throw new RuntimeExc" />
358+
<property name="message" value="Use `throw runtimeException('Error Message')` or `throw exception('Error message')`" />
359+
<property name="id" value="MatcherThrowingExceptions_Error" />
360+
<property name="fileExtensions" value="java"/>
361+
</module>
355362
</module>

jdi-light-material-ui/src/main/java/com/epam/jdi/light/material/elements/displaydata/table/MUITable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.epam.jdi.light.material.elements.displaydata.table;
22

33
import com.epam.jdi.light.asserts.generic.HasAssert;
4-
import com.epam.jdi.light.common.Exceptions;
54
import com.epam.jdi.light.common.JDIAction;
65
import com.epam.jdi.light.elements.base.UIBaseElement;
76
import com.epam.jdi.light.elements.common.UIElement;
@@ -21,6 +20,7 @@
2120
import java.util.*;
2221
import java.util.stream.Collectors;
2322

23+
import static com.epam.jdi.light.common.Exceptions.runtimeException;
2424
import static com.epam.jdi.light.driver.WebDriverFactory.jsExecute;
2525

2626
public class MUITable extends UIBaseElement<MUITableAssert> implements HasAssert<MUITableAssert>, ISetup {
@@ -40,7 +40,7 @@ public void setup(Field field) {
4040
boolean hasUI = FillFromAnnotationRules.fieldHasAnnotation(field, UI.class, MUITable.class);
4141
if (!FillFromAnnotationRules.fieldHasAnnotation(field, JMUITable.class, MUITable.class)
4242
&& !hasUI) {
43-
throw Exceptions.runtimeException(String.format("Table '%s' initialisation failed", core().getName()));
43+
throw runtimeException(String.format("Table '%s' initialisation failed", core().getName()));
4444
}
4545
if (hasUI) {
4646
setupFromUI(field);
@@ -62,7 +62,7 @@ private void setupFromUI(Field field) {
6262
columnFilter = new MUITableColumnFilter((JMUITableColumnFilter) uiDefaults.getDeclaredMethod("columnFilter").getDefaultValue());
6363
columnConfig = new MUITableColumnConfig((JMUITableColumnConfig) uiDefaults.getDeclaredMethod("columnConfig").getDefaultValue());
6464
} catch (NoSuchMethodException e) {
65-
throw new RuntimeException(e);
65+
throw runtimeException("Error during MUITable initialization", e);
6666
}
6767
}
6868

jdi-light-material-ui/src/main/java/com/epam/jdi/light/material/elements/feedback/progress/Progress.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import com.epam.jdi.light.material.asserts.feedback.ProgressAssert;
1010

1111
import java.lang.reflect.Field;
12-
import java.util.NoSuchElementException;
1312

13+
import static com.epam.jdi.light.common.Exceptions.runtimeException;
1414
import static com.epam.jdi.light.driver.WebDriverByUtils.NAME_TO_LOCATOR;
1515
import static com.epam.jdi.light.elements.init.UIFactory.$;
1616
import static com.epam.jdi.light.elements.pageobjects.annotations.objects.FillFromAnnotationRules.fieldHasAnnotation;
@@ -49,15 +49,13 @@ public Label label() {
4949
* Gets the current value of progress.
5050
*
5151
* @return current progress value as {@code int}
52-
* @throws NoSuchElementException if 'value' attribute doesn't exist
53-
* (i.e. the progress is indeterminate)
5452
*/
5553
@JDIAction("Get '{name}' value now")
5654
public int getValueNow() {
5755
if (isDeterminate()) {
5856
return Integer.parseInt(core().attr("aria-valuenow"));
5957
}
60-
throw new NoSuchElementException("No exist 'value' attribute");
58+
throw runtimeException("No exist 'value' attribute");
6159
}
6260

6361
/**

jdi-light-mobile/src/main/java/com/epam/jdi/light/mobile/MobileUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
*/
3535
public final class MobileUtils {
3636
private MobileUtils() {
37-
throw new IllegalStateException("Utility class");
3837
}
3938

4039
public static <I> void executeDriverMethod(Class<I> clazz, Consumer<I> consumer) {

jdi-light-mobile/src/main/java/com/epam/jdi/light/mobile/elements/common/app/android/RatingBar.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.epam.jdi.light.mobile.elements.common.app.android;
22

3+
import com.epam.jdi.light.common.JDIAction;
34
import com.epam.jdi.light.mobile.asserts.RatingBarAssert;
45
import com.epam.jdi.light.mobile.elements.base.MobileAppBaseElement;
56
import com.epam.jdi.light.mobile.interfaces.HasTouchActions;
@@ -8,21 +9,26 @@
89
import io.appium.java_client.touch.offset.PointOption;
910
import org.openqa.selenium.Rectangle;
1011

12+
import static com.epam.jdi.light.common.Exceptions.runtimeException;
13+
1114
public class RatingBar extends MobileAppBaseElement implements HasTouchActions {
1215

1316
public double getRating() {
1417
return Double.parseDouble(core().getText());
1518
}
1619

20+
// @todo Check if 1.0 is really the minimum, as typically 0 is minimum
21+
@JDIAction("Setup rating value to {0} with the max value as {1} (Value should not greater then max and not less then 1.0)")
1722
public void setRatingByClick(double value, double fullStars) {
1823
if (value > fullStars || value < 1.0) {
19-
throw new IllegalArgumentException("New value should be in the range [1.0, " + fullStars + "].");
24+
throw runtimeException("New value should be in the range [1.0, " + fullStars + "].");
2025
}
2126
Rectangle position = core().getRect();
2227
int stepLength = (int) (position.width / fullStars);
2328
int offset = (int) (stepLength * (value - 1));
2429
int newRatingPositionX = position.x + offset + (int) (stepLength * 0.6);
2530
int newRatingPositionY = position.y + position.height / 2;
31+
// @todo Change TouchAction in mobile project, as it is depricated
2632
TouchAction touchAction = new TouchAction((PerformsTouchActions) core().driver());
2733
touchAction.tap(PointOption.point(newRatingPositionX, newRatingPositionY)).perform();
2834
}

jdi-light-mobile/src/main/java/com/epam/jdi/light/mobile/elements/complex/Spinner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.List;
1515
import java.util.NoSuchElementException;
1616

17+
import static com.epam.jdi.light.common.Exceptions.exception;
1718
import static com.epam.jdi.light.driver.WebDriverFactory.getDriver;
1819

1920
public class Spinner extends MobileAppBaseElement<TextAssert>
@@ -44,7 +45,7 @@ public void hasItems(String[] items) {
4445
} catch (NoSuchElementException e) {
4546
StringBuilder builder = new StringBuilder();
4647
elements.forEach(el -> builder.append(el.getText()).append(", "));
47-
throw new NoSuchElementException("One of the expected items is missing in the spinner list. \nExpected:\n" +
48+
throw exception(e, "One of the expected items is missing in the spinner list. \nExpected:\n" +
4849
Arrays.asList(items) + "\nActual:\n" + builder);
4950
}
5051
}

jdi-light-vuetify/src/main/java/com/epam/jdi/light/vuetify/elements/complex/Dialog.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.epam.jdi.light.vuetify.elements.common.VuetifyButton;
99
import com.epam.jdi.light.vuetify.interfaces.HasTheme;
1010

11+
import static com.epam.jdi.light.common.Exceptions.runtimeException;
1112
import static org.openqa.selenium.Keys.ESCAPE;
1213

1314
/**
@@ -79,7 +80,7 @@ public void close() {
7980
dialogWindow().focus();
8081
press(ESCAPE);
8182
} else {
82-
throw new RuntimeException("Dialog cannot be closed by pressing esc key");
83+
throw runtimeException("Dialog cannot be closed by pressing esc key");
8384
}
8485
}
8586

jdi-light-vuetify/src/main/java/com/epam/jdi/light/vuetify/elements/complex/TreeViewNode.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public UIElement iCore() {
122122
@Override
123123
@JDIAction("Get '{name}' text value")
124124
public String getValue() {
125-
126125
return iCore().getText();
127126
}
128127

@@ -140,7 +139,6 @@ public int size() {
140139

141140
@JDIAction("Get '{name}' check list")
142141
public WebList checkList() {
143-
144142
return new WebList();
145143
}
146144

@@ -352,7 +350,7 @@ public void refresh() {
352350

353351
@Override
354352
public void clear() {
355-
throw new UnsupportedOperationException();
353+
throw runtimeException("Clear operation is not supported by TreeViewNode");
356354
}
357355

358356
protected TreeViewNode create(UIElement base) {

jdi-light-vuetify/src/main/java/com/epam/jdi/light/vuetify/elements/complex/bars/AppBar.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.epam.jdi.light.vuetify.asserts.bars.AppBarAssert;
66
import com.epam.jdi.light.vuetify.elements.common.Image;
77

8+
import static com.epam.jdi.light.common.Exceptions.runtimeException;
89
import static com.epam.jdi.light.driver.WebDriverFactory.jsExecute;
910

1011
/**
@@ -33,7 +34,7 @@ public Image backgroundImage() {
3334
if (hasBackgroundImage()) {
3435
return new Image().setCore(Image.class, core().find(IMAGE_LOCATOR));
3536
} else {
36-
throw new RuntimeException("Element doesn't have image");
37+
throw runtimeException("Element doesn't have image");
3738
}
3839
}
3940

jdi-light-vuetify/src/main/java/com/epam/jdi/light/vuetify/elements/complex/tables/DataTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void groupBy(String colName) {
202202
return;
203203
}
204204
}
205-
205+
// @todo add a public method to check if grouping by column is available
206206
//check if it is already grouped by
207207
if (groups.isEmpty()) {
208208
throw runtimeException(String.format("Grouping by column '%s' is not available", colName));

0 commit comments

Comments
 (0)