Skip to content

Commit 51ca6be

Browse files
committed
Use Preconditions.checkState() from Google Guava.
No functional changes.
1 parent 2d9fa4a commit 51ca6be

File tree

5 files changed

+48
-45
lines changed

5 files changed

+48
-45
lines changed

src/test/java/ru/mystamps/web/tests/cases/WhenUserAtAnyPage.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import static org.fest.assertions.Assertions.assertThat;
2929

30+
import static com.google.common.base.Preconditions.checkState;
31+
3032
import static ru.mystamps.web.SiteMap.AUTHENTICATION_PAGE_URL;
3133
import static ru.mystamps.web.SiteMap.REGISTRATION_PAGE_URL;
3234
import static ru.mystamps.web.tests.TranslationUtils.tr;
@@ -93,12 +95,11 @@ private void checkServerResponseCode() {
9395
}
9496

9597
private void shouldHaveTitle() {
96-
if (title == null) {
97-
throw new IllegalStateException(
98-
"Page title was not set!"
99-
+ " Did you call hasTitle() or hasTitleWithoutStandardPrefix() before?"
100-
);
101-
}
98+
checkState(
99+
title != null,
100+
"Page title was not set!"
101+
+ " Did you call hasTitle() or hasTitleWithoutStandardPrefix() before?"
102+
);
102103

103104
assertThat(page.getTitle())
104105
.overridingErrorMessage("title should be '" + title + "'")

src/test/java/ru/mystamps/web/tests/fest/AbstractPageWithFormAssert.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.fest.assertions.GenericAssert;
2222
import org.fest.assertions.Assertions;
2323

24+
import static com.google.common.base.Preconditions.checkState;
25+
2426
import ru.mystamps.web.tests.page.AbstractPageWithForm;
2527

2628
/**
@@ -48,7 +50,7 @@ public AbstractPageWithFormAssert field(final String fieldName) {
4850

4951
public AbstractPageWithFormAssert hasError(final String expectedErrorMessage) {
5052
isNotNull();
51-
checkThatFieldNameNotNull();
53+
checkState(fieldName != null, "Error in test case: field name should be specified");
5254

5355
final String errorMessage = actual.getFieldError(fieldName);
5456

@@ -68,7 +70,7 @@ public AbstractPageWithFormAssert hasError(final String expectedErrorMessage) {
6870

6971
public AbstractPageWithFormAssert hasNoError() {
7072
isNotNull();
71-
checkThatFieldNameNotNull();
73+
checkState(fieldName != null, "Error in test case: field name should be specified");
7274

7375
final String msg = String.format(
7476
"Expected that field '%s' should not have any error",
@@ -84,7 +86,7 @@ public AbstractPageWithFormAssert hasNoError() {
8486

8587
public AbstractPageWithFormAssert hasValue(final String expectedValue) {
8688
isNotNull();
87-
checkThatFieldNameNotNull();
89+
checkState(fieldName != null, "Error in test case: field name should be specified");
8890

8991
final String value = actual.getFieldValue(fieldName);
9092

@@ -102,10 +104,4 @@ public AbstractPageWithFormAssert hasValue(final String expectedValue) {
102104
return this;
103105
}
104106

105-
private void checkThatFieldNameNotNull() {
106-
if (fieldName == null) {
107-
throw new IllegalArgumentException("Error in test case: field name not specified");
108-
}
109-
}
110-
111107
}

src/test/java/ru/mystamps/web/tests/page/AbstractPageWithForm.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import lombok.Getter;
2727

28+
import static com.google.common.base.Preconditions.checkState;
29+
2830
import ru.mystamps.web.tests.WebElementUtils;
2931
import ru.mystamps.web.tests.page.element.Form;
3032
import ru.mystamps.web.tests.page.element.Form.Field;
@@ -60,30 +62,29 @@ public boolean isSubmitButtonExists(final SubmitButton button) {
6062
}
6163

6264
public void submit() {
63-
if (form == null) {
64-
throw new IllegalStateException(
65-
"You are trying to submit form at page which does not have form"
66-
);
67-
}
65+
66+
checkState(
67+
form != null,
68+
"You are trying to submit form at page which does not have form"
69+
);
6870

6971
final List<SubmitButton> buttons = form.getSubmitButtons();
70-
if (buttons.isEmpty()) {
71-
throw new IllegalStateException(
72-
"You are trying to submit form at page which does not have submit button"
73-
);
74-
}
72+
73+
checkState(
74+
!buttons.isEmpty(),
75+
"You are trying to submit form at page which does not have submit button"
76+
);
7577

7678
final String xpathOfFirstSubmitButton = buttons.get(0).toString();
7779

7880
getElementByXPath(xpathOfFirstSubmitButton).submit();
7981
}
8082

8183
public boolean formExists() {
82-
if (form == null) {
83-
throw new IllegalStateException(
84-
"You are trying to check form at page which does not has form"
85-
);
86-
}
84+
checkState(
85+
form != null,
86+
"You are trying to check form at page which does not has form"
87+
);
8788

8889
return elementWithXPathExists(form.toString());
8990
}
@@ -97,11 +98,10 @@ public boolean inputHasAsterisk(final String id) {
9798
}
9899

99100
public String getFieldValue(final String name) {
100-
if (form == null) {
101-
throw new IllegalStateException(
102-
"You are trying to find field at page which does not have form"
103-
);
104-
}
101+
checkState(
102+
form != null,
103+
"You are trying to find field at page which does not have form"
104+
);
105105

106106
final String xpathField = form.getField(name).toString();
107107

src/test/java/ru/mystamps/web/tests/page/ActivateAccountPage.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.openqa.selenium.WebDriver;
2222

23+
import static com.google.common.base.Preconditions.checkState;
24+
2325
import static ru.mystamps.web.SiteMap.ACTIVATE_ACCOUNT_PAGE_URL;
2426

2527
import static ru.mystamps.web.tests.TranslationUtils.tr;
@@ -73,16 +75,17 @@ public void activateAccount(
7375
final String passwordConfirmation,
7476
final String activationKey) {
7577

76-
if (login == null
78+
final boolean allFieldsAreNull =
79+
login == null
7780
&& name == null
7881
&& password == null
7982
&& passwordConfirmation == null
80-
&& activationKey == null) {
81-
82-
throw new IllegalStateException(
83-
"Login, name, password with confirmation and activation key should not be null"
84-
);
85-
}
83+
&& activationKey == null;
84+
85+
checkState(
86+
!allFieldsAreNull,
87+
"Login, name, password with confirmation and activation key should not be null"
88+
);
8689

8790
fillLogin(login);
8891
fillName(name);

src/test/java/ru/mystamps/web/tests/page/AuthAccountPage.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.openqa.selenium.WebDriver;
2222

23+
import static com.google.common.base.Preconditions.checkState;
24+
2325
import static ru.mystamps.web.SiteMap.AUTHENTICATION_PAGE_URL;
2426

2527
import static ru.mystamps.web.tests.TranslationUtils.tr;
@@ -57,9 +59,10 @@ public boolean authenticationFormExists() {
5759
}
5860

5961
public void authorizeUser(final String login, final String password) {
60-
if (login == null && password == null) {
61-
throw new IllegalStateException("Login and password should not be a null");
62-
}
62+
checkState(
63+
!(login == null && password == null),
64+
"Login and password should not be a null"
65+
);
6366

6467
fillLogin(login);
6568
fillPassword(password);

0 commit comments

Comments
 (0)