Skip to content
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3358f16
[fix]bugfix:Prevent Long.parseLong() error when search param is a float
starryCoder Jun 20, 2025
63e7379
Merge branch 'master' into fix-search-float-parsing-error
starryCoder Jun 20, 2025
de247da
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 20, 2025
e771f97
Merge branch 'master' into fix-search-float-parsing-error
tomsun28 Jun 20, 2025
9170f9a
add unit tests for getMonitors and isLong
starryCoder Jun 21, 2025
05ee462
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 21, 2025
b4939fc
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 21, 2025
68ac004
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 22, 2025
fab91f4
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 22, 2025
4361986
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 23, 2025
3ff5deb
Merge branch 'master' into fix-search-float-parsing-error
Calvin979 Jun 23, 2025
d3c4a53
Merge branch 'master' into fix-search-float-parsing-error
lynx009 Jun 24, 2025
bda9565
Merge branch 'master' into fix-search-float-parsing-error
lynx009 Jun 24, 2025
7e66038
Merge branch 'master' into fix-search-float-parsing-error
Calvin979 Jun 27, 2025
28e1430
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 28, 2025
1c4ccd1
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 28, 2025
2c3ee59
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jun 29, 2025
e78cb91
Merge branch 'master' into fix-search-float-parsing-error
Calvin979 Jun 30, 2025
9f1a82c
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jul 1, 2025
32b8271
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jul 1, 2025
7992e1b
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jul 2, 2025
7402633
Merge branch 'master' into fix-search-float-parsing-error
yuluo-yx Jul 2, 2025
df1c3e2
Merge branch 'master' into fix-search-float-parsing-error
pwallk Jul 6, 2025
6cc3151
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jul 10, 2025
d6c2c89
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Jul 10, 2025
93d9562
Merge branch 'master' into fix-search-float-parsing-error
yuluo-yx Jul 12, 2025
917d1c9
Merge branch 'master' into fix-search-float-parsing-error
Aias00 Aug 5, 2025
12e65a9
Merge branch 'master' into fix-search-float-parsing-error
yuluo-yx Aug 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public final class CommonUtil {
private static final Pattern PHONE_PATTERN = Pattern.compile("^(((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(19[0-9])|(18[0-9])|(17[0-9]))+\\d{8})?$");

private static final Pattern NUMBER_PATTERN = Pattern.compile("^[-+]?[0-9]*\\.?[0-9]+$");

private static final Pattern LONG_PATTERN = Pattern.compile("^[-+]?[0-9]+$");

private static final int PHONE_LENGTH = 11;

Expand Down Expand Up @@ -96,6 +98,18 @@ public static boolean isNumeric(String str) {
return NUMBER_PATTERN.matcher(str).matches();
}

/**
* whether the string is Long
* @param str string
* @return boolean
*/
public static boolean isLong(String str) {
if (StringUtils.isBlank(str)) {
return false;
}
return LONG_PATTERN.matcher(str).matches();
}
Comment on lines +106 to +111
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi can you help add some unit test cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi can you help add some unit test cases?

Hi, I've added unit tests for isLong, and I noticed that the mock behavior covers the exception (monitorDao.findAll() directly returns mockPage without actually executing the query). Then I ensured test coverage by triggering toPredicate() with thenAnswer().


/**
* Converts the time string str to seconds
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ void testParseIsNumeric() {
assertFalse(CommonUtil.isNumeric("445_126"));
}

@Test
void testParseIsLong() {
assertTrue(CommonUtil.isLong("1234"));
assertTrue(CommonUtil.isLong("0"));
assertFalse(CommonUtil.isLong(null));
assertFalse(CommonUtil.isLong(""));
assertFalse(CommonUtil.isLong(" "));
assertFalse(CommonUtil.isLong("12.34"));
assertFalse(CommonUtil.isLong("1,234"));
assertFalse(CommonUtil.isLong("1234L"));
assertFalse(CommonUtil.isLong("296.347%"));
assertFalse(CommonUtil.isLong("445_126"));
}

@Test
void testParseTimeStrToSecond() {
assertEquals(36000, CommonUtil.parseTimeStrToSecond("10:00"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public Page<Monitor> getMonitors(List<Long> monitorIds, String app, String searc
if (StringUtils.hasText(search)) {
Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + search + "%");
Predicate predicateName = criteriaBuilder.like(criteriaBuilder.lower(root.get("name")), "%" + search.toLowerCase() + "%");
if (CommonUtil.isNumeric(search)){
if (CommonUtil.isLong(search)){
Predicate predicateId = criteriaBuilder.equal(root.get("id"), Long.parseLong(search));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long.parseLong can still fail if the number has lots of digits, eg 20 or more.
9,223,372,036,854,775,807 is the max long in Java.

orList.add(predicateId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
Expand All @@ -31,6 +31,9 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.apache.hertzbeat.alert.dao.AlertDefineBindDao;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.job.Job;
Expand Down Expand Up @@ -650,8 +653,16 @@ void getMonitorDto() {

@Test
void getMonitors() {
doReturn(Page.empty()).when(monitorDao).findAll(any(Specification.class), any(PageRequest.class));
assertNotNull(monitorService.getMonitors(null, null, null, null, "gmtCreate", "desc", 1, 1, null));
when(monitorDao.findAll(any(Specification.class), any(PageRequest.class))).thenAnswer((invocation)->{
Specification<Monitor> spec = invocation.getArgument(0);
CriteriaBuilder cb = mock(CriteriaBuilder.class);
CriteriaQuery<?> query = mock(CriteriaQuery.class);
Root<Monitor> root = mock(Root.class);
spec.toPredicate(root, query, cb);
return Page.empty();
});
assertNotNull(monitorService.getMonitors(null, null, "9.111", null, "gmtCreate", "desc", 1, 1, null));
assertNotNull(monitorService.getMonitors(null, null, "9", null, "gmtCreate", "desc", 1, 1, null));
}

@Test
Expand Down