Skip to content

Commit 2d9fa4a

Browse files
committed
Use Preconditions.checkArgument() from Google Guava.
Also added Google Guava library to project dependencies. No functional changes.
1 parent 0d5045d commit 2d9fa4a

File tree

6 files changed

+29
-50
lines changed

6 files changed

+29
-50
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@
203203
<version>2.0.8</version>
204204
</dependency>
205205

206+
<dependency>
207+
<groupId>com.google.guava</groupId>
208+
<artifactId>guava</artifactId>
209+
<version>r09</version>
210+
</dependency>
211+
206212
</dependencies>
207213

208214
<build>

src/main/config/checkstyle-suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<suppressions>
77

88
<suppress checks="LineLength" files="AbstractPageWithForm.java" lines="27,28,33" />
9-
<suppress checks="LineLength" files="Form.java" lines="30,31,36" />
9+
<suppress checks="LineLength" files="Form.java" lines="32,33,38" />
1010

1111
<!-- false positives due to Lombok usage -->
1212
<suppress checks="HideUtilityClassConstructor" files="ru.mystamps.web.model" />

src/main/java/ru/mystamps/web/service/SiteService.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.springframework.stereotype.Service;
3030
import org.springframework.transaction.annotation.Transactional;
3131

32+
import static com.google.common.base.Preconditions.checkArgument;
33+
3234
import ru.mystamps.web.dao.SuspiciousActivityDao;
3335
import ru.mystamps.web.dao.SuspiciousActivityTypeDao;
3436
import ru.mystamps.web.dao.UserDao;
@@ -79,13 +81,8 @@ private void log(
7981
final String referer,
8082
final String agent) {
8183

82-
if (type == null) {
83-
throw new IllegalArgumentException("Type of suspicious activity was not set");
84-
}
85-
86-
if (page == null) {
87-
throw new IllegalArgumentException("Page should be non null");
88-
}
84+
checkArgument(type != null, "Type of suspicious activity was not set");
85+
checkArgument(page != null, "Page should be non null");
8986

9087
final SuspiciousActivity activity = new SuspiciousActivity();
9188
activity.setType(type);

src/main/java/ru/mystamps/web/service/UserService.java

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.stereotype.Service;
3232
import org.springframework.transaction.annotation.Transactional;
3333

34+
import static com.google.common.base.Preconditions.checkArgument;
35+
3436
import ru.mystamps.web.entity.User;
3537
import ru.mystamps.web.entity.UsersActivation;
3638
import ru.mystamps.web.dao.UserDao;
@@ -49,9 +51,7 @@ public class UserService {
4951

5052
@Transactional
5153
public void addRegistrationRequest(final String email) {
52-
if (email == null) {
53-
throw new IllegalArgumentException("Email should be non null");
54-
}
54+
checkArgument(email != null, "Email should be non null");
5555

5656
final UsersActivation activation = new UsersActivation();
5757

@@ -65,9 +65,7 @@ public void addRegistrationRequest(final String email) {
6565
public UsersActivation findRegistrationRequestByActivationKey(
6666
final String activationKey) {
6767

68-
if (activationKey == null) {
69-
throw new IllegalArgumentException("Activation key should be non null");
70-
}
68+
checkArgument(activationKey != null, "Activation key should be non null");
7169

7270
return usersActivation.findByActivationKey(activationKey);
7371
}
@@ -76,21 +74,10 @@ public UsersActivation findRegistrationRequestByActivationKey(
7674
public void registerUser(final String login, final String password,
7775
final String name, final String activationKey) {
7876

79-
if (login == null) {
80-
throw new IllegalArgumentException("Login should be non null");
81-
}
82-
83-
if (password == null) {
84-
throw new IllegalArgumentException("Password should be non null");
85-
}
86-
87-
if (name == null) {
88-
throw new IllegalArgumentException("Name should be non null");
89-
}
90-
91-
if (activationKey == null) {
92-
throw new IllegalArgumentException("Activation key should be non null");
93-
}
77+
checkArgument(login != null, "Login should be non null");
78+
checkArgument(password != null, "Password should be non null");
79+
checkArgument(name != null, "Name should be non null");
80+
checkArgument(activationKey != null, "Activation key should be non null");
9481

9582
// use login as name if name is not provided
9683
final String finalName;
@@ -134,22 +121,15 @@ public void registerUser(final String login, final String password,
134121

135122
@Transactional(readOnly = true)
136123
public User findByLogin(final String login) {
137-
if (login == null) {
138-
throw new IllegalArgumentException("Login should be non null");
139-
}
124+
checkArgument(login != null, "Login should be non null");
140125

141126
return users.findByLogin(login);
142127
}
143128

144129
@Transactional(readOnly = true)
145130
public User findByLoginAndPassword(final String login, final String password) {
146-
if (login == null) {
147-
throw new IllegalArgumentException("Login should be non null");
148-
}
149-
150-
if (password == null) {
151-
throw new IllegalArgumentException("Password should be non null");
152-
}
131+
checkArgument(login != null, "Login should be non null");
132+
checkArgument(password != null, "Password should be non null");
153133

154134
final User user = users.findByLogin(login);
155135
if (user == null) {

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.openqa.selenium.support.CacheLookup;
3131
import org.openqa.selenium.support.FindBy;
3232

33+
import static com.google.common.base.Preconditions.checkArgument;
34+
3335
import ru.mystamps.web.tests.WebElementUtils;
3436

3537
import static ru.mystamps.web.SiteMap.SITE_URL;
@@ -232,13 +234,8 @@ protected String getLinkHref(final WebElement link) {
232234
}
233235

234236
public void login(final String login, final String password) {
235-
if ("".equals(login)) {
236-
throw new IllegalArgumentException("login must be not null and not empty");
237-
}
238-
239-
if ("".equals(password)) {
240-
throw new IllegalArgumentException("password must be not null and not empty");
241-
}
237+
checkArgument(!"".equals(login), "login must be not null and not empty");
238+
checkArgument(!"".equals(password), "password must be not null and not empty");
242239

243240
// TODO: check than we already authenticated and do nothing
244241
final AuthAccountPage authPage = new AuthAccountPage(driver);

src/test/java/ru/mystamps/web/tests/page/element/Form.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import lombok.Getter;
2525
import lombok.Setter;
2626

27+
import static com.google.common.base.Preconditions.checkArgument;
28+
2729
public final class Form {
2830
private static final String FORM_LOCATOR = "//form";
2931
private static final String INPUT_FIELD_LOCATOR = "//input[@name=\"%s\"]";
@@ -194,10 +196,7 @@ public boolean hasInvalidValue() {
194196
}
195197

196198
public Field invalidValue(final String invalidValue) {
197-
if ("".equals(invalidValue)) {
198-
throw new IllegalArgumentException("Invalid value for field should be non empty");
199-
}
200-
199+
checkArgument(!"".equals(invalidValue), "Invalid value for field should be non empty");
201200
this.invalidValue = invalidValue;
202201
return this;
203202
}

0 commit comments

Comments
 (0)