Merge pull request '完成单元测试' (#11) from ZhouXY108/plusone-commons:1.x.x into 1.x.x
Reviewed-on: #111.x.x
commit
0eee05d46a
|
@ -1,16 +1,16 @@
|
|||
[ ] 未开始测试 - 15 (21.43%)
|
||||
[-] 测试未完成 - 11 (15.71%)
|
||||
[Y] 测试完成 - 23 (32.86%)
|
||||
[x] 无需测试 - 21 (30.00%)
|
||||
[ ] 未开始测试 - 0 (0.00%)
|
||||
[*] 测试未完成 - 0 (0.00%)
|
||||
[Y] 测试完成 - 40 (64.52%)
|
||||
[-] 无需测试 - 22 (35.48%)
|
||||
|
||||
xyz.zhouxy.plusone.commons
|
||||
├───annotation
|
||||
│ ReaderMethod.java [x]
|
||||
│ StaticFactoryMethod.java [x]
|
||||
│ UnsupportedOperation.java [x]
|
||||
│ ValueObject.java [x]
|
||||
│ Virtual.java [x]
|
||||
│ WriterMethod.java [x]
|
||||
│ ReaderMethod.java [-]
|
||||
│ StaticFactoryMethod.java [-]
|
||||
│ UnsupportedOperation.java [-]
|
||||
│ ValueObject.java [-]
|
||||
│ Virtual.java [-]
|
||||
│ WriterMethod.java [-]
|
||||
│
|
||||
├───base
|
||||
│ BoolRef.java [Y]
|
||||
|
@ -20,82 +20,72 @@ xyz.zhouxy.plusone.commons
|
|||
│ IWithCode.java [Y]
|
||||
│ IWithIntCode.java [Y]
|
||||
│ IWithLongCode.java [Y]
|
||||
│ JRE.java [ ]
|
||||
│ LongRef.java [Y]
|
||||
│ Ref.java [Y]
|
||||
│
|
||||
├───collection
|
||||
│ AbstractMapWrapper.java [ ]
|
||||
│ CollectionTools.java [Y]
|
||||
│ MapWrapper.java [ ]
|
||||
│ SafeConcurrentHashMap.java [ ]
|
||||
│
|
||||
├───constant
|
||||
│ PatternConsts.java [ ]
|
||||
│ RegexConsts.java [ ]
|
||||
│ PatternConsts.java [Y]
|
||||
│ RegexConsts.java [Y]
|
||||
│
|
||||
├───exception
|
||||
│ │ DataNotExistsException.java [x]
|
||||
│ │ DataNotExistsException.java [-]
|
||||
│ │ ParsingFailureException.java [Y]
|
||||
│ │ ExceptionType.java [Y]
|
||||
│ │
|
||||
│ ├───business
|
||||
│ │ BizException.java [x]
|
||||
│ │ BizException.java [-]
|
||||
│ │ InvalidInputException.java [Y]
|
||||
│ │ RequestParamsException.java [x]
|
||||
│ │ RequestParamsException.java [-]
|
||||
│ │
|
||||
│ └───system
|
||||
│ DataOperationResultException.java [x]
|
||||
│ NoAvailableMacFoundException.java [x]
|
||||
│ SysException.java [x]
|
||||
│ DataOperationResultException.java [-]
|
||||
│ NoAvailableMacFoundException.java [-]
|
||||
│ SysException.java [-]
|
||||
│
|
||||
├───function
|
||||
│ BoolUnaryOperator.java [x]
|
||||
│ CharUnaryOperator.java [x]
|
||||
│ Executable.java [x]
|
||||
│ OptionalSupplier.java [x]
|
||||
│ BoolUnaryOperator.java [-]
|
||||
│ CharUnaryOperator.java [-]
|
||||
│ Executable.java [-]
|
||||
│ OptionalSupplier.java [-]
|
||||
│ PredicateTools.java [Y]
|
||||
│ ThrowingConsumer.java [x]
|
||||
│ ThrowingPredicate.java [x]
|
||||
│ ThrowingSupplier.java [x]
|
||||
│ ToOptionalBiFunction.java [x]
|
||||
│ ToOptionalFunction.java [x]
|
||||
│ ThrowingConsumer.java [-]
|
||||
│ ThrowingPredicate.java [-]
|
||||
│ ThrowingSupplier.java [-]
|
||||
│ ToOptionalBiFunction.java [-]
|
||||
│ ToOptionalFunction.java [-]
|
||||
│
|
||||
├───model
|
||||
│ │ Chinese2ndGenIDCardNumber.java [-]
|
||||
│ │ Gender.java [ ]
|
||||
│ │ IDCardNumber.java [ ]
|
||||
│ │ ValidatableStringRecord.java [-]
|
||||
│ │ Chinese2ndGenIDCardNumber.java [Y]
|
||||
│ │ Gender.java [-]
|
||||
│ │ IDCardNumber.java [Y]
|
||||
│ │ ValidatableStringRecord.java [Y]
|
||||
│ │
|
||||
│ └───dto
|
||||
│ PageResult.java [-]
|
||||
│ PagingAndSortingQueryParams.java [-]
|
||||
│ PagingParams.java [-]
|
||||
│ UnifiedResponse.java [-]
|
||||
│
|
||||
├───sql
|
||||
│ JdbcSql.java [ ]
|
||||
│ MyBatisSql.java [-]
|
||||
│ SQL.java [ ]
|
||||
│ PageResult.java [Y]
|
||||
│ PagingAndSortingQueryParams.java [Y]
|
||||
│ PagingParams.java [Y]
|
||||
│ UnifiedResponse.java [Y]
|
||||
│
|
||||
├───time
|
||||
│ Quarter.java [Y]
|
||||
│ YearQuarter.java [-]
|
||||
│ YearQuarter.java [Y]
|
||||
│
|
||||
└───util
|
||||
ArrayTools.java [-]
|
||||
ArrayTools.java [Y]
|
||||
AssertTools.java [Y]
|
||||
BigDecimals.java [Y]
|
||||
ConcurrentHashMapTools.java [-]
|
||||
DateTimeTools.java [-]
|
||||
DateTimeTools.java [Y]
|
||||
Enumeration.java [Y]
|
||||
EnumTools.java [Y]
|
||||
IdGenerator.java [ ]
|
||||
IdWorker.java [ ]
|
||||
IdGenerator.java [Y]
|
||||
IdWorker.java [Y]
|
||||
Numbers.java [Y]
|
||||
OptionalTools.java [Y]
|
||||
RandomTools.java [ ]
|
||||
RegexTools.java [ ]
|
||||
SnowflakeIdGenerator.java [ ]
|
||||
RandomTools.java [Y]
|
||||
RegexTools.java [Y]
|
||||
SnowflakeIdGenerator.java [Y]
|
||||
StringTools.java [Y]
|
||||
TreeBuilder.java [Y]
|
||||
|
|
14
pom.xml
14
pom.xml
|
@ -82,6 +82,20 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.5.17</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>2.2.224</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Jackson -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
|
|
|
@ -1,740 +0,0 @@
|
|||
/*
|
||||
* Copyright 2009-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.ibatis.jdbc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Clinton Begin
|
||||
* @author Jeff Butler
|
||||
* @author Adam Gent
|
||||
* @author Kazuki Shimizu
|
||||
*/
|
||||
public abstract class AbstractSQL<T> {
|
||||
|
||||
private static final String AND = ") \nAND (";
|
||||
private static final String OR = ") \nOR (";
|
||||
|
||||
private final SQLStatement sql = new SQLStatement();
|
||||
|
||||
public abstract T getSelf();
|
||||
|
||||
public T UPDATE(String table) {
|
||||
sql().statementType = SQLStatement.StatementType.UPDATE;
|
||||
sql().tables.add(table);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T SET(String sets) {
|
||||
sql().sets.add(sets);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the.
|
||||
*
|
||||
* @param sets
|
||||
* the sets
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T SET(String... sets) {
|
||||
sql().sets.addAll(Arrays.asList(sets));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T INSERT_INTO(String tableName) {
|
||||
sql().statementType = SQLStatement.StatementType.INSERT;
|
||||
sql().tables.add(tableName);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T VALUES(String columns, String values) {
|
||||
INTO_COLUMNS(columns);
|
||||
INTO_VALUES(values);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Into columns.
|
||||
*
|
||||
* @param columns
|
||||
* the columns
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T INTO_COLUMNS(String... columns) {
|
||||
sql().columns.addAll(Arrays.asList(columns));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Into values.
|
||||
*
|
||||
* @param values
|
||||
* the values
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T INTO_VALUES(String... values) {
|
||||
List<String> list = sql().valuesList.get(sql().valuesList.size() - 1);
|
||||
Collections.addAll(list, values);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T SELECT(String columns) {
|
||||
sql().statementType = SQLStatement.StatementType.SELECT;
|
||||
sql().select.add(columns);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Select.
|
||||
*
|
||||
* @param columns
|
||||
* the columns
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T SELECT(String... columns) {
|
||||
sql().statementType = SQLStatement.StatementType.SELECT;
|
||||
sql().select.addAll(Arrays.asList(columns));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T SELECT_DISTINCT(String columns) {
|
||||
sql().distinct = true;
|
||||
SELECT(columns);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Select distinct.
|
||||
*
|
||||
* @param columns
|
||||
* the columns
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T SELECT_DISTINCT(String... columns) {
|
||||
sql().distinct = true;
|
||||
SELECT(columns);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T DELETE_FROM(String table) {
|
||||
sql().statementType = SQLStatement.StatementType.DELETE;
|
||||
sql().tables.add(table);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T FROM(String table) {
|
||||
sql().tables.add(table);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* From.
|
||||
*
|
||||
* @param tables
|
||||
* the tables
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T FROM(String... tables) {
|
||||
sql().tables.addAll(Arrays.asList(tables));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T JOIN(String join) {
|
||||
sql().join.add(join);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Join.
|
||||
*
|
||||
* @param joins
|
||||
* the joins
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T JOIN(String... joins) {
|
||||
sql().join.addAll(Arrays.asList(joins));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T INNER_JOIN(String join) {
|
||||
sql().innerJoin.add(join);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inner join.
|
||||
*
|
||||
* @param joins
|
||||
* the joins
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T INNER_JOIN(String... joins) {
|
||||
sql().innerJoin.addAll(Arrays.asList(joins));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T LEFT_OUTER_JOIN(String join) {
|
||||
sql().leftOuterJoin.add(join);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Left outer join.
|
||||
*
|
||||
* @param joins
|
||||
* the joins
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T LEFT_OUTER_JOIN(String... joins) {
|
||||
sql().leftOuterJoin.addAll(Arrays.asList(joins));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T RIGHT_OUTER_JOIN(String join) {
|
||||
sql().rightOuterJoin.add(join);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Right outer join.
|
||||
*
|
||||
* @param joins
|
||||
* the joins
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T RIGHT_OUTER_JOIN(String... joins) {
|
||||
sql().rightOuterJoin.addAll(Arrays.asList(joins));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T OUTER_JOIN(String join) {
|
||||
sql().outerJoin.add(join);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Outer join.
|
||||
*
|
||||
* @param joins
|
||||
* the joins
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T OUTER_JOIN(String... joins) {
|
||||
sql().outerJoin.addAll(Arrays.asList(joins));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T WHERE(String conditions) {
|
||||
sql().where.add(conditions);
|
||||
sql().lastList = sql().where;
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Where.
|
||||
*
|
||||
* @param conditions
|
||||
* the conditions
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T WHERE(String... conditions) {
|
||||
sql().where.addAll(Arrays.asList(conditions));
|
||||
sql().lastList = sql().where;
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T OR() {
|
||||
sql().lastList.add(OR);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T AND() {
|
||||
sql().lastList.add(AND);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T GROUP_BY(String columns) {
|
||||
sql().groupBy.add(columns);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Group by.
|
||||
*
|
||||
* @param columns
|
||||
* the columns
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T GROUP_BY(String... columns) {
|
||||
sql().groupBy.addAll(Arrays.asList(columns));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T HAVING(String conditions) {
|
||||
sql().having.add(conditions);
|
||||
sql().lastList = sql().having;
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Having.
|
||||
*
|
||||
* @param conditions
|
||||
* the conditions
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T HAVING(String... conditions) {
|
||||
sql().having.addAll(Arrays.asList(conditions));
|
||||
sql().lastList = sql().having;
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T ORDER_BY(String columns) {
|
||||
sql().orderBy.add(columns);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Order by.
|
||||
*
|
||||
* @param columns
|
||||
* the columns
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.4.2
|
||||
*/
|
||||
public T ORDER_BY(String... columns) {
|
||||
sql().orderBy.addAll(Arrays.asList(columns));
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the limit variable string(e.g. {@code "#{limit}"}).
|
||||
*
|
||||
* @param variable
|
||||
* a limit variable string
|
||||
*
|
||||
* @return a self instance
|
||||
*
|
||||
* @see #OFFSET(String)
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T LIMIT(String variable) {
|
||||
sql().limit = variable;
|
||||
sql().limitingRowsStrategy = SQLStatement.LimitingRowsStrategy.OFFSET_LIMIT;
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the limit value.
|
||||
*
|
||||
* @param value
|
||||
* an offset value
|
||||
*
|
||||
* @return a self instance
|
||||
*
|
||||
* @see #OFFSET(long)
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T LIMIT(int value) {
|
||||
return LIMIT(String.valueOf(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the offset variable string(e.g. {@code "#{offset}"}).
|
||||
*
|
||||
* @param variable
|
||||
* a offset variable string
|
||||
*
|
||||
* @return a self instance
|
||||
*
|
||||
* @see #LIMIT(String)
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T OFFSET(String variable) {
|
||||
sql().offset = variable;
|
||||
sql().limitingRowsStrategy = SQLStatement.LimitingRowsStrategy.OFFSET_LIMIT;
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the offset value.
|
||||
*
|
||||
* @param value
|
||||
* an offset value
|
||||
*
|
||||
* @return a self instance
|
||||
*
|
||||
* @see #LIMIT(int)
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T OFFSET(long value) {
|
||||
return OFFSET(String.valueOf(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the fetch first rows variable string(e.g. {@code "#{fetchFirstRows}"}).
|
||||
*
|
||||
* @param variable
|
||||
* a fetch first rows variable string
|
||||
*
|
||||
* @return a self instance
|
||||
*
|
||||
* @see #OFFSET_ROWS(String)
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T FETCH_FIRST_ROWS_ONLY(String variable) {
|
||||
sql().limit = variable;
|
||||
sql().limitingRowsStrategy = SQLStatement.LimitingRowsStrategy.ISO;
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the fetch first rows value.
|
||||
*
|
||||
* @param value
|
||||
* a fetch first rows value
|
||||
*
|
||||
* @return a self instance
|
||||
*
|
||||
* @see #OFFSET_ROWS(long)
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T FETCH_FIRST_ROWS_ONLY(int value) {
|
||||
return FETCH_FIRST_ROWS_ONLY(String.valueOf(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the offset rows variable string(e.g. {@code "#{offset}"}).
|
||||
*
|
||||
* @param variable
|
||||
* a offset rows variable string
|
||||
*
|
||||
* @return a self instance
|
||||
*
|
||||
* @see #FETCH_FIRST_ROWS_ONLY(String)
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T OFFSET_ROWS(String variable) {
|
||||
sql().offset = variable;
|
||||
sql().limitingRowsStrategy = SQLStatement.LimitingRowsStrategy.ISO;
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the offset rows value.
|
||||
*
|
||||
* @param value
|
||||
* an offset rows value
|
||||
*
|
||||
* @return a self instance
|
||||
*
|
||||
* @see #FETCH_FIRST_ROWS_ONLY(int)
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T OFFSET_ROWS(long value) {
|
||||
return OFFSET_ROWS(String.valueOf(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* used to add a new inserted row while do multi-row insert.
|
||||
*
|
||||
* @return the t
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public T ADD_ROW() {
|
||||
sql().valuesList.add(new ArrayList<>());
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
private SQLStatement sql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public <A extends Appendable> A usingAppender(A a) {
|
||||
sql().sql(a);
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sql().sql(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static class SafeAppendable {
|
||||
private final Appendable appendable;
|
||||
private boolean empty = true;
|
||||
|
||||
public SafeAppendable(Appendable a) {
|
||||
this.appendable = a;
|
||||
}
|
||||
|
||||
public SafeAppendable append(CharSequence s) {
|
||||
try {
|
||||
if (empty && s.length() > 0) {
|
||||
empty = false;
|
||||
}
|
||||
appendable.append(s);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return empty;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class SQLStatement {
|
||||
|
||||
public enum StatementType {
|
||||
|
||||
DELETE,
|
||||
|
||||
INSERT,
|
||||
|
||||
SELECT,
|
||||
|
||||
UPDATE
|
||||
|
||||
}
|
||||
|
||||
private enum LimitingRowsStrategy {
|
||||
NOP {
|
||||
@Override
|
||||
protected void appendClause(SafeAppendable builder, String offset, String limit) {
|
||||
// NOP
|
||||
}
|
||||
},
|
||||
ISO {
|
||||
@Override
|
||||
protected void appendClause(SafeAppendable builder, String offset, String limit) {
|
||||
if (offset != null) {
|
||||
builder.append(" OFFSET ").append(offset).append(" ROWS");
|
||||
}
|
||||
if (limit != null) {
|
||||
builder.append(" FETCH FIRST ").append(limit).append(" ROWS ONLY");
|
||||
}
|
||||
}
|
||||
},
|
||||
OFFSET_LIMIT {
|
||||
@Override
|
||||
protected void appendClause(SafeAppendable builder, String offset, String limit) {
|
||||
if (limit != null) {
|
||||
builder.append(" LIMIT ").append(limit);
|
||||
}
|
||||
if (offset != null) {
|
||||
builder.append(" OFFSET ").append(offset);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
protected abstract void appendClause(SafeAppendable builder, String offset, String limit);
|
||||
|
||||
}
|
||||
|
||||
StatementType statementType;
|
||||
List<String> sets = new ArrayList<>();
|
||||
List<String> select = new ArrayList<>();
|
||||
List<String> tables = new ArrayList<>();
|
||||
List<String> join = new ArrayList<>();
|
||||
List<String> innerJoin = new ArrayList<>();
|
||||
List<String> outerJoin = new ArrayList<>();
|
||||
List<String> leftOuterJoin = new ArrayList<>();
|
||||
List<String> rightOuterJoin = new ArrayList<>();
|
||||
List<String> where = new ArrayList<>();
|
||||
List<String> having = new ArrayList<>();
|
||||
List<String> groupBy = new ArrayList<>();
|
||||
List<String> orderBy = new ArrayList<>();
|
||||
List<String> lastList = new ArrayList<>();
|
||||
List<String> columns = new ArrayList<>();
|
||||
List<List<String>> valuesList = new ArrayList<>();
|
||||
boolean distinct;
|
||||
String offset;
|
||||
String limit;
|
||||
LimitingRowsStrategy limitingRowsStrategy = LimitingRowsStrategy.NOP;
|
||||
|
||||
public SQLStatement() {
|
||||
// Prevent Synthetic Access
|
||||
valuesList.add(new ArrayList<>());
|
||||
}
|
||||
|
||||
private void sqlClause(SafeAppendable builder, String keyword, List<String> parts, String open, String close,
|
||||
String conjunction) {
|
||||
if (!parts.isEmpty()) {
|
||||
if (!builder.isEmpty()) {
|
||||
builder.append("\n");
|
||||
}
|
||||
builder.append(keyword);
|
||||
builder.append(" ");
|
||||
builder.append(open);
|
||||
String last = "________";
|
||||
for (int i = 0, n = parts.size(); i < n; i++) {
|
||||
String part = parts.get(i);
|
||||
if (i > 0 && !part.equals(AND) && !part.equals(OR) && !last.equals(AND) && !last.equals(OR)) {
|
||||
builder.append(conjunction);
|
||||
}
|
||||
builder.append(part);
|
||||
last = part;
|
||||
}
|
||||
builder.append(close);
|
||||
}
|
||||
}
|
||||
|
||||
private String selectSQL(SafeAppendable builder) {
|
||||
if (distinct) {
|
||||
sqlClause(builder, "SELECT DISTINCT", select, "", "", ", ");
|
||||
} else {
|
||||
sqlClause(builder, "SELECT", select, "", "", ", ");
|
||||
}
|
||||
|
||||
sqlClause(builder, "FROM", tables, "", "", ", ");
|
||||
joins(builder);
|
||||
sqlClause(builder, "WHERE", where, "(", ")", " AND ");
|
||||
sqlClause(builder, "GROUP BY", groupBy, "", "", ", ");
|
||||
sqlClause(builder, "HAVING", having, "(", ")", " AND ");
|
||||
sqlClause(builder, "ORDER BY", orderBy, "", "", ", ");
|
||||
limitingRowsStrategy.appendClause(builder, offset, limit);
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private void joins(SafeAppendable builder) {
|
||||
sqlClause(builder, "JOIN", join, "", "", "\nJOIN ");
|
||||
sqlClause(builder, "INNER JOIN", innerJoin, "", "", "\nINNER JOIN ");
|
||||
sqlClause(builder, "OUTER JOIN", outerJoin, "", "", "\nOUTER JOIN ");
|
||||
sqlClause(builder, "LEFT OUTER JOIN", leftOuterJoin, "", "", "\nLEFT OUTER JOIN ");
|
||||
sqlClause(builder, "RIGHT OUTER JOIN", rightOuterJoin, "", "", "\nRIGHT OUTER JOIN ");
|
||||
}
|
||||
|
||||
private String insertSQL(SafeAppendable builder) {
|
||||
sqlClause(builder, "INSERT INTO", tables, "", "", "");
|
||||
sqlClause(builder, "", columns, "(", ")", ", ");
|
||||
for (int i = 0; i < valuesList.size(); i++) {
|
||||
sqlClause(builder, i > 0 ? "," : "VALUES", valuesList.get(i), "(", ")", ", ");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private String deleteSQL(SafeAppendable builder) {
|
||||
sqlClause(builder, "DELETE FROM", tables, "", "", "");
|
||||
sqlClause(builder, "WHERE", where, "(", ")", " AND ");
|
||||
limitingRowsStrategy.appendClause(builder, null, limit);
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private String updateSQL(SafeAppendable builder) {
|
||||
sqlClause(builder, "UPDATE", tables, "", "", "");
|
||||
joins(builder);
|
||||
sqlClause(builder, "SET", sets, "", "", ", ");
|
||||
sqlClause(builder, "WHERE", where, "(", ")", " AND ");
|
||||
limitingRowsStrategy.appendClause(builder, null, limit);
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public String sql(Appendable a) {
|
||||
SafeAppendable builder = new SafeAppendable(a);
|
||||
if (statementType == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String answer;
|
||||
|
||||
switch (statementType) {
|
||||
case DELETE:
|
||||
answer = deleteSQL(builder);
|
||||
break;
|
||||
|
||||
case INSERT:
|
||||
answer = insertSQL(builder);
|
||||
break;
|
||||
|
||||
case SELECT:
|
||||
answer = selectSQL(builder);
|
||||
break;
|
||||
|
||||
case UPDATE:
|
||||
answer = updateSQL(builder);
|
||||
break;
|
||||
|
||||
default:
|
||||
answer = null;
|
||||
}
|
||||
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.base;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
/**
|
||||
* JRE version
|
||||
*/
|
||||
public class JRE {
|
||||
|
||||
private static final int JAVA_8 = 8;
|
||||
|
||||
public static final int CURRENT_VERSION = getJre();
|
||||
|
||||
public static boolean isJava8() {
|
||||
return CURRENT_VERSION == JAVA_8;
|
||||
}
|
||||
|
||||
private static int getJre() {
|
||||
String version = System.getProperty("java.version");
|
||||
boolean isNotBlank = StringTools.isNotBlank(version);
|
||||
if (isNotBlank && version.startsWith("1.8")) {
|
||||
return JAVA_8;
|
||||
}
|
||||
// if JRE version is 9 or above, we can get version from
|
||||
// java.lang.Runtime.version()
|
||||
try {
|
||||
return getMajorVersion(version);
|
||||
} catch (Exception e) {
|
||||
// assuming that JRE version is 8.
|
||||
}
|
||||
// default java 8
|
||||
return JAVA_8;
|
||||
}
|
||||
|
||||
private static int getMajorVersion(String version) {
|
||||
if (version.startsWith("1.")) {
|
||||
return Integer.parseInt(version.substring(2, 3));
|
||||
} else {
|
||||
int dotIndex = version.indexOf(".");
|
||||
return (dotIndex != -1) ? Integer.parseInt(version.substring(0, dotIndex)) : Integer.parseInt(version);
|
||||
}
|
||||
}
|
||||
|
||||
private JRE() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
|
@ -1,241 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools;
|
||||
|
||||
/**
|
||||
* AbstractMapWrapper
|
||||
*
|
||||
* <p>
|
||||
* 包装了一个 {@link Map}。
|
||||
* 主要在于获取值时,如果 Key 不存在,是抛出异常;如果 Key 存在,则将 value 包装在 {@link Optional} 中 返回。
|
||||
* 此外可以定义 Key 和 Value 的检查规则。
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
@Beta
|
||||
public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V, T>> {
|
||||
|
||||
private final Map<K, V> map;
|
||||
private final Consumer<K> keyChecker;
|
||||
private final Consumer<V> valueChecker;
|
||||
|
||||
protected AbstractMapWrapper(Map<K, V> map,
|
||||
@Nullable Consumer<K> keyChecker,
|
||||
@Nullable Consumer<V> valueChecker) {
|
||||
this.map = map;
|
||||
this.keyChecker = keyChecker;
|
||||
this.valueChecker = valueChecker;
|
||||
}
|
||||
|
||||
public final T put(K key, V value) {
|
||||
if (this.keyChecker != null) {
|
||||
this.keyChecker.accept(key);
|
||||
}
|
||||
if (this.valueChecker != null) {
|
||||
this.valueChecker.accept(value);
|
||||
}
|
||||
this.map.put(key, value);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public final T putAll(Map<? extends K, ? extends V> m) {
|
||||
m.forEach(this::put);
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。
|
||||
* 将 {@code value}(可为 {@code null})装进 {@link Optional} 中后返回。
|
||||
* <i>为了这碟醋包的这盘饺子。</i>
|
||||
*
|
||||
* @param key 键
|
||||
* @return 可缺失的值
|
||||
* @throws IllegalArgumentException key 不存在时抛出。
|
||||
*/
|
||||
@Nonnull
|
||||
public Optional<V> get(K key) {
|
||||
AssertTools.checkArgument(this.map.containsKey(key), "Key does not exist");
|
||||
return Optional.ofNullable(this.map.get(key));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public final <R> Optional<R> getAndConvert(K key) {
|
||||
return get(key).map(v -> (R) v);
|
||||
}
|
||||
|
||||
public final <R> Optional<R> getAndConvert(K key, Function<V, R> mapper) {
|
||||
return get(key).map(mapper);
|
||||
}
|
||||
|
||||
public final boolean containsKey(Object key) {
|
||||
return this.map.containsKey(key);
|
||||
}
|
||||
|
||||
public final int size() {
|
||||
return this.map.size();
|
||||
}
|
||||
|
||||
public final Set<K> keySet() {
|
||||
return this.map.keySet();
|
||||
}
|
||||
|
||||
public final Collection<V> values() {
|
||||
return this.map.values();
|
||||
}
|
||||
|
||||
public final Set<Entry<K, V>> entrySet() {
|
||||
return this.map.entrySet();
|
||||
}
|
||||
|
||||
public final void clear() {
|
||||
this.map.clear();
|
||||
}
|
||||
|
||||
public final boolean containsValue(Object value) {
|
||||
return this.map.containsValue(value);
|
||||
}
|
||||
|
||||
public final boolean isEmpty() {
|
||||
return this.map.isEmpty();
|
||||
}
|
||||
|
||||
public final V remove(Object key) {
|
||||
return this.map.remove(key);
|
||||
}
|
||||
|
||||
public final V putIfAbsent(K key, V value) {
|
||||
if (this.keyChecker != null) {
|
||||
this.keyChecker.accept(key);
|
||||
}
|
||||
if (this.valueChecker != null) {
|
||||
this.valueChecker.accept(value);
|
||||
}
|
||||
return this.map.putIfAbsent(key, value);
|
||||
}
|
||||
|
||||
public final V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||
if (this.keyChecker != null) {
|
||||
this.keyChecker.accept(key);
|
||||
}
|
||||
Function<? super K, ? extends V> func = (K k) -> {
|
||||
V value = mappingFunction.apply(k);
|
||||
if (this.valueChecker != null) {
|
||||
this.valueChecker.accept(value);
|
||||
}
|
||||
return value;
|
||||
};
|
||||
if (this.map instanceof ConcurrentHashMap) {
|
||||
return ConcurrentHashMapTools.computeIfAbsent(
|
||||
(ConcurrentHashMap<K, V>) this.map, key, func);
|
||||
} else {
|
||||
return this.map.computeIfAbsent(key, func);
|
||||
}
|
||||
}
|
||||
|
||||
public final Map<K, V> exportMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
public final Map<K, V> exportUnmodifiableMap() {
|
||||
return Collections.unmodifiableMap(this.map);
|
||||
}
|
||||
|
||||
protected abstract T getSelf();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.map.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AbstractMapWrapper<?, ?, ?> other = (AbstractMapWrapper<?, ?, ?>) obj;
|
||||
return Objects.equals(map, other.map);
|
||||
}
|
||||
|
||||
public abstract static class Builder<K, V, T extends AbstractMapWrapper<K, V, T>> {
|
||||
protected final Map<K, V> map;
|
||||
protected Consumer<K> keyChecker;
|
||||
protected Consumer<V> valueChecker;
|
||||
|
||||
protected Builder(Map<K, V> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Builder<K, V, T> keyChecker(@Nullable Consumer<K> keyChecker) {
|
||||
this.keyChecker = keyChecker;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<K, V, T> valueChecker(@Nullable Consumer<V> valueChecker) {
|
||||
this.valueChecker = valueChecker;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<K, V, T> put(K key, V value) {
|
||||
if (this.keyChecker != null) {
|
||||
this.keyChecker.accept(key);
|
||||
}
|
||||
if (this.valueChecker != null) {
|
||||
this.valueChecker.accept(value);
|
||||
}
|
||||
this.map.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<K, V, T> putAll(Map<? extends K, ? extends V> m) {
|
||||
m.forEach(this::put);
|
||||
return this;
|
||||
}
|
||||
|
||||
public abstract T build();
|
||||
|
||||
public abstract T buildUnmodifiableMap();
|
||||
}
|
||||
}
|
|
@ -1,95 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
/**
|
||||
* MapWrapper
|
||||
*
|
||||
* <p>
|
||||
* Map 包装器的默认实现
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Beta
|
||||
public final class MapWrapper<K, V> extends AbstractMapWrapper<K, V, MapWrapper<K, V>> {
|
||||
|
||||
private MapWrapper(Map<K, V> map, Consumer<K> keyChecker, Consumer<V> valueChecker) {
|
||||
super(map, keyChecker, valueChecker);
|
||||
}
|
||||
|
||||
public static <K, V> Builder<K, V> wrap(Map<K, V> map) {
|
||||
return new Builder<>(map);
|
||||
}
|
||||
|
||||
public static <K, V> Builder<K, V> wrapHashMap() {
|
||||
return new Builder<>(new HashMap<>());
|
||||
}
|
||||
|
||||
public static <K, V> Builder<K, V> wrapHashMap(int initialCapacity) {
|
||||
return new Builder<>(new HashMap<>(initialCapacity));
|
||||
}
|
||||
|
||||
public static <K, V> Builder<K, V> wrapHashMap(int initialCapacity, float loadFactor) {
|
||||
return new Builder<>(new HashMap<>(initialCapacity, loadFactor));
|
||||
}
|
||||
|
||||
public static <K extends Comparable<? super K>, V> Builder<K, V> wrapTreeMap() {
|
||||
return new Builder<>(new TreeMap<>());
|
||||
}
|
||||
|
||||
public static <K, V> Builder<K, V> wrapTreeMap(SortedMap<K, ? extends V> m) {
|
||||
return new Builder<>(new TreeMap<>(m));
|
||||
}
|
||||
|
||||
public static <K, V> Builder<K, V> wrapTreeMap(Comparator<? super K> comparator) {
|
||||
return new Builder<>(new TreeMap<>(comparator));
|
||||
}
|
||||
|
||||
public static final class Builder<K, V> extends AbstractMapWrapper.Builder<K, V, MapWrapper<K, V>> {
|
||||
|
||||
private Builder(Map<K, V> map) {
|
||||
super(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MapWrapper<K, V> build() {
|
||||
return new MapWrapper<>(map, keyChecker, valueChecker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MapWrapper<K, V> buildUnmodifiableMap() {
|
||||
return new MapWrapper<>(Collections.unmodifiableMap(map), keyChecker, valueChecker);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MapWrapper<K, V> getSelf() {
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.JRE;
|
||||
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools;
|
||||
|
||||
/**
|
||||
* SafeConcurrentHashMap
|
||||
*
|
||||
* <p>
|
||||
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
||||
* 使用 Java 8 时,可使用这个类进行替换。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0
|
||||
* @see ConcurrentHashMap
|
||||
* @see ConcurrentHashMapTools#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function)
|
||||
*/
|
||||
@ThreadSafe
|
||||
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
||||
|
||||
private static final long serialVersionUID = 4352954948768449595L;
|
||||
|
||||
/**
|
||||
* Creates a new, empty map with the default initial table size (16).
|
||||
*/
|
||||
public SafeConcurrentHashMap() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new, empty map with an initial table size
|
||||
* accommodating the specified number of elements without the need
|
||||
* to dynamically resize.
|
||||
*
|
||||
* @param initialCapacity The implementation performs internal
|
||||
* sizing to accommodate this many elements.
|
||||
* @throws IllegalArgumentException if the initial capacity of
|
||||
* elements is negative
|
||||
*/
|
||||
public SafeConcurrentHashMap(int initialCapacity) {
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new map with the same mappings as the given map.
|
||||
*
|
||||
* @param m the map
|
||||
*/
|
||||
public SafeConcurrentHashMap(Map<? extends K, ? extends V> m) {
|
||||
super(m);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new, empty map with an initial table size based on
|
||||
* the given number of elements ({@code initialCapacity}) and
|
||||
* initial table density ({@code loadFactor}).
|
||||
*
|
||||
* @param initialCapacity the initial capacity. The implementation
|
||||
* performs internal sizing to accommodate this many elements,
|
||||
* given the specified load factor.
|
||||
* @param loadFactor the load factor (table density) for
|
||||
* establishing the initial table size
|
||||
* @throws IllegalArgumentException if the initial capacity of
|
||||
* elements is negative or the load factor is nonpositive
|
||||
* @since 1.6
|
||||
*/
|
||||
public SafeConcurrentHashMap(int initialCapacity, float loadFactor) {
|
||||
super(initialCapacity, loadFactor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new, empty map with an initial table size based on
|
||||
* the given number of elements ({@code initialCapacity}), table
|
||||
* density ({@code loadFactor}), and number of concurrently
|
||||
* updating threads ({@code concurrencyLevel}).
|
||||
*
|
||||
* @param initialCapacity the initial capacity. The implementation
|
||||
* performs internal sizing to accommodate this many elements,
|
||||
* given the specified load factor.
|
||||
* @param loadFactor the load factor (table density) for
|
||||
* establishing the initial table size
|
||||
* @param concurrencyLevel the estimated number of concurrently
|
||||
* updating threads. The implementation may use this value as
|
||||
* a sizing hint.
|
||||
* @throws IllegalArgumentException if the initial capacity is
|
||||
* negative or the load factor or concurrencyLevel are
|
||||
* nonpositive
|
||||
*/
|
||||
public SafeConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
|
||||
super(initialCapacity, loadFactor, concurrencyLevel);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||
return JRE.isJava8()
|
||||
? ConcurrentHashMapTools.computeIfAbsentForJava8(this, key, mappingFunction)
|
||||
: super.computeIfAbsent(key, mappingFunction);
|
||||
}
|
||||
}
|
|
@ -27,33 +27,70 @@ import java.util.regex.Pattern;
|
|||
*/
|
||||
public final class PatternConsts {
|
||||
|
||||
/** yyyyMMdd */
|
||||
/**
|
||||
* yyyyMMdd
|
||||
*
|
||||
* @see RegexConsts#BASIC_ISO_DATE
|
||||
* </p>
|
||||
*/
|
||||
public static final Pattern BASIC_ISO_DATE = Pattern.compile(RegexConsts.BASIC_ISO_DATE);
|
||||
|
||||
/** yyyy-MM-dd */
|
||||
/**
|
||||
* yyyy-MM-dd
|
||||
*
|
||||
* @see RegexConsts#ISO_LOCAL_DATE
|
||||
*/
|
||||
public static final Pattern ISO_LOCAL_DATE = Pattern.compile(RegexConsts.ISO_LOCAL_DATE);
|
||||
|
||||
/** 密码 */
|
||||
/**
|
||||
* 密码
|
||||
*
|
||||
* @see RegexConsts#PASSWORD
|
||||
*/
|
||||
public static final Pattern PASSWORD = Pattern.compile(RegexConsts.PASSWORD);
|
||||
|
||||
/** 验证码 */
|
||||
/**
|
||||
* 验证码
|
||||
*
|
||||
* @see RegexConsts#CAPTCHA
|
||||
*/
|
||||
public static final Pattern CAPTCHA = Pattern.compile(RegexConsts.CAPTCHA);
|
||||
|
||||
/** 邮箱地址 */
|
||||
/**
|
||||
* 邮箱地址
|
||||
*
|
||||
* @see RegexConsts#EMAIL
|
||||
*/
|
||||
public static final Pattern EMAIL = Pattern.compile(RegexConsts.EMAIL);
|
||||
|
||||
/** 中国大陆手机号 */
|
||||
/**
|
||||
* 中国大陆手机号
|
||||
*
|
||||
* @see RegexConsts#MOBILE_PHONE
|
||||
*/
|
||||
public static final Pattern MOBILE_PHONE = Pattern.compile(RegexConsts.MOBILE_PHONE);
|
||||
|
||||
/** 用户名 */
|
||||
/**
|
||||
* 用户名
|
||||
*
|
||||
* @see RegexConsts#USERNAME
|
||||
*/
|
||||
public static final Pattern USERNAME = Pattern.compile(RegexConsts.USERNAME);
|
||||
|
||||
/** 昵称 */
|
||||
/**
|
||||
* 昵称
|
||||
*
|
||||
* @see RegexConsts#NICKNAME
|
||||
*/
|
||||
public static final Pattern NICKNAME = Pattern.compile(RegexConsts.NICKNAME);
|
||||
|
||||
/** 中国第二代居民身份证 */
|
||||
/**
|
||||
* 中国第二代居民身份证
|
||||
*
|
||||
* @see RegexConsts#CHINESE_2ND_ID_CARD_NUMBER
|
||||
*/
|
||||
public static final Pattern CHINESE_2ND_ID_CARD_NUMBER
|
||||
= Pattern.compile(RegexConsts.CHINESE_2ND_ID_CARD_NUMBER);
|
||||
= Pattern.compile(RegexConsts.CHINESE_2ND_ID_CARD_NUMBER, Pattern.CASE_INSENSITIVE);
|
||||
|
||||
private PatternConsts() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
|
|
|
@ -24,9 +24,9 @@ package xyz.zhouxy.plusone.commons.constant;
|
|||
*/
|
||||
public final class RegexConsts {
|
||||
|
||||
public static final String BASIC_ISO_DATE = "^(?<y>\\d{4})(?<M>\\d{2})(?<d>\\d{2})";
|
||||
public static final String BASIC_ISO_DATE = "^(?<yyyy>\\d{4,9})(?<MM>\\d{2})(?<dd>\\d{2})";
|
||||
|
||||
public static final String ISO_LOCAL_DATE = "^(?<y>\\d{4})-(?<M>\\d{2})-(?<d>\\d{2})";
|
||||
public static final String ISO_LOCAL_DATE = "^(?<yyyy>\\d{4,9})-(?<MM>\\d{2})-(?<dd>\\d{2})";
|
||||
|
||||
public static final String PASSWORD = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[\\w\\\\!#$%&'*\\+\\-/=?^`{|}~@\\(\\)\\[\\]\",\\.;':><]{8,32}$";
|
||||
|
||||
|
@ -38,12 +38,12 @@ public final class RegexConsts {
|
|||
|
||||
public static final String MOBILE_PHONE = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
|
||||
|
||||
public static final String USERNAME = "^[\\w_.@\\\\]{4,36}$";
|
||||
public static final String USERNAME = "^[\\w-_.@]{4,36}$";
|
||||
|
||||
public static final String NICKNAME = "^[\\w_.@\\\\]{4,36}$";
|
||||
public static final String NICKNAME = "^[\\w-_.@]{4,36}$";
|
||||
|
||||
public static final String CHINESE_2ND_ID_CARD_NUMBER
|
||||
= "^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})(?<birthDate>\\d{8})\\d{2}(?<gender>\\d)([\\dXx])$";
|
||||
= "^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})(?<birthDate>\\d{8})\\d{2}(?<gender>\\d)([\\dX])$";
|
||||
|
||||
private RegexConsts() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
|
|
|
@ -16,11 +16,9 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
@ -46,9 +44,9 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
|
|||
*/
|
||||
@ValueObject
|
||||
@Immutable
|
||||
public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
|
||||
|
||||
private final String value;
|
||||
public class Chinese2ndGenIDCardNumber
|
||||
extends ValidatableStringRecord<Chinese2ndGenIDCardNumber>
|
||||
implements IDCardNumber {
|
||||
|
||||
/** 省份编码 */
|
||||
private final String provinceCode;
|
||||
|
@ -63,57 +61,49 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
|
|||
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
|
||||
|
||||
private Chinese2ndGenIDCardNumber(String value, String provinceCode, String cityCode, String countyCode,
|
||||
Gender gender, LocalDate birthDate) {
|
||||
this.value = value;
|
||||
this.provinceCode = provinceCode;
|
||||
this.cityCode = cityCode;
|
||||
this.countyCode = countyCode;
|
||||
this.gender = gender;
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
private Chinese2ndGenIDCardNumber(String value) {
|
||||
super(value.toUpperCase(), PatternConsts.CHINESE_2ND_ID_CARD_NUMBER, () -> "二代居民身份证校验失败:" + value);
|
||||
|
||||
public static Chinese2ndGenIDCardNumber of(final String value) {
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(value), "二代居民身份证校验失败:号码为空");
|
||||
final String idNumber = value.toUpperCase();
|
||||
final Matcher matcher = getMatcher();
|
||||
|
||||
final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(idNumber);
|
||||
AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
|
||||
final String provinceCodeValue = matcher.group("province");
|
||||
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCodeValue));
|
||||
|
||||
final String provinceCode = matcher.group("province");
|
||||
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
|
||||
final String cityCodeValue = matcher.group("city");
|
||||
final String countyCodeValue = matcher.group("county");
|
||||
|
||||
final String cityCode = matcher.group("city");
|
||||
final String countyCode = matcher.group("county");
|
||||
|
||||
final Gender gender;
|
||||
final LocalDate birthDate;
|
||||
final Gender genderValue;
|
||||
final LocalDate birthDateValue;
|
||||
|
||||
try {
|
||||
// 出生日期
|
||||
final String birthDateStr = matcher.group("birthDate");
|
||||
birthDate = LocalDate.parse(birthDateStr, DATE_FORMATTER);
|
||||
birthDateValue = LocalDate.parse(birthDateStr, DATE_FORMATTER);
|
||||
|
||||
// 性别
|
||||
final int genderCode = Integer.parseInt(matcher.group("gender"));
|
||||
gender = genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE;
|
||||
genderValue = genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
|
||||
return new Chinese2ndGenIDCardNumber(idNumber, provinceCode, cityCode, countyCode, gender, birthDate);
|
||||
this.provinceCode = provinceCodeValue;
|
||||
this.cityCode = cityCodeValue;
|
||||
this.countyCode = countyCodeValue;
|
||||
this.gender = genderValue;
|
||||
this.birthDate = birthDateValue;
|
||||
}
|
||||
|
||||
public static Chinese2ndGenIDCardNumber of(final String value) {
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(value), "二代居民身份证校验失败:号码为空");
|
||||
return new Chinese2ndGenIDCardNumber(value);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #region - reader methods
|
||||
// ================================
|
||||
|
||||
@ReaderMethod
|
||||
public String value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@ReaderMethod
|
||||
public String getProvinceCode() {
|
||||
return provinceCode;
|
||||
|
@ -213,18 +203,11 @@ public class Chinese2ndGenIDCardNumber implements IDCardNumber, Serializable {
|
|||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(value);
|
||||
return super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (!(obj instanceof Chinese2ndGenIDCardNumber))
|
||||
return false;
|
||||
Chinese2ndGenIDCardNumber other = (Chinese2ndGenIDCardNumber) obj;
|
||||
return Objects.equals(value, other.value);
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 20241202095400L;
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ public interface IDCardNumber {
|
|||
LocalDate getBirthDate();
|
||||
|
||||
/** 计算年龄 */
|
||||
default int calculateAge() {
|
||||
default int getAge() {
|
||||
LocalDate now = LocalDate.now();
|
||||
return Period.between(getBirthDate(), now).getYears();
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import java.util.regex.Pattern;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
|
@ -31,8 +32,8 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
|
|||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract class ValidatableStringRecord
|
||||
implements Comparable<ValidatableStringRecord> {
|
||||
public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<T>>
|
||||
implements Comparable<T> {
|
||||
|
||||
@Nonnull
|
||||
private final String value;
|
||||
|
@ -62,18 +63,19 @@ public abstract class ValidatableStringRecord
|
|||
*
|
||||
* @return 字符串(不为空)
|
||||
*/
|
||||
@ReaderMethod
|
||||
public final String value() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ValidatableStringRecord o) {
|
||||
return this.value.compareTo(o.value);
|
||||
public int compareTo(T o) {
|
||||
return this.value.compareTo(o.value());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(value);
|
||||
return Objects.hash(getClass(), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -84,8 +86,9 @@ public abstract class ValidatableStringRecord
|
|||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
ValidatableStringRecord other = (ValidatableStringRecord) obj;
|
||||
return Objects.equals(value, other.value);
|
||||
@SuppressWarnings("unchecked")
|
||||
T other = (T) obj;
|
||||
return Objects.equals(value, other.value());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -16,10 +16,11 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
|
||||
/**
|
||||
* 返回分页查询的结果
|
||||
|
@ -36,8 +37,7 @@ public class PageResult<T> {
|
|||
private final List<T> content;
|
||||
|
||||
private PageResult(List<T> content, long total) {
|
||||
AssertTools.checkNotNull(content, "Content must not be null.");
|
||||
this.content = content;
|
||||
this.content = CollectionTools.nullToEmptyList(content);
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
|
@ -46,6 +46,11 @@ public class PageResult<T> {
|
|||
return new PageResult<>(content, total);
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(PageResult.class)
|
||||
public static <T> PageResult<T> empty() {
|
||||
return new PageResult<>(Collections.emptyList(), 0L);
|
||||
}
|
||||
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
@ -56,6 +61,6 @@ public class PageResult<T> {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PageDTO [total=" + total + ", content=" + content + "]";
|
||||
return "PageResult [total=" + total + ", content=" + content + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.sql;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
public class JdbcSql extends SQL<JdbcSql> {
|
||||
|
||||
JdbcSql() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static JdbcSql newSql() {
|
||||
return new JdbcSql();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JdbcSql getSelf() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String IN(String col, Collection<?> c) { // NOSONAR
|
||||
return IN(col, c.size());
|
||||
}
|
||||
|
||||
public static <T> String IN(String col, T[] c) { // NOSONAR
|
||||
return IN(col, c.length);
|
||||
}
|
||||
|
||||
private static String IN(String col, int length) { // NOSONAR
|
||||
if (length == 0) {
|
||||
return "false";
|
||||
}
|
||||
return col + " IN (" + buildQuestionsList(length) + ')';
|
||||
}
|
||||
|
||||
public static String NOT_IN(String col, Collection<?> c) { // NOSONAR
|
||||
return NOT_IN(col, c.size());
|
||||
}
|
||||
|
||||
public static <T> String NOT_IN(String col, T[] c) { // NOSONAR
|
||||
return NOT_IN(col, c.length);
|
||||
}
|
||||
|
||||
private static String NOT_IN(String col, int length) { // NOSONAR
|
||||
if (length == 0) {
|
||||
return "true";
|
||||
}
|
||||
return col + " NOT IN (" + buildQuestionsList(length) + ')';
|
||||
}
|
||||
|
||||
private static String buildQuestionsList(int times) {
|
||||
final int length = times <= 0 ? 0 : (times * 3 - 2);
|
||||
return StringTools.repeat("?, ", times, length);
|
||||
}
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.sql;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
@Beta
|
||||
public class MyBatisSql extends SQL<MyBatisSql> {
|
||||
|
||||
private final boolean withScript;
|
||||
|
||||
MyBatisSql(boolean withScript) {
|
||||
super();
|
||||
this.withScript = withScript;
|
||||
}
|
||||
|
||||
public static MyBatisSql newSql() {
|
||||
return new MyBatisSql(false);
|
||||
}
|
||||
|
||||
public static MyBatisSql newScriptSql() {
|
||||
return new MyBatisSql(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyBatisSql getSelf() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String IN(String col, String paramName) { // NOSONAR
|
||||
return " " + col + " IN" + buildForeach(col, paramName);
|
||||
}
|
||||
|
||||
public static String NOT_IN(String col, String paramName) { // NOSONAR
|
||||
return col + " NOT IN" + buildForeach(col, paramName);
|
||||
}
|
||||
|
||||
private static String buildForeach(String col, String paramName) {
|
||||
final String format = "<foreach" +
|
||||
" item=\"%s\"" +
|
||||
" index=\"index\"" +
|
||||
" collection=\"%s\"" +
|
||||
" open=\"(\"" +
|
||||
" separator=\",\"" +
|
||||
" close=\")\"" +
|
||||
">" +
|
||||
"#{%s}" +
|
||||
"</foreach>";
|
||||
return String.format(format, col, paramName, col);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (withScript) {
|
||||
return "<script>\n" + super.toString() + "\n</script>";
|
||||
}
|
||||
return super.toString();
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.sql;
|
||||
|
||||
import org.apache.ibatis.jdbc.AbstractSQL;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
/**
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
@Beta
|
||||
public abstract class SQL<T> extends AbstractSQL<T> {
|
||||
|
||||
public static JdbcSql newJdbcSql() {
|
||||
return new JdbcSql();
|
||||
}
|
||||
|
||||
public static MyBatisSql newMyBatisSql(boolean withScript) {
|
||||
return new MyBatisSql(withScript);
|
||||
}
|
||||
|
||||
public T WHERE(boolean condition, String sqlCondition) { // NOSONAR
|
||||
if (condition) {
|
||||
return WHERE(sqlCondition);
|
||||
}
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T WHERE(boolean condition, String ifSqlCondition, String elseSqlCondition) { // NOSONAR
|
||||
return WHERE(condition ? ifSqlCondition : elseSqlCondition);
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.time;
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
import java.time.temporal.ChronoField;
|
||||
|
@ -171,7 +172,8 @@ public enum Quarter implements IWithIntCode {
|
|||
// Getters end
|
||||
|
||||
public static int checkValidIntValue(int value) {
|
||||
AssertTools.checkArgument(value >= 1 && value <= 4, () -> "Invalid value for Quarter: " + value);
|
||||
AssertTools.checkCondition(value >= 1 && value <= 4,
|
||||
() -> new DateTimeException("Invalid value for Quarter: " + value));
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ import java.io.Serializable;
|
|||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.time.YearMonth;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
@ -52,7 +53,6 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
private final LocalDate lastDate;
|
||||
|
||||
private YearQuarter(int year, @Nonnull Quarter quarter) {
|
||||
AssertTools.checkNotNull(quarter, "Quarter can not be null.");
|
||||
this.year = year;
|
||||
this.quarter = quarter;
|
||||
this.firstDate = quarter.firstMonthDay().atYear(year);
|
||||
|
@ -70,7 +70,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(int year, int quarter) {
|
||||
return of(year, Quarter.of(quarter));
|
||||
return new YearQuarter(YEAR.checkValidIntValue(year), Quarter.of(quarter));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,8 +81,8 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
* @return {@link YearQuarter} 实例
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(int year, @Nonnull Quarter quarter) {
|
||||
return new YearQuarter(year, quarter);
|
||||
public static YearQuarter of(int year, Quarter quarter) {
|
||||
return new YearQuarter(YEAR.checkValidIntValue(year), Objects.requireNonNull(quarter));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,8 +92,9 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
* @return {@link YearQuarter} 实例
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(@Nonnull LocalDate date) {
|
||||
return of(date.getYear(), Quarter.fromMonth(date.getMonth()));
|
||||
public static YearQuarter of(LocalDate date) {
|
||||
AssertTools.checkNotNull(date);
|
||||
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -103,12 +104,13 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
* @return {@link YearQuarter} 实例
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(@Nonnull Date date) {
|
||||
public static YearQuarter of(Date date) {
|
||||
AssertTools.checkNotNull(date);
|
||||
@SuppressWarnings("deprecation")
|
||||
final int year = date.getYear() + 1900;
|
||||
final int yearValue = YEAR.checkValidIntValue(date.getYear() + 1900L);
|
||||
@SuppressWarnings("deprecation")
|
||||
final int month = date.getMonth() + 1;
|
||||
return of(year, Quarter.fromMonth(month));
|
||||
final int monthValue = date.getMonth() + 1;
|
||||
return new YearQuarter(yearValue, Quarter.fromMonth(monthValue));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,7 +121,10 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(Calendar date) {
|
||||
return of(date.get(Calendar.YEAR), Quarter.fromMonth(date.get(Calendar.MONTH) + 1));
|
||||
AssertTools.checkNotNull(date);
|
||||
final int yearValue = ChronoField.YEAR.checkValidIntValue(date.get(Calendar.YEAR));
|
||||
final int monthValue = date.get(Calendar.MONTH) + 1;
|
||||
return new YearQuarter(yearValue, Quarter.fromMonth(monthValue));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -130,9 +135,15 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(YearMonth yearMonth) {
|
||||
AssertTools.checkNotNull(yearMonth);
|
||||
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter now() {
|
||||
return of(LocalDate.now());
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - Getters
|
||||
|
@ -185,7 +196,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
|
||||
// #region - computes
|
||||
|
||||
public YearQuarter plusQuarters(long quartersToAdd) { // TODO 单元测试
|
||||
public YearQuarter plusQuarters(long quartersToAdd) {
|
||||
if (quartersToAdd == 0L) {
|
||||
return this;
|
||||
}
|
||||
|
@ -193,27 +204,27 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
long calcQuarters = quarterCount + quartersToAdd; // safe overflow
|
||||
int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcQuarters, 4));
|
||||
int newQuarter = (int) Math.floorMod(calcQuarters, 4) + 1;
|
||||
return of(newYear, Quarter.of(newQuarter));
|
||||
return new YearQuarter(newYear, Quarter.of(newQuarter));
|
||||
}
|
||||
|
||||
public YearQuarter minusQuarters(long quartersToAdd) { // TODO 单元测试
|
||||
public YearQuarter minusQuarters(long quartersToAdd) {
|
||||
return plusQuarters(-quartersToAdd);
|
||||
}
|
||||
|
||||
public YearQuarter nextQuarter() { // TODO 单元测试
|
||||
public YearQuarter nextQuarter() {
|
||||
return plusQuarters(1L);
|
||||
}
|
||||
|
||||
public YearQuarter lastQuarter() { // TODO 单元测试
|
||||
public YearQuarter lastQuarter() {
|
||||
return minusQuarters(1L);
|
||||
}
|
||||
|
||||
public YearQuarter plusYears(long yearsToAdd) { // TODO 单元测试
|
||||
public YearQuarter plusYears(long yearsToAdd) {
|
||||
if (yearsToAdd == 0L) {
|
||||
return this;
|
||||
}
|
||||
int newYear = YEAR.checkValidIntValue(this.year + yearsToAdd); // safe overflow
|
||||
return of(newYear, this.quarter);
|
||||
return new YearQuarter(newYear, this.quarter);
|
||||
}
|
||||
|
||||
public YearQuarter minusYears(long yearsToAdd) {
|
||||
|
@ -270,11 +281,11 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
|||
return this.compareTo(other) > 0;
|
||||
}
|
||||
|
||||
public static YearQuarter min(YearQuarter yearQuarter1, YearQuarter yearQuarter2) { // TODO 单元测试
|
||||
public static YearQuarter min(YearQuarter yearQuarter1, YearQuarter yearQuarter2) {
|
||||
return yearQuarter1.compareTo(yearQuarter2) <= 0 ? yearQuarter1 : yearQuarter2;
|
||||
}
|
||||
|
||||
public static YearQuarter max(YearQuarter yearQuarter1, YearQuarter yearQuarter2) { // TODO 单元测试
|
||||
public static YearQuarter max(YearQuarter yearQuarter1, YearQuarter yearQuarter2) {
|
||||
return yearQuarter1.compareTo(yearQuarter2) >= 0 ? yearQuarter1 : yearQuarter2;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.commons.util;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
@ -260,12 +261,7 @@ public class ArrayTools {
|
|||
*/
|
||||
public static <T> boolean isAllElementsNotNull(@Nonnull final T[] arr) {
|
||||
AssertTools.checkArgument(arr != null, "The array cannot be null.");
|
||||
for (T element : arr) {
|
||||
if (element == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return Arrays.stream(arr).allMatch(Objects::nonNull);
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
@ -626,7 +622,7 @@ public class ArrayTools {
|
|||
|
||||
// fill - char
|
||||
|
||||
public static void fill(char[] a, char... values) {
|
||||
public static void fill(char[] a, char[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
|
@ -634,9 +630,9 @@ public class ArrayTools {
|
|||
fill(a, 0, a.length, values != null ? values.toCharArray() : EMPTY_CHAR_ARRAY);
|
||||
}
|
||||
|
||||
public static void fill(char[] a, int fromIndex, int toIndex, char... values) {
|
||||
public static void fill(char[] a, int fromIndex, int toIndex, char[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values.length == 0) {
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
final int start = Integer.max(fromIndex, 0);
|
||||
|
@ -659,13 +655,13 @@ public class ArrayTools {
|
|||
|
||||
// fill - byte
|
||||
|
||||
public static void fill(byte[] a, byte... values) {
|
||||
public static void fill(byte[] a, byte[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(byte[] a, int fromIndex, int toIndex, byte... values) {
|
||||
public static void fill(byte[] a, int fromIndex, int toIndex, byte[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values.length == 0) {
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
final int start = Integer.max(fromIndex, 0);
|
||||
|
@ -688,13 +684,13 @@ public class ArrayTools {
|
|||
|
||||
// fill - short
|
||||
|
||||
public static void fill(short[] a, short... values) {
|
||||
public static void fill(short[] a, short[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(short[] a, int fromIndex, int toIndex, short... values) {
|
||||
public static void fill(short[] a, int fromIndex, int toIndex, short[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values.length == 0) {
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
final int start = Integer.max(fromIndex, 0);
|
||||
|
@ -717,13 +713,13 @@ public class ArrayTools {
|
|||
|
||||
// fill - int
|
||||
|
||||
public static void fill(int[] a, int... values) {
|
||||
public static void fill(int[] a, int[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(int[] a, int fromIndex, int toIndex, int... values) {
|
||||
public static void fill(int[] a, int fromIndex, int toIndex, int[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values.length == 0) {
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
final int start = Integer.max(fromIndex, 0);
|
||||
|
@ -746,13 +742,13 @@ public class ArrayTools {
|
|||
|
||||
// fill - long
|
||||
|
||||
public static void fill(long[] a, long... values) {
|
||||
public static void fill(long[] a, long[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(long[] a, int fromIndex, int toIndex, long... values) {
|
||||
public static void fill(long[] a, int fromIndex, int toIndex, long[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values.length == 0) {
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
final int start = Integer.max(fromIndex, 0);
|
||||
|
@ -775,13 +771,13 @@ public class ArrayTools {
|
|||
|
||||
// fill - float
|
||||
|
||||
public static void fill(float[] a, float... values) {
|
||||
public static void fill(float[] a, float[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(float[] a, int fromIndex, int toIndex, float... values) {
|
||||
public static void fill(float[] a, int fromIndex, int toIndex, float[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values.length == 0) {
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
final int start = Integer.max(fromIndex, 0);
|
||||
|
@ -804,13 +800,13 @@ public class ArrayTools {
|
|||
|
||||
// fill - double
|
||||
|
||||
public static void fill(double[] a, double... values) {
|
||||
public static void fill(double[] a, double[] values) {
|
||||
fill(a, 0, a.length, values);
|
||||
}
|
||||
|
||||
public static void fill(double[] a, int fromIndex, int toIndex, double... values) {
|
||||
public static void fill(double[] a, int fromIndex, int toIndex, double[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values.length == 0) {
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
final int start = Integer.max(fromIndex, 0);
|
||||
|
@ -868,7 +864,7 @@ public class ArrayTools {
|
|||
|
||||
// #region - indexOf
|
||||
|
||||
public static <T> int indexOf(T[] arr, Predicate<? super T> predicate) {
|
||||
public static <T> int indexOfWithPredicate(T[] arr, Predicate<? super T> predicate) {
|
||||
AssertTools.checkNotNull(predicate);
|
||||
if (isNullOrEmpty(arr)) {
|
||||
return NOT_FOUND_INDEX;
|
||||
|
@ -882,7 +878,7 @@ public class ArrayTools {
|
|||
}
|
||||
|
||||
public static <T> int indexOf(T[] arr, T obj) {
|
||||
return indexOf(arr, item -> Objects.equals(item, obj));
|
||||
return indexOfWithPredicate(arr, item -> Objects.equals(item, obj));
|
||||
}
|
||||
|
||||
public static int indexOf(char[] arr, char value) {
|
||||
|
@ -973,7 +969,7 @@ public class ArrayTools {
|
|||
|
||||
// #region - lastIndexOf
|
||||
|
||||
public static <T> int lastIndexOf(T[] arr, @Nonnull Predicate<? super T> predicate) {
|
||||
public static <T> int lastIndexOfWithPredicate(T[] arr, @Nonnull Predicate<? super T> predicate) {
|
||||
AssertTools.checkNotNull(predicate);
|
||||
if (isNullOrEmpty(arr)) {
|
||||
return NOT_FOUND_INDEX;
|
||||
|
@ -987,7 +983,7 @@ public class ArrayTools {
|
|||
}
|
||||
|
||||
public static <T> int lastIndexOf(T[] arr, T obj) {
|
||||
return lastIndexOf(arr, item -> Objects.equals(item, obj));
|
||||
return lastIndexOfWithPredicate(arr, item -> Objects.equals(item, obj));
|
||||
}
|
||||
|
||||
public static int lastIndexOf(char[] arr, char value) {
|
||||
|
@ -1111,7 +1107,7 @@ public class ArrayTools {
|
|||
}
|
||||
|
||||
public static boolean containsValue(BigDecimal[] arr, BigDecimal obj) {
|
||||
return indexOf(arr, item -> BigDecimals.equalsValue(item, obj)) > NOT_FOUND_INDEX;
|
||||
return indexOfWithPredicate(arr, item -> BigDecimals.equalsValue(item, obj)) > NOT_FOUND_INDEX;
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.JRE;
|
||||
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* ConcurrentHashMapTools
|
||||
*
|
||||
* <p>
|
||||
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
||||
* 可使用这个工具类的 {@link computeIfAbsentForJava8} 进行替换。
|
||||
*
|
||||
* <p>
|
||||
* <b>NOTE: 方法来自Dubbo,见:issues#2349</b>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0
|
||||
* @see ConcurrentHashMap
|
||||
* @see SafeConcurrentHashMap
|
||||
*/
|
||||
public class ConcurrentHashMapTools {
|
||||
|
||||
public static <K, V> V computeIfAbsent(
|
||||
ConcurrentHashMap<K, V> map, final K key, // NOSONAR
|
||||
final Function<? super K, ? extends V> mappingFunction) {
|
||||
Objects.requireNonNull(map, "map");
|
||||
return JRE.isJava8()
|
||||
? computeIfAbsentForJava8(map, key, mappingFunction)
|
||||
: map.computeIfAbsent(key, mappingFunction);
|
||||
}
|
||||
|
||||
public static <K, V> V computeIfAbsentForJava8(
|
||||
ConcurrentHashMap<K, V> map, final K key, // NOSONAR
|
||||
final Function<? super K, ? extends V> mappingFunction) {
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(mappingFunction);
|
||||
V v = map.get(key);
|
||||
if (null == v) {
|
||||
v = mappingFunction.apply(key);
|
||||
if (null == v) {
|
||||
return null;
|
||||
}
|
||||
final V res = map.putIfAbsent(key, v);
|
||||
if (null != res) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
private ConcurrentHashMapTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static java.time.temporal.ChronoField.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
@ -30,7 +32,6 @@ import java.util.Calendar;
|
|||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||
|
@ -46,29 +47,27 @@ public class DateTimeTools {
|
|||
// #region - toString
|
||||
|
||||
public static String toYearString(int year) {
|
||||
return Integer.toString(year);
|
||||
return Integer.toString(YEAR.checkValidIntValue(year));
|
||||
}
|
||||
|
||||
public static String toYearString(Year year) {
|
||||
return year.toString();
|
||||
}
|
||||
|
||||
public static String toMonthString(int monthValue) {
|
||||
return Strings.padStart(Integer.toString(monthValue), 2, '0');
|
||||
public static String toMonthStringM(int monthValue) {
|
||||
return Integer.toString(MONTH_OF_YEAR.checkValidIntValue(monthValue));
|
||||
}
|
||||
|
||||
public static String toMonthString(int monthValue, boolean padStart) {
|
||||
return padStart ? toMonthString(monthValue) : Integer.toString(monthValue);
|
||||
public static String toMonthStringMM(int monthValue) {
|
||||
return String.format("%02d", MONTH_OF_YEAR.checkValidIntValue(monthValue));
|
||||
}
|
||||
|
||||
public static String toMonthString(Month month) {
|
||||
final int monthValue = month.getValue();
|
||||
return Strings.padStart(Integer.toString(monthValue), 2, '0');
|
||||
public static String toMonthStringM(Month month) {
|
||||
return Integer.toString(month.getValue());
|
||||
}
|
||||
|
||||
public static String toMonthString(Month month, boolean padStart) {
|
||||
final int monthValue = month.getValue();
|
||||
return padStart ? toMonthString(month) : Integer.toString(monthValue);
|
||||
public static String toMonthStringMM(Month month) {
|
||||
return String.format("%02d", month.getValue());
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
@ -282,7 +281,7 @@ public class DateTimeTools {
|
|||
* @param zone 时区
|
||||
* @return 带时区的地区时间
|
||||
*/
|
||||
public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime, ZoneId zone) { // NOSONAR
|
||||
public static ZonedDateTime toZonedDateTime(LocalDateTime localDateTime, ZoneId zone) {
|
||||
return ZonedDateTime.of(localDateTime, zone);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,9 +21,17 @@ import java.util.Objects;
|
|||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
@Beta
|
||||
/**
|
||||
* ID 生成器
|
||||
*
|
||||
* <p>
|
||||
* 生成 UUID 和 修改版雪花ID(Seata 版本)
|
||||
* </p>
|
||||
*
|
||||
* @see UUID
|
||||
* @see IdWorker
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a>
|
||||
*/
|
||||
public class IdGenerator {
|
||||
|
||||
// ===== UUID =====
|
||||
|
|
|
@ -24,7 +24,26 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||
|
||||
import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
|
||||
|
||||
|
||||
/**
|
||||
* Seata 提供的修改版雪花ID。
|
||||
* <p>
|
||||
* 大体思路为:
|
||||
* <ol>
|
||||
* <li>每个机器线程安全地生成序列,前面加上机器的id,这样就不会与其它机器的id相冲突。</li>
|
||||
* <li>时间戳作为序列的“预留位”,它更像是应用启动时最开始的序列的一部分,在一个时间戳里生成 4096 个 id 之后,直接生成下一个时间戳的 id。</li>
|
||||
* </ol>
|
||||
* </p>
|
||||
* <p>
|
||||
* 详情见以下介绍:
|
||||
* <ul>
|
||||
* <li><a href="https://seata.apache.org/zh-cn/blog/seata-analysis-UUID-generator/">Seata基于改良版雪花算法的分布式UUID生成器分析</a></li>
|
||||
* <li><a href="https://seata.apache.org/zh-cn/blog/seata-snowflake-explain">关于新版雪花算法的答疑</a></li>
|
||||
* <li><a href="https://juejin.cn/post/7264387737276203065">在开源项目中看到一个改良版的雪花算法,现在它是你的了。</a></li>
|
||||
* <li><a href="https://juejin.cn/post/7265516484029743138">关于若干读者,阅读“改良版雪花算法”后提出的几个共性问题的回复。</a></li>
|
||||
* </ul>
|
||||
* </p>
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a>
|
||||
*/
|
||||
public class IdWorker {
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
@ -23,14 +24,40 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* 随机工具类
|
||||
* <p>
|
||||
* 建议调用方自行维护 Random 对象
|
||||
* </p>
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a>
|
||||
*/
|
||||
public final class RandomTools {
|
||||
|
||||
public static final SecureRandom DEFAULT_SECURE_RANDOM = new SecureRandom();
|
||||
private static final SecureRandom DEFAULT_SECURE_RANDOM;
|
||||
|
||||
static {
|
||||
SecureRandom secureRandom = null;
|
||||
try {
|
||||
secureRandom = SecureRandom.getInstanceStrong(); // 获取高强度安全随机数生成器
|
||||
}
|
||||
catch (NoSuchAlgorithmException e) {
|
||||
secureRandom = new SecureRandom(); // 获取普通的安全随机数生成器
|
||||
}
|
||||
DEFAULT_SECURE_RANDOM = secureRandom;
|
||||
}
|
||||
|
||||
public static final String CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
public static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
|
||||
public static final String NUMBERS = "0123456789";
|
||||
|
||||
public static SecureRandom defaultSecureRandom() {
|
||||
return DEFAULT_SECURE_RANDOM;
|
||||
}
|
||||
|
||||
public static ThreadLocalRandom currentThreadLocalRandom() {
|
||||
return ThreadLocalRandom.current();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用传入的随机数生成器,生成指定长度的字符串
|
||||
*
|
||||
|
|
|
@ -89,22 +89,6 @@ public final class RegexTools {
|
|||
return getPatternsInternal(patterns);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动缓存 Pattern 实例。
|
||||
*
|
||||
* @param pattern 要缓存的 {@link Pattern} 实例
|
||||
* @return 缓存的 Pattern 实例。如果缓存已满,则返回 {@code null}。
|
||||
*/
|
||||
public static Pattern cachePattern(final Pattern pattern) {
|
||||
AssertTools.checkNotNull(pattern, "The pattern can not be null.");
|
||||
if (PATTERN_CACHE.size() >= MAX_CACHE_SIZE) {
|
||||
return null;
|
||||
}
|
||||
final String patternStr = pattern.pattern();
|
||||
final Pattern pre = PATTERN_CACHE.putIfAbsent(patternStr, pattern);
|
||||
return pre != null ? pre : pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 {@code input} 是否匹配 {@code pattern}。
|
||||
*
|
||||
|
@ -285,7 +269,6 @@ public final class RegexTools {
|
|||
* 获取 {@link Pattern} 实例。
|
||||
*
|
||||
* @param pattern 正则表达式
|
||||
* @param cachePattern 是否缓存 {@link Pattern} 实例
|
||||
* @return {@link Pattern} 实例
|
||||
*/
|
||||
@Nonnull
|
||||
|
|
|
@ -18,12 +18,9 @@ package xyz.zhouxy.plusone.commons.util;
|
|||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
/**
|
||||
* Twitter_Snowflake
|
||||
* Twitter 版雪花算法
|
||||
*/
|
||||
@Beta
|
||||
public class SnowflakeIdGenerator {
|
||||
|
||||
// ==============================Fields===========================================
|
||||
|
@ -67,9 +64,6 @@ public class SnowflakeIdGenerator {
|
|||
/** 上次生成 ID 的时间截 */
|
||||
private long lastTimestamp = -1L;
|
||||
|
||||
/** 锁对象 */
|
||||
private final Object lock = new Object();
|
||||
|
||||
// ==============================Constructors=====================================
|
||||
|
||||
/**
|
||||
|
@ -93,51 +87,47 @@ public class SnowflakeIdGenerator {
|
|||
*
|
||||
* @return SnowflakeId
|
||||
*/
|
||||
public long nextId() {
|
||||
long timestamp;
|
||||
synchronized (lock) {
|
||||
timestamp = timeGen();
|
||||
public synchronized long nextId() {
|
||||
long timestamp = timeGen();
|
||||
|
||||
// 发生了回拨,此刻时间小于上次发号时间
|
||||
if (timestamp < lastTimestamp) {
|
||||
long offset = lastTimestamp - timestamp;
|
||||
if (offset <= 5) {
|
||||
// 时间偏差大小小于5ms,则等待两倍时间
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(offset << 1);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
timestamp = timeGen();
|
||||
if (timestamp < lastTimestamp) {
|
||||
// 还是小于,抛异常上报
|
||||
throwClockBackwardsEx(lastTimestamp, timestamp);
|
||||
}
|
||||
} else {
|
||||
// 发生了回拨,此刻时间小于上次发号时间
|
||||
if (timestamp < lastTimestamp) {
|
||||
long offset = lastTimestamp - timestamp;
|
||||
if (offset <= 5) {
|
||||
// 时间偏差大小小于5ms,则等待两倍时间
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(offset << 1);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
timestamp = timeGen();
|
||||
if (timestamp < lastTimestamp) {
|
||||
// 还是小于,抛异常上报
|
||||
throwClockBackwardsEx(lastTimestamp, timestamp);
|
||||
}
|
||||
} else {
|
||||
throwClockBackwardsEx(lastTimestamp, timestamp);
|
||||
}
|
||||
|
||||
// 如果是同一时间生成的,则进行毫秒内序列
|
||||
if (lastTimestamp == timestamp) {
|
||||
sequence = (sequence + 1) & SEQUENCE_MASK;
|
||||
// 毫秒内序列溢出
|
||||
if (sequence == 0) {
|
||||
// 阻塞到下一个毫秒,获得新的时间戳
|
||||
timestamp = tilNextMillis(lastTimestamp);
|
||||
}
|
||||
}
|
||||
// 时间戳改变,毫秒内序列重置
|
||||
else {
|
||||
sequence = 0L;
|
||||
}
|
||||
|
||||
// 上次生成 ID 的时间截
|
||||
lastTimestamp = timestamp;
|
||||
|
||||
}
|
||||
|
||||
// 如果是同一时间生成的,则进行毫秒内序列
|
||||
if (lastTimestamp == timestamp) {
|
||||
sequence = (sequence + 1) & SEQUENCE_MASK;
|
||||
// 毫秒内序列溢出
|
||||
if (sequence == 0) {
|
||||
// 阻塞到下一个毫秒,获得新的时间戳
|
||||
timestamp = tilNextMillis(lastTimestamp);
|
||||
}
|
||||
}
|
||||
// 时间戳改变,毫秒内序列重置
|
||||
else {
|
||||
sequence = 0L;
|
||||
}
|
||||
|
||||
// 上次生成 ID 的时间截
|
||||
lastTimestamp = timestamp;
|
||||
|
||||
// 移位并通过或运算拼到一起组成64位的ID
|
||||
return ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | datacenterIdAndWorkerId | sequence;
|
||||
}
|
||||
|
|
|
@ -1,15 +1,39 @@
|
|||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
public class CollectionToolsTests {
|
||||
@Test
|
||||
void testIsEmpty() {
|
||||
|
@ -30,4 +54,19 @@ public class CollectionToolsTests {
|
|||
assertFalse(CollectionTools.isEmpty(map));
|
||||
assertTrue(CollectionTools.isNotEmpty(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNullToEmpty() {
|
||||
List<String> list = Lists.newArrayList("Java", "C", "C++", "C#");
|
||||
assertSame(list, CollectionTools.nullToEmptyList(list));
|
||||
assertEquals(Collections.emptyList(), CollectionTools.nullToEmptyList(null));
|
||||
|
||||
Set<String> set = Sets.newHashSet("Java", "C", "C++", "C#");
|
||||
assertSame(set, CollectionTools.nullToEmptySet(set));
|
||||
assertEquals(Collections.emptySet(), CollectionTools.nullToEmptySet(null));
|
||||
|
||||
Map<String, Integer> map = ImmutableMap.of("K1", 1, "K2", 2, "K3", 3);
|
||||
assertSame(map, CollectionTools.nullToEmptyMap(map));
|
||||
assertEquals(Collections.emptyMap(), CollectionTools.nullToEmptyMap(null));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,235 @@
|
|||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.constant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public //
|
||||
class PatternConstsTests {
|
||||
|
||||
// ================================
|
||||
// #region - BASIC_ISO_DATE
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void testBasicIsoDate_ValidDate() {
|
||||
Matcher matcher = PatternConsts.BASIC_ISO_DATE.matcher("20241229");
|
||||
assertTrue(matcher.matches());
|
||||
|
||||
assertEquals("2024", matcher.group(1));
|
||||
assertEquals("12", matcher.group(2));
|
||||
assertEquals("29", matcher.group(3));
|
||||
assertEquals("2024", matcher.group("yyyy"));
|
||||
assertEquals("12", matcher.group("MM"));
|
||||
assertEquals("29", matcher.group("dd"));
|
||||
|
||||
// LeapYearFeb29()
|
||||
assertTrue(PatternConsts.BASIC_ISO_DATE.matcher("20200229").matches());
|
||||
|
||||
// BoundaryMin()
|
||||
assertTrue(PatternConsts.BASIC_ISO_DATE.matcher("00000101").matches());
|
||||
|
||||
// BoundaryMax()
|
||||
assertTrue(PatternConsts.BASIC_ISO_DATE.matcher("9999999991231").matches());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"20231301", // InvalidMonth
|
||||
"20230230", // InvalidDay
|
||||
"20210229", // NonLeapYearFeb29
|
||||
})
|
||||
void testBasicIsoDate_InvalidDate_butMatches(String date) {
|
||||
// 虽然日期有误,但这个正则无法判断。实际工作中,应使用日期时间 API。
|
||||
Matcher matcher = PatternConsts.BASIC_ISO_DATE.matcher(date);
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"2023041", // TooShort
|
||||
"99999999990415", // TooLong
|
||||
"2023-04-15", // NonNumeric
|
||||
})
|
||||
void testBasicIsoDate_InvalidDate_Mismatches(String date) {
|
||||
Matcher matcher = PatternConsts.BASIC_ISO_DATE.matcher(date);
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - BASIC_ISO_DATE
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ISO_LOCAL_DATE
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void testIsoLocalDate_ValidDate() {
|
||||
Matcher matcher = PatternConsts.ISO_LOCAL_DATE.matcher("2024-12-29");
|
||||
assertTrue(matcher.matches());
|
||||
assertEquals("2024", matcher.group("yyyy"));
|
||||
assertEquals("12", matcher.group("MM"));
|
||||
assertEquals("29", matcher.group("dd"));
|
||||
|
||||
// LeapYearFeb29()
|
||||
assertTrue(PatternConsts.ISO_LOCAL_DATE.matcher("2020-02-29").matches());
|
||||
|
||||
// BoundaryMin()
|
||||
assertTrue(PatternConsts.ISO_LOCAL_DATE.matcher("0000-01-01").matches());
|
||||
|
||||
// BoundaryMax()
|
||||
assertTrue(PatternConsts.ISO_LOCAL_DATE.matcher("999999999-12-31").matches());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"2023-13-01", // InvalidMonth
|
||||
"2023-02-30", // InvalidDay
|
||||
"2021-02-29", // NonLeapYearFeb29
|
||||
})
|
||||
void testIsoLocalDate_InvalidDate_butMatches(String date) {
|
||||
// 虽然日期有误,但这个正则无法判断。实际工作中,应使用日期时间 API。
|
||||
Matcher matcher = PatternConsts.ISO_LOCAL_DATE.matcher(date);
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"2023-04-1", // TooShort
|
||||
"9999999999-04-15", // TooLong
|
||||
"20230415",
|
||||
})
|
||||
void testIsoLocalDate_InvalidDate_Mismatches(String date) {
|
||||
Matcher matcher = PatternConsts.ISO_LOCAL_DATE.matcher(date);
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - ISO_LOCAL_DATE
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - PASSWORD
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void testPassword_ValidPassword_Matches() {
|
||||
assertTrue(PatternConsts.PASSWORD.matcher("Abc123!@#").matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPassword_InvalidPassword_Mismatches() {
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("Abc123 !@#").matches()); // 带空格
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("Abc123!@# ").matches()); // 带空格
|
||||
assertFalse(PatternConsts.PASSWORD.matcher(" Abc123!@#").matches()); // 带空格
|
||||
assertFalse(PatternConsts.PASSWORD.matcher(" Abc123!@# ").matches()); // 带空格
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("77553366998844113322").matches()); // 纯数字
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("poiujhgbfdsazxcfvghj").matches()); // 纯小写字母
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("POIUJHGBFDSAZXCFVGHJ").matches()); // 纯大写字母
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("!#$%&'*\\+-/=?^`{|}~@()[]\",.;':").matches()); // 纯特殊字符
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("sdfrghbv525842582752").matches()); // 没有小写字母
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("SDFRGHBV525842582752").matches()); // 没有小写字母
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("sdfrghbvSDFRGHBV").matches()); // 没有数字
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("Abc1!").matches()); // 太短
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("Abc1!Abc1!Abc1!Abc1!Abc1!Abc1!Abc1!").matches()); // 太长
|
||||
assertFalse(PatternConsts.PASSWORD.matcher("").matches());
|
||||
assertFalse(PatternConsts.PASSWORD.matcher(" ").matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - PASSWORD
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - EMAIL
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
public void testValidEmails() {
|
||||
assertTrue(PatternConsts.EMAIL.matcher("test@example.com").matches());
|
||||
assertTrue(PatternConsts.EMAIL.matcher("user.name+tag+sorting@example.com").matches());
|
||||
assertTrue(PatternConsts.EMAIL.matcher("user@sub.example.com").matches());
|
||||
assertTrue(PatternConsts.EMAIL.matcher("user@123.123.123.123").matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidEmails() {
|
||||
assertFalse(PatternConsts.EMAIL.matcher(".username@example.com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("@missingusername.com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("plainaddress").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username..username@example.com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username.@example.com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@-example.com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@-example.com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@.com.com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@.com.my").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@.com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@com.").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@example..com").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@example.com-").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@example.com.").matches());
|
||||
assertFalse(PatternConsts.EMAIL.matcher("username@example").matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - EMAIL
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - Chinese2ndIdCardNumber
|
||||
// ================================
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"44520019900101456X",
|
||||
"44520019900101456x",
|
||||
"445200199001014566",
|
||||
})
|
||||
void testChinese2ndIdCardNumber_ValidChinese2ndIdCardNumber(String value) {
|
||||
Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(value);
|
||||
assertTrue(matcher.matches());
|
||||
assertEquals("44", matcher.group("province"));
|
||||
assertEquals("4452", matcher.group("city"));
|
||||
assertEquals("445200", matcher.group("county"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"4452200199001014566",
|
||||
"44520199001014566",
|
||||
" ",
|
||||
"",
|
||||
})
|
||||
void testChinese2ndIdCardNumber_InvalidChinese2ndIdCardNumber(String value) {
|
||||
assertFalse(PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(value).matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - Chinese2ndIdCardNumber
|
||||
// ================================
|
||||
}
|
|
@ -17,35 +17,27 @@
|
|||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.regex.Matcher;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
|
||||
@Slf4j
|
||||
public class Chinese2ndGenIDCardNumberTests {
|
||||
|
||||
@Test
|
||||
void testPattern() {
|
||||
Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher("11010520000101111X");
|
||||
assertTrue(matcher.matches());
|
||||
for (int i = 0; i < matcher.groupCount(); i++) {
|
||||
log.info("{}: {}", i, matcher.group(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
void testOf_success() {
|
||||
Chinese2ndGenIDCardNumber idCardNumber = Chinese2ndGenIDCardNumber.of("11010520000101111X");
|
||||
assertEquals("11010520000101111X", idCardNumber.value());
|
||||
assertEquals(LocalDate.of(2000, 1, 1), idCardNumber.getBirthDate());
|
||||
assertEquals(Gender.MALE, idCardNumber.getGender());
|
||||
assertSame(Gender.MALE, idCardNumber.getGender());
|
||||
assertEquals("110105", idCardNumber.getCountyCode());
|
||||
assertEquals("110105000000", idCardNumber.getFullCountyCode());
|
||||
|
||||
|
@ -57,17 +49,43 @@ public class Chinese2ndGenIDCardNumberTests {
|
|||
|
||||
assertEquals("北京", idCardNumber.getProvinceName());
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> Chinese2ndGenIDCardNumber.of("1101520000101111"));
|
||||
assertEquals("1***************1X", idCardNumber.toDesensitizedString());
|
||||
assertEquals("110***********111X", idCardNumber.toDesensitizedString(3, 4));
|
||||
assertEquals("110###############", idCardNumber.toDesensitizedString('#', 3, 0));
|
||||
assertEquals("11010520000101111X", idCardNumber.toDesensitizedString(10, 8));
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> Chinese2ndGenIDCardNumber.of("11010520002101111X"));
|
||||
assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(-1, 5));
|
||||
assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(5, -1));
|
||||
assertThrows(IllegalArgumentException.class, () -> idCardNumber.toDesensitizedString(10, 9));
|
||||
}
|
||||
|
||||
try {
|
||||
Chinese2ndGenIDCardNumber.of("11010520002101111X");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertTrue(e.getCause() instanceof DateTimeParseException);
|
||||
@Test
|
||||
void testOf_blankValue() {
|
||||
String[] strings = { null, "", " " };
|
||||
for (String value : strings) {
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||
() -> Chinese2ndGenIDCardNumber.of(value));
|
||||
assertEquals("二代居民身份证校验失败:号码为空", e.getMessage());
|
||||
}
|
||||
}
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "1101520000101111", "110A0520000101111X", "110105220000101111X" })
|
||||
void testOf_mismatched(String value) {
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||
() -> Chinese2ndGenIDCardNumber.of(value));
|
||||
assertEquals("二代居民身份证校验失败:" + value, e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_wrongBirthDate() {
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||
() -> Chinese2ndGenIDCardNumber.of("11010520002101111X"));
|
||||
assertTrue(e.getCause() instanceof DateTimeParseException);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_wrongProvince() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> Chinese2ndGenIDCardNumber.of("99010520000101111X"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ class User {
|
|||
}
|
||||
|
||||
@ValueObject
|
||||
class Email extends ValidatableStringRecord {
|
||||
class Email extends ValidatableStringRecord<Email> {
|
||||
private Email(String value) {
|
||||
super(value, PatternConsts.EMAIL);
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ class Email extends ValidatableStringRecord {
|
|||
}
|
||||
|
||||
@ValueObject
|
||||
class Username extends ValidatableStringRecord {
|
||||
class Username extends ValidatableStringRecord<Username> {
|
||||
private Username(String username) {
|
||||
super(username, PatternConsts.USERNAME);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,19 @@
|
|||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
@ -13,6 +29,7 @@ import lombok.AllArgsConstructor;
|
|||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.commons.exception.business.BizException;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.UnifiedResponse.SuccessResult;
|
||||
|
||||
@Slf4j
|
||||
|
@ -33,7 +50,7 @@ class UnifiedResponseTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithOutArgument() throws Exception {
|
||||
void testSuccess_WithoutArgument() throws Exception {
|
||||
// 1. success without argument
|
||||
UnifiedResponse success = UnifiedResponse.success();
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, success.getStatus());
|
||||
|
@ -56,68 +73,6 @@ class UnifiedResponseTests {
|
|||
assertEquals("{\"status\":\"2000000\",\"message\":\"成功\"}", jacksonSuccessWithMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithNullMessage() throws Exception {
|
||||
// 3. success with null message
|
||||
UnifiedResponse successWithNullMessage = UnifiedResponse.success(null);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithNullMessage.getStatus());
|
||||
assertEquals("", successWithNullMessage.getMessage());
|
||||
assertNull(successWithNullMessage.getData());
|
||||
String jacksonSuccessWithNullMessage = jackson.writeValueAsString(successWithNullMessage);
|
||||
log.info("jacksonSuccessWithNullMessage: {}", jacksonSuccessWithNullMessage);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithNullMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithEmptyMessage() throws Exception {
|
||||
// 4. success with empty message
|
||||
UnifiedResponse successWithEmptyMessage = UnifiedResponse.success("");
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithEmptyMessage.getStatus());
|
||||
assertEquals("", successWithEmptyMessage.getMessage());
|
||||
assertNull(successWithEmptyMessage.getData());
|
||||
String jacksonSuccessWithEmptyMessage = jackson.writeValueAsString(successWithEmptyMessage);
|
||||
log.info("jacksonSuccessWithEmptyMessage: {}", jacksonSuccessWithEmptyMessage);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithEmptyMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithData_String() throws Exception {
|
||||
UnifiedResponse successWithStringData = UnifiedResponse.success("查询成功", "zhouxy");
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithStringData.getStatus());
|
||||
assertEquals("查询成功", successWithStringData.getMessage());
|
||||
assertEquals("zhouxy", successWithStringData.getData());
|
||||
String jacksonSuccessWithStringData = jackson.writeValueAsString(successWithStringData);
|
||||
log.info("jacksonSuccessWithStringData: {}", jacksonSuccessWithStringData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":\"zhouxy\"}", jacksonSuccessWithStringData);
|
||||
|
||||
assertEquals("{status: \"2000000\", message: \"查询成功\", data: \"zhouxy\"}", successWithStringData.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithData_Integer() throws Exception {
|
||||
final UnifiedResponse successWithIntegerData = UnifiedResponse.success("查询成功", 1);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithIntegerData.getStatus());
|
||||
assertEquals("查询成功", successWithIntegerData.getMessage());
|
||||
assertEquals(1, successWithIntegerData.getData());
|
||||
final String jacksonSuccessWithIntegerData = jackson.writeValueAsString(successWithIntegerData);
|
||||
log.info("jacksonSuccessWithIntegerData: {}", jacksonSuccessWithIntegerData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":1}", jacksonSuccessWithIntegerData);
|
||||
|
||||
assertEquals("{status: \"2000000\", message: \"查询成功\", data: 1}", successWithIntegerData.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithData_PageResult() throws Exception {
|
||||
UnifiedResponse successWithData = UnifiedResponse.success("查询成功", pageResult);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithData.getStatus());
|
||||
assertEquals("查询成功", successWithData.getMessage());
|
||||
assertNotNull(successWithData.getData());
|
||||
assertEquals(pageResult, successWithData.getData());
|
||||
String jacksonSuccessWithData = jackson.writeValueAsString(successWithData);
|
||||
log.info("jacksonSuccessWithData: {}", jacksonSuccessWithData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":{\"total\":108,\"content\":[{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"},{\"username\":\"zhouxy2\",\"email\":\"zhouxy2@gmail.com\"}]}}", jacksonSuccessWithData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithMessageAndNullData() throws Exception {
|
||||
// success with message and null data
|
||||
|
@ -133,17 +88,53 @@ class UnifiedResponseTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithNullMessageAndData() throws Exception {
|
||||
// success with null message and data
|
||||
final User user = new User("zhouxy", "zhouxy@code108.cn");
|
||||
final UnifiedResponse successWithNullMessageAndData = UnifiedResponse.success(null, user);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithNullMessageAndData.getStatus());
|
||||
assertEquals("", successWithNullMessageAndData.getMessage());
|
||||
assertEquals(user, successWithNullMessageAndData.getData());
|
||||
final String jacksonSuccessWithNullMessageAndData = jackson.writeValueAsString(successWithNullMessageAndData);
|
||||
log.info("jacksonSuccessWithNullMessageAndData: {}", jacksonSuccessWithNullMessageAndData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@code108.cn\"}}",
|
||||
jacksonSuccessWithNullMessageAndData);
|
||||
void testSuccess_WithMessageAndStringData() throws Exception {
|
||||
UnifiedResponse successWithStringData = UnifiedResponse.success("查询成功", "zhouxy");
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithStringData.getStatus());
|
||||
assertEquals("查询成功", successWithStringData.getMessage());
|
||||
assertEquals("zhouxy", successWithStringData.getData());
|
||||
String jacksonSuccessWithStringData = jackson.writeValueAsString(successWithStringData);
|
||||
log.info("jacksonSuccessWithStringData: {}", jacksonSuccessWithStringData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":\"zhouxy\"}", jacksonSuccessWithStringData);
|
||||
|
||||
assertEquals("{status: \"2000000\", message: \"查询成功\", data: \"zhouxy\"}", successWithStringData.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithMessageAndIntegerData() throws Exception {
|
||||
final UnifiedResponse successWithIntegerData = UnifiedResponse.success("查询成功", 1);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithIntegerData.getStatus());
|
||||
assertEquals("查询成功", successWithIntegerData.getMessage());
|
||||
assertEquals(1, successWithIntegerData.getData());
|
||||
final String jacksonSuccessWithIntegerData = jackson.writeValueAsString(successWithIntegerData);
|
||||
log.info("jacksonSuccessWithIntegerData: {}", jacksonSuccessWithIntegerData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":1}", jacksonSuccessWithIntegerData);
|
||||
|
||||
assertEquals("{status: \"2000000\", message: \"查询成功\", data: 1}", successWithIntegerData.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithMessageAndData() throws Exception {
|
||||
UnifiedResponse successWithData = UnifiedResponse.success("查询成功", pageResult);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithData.getStatus());
|
||||
assertEquals("查询成功", successWithData.getMessage());
|
||||
assertNotNull(successWithData.getData());
|
||||
assertEquals(pageResult, successWithData.getData());
|
||||
String jacksonSuccessWithData = jackson.writeValueAsString(successWithData);
|
||||
log.info("jacksonSuccessWithData: {}", jacksonSuccessWithData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"查询成功\",\"data\":{\"total\":108,\"content\":[{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"},{\"username\":\"zhouxy2\",\"email\":\"zhouxy2@gmail.com\"}]}}", jacksonSuccessWithData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess_WithNullMessage() throws Exception {
|
||||
// 3. success with null message
|
||||
UnifiedResponse successWithNullMessage = UnifiedResponse.success(null);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithNullMessage.getStatus());
|
||||
assertEquals("", successWithNullMessage.getMessage());
|
||||
assertNull(successWithNullMessage.getData());
|
||||
String jacksonSuccessWithNullMessage = jackson.writeValueAsString(successWithNullMessage);
|
||||
log.info("jacksonSuccessWithNullMessage: {}", jacksonSuccessWithNullMessage);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithNullMessage);
|
||||
}
|
||||
|
||||
// success with null message and null data
|
||||
|
@ -161,18 +152,30 @@ class UnifiedResponseTests {
|
|||
assertEquals("{status: \"2000000\", message: \"\", data: null}", successWithNullMessageAndNullData.toString());
|
||||
}
|
||||
|
||||
// success with empty message and data
|
||||
@Test
|
||||
void testSuccess_WithEmptyMessageAndData() throws Exception {
|
||||
final User user = new User("zhouxy", "zhouxy@gmail.com");
|
||||
final UnifiedResponse successWithEmptyMessageAndData = UnifiedResponse.success("", user);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithEmptyMessageAndData.getStatus());
|
||||
assertEquals("", successWithEmptyMessageAndData.getMessage());
|
||||
assertEquals(user, successWithEmptyMessageAndData.getData());
|
||||
void testSuccess_WithNullMessageAndData() throws Exception {
|
||||
// success with null message and data
|
||||
final User user = new User("zhouxy", "zhouxy@code108.cn");
|
||||
final UnifiedResponse successWithNullMessageAndData = UnifiedResponse.success(null, user);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithNullMessageAndData.getStatus());
|
||||
assertEquals("", successWithNullMessageAndData.getMessage());
|
||||
assertEquals(user, successWithNullMessageAndData.getData());
|
||||
final String jacksonSuccessWithNullMessageAndData = jackson.writeValueAsString(successWithNullMessageAndData);
|
||||
log.info("jacksonSuccessWithNullMessageAndData: {}", jacksonSuccessWithNullMessageAndData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@code108.cn\"}}",
|
||||
jacksonSuccessWithNullMessageAndData);
|
||||
}
|
||||
|
||||
final String jacksonSuccessWithEmptyMessageAndData = jackson.writeValueAsString(successWithEmptyMessageAndData);
|
||||
log.info("jacksonSuccessWithEmptyMessageAndData: {}", jacksonSuccessWithEmptyMessageAndData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}", jacksonSuccessWithEmptyMessageAndData);
|
||||
@Test
|
||||
void testSuccess_WithEmptyMessage() throws Exception {
|
||||
// 4. success with empty message
|
||||
UnifiedResponse successWithEmptyMessage = UnifiedResponse.success("");
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithEmptyMessage.getStatus());
|
||||
assertEquals("", successWithEmptyMessage.getMessage());
|
||||
assertNull(successWithEmptyMessage.getData());
|
||||
String jacksonSuccessWithEmptyMessage = jackson.writeValueAsString(successWithEmptyMessage);
|
||||
log.info("jacksonSuccessWithEmptyMessage: {}", jacksonSuccessWithEmptyMessage);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"\"}", jacksonSuccessWithEmptyMessage);
|
||||
}
|
||||
|
||||
// success with empty message and null data
|
||||
|
@ -190,22 +193,297 @@ class UnifiedResponseTests {
|
|||
assertEquals("{status: \"2000000\", message: \"\", data: null}", successWithEmptyMessageAndNullData.toString());
|
||||
}
|
||||
|
||||
// success with empty message and data
|
||||
@Test
|
||||
void testError_Status_And_Message() throws Exception {
|
||||
void testSuccess_WithEmptyMessageAndData() throws Exception {
|
||||
final User user = new User("zhouxy", "zhouxy@gmail.com");
|
||||
final UnifiedResponse successWithEmptyMessageAndData = UnifiedResponse.success("", user);
|
||||
assertEquals(SuccessResult.SUCCESS_STATUS, successWithEmptyMessageAndData.getStatus());
|
||||
assertEquals("", successWithEmptyMessageAndData.getMessage());
|
||||
assertEquals(user, successWithEmptyMessageAndData.getData());
|
||||
|
||||
final String jacksonSuccessWithEmptyMessageAndData = jackson.writeValueAsString(successWithEmptyMessageAndData);
|
||||
log.info("jacksonSuccessWithEmptyMessageAndData: {}", jacksonSuccessWithEmptyMessageAndData);
|
||||
assertEquals("{\"status\":\"2000000\",\"message\":\"\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}", jacksonSuccessWithEmptyMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithMessage() throws Exception {
|
||||
UnifiedResponse errorWithMessage = UnifiedResponse.error("查询失败");
|
||||
assertEquals("9999999", errorWithMessage.getStatus());
|
||||
assertEquals("查询失败", errorWithMessage.getMessage());
|
||||
assertNull(errorWithMessage.getData());
|
||||
|
||||
final String jacksonErrorWithMessage = jackson.writeValueAsString(errorWithMessage);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\"}", jacksonErrorWithMessage);
|
||||
final String gsonErrorWithMessage = gson.toJson(errorWithMessage);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\"}", gsonErrorWithMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithMessage_AndNullData() throws Exception {
|
||||
UnifiedResponse errorWithMessageAndNullData = UnifiedResponse.error("查询失败", (Object) null);
|
||||
assertEquals("9999999", errorWithMessageAndNullData.getStatus());
|
||||
assertEquals("查询失败", errorWithMessageAndNullData.getMessage());
|
||||
assertNull(errorWithMessageAndNullData.getData());
|
||||
|
||||
final String jacksonErrorWithMessageAndNullData = jackson.writeValueAsString(errorWithMessageAndNullData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\"}", jacksonErrorWithMessageAndNullData);
|
||||
final String gsonErrorWithMessageAndNullData = gson.toJson(errorWithMessageAndNullData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\"}", gsonErrorWithMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithMessage_AndData() throws Exception {
|
||||
final User user = new User("zhouxy", "zhouxy@gmail.com");
|
||||
UnifiedResponse errorWithMessageAndData = UnifiedResponse.error("查询失败", user);
|
||||
assertEquals("9999999", errorWithMessageAndData.getStatus());
|
||||
assertEquals("查询失败", errorWithMessageAndData.getMessage());
|
||||
assertEquals(user, errorWithMessageAndData.getData());
|
||||
|
||||
final String jacksonErrorWithMessageAndData = jackson.writeValueAsString(errorWithMessageAndData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}",
|
||||
jacksonErrorWithMessageAndData);
|
||||
final String gsonErrorWithMessageAndData = gson.toJson(errorWithMessageAndData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"查询失败\",\"data\":{\"username\":\"zhouxy\",\"email\":\"zhouxy@gmail.com\"}}",
|
||||
gsonErrorWithMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithNullMessage() throws Exception {
|
||||
UnifiedResponse errorWithNullMessage = UnifiedResponse.error((String) null);
|
||||
assertEquals("9999999", errorWithNullMessage.getStatus());
|
||||
assertEquals("", errorWithNullMessage.getMessage());
|
||||
assertNull(errorWithNullMessage.getData());
|
||||
|
||||
final String jacksonErrorWithNullMessage = jackson.writeValueAsString(errorWithNullMessage);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", jacksonErrorWithNullMessage);
|
||||
final String gsonErrorWithNullMessage = gson.toJson(errorWithNullMessage);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", gsonErrorWithNullMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithNullMessage_AndNullData() throws Exception {
|
||||
UnifiedResponse errorWithNullMessageAndNullData = UnifiedResponse.error((String) null, (User) null);
|
||||
assertEquals("9999999", errorWithNullMessageAndNullData.getStatus());
|
||||
assertEquals("", errorWithNullMessageAndNullData.getMessage());
|
||||
assertNull(errorWithNullMessageAndNullData.getData());
|
||||
|
||||
final String jacksonErrorWithNullMessageAndNullData = jackson.writeValueAsString(errorWithNullMessageAndNullData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", jacksonErrorWithNullMessageAndNullData);
|
||||
final String gsonErrorWithNullMessageAndNullData = gson.toJson(errorWithNullMessageAndNullData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", gsonErrorWithNullMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithNullMessage_AndData() throws Exception {
|
||||
final User user = new User("zhouxy1", "zhouxy1@gmail.com");
|
||||
UnifiedResponse errorWithNullMessageAndData = UnifiedResponse.error((String) null, user);
|
||||
assertEquals("9999999", errorWithNullMessageAndData.getStatus());
|
||||
assertEquals("", errorWithNullMessageAndData.getMessage());
|
||||
assertEquals(user, errorWithNullMessageAndData.getData());
|
||||
|
||||
final String jacksonErrorWithNullMessageAndData = jackson.writeValueAsString(errorWithNullMessageAndData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\",\"data\":{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"}}",
|
||||
jacksonErrorWithNullMessageAndData);
|
||||
final String gsonErrorWithNullMessageAndData = gson.toJson(errorWithNullMessageAndData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\",\"data\":{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"}}",
|
||||
gsonErrorWithNullMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithEmptyMessage() throws Exception {
|
||||
UnifiedResponse errorWithEmptyMessage = UnifiedResponse.error("");
|
||||
assertEquals("9999999", errorWithEmptyMessage.getStatus());
|
||||
assertEquals("", errorWithEmptyMessage.getMessage());
|
||||
assertNull(errorWithEmptyMessage.getData());
|
||||
final String jacksonErrorWithEmptyMessage = jackson.writeValueAsString(errorWithEmptyMessage);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", jacksonErrorWithEmptyMessage);
|
||||
final String gsonErrorWithEmptyMessage = gson.toJson(errorWithEmptyMessage);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", gsonErrorWithEmptyMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithEmptyMessage_AndNullData() throws Exception {
|
||||
UnifiedResponse errorWithEmptyMessageAndNullData = UnifiedResponse.error("", (User) null);
|
||||
assertEquals("9999999", errorWithEmptyMessageAndNullData.getStatus());
|
||||
assertEquals("", errorWithEmptyMessageAndNullData.getMessage());
|
||||
assertNull(errorWithEmptyMessageAndNullData.getData());
|
||||
|
||||
final String jacksonErrorEmptyNullMessageAndNullData = jackson.writeValueAsString(errorWithEmptyMessageAndNullData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", jacksonErrorEmptyNullMessageAndNullData);
|
||||
final String gsonErrorWithEmptyMessageAndNullData = gson.toJson(errorWithEmptyMessageAndNullData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\"}", gsonErrorWithEmptyMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithEmptyMessage_AndData() throws Exception {
|
||||
final User user = new User("zhouxy1", "zhouxy1@gmail.com");
|
||||
UnifiedResponse errorWithEmptyMessageAndData = UnifiedResponse.error("", user);
|
||||
assertEquals("9999999", errorWithEmptyMessageAndData.getStatus());
|
||||
assertEquals("", errorWithEmptyMessageAndData.getMessage());
|
||||
assertEquals(user, errorWithEmptyMessageAndData.getData());
|
||||
|
||||
final String jacksonErrorWithEmptyMessageAndData = jackson.writeValueAsString(errorWithEmptyMessageAndData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\",\"data\":{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"}}",
|
||||
jacksonErrorWithEmptyMessageAndData);
|
||||
final String gsonErrorWithEmptyMessageAndData = gson.toJson(errorWithEmptyMessageAndData);
|
||||
assertEquals("{\"status\":\"9999999\",\"message\":\"\",\"data\":{\"username\":\"zhouxy1\",\"email\":\"zhouxy1@gmail.com\"}}",
|
||||
gsonErrorWithEmptyMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndMessage() throws Exception {
|
||||
final UnifiedResponse errorWithStatusAndMessage = UnifiedResponse.error(108, "查询失败");
|
||||
assertEquals(108, errorWithStatusAndMessage.getStatus());
|
||||
assertEquals("查询失败", errorWithStatusAndMessage.getMessage());
|
||||
assertNull(errorWithStatusAndMessage.getData());
|
||||
assertEquals("{status: 108, message: \"查询失败\", data: null}", errorWithStatusAndMessage.toString());
|
||||
|
||||
final String jacksonErrorWithStatusAndMessage = jackson.writeValueAsString(errorWithStatusAndMessage);
|
||||
log.info("jacksonErrorWithStatusAndMessage: {}", jacksonErrorWithStatusAndMessage);
|
||||
assertEquals("{\"status\":108,\"message\":\"查询失败\"}", jacksonErrorWithStatusAndMessage);
|
||||
|
||||
assertEquals("{status: 108, message: \"查询失败\", data: null}", errorWithStatusAndMessage.toString());
|
||||
final String gsonErrorWithStatusAndMessage = gson.toJson(errorWithStatusAndMessage);
|
||||
assertEquals("{\"status\":108,\"message\":\"查询失败\"}", gsonErrorWithStatusAndMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_NullStatus() {
|
||||
void testError_WithStatusAndMessage_AndNullData() throws Exception {
|
||||
final UnifiedResponse errorWithStatusAndMessageAndNullData = UnifiedResponse.error(108, "查询失败", null);
|
||||
assertEquals(108, errorWithStatusAndMessageAndNullData.getStatus());
|
||||
assertEquals("查询失败", errorWithStatusAndMessageAndNullData.getMessage());
|
||||
assertNull(errorWithStatusAndMessageAndNullData.getData());
|
||||
assertEquals("{status: 108, message: \"查询失败\", data: null}", errorWithStatusAndMessageAndNullData.toString());
|
||||
|
||||
final String jacksonErrorWithStatusAndMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndMessageAndNullData);
|
||||
log.info("jacksonErrorWithStatusAndMessage: {}", jacksonErrorWithStatusAndMessageAndNullData);
|
||||
assertEquals("{\"status\":108,\"message\":\"查询失败\"}", jacksonErrorWithStatusAndMessageAndNullData);
|
||||
|
||||
final String gsonErrorWithStatusAndMessageAndNullData = gson.toJson(errorWithStatusAndMessageAndNullData);
|
||||
assertEquals("{\"status\":108,\"message\":\"查询失败\"}", gsonErrorWithStatusAndMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndMessage_AndData() throws Exception {
|
||||
final PageResult<User> emptyPageResult = PageResult.empty();
|
||||
final UnifiedResponse errorWithStatusAndMessageAndData = UnifiedResponse.error(108, "查询失败", emptyPageResult);
|
||||
assertEquals(108, errorWithStatusAndMessageAndData.getStatus());
|
||||
assertEquals("查询失败", errorWithStatusAndMessageAndData.getMessage());
|
||||
assertEquals(emptyPageResult, errorWithStatusAndMessageAndData.getData());
|
||||
assertEquals("{status: 108, message: \"查询失败\", data: PageResult [total=0, content=[]]}", errorWithStatusAndMessageAndData.toString());
|
||||
|
||||
final String jacksonErrorWithStatusAndMessageAndData = jackson.writeValueAsString(errorWithStatusAndMessageAndData);
|
||||
assertEquals("{\"status\":108,\"message\":\"查询失败\",\"data\":{\"total\":0,\"content\":[]}}",
|
||||
jacksonErrorWithStatusAndMessageAndData);
|
||||
|
||||
final String gsonErrorWithStatusAndMessageAndData = gson.toJson(errorWithStatusAndMessageAndData);
|
||||
assertEquals("{\"status\":108,\"message\":\"查询失败\",\"data\":{\"total\":0,\"content\":[]}}",
|
||||
gsonErrorWithStatusAndMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndNullMessage() throws Exception {
|
||||
UnifiedResponse errorWithStatusAndNullMessage = UnifiedResponse.error(500, (String) null);
|
||||
assertEquals(500, errorWithStatusAndNullMessage.getStatus());
|
||||
assertEquals("", errorWithStatusAndNullMessage.getMessage());
|
||||
assertNull(errorWithStatusAndNullMessage.getData());
|
||||
|
||||
final String jacksonErrorWithStatusAndNullMessage = jackson.writeValueAsString(errorWithStatusAndNullMessage);
|
||||
assertEquals("{\"status\":500,\"message\":\"\"}", jacksonErrorWithStatusAndNullMessage);
|
||||
|
||||
final String gsonErrorWithStatusAndNullMessage = gson.toJson(errorWithStatusAndNullMessage);
|
||||
assertEquals("{\"status\":500,\"message\":\"\"}", gsonErrorWithStatusAndNullMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndNullMessage_AndNullData() throws Exception {
|
||||
UnifiedResponse errorWithStatusAndNullMessageAndNullData = UnifiedResponse.error(500, (String) null, null);
|
||||
|
||||
assertEquals(500, errorWithStatusAndNullMessageAndNullData.getStatus());
|
||||
assertEquals("", errorWithStatusAndNullMessageAndNullData.getMessage());
|
||||
assertNull(errorWithStatusAndNullMessageAndNullData.getData());
|
||||
|
||||
final String jacksonErrorWithStatusAndNullMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndNullMessageAndNullData);
|
||||
assertEquals("{\"status\":500,\"message\":\"\"}", jacksonErrorWithStatusAndNullMessageAndNullData);
|
||||
|
||||
final String gsonErrorWithStatusAndNullMessageAndNullData = gson.toJson(errorWithStatusAndNullMessageAndNullData);
|
||||
assertEquals("{\"status\":500,\"message\":\"\"}", gsonErrorWithStatusAndNullMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndNullMessage_AndData() throws Exception {
|
||||
PageResult<User> emptyPageResult = PageResult.empty();
|
||||
UnifiedResponse errorWithStatusAndNullMessageAndData = UnifiedResponse.error(500, (String) null, emptyPageResult);
|
||||
assertEquals(500, errorWithStatusAndNullMessageAndData.getStatus());
|
||||
assertEquals("", errorWithStatusAndNullMessageAndData.getMessage());
|
||||
assertEquals(emptyPageResult, errorWithStatusAndNullMessageAndData.getData());
|
||||
final String jacksonErrorWithStatusAndNullMessageAndData = jackson.writeValueAsString(errorWithStatusAndNullMessageAndData);
|
||||
assertEquals("{\"status\":500,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonErrorWithStatusAndNullMessageAndData);
|
||||
final String gsonErrorWithStatusAndNullMessageAndData = gson.toJson(errorWithStatusAndNullMessageAndData);
|
||||
assertEquals("{\"status\":500,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonErrorWithStatusAndNullMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndEmptyMessage() throws Exception {
|
||||
UnifiedResponse errorWithStatusAndEmptyMessage = UnifiedResponse.error(500, "");
|
||||
assertEquals(500, errorWithStatusAndEmptyMessage.getStatus());
|
||||
assertEquals("", errorWithStatusAndEmptyMessage.getMessage());
|
||||
assertNull(errorWithStatusAndEmptyMessage.getData());
|
||||
|
||||
final String jacksonErrorWithStatusAndEmptyMessage = jackson.writeValueAsString(errorWithStatusAndEmptyMessage);
|
||||
assertEquals("{\"status\":500,\"message\":\"\"}", jacksonErrorWithStatusAndEmptyMessage);
|
||||
|
||||
final String gsonErrorWithStatusAndEmptyMessage = gson.toJson(errorWithStatusAndEmptyMessage);
|
||||
assertEquals("{\"status\":500,\"message\":\"\"}", gsonErrorWithStatusAndEmptyMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndEmptyMessage_AndNullData() throws Exception {
|
||||
UnifiedResponse errorWithStatusAndEmptyMessageAndNullData = UnifiedResponse.error(500, "", null);
|
||||
|
||||
assertEquals(500, errorWithStatusAndEmptyMessageAndNullData.getStatus());
|
||||
assertEquals("", errorWithStatusAndEmptyMessageAndNullData.getMessage());
|
||||
assertNull(errorWithStatusAndEmptyMessageAndNullData.getData());
|
||||
|
||||
final String jacksonErrorWithStatusAndEmptyMessageAndNullData = jackson.writeValueAsString(errorWithStatusAndEmptyMessageAndNullData);
|
||||
assertEquals("{\"status\":500,\"message\":\"\"}", jacksonErrorWithStatusAndEmptyMessageAndNullData);
|
||||
|
||||
final String gsonErrorWithStatusAndEmptyMessageAndNullData = gson.toJson(errorWithStatusAndEmptyMessageAndNullData);
|
||||
assertEquals("{\"status\":500,\"message\":\"\"}", gsonErrorWithStatusAndEmptyMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndEmptyMessage_AndData() throws Exception {
|
||||
PageResult<User> emptyPageResult = PageResult.empty();
|
||||
UnifiedResponse errorWithStatusAndEmptyMessageAndData = UnifiedResponse.error(500, "", emptyPageResult);
|
||||
assertEquals(500, errorWithStatusAndEmptyMessageAndData.getStatus());
|
||||
assertEquals("", errorWithStatusAndEmptyMessageAndData.getMessage());
|
||||
assertEquals(emptyPageResult, errorWithStatusAndEmptyMessageAndData.getData());
|
||||
final String jacksonErrorWithStatusAndEmptyMessageAndData = jackson.writeValueAsString(errorWithStatusAndEmptyMessageAndData);
|
||||
assertEquals("{\"status\":500,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonErrorWithStatusAndEmptyMessageAndData);
|
||||
final String gsonErrorWithStatusAndEmptyMessageAndData = gson.toJson(errorWithStatusAndEmptyMessageAndData);
|
||||
assertEquals("{\"status\":500,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonErrorWithStatusAndEmptyMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndThrowable() throws Exception {
|
||||
final IllegalArgumentException e = new IllegalArgumentException("ID cannot be null");
|
||||
final UnifiedResponse errorWithStatusThrowable = UnifiedResponse.error(500, e);
|
||||
assertEquals(500, errorWithStatusThrowable.getStatus());
|
||||
assertEquals("ID cannot be null", errorWithStatusThrowable.getMessage());
|
||||
assertNull(errorWithStatusThrowable.getData());
|
||||
assertEquals("{\"status\":500,\"message\":\"ID cannot be null\"}", jackson.writeValueAsString(errorWithStatusThrowable));
|
||||
assertEquals("{\"status\":500,\"message\":\"ID cannot be null\"}", gson.toJson(errorWithStatusThrowable));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithStatusAndNullThrowable() {
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(500, (Throwable) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_WithNullStatus() {
|
||||
final Object nullStatus = null;
|
||||
final String nullMessage = null;
|
||||
final User user = new User("zhouxy", "zhouxy@gmail.com");
|
||||
|
@ -222,36 +500,172 @@ class UnifiedResponseTests {
|
|||
|
||||
// null message
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, nullMessage));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "nullMessage", null));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "nullMessage", user));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "查询失败", null));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, "查询失败", user));
|
||||
|
||||
// Throwable
|
||||
// TODO
|
||||
BizException bizException = new BizException("业务异常");
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, bizException));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.error(nullStatus, (Throwable) null));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testOf_WithStatusAndMessage() throws Exception {
|
||||
final UnifiedResponse ofWithStatusAndMessage = UnifiedResponse.of(108, "This is a message.");
|
||||
assertEquals(108, ofWithStatusAndMessage.getStatus());
|
||||
assertEquals("This is a message.", ofWithStatusAndMessage.getMessage());
|
||||
assertNull(ofWithStatusAndMessage.getData());
|
||||
|
||||
final String jacksonOfWithStatusAndMessage = jackson.writeValueAsString(ofWithStatusAndMessage);
|
||||
log.info("jacksonOfWithStatusAndMessage: {}", jacksonOfWithStatusAndMessage);
|
||||
assertEquals("{\"status\":108,\"message\":\"This is a message.\"}", jacksonOfWithStatusAndMessage);
|
||||
|
||||
assertEquals("{status: 108, message: \"This is a message.\", data: null}", ofWithStatusAndMessage.toString());
|
||||
|
||||
final String gsonOfWithStatusAndMessage = gson.toJson(ofWithStatusAndMessage);
|
||||
assertEquals("{\"status\":108,\"message\":\"This is a message.\"}", gsonOfWithStatusAndMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_Status_And_NullMessage() throws Exception {
|
||||
// TODO
|
||||
void testOf_WithStatusAndMessage_AndNullData() throws Exception {
|
||||
final UnifiedResponse ofWithStatusAndMessageAndNullData = UnifiedResponse.of(108, "This is a message.", null);
|
||||
assertEquals(108, ofWithStatusAndMessageAndNullData.getStatus());
|
||||
assertEquals("This is a message.", ofWithStatusAndMessageAndNullData.getMessage());
|
||||
assertNull(ofWithStatusAndMessageAndNullData.getData());
|
||||
|
||||
final String jacksonOfWithStatusAndMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndMessageAndNullData);
|
||||
log.info("jacksonOfWithStatusAndMessage: {}", jacksonOfWithStatusAndMessageAndNullData);
|
||||
assertEquals("{\"status\":108,\"message\":\"This is a message.\"}", jacksonOfWithStatusAndMessageAndNullData);
|
||||
|
||||
assertEquals("{status: 108, message: \"This is a message.\", data: null}", ofWithStatusAndMessageAndNullData.toString());
|
||||
|
||||
final String gsonOfWithStatusAndMessageAndNullData = gson.toJson(ofWithStatusAndMessageAndNullData);
|
||||
assertEquals("{\"status\":108,\"message\":\"This is a message.\"}", gsonOfWithStatusAndMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_Message() throws Exception {
|
||||
// TODO
|
||||
void testOf_WithStatusAndMessage_AndData() throws Exception {
|
||||
final PageResult<User> emptyPageResult = PageResult.empty();
|
||||
final UnifiedResponse ofWithStatusAndMessageAndData
|
||||
= UnifiedResponse.of(108, "This is a message.", emptyPageResult);
|
||||
assertEquals("{status: 108, message: \"This is a message.\", data: PageResult [total=0, content=[]]}",
|
||||
ofWithStatusAndMessageAndData.toString());
|
||||
assertEquals(108, ofWithStatusAndMessageAndData.getStatus());
|
||||
assertEquals("This is a message.", ofWithStatusAndMessageAndData.getMessage());
|
||||
assertEquals(emptyPageResult, ofWithStatusAndMessageAndData.getData());
|
||||
final String jacksonOfWithStatusAndMessageAndData = jackson.writeValueAsString(ofWithStatusAndMessageAndData);
|
||||
assertEquals("{\"status\":108,\"message\":\"This is a message.\",\"data\":{\"total\":0,\"content\":[]}}",
|
||||
jacksonOfWithStatusAndMessageAndData);
|
||||
final String gsonOfWithStatusAndMessageAndData = gson.toJson(ofWithStatusAndMessageAndData);
|
||||
assertEquals("{\"status\":108,\"message\":\"This is a message.\",\"data\":{\"total\":0,\"content\":[]}}",
|
||||
gsonOfWithStatusAndMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_NullMessage() throws Exception {
|
||||
// TODO
|
||||
void testOf_WithStatusAndNullMessage() throws Exception {
|
||||
UnifiedResponse ofWithStatusAndNullMessage = UnifiedResponse.of(108, (String) null);
|
||||
assertEquals(108, ofWithStatusAndNullMessage.getStatus());
|
||||
assertEquals("", ofWithStatusAndNullMessage.getMessage());
|
||||
assertNull(ofWithStatusAndNullMessage.getData());
|
||||
|
||||
final String jacksonOfWithStatusAndNullMessage = jackson.writeValueAsString(ofWithStatusAndNullMessage);
|
||||
assertEquals("{\"status\":108,\"message\":\"\"}", jacksonOfWithStatusAndNullMessage);
|
||||
|
||||
final String gsonOfWithStatusAndNullMessage = gson.toJson(ofWithStatusAndNullMessage);
|
||||
assertEquals("{\"status\":108,\"message\":\"\"}", gsonOfWithStatusAndNullMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testError_EmptyMessage() throws Exception {
|
||||
// TODO
|
||||
void testOf_WithStatusAndNullMessage_AndNullData() throws Exception {
|
||||
UnifiedResponse ofWithStatusAndNullMessageAndNullData = UnifiedResponse.of(108, (String) null, null);
|
||||
|
||||
assertEquals(108, ofWithStatusAndNullMessageAndNullData.getStatus());
|
||||
assertEquals("", ofWithStatusAndNullMessageAndNullData.getMessage());
|
||||
assertNull(ofWithStatusAndNullMessageAndNullData.getData());
|
||||
|
||||
final String jacksonOfWithStatusAndNullMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndNullMessageAndNullData);
|
||||
assertEquals("{\"status\":108,\"message\":\"\"}", jacksonOfWithStatusAndNullMessageAndNullData);
|
||||
|
||||
final String gsonOfWithStatusAndNullMessageAndNullData = gson.toJson(ofWithStatusAndNullMessageAndNullData);
|
||||
assertEquals("{\"status\":108,\"message\":\"\"}", gsonOfWithStatusAndNullMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf() {
|
||||
void testOf_WithStatusAndNullMessage_AndData() throws Exception {
|
||||
PageResult<User> emptyPageResult = PageResult.empty();
|
||||
UnifiedResponse ofWithStatusAndNullMessageAndData = UnifiedResponse.of(108, (String) null, emptyPageResult);
|
||||
assertEquals(108, ofWithStatusAndNullMessageAndData.getStatus());
|
||||
assertEquals("", ofWithStatusAndNullMessageAndData.getMessage());
|
||||
assertEquals(emptyPageResult, ofWithStatusAndNullMessageAndData.getData());
|
||||
final String jacksonOfWithStatusAndNullMessageAndData = jackson.writeValueAsString(ofWithStatusAndNullMessageAndData);
|
||||
assertEquals("{\"status\":108,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonOfWithStatusAndNullMessageAndData);
|
||||
final String gsonOfWithStatusAndNullMessageAndData = gson.toJson(ofWithStatusAndNullMessageAndData);
|
||||
assertEquals("{\"status\":108,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonOfWithStatusAndNullMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithStatusAndEmptyMessage() throws Exception {
|
||||
UnifiedResponse ofWithStatusAndEmptyMessage = UnifiedResponse.of(108, "");
|
||||
assertEquals(108, ofWithStatusAndEmptyMessage.getStatus());
|
||||
assertEquals("", ofWithStatusAndEmptyMessage.getMessage());
|
||||
assertNull(ofWithStatusAndEmptyMessage.getData());
|
||||
|
||||
final String jacksonOfWithStatusAndEmptyMessage = jackson.writeValueAsString(ofWithStatusAndEmptyMessage);
|
||||
assertEquals("{\"status\":108,\"message\":\"\"}", jacksonOfWithStatusAndEmptyMessage);
|
||||
|
||||
final String gsonOfWithStatusAndEmptyMessage = gson.toJson(ofWithStatusAndEmptyMessage);
|
||||
assertEquals("{\"status\":108,\"message\":\"\"}", gsonOfWithStatusAndEmptyMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithStatusAndEmptyMessage_AndNullData() throws Exception {
|
||||
UnifiedResponse ofWithStatusAndEmptyMessageAndNullData = UnifiedResponse.of(108, "", null);
|
||||
|
||||
assertEquals(108, ofWithStatusAndEmptyMessageAndNullData.getStatus());
|
||||
assertEquals("", ofWithStatusAndEmptyMessageAndNullData.getMessage());
|
||||
assertNull(ofWithStatusAndEmptyMessageAndNullData.getData());
|
||||
|
||||
final String jacksonOfWithStatusAndEmptyMessageAndNullData = jackson.writeValueAsString(ofWithStatusAndEmptyMessageAndNullData);
|
||||
assertEquals("{\"status\":108,\"message\":\"\"}", jacksonOfWithStatusAndEmptyMessageAndNullData);
|
||||
|
||||
final String gsonOfWithStatusAndEmptyMessageAndNullData = gson.toJson(ofWithStatusAndEmptyMessageAndNullData);
|
||||
assertEquals("{\"status\":108,\"message\":\"\"}", gsonOfWithStatusAndEmptyMessageAndNullData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithStatusAndEmptyMessage_AndData() throws Exception {
|
||||
PageResult<User> emptyPageResult = PageResult.empty();
|
||||
UnifiedResponse ofWithStatusAndEmptyMessageAndData = UnifiedResponse.of(108, "", emptyPageResult);
|
||||
assertEquals(108, ofWithStatusAndEmptyMessageAndData.getStatus());
|
||||
assertEquals("", ofWithStatusAndEmptyMessageAndData.getMessage());
|
||||
assertEquals(emptyPageResult, ofWithStatusAndEmptyMessageAndData.getData());
|
||||
final String jacksonOfWithStatusAndEmptyMessageAndData = jackson.writeValueAsString(ofWithStatusAndEmptyMessageAndData);
|
||||
assertEquals("{\"status\":108,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", jacksonOfWithStatusAndEmptyMessageAndData);
|
||||
final String gsonOfWithStatusAndEmptyMessageAndData = gson.toJson(ofWithStatusAndEmptyMessageAndData);
|
||||
assertEquals("{\"status\":108,\"message\":\"\",\"data\":{\"total\":0,\"content\":[]}}", gsonOfWithStatusAndEmptyMessageAndData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOf_WithNullStatus() {
|
||||
final Object nullStatus = null;
|
||||
final String nullMessage = null;
|
||||
final User user = new User("zhouxy", "zhouxy@gmail.com");
|
||||
|
||||
// message
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败"));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败", null));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败", user));
|
||||
|
||||
// empty message
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, ""));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "", null));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "", user));
|
||||
|
||||
// null message
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, nullMessage));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败", null));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponse.of(nullStatus, "查询失败", user));
|
||||
}
|
||||
|
||||
@Data
|
||||
|
|
|
@ -16,88 +16,217 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.model.dto.test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Statement;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.io.Resources;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PageResult;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingParams;
|
||||
|
||||
@Slf4j
|
||||
public //
|
||||
class PagingAndSortingQueryParamsTests {
|
||||
public class PagingAndSortingQueryParamsTests {
|
||||
|
||||
static SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
@BeforeAll
|
||||
static void setUp() throws Exception {
|
||||
initDatabase();
|
||||
|
||||
String resource = "mybatis-config.xml";
|
||||
InputStream inputStream = Resources.getResourceAsStream(resource);
|
||||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
|
||||
}
|
||||
|
||||
static void initDatabase() throws Exception {
|
||||
JdbcDataSource dataSource = new JdbcDataSource();
|
||||
dataSource.setURL("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE;MODE=MySQL");
|
||||
dataSource.setUser("sa");
|
||||
dataSource.setPassword("");
|
||||
|
||||
List<AccountVO> data = Lists.newArrayList(
|
||||
new AccountVO(1L, "zhouxy01", "zhouxy01@qq.com", 0, 108L, LocalDateTime.of(2020, 1, 1, 13, 15), 0L),
|
||||
new AccountVO(2L, "zhouxy02", "zhouxy02@qq.com", 0, 108L, LocalDateTime.of(2020, 1, 2, 13, 15), 0L),
|
||||
new AccountVO(3L, "zhouxy03", "zhouxy03@qq.com", 0, 108L, LocalDateTime.of(2020, 1, 3, 13, 15), 0L),
|
||||
new AccountVO(4L, "zhouxy04", "zhouxy04@qq.com", 0, 108L, LocalDateTime.of(2020, 1, 4, 13, 15), 0L),
|
||||
new AccountVO(5L, "zhouxy05", "zhouxy05@qq.com", 0, 108L, LocalDateTime.of(2020, 1, 5, 13, 15), 0L),
|
||||
new AccountVO(6L, "zhouxy06", "zhouxy06@qq.com", 0, 108L, LocalDateTime.of(2024, 1, 6, 13, 15), 0L),
|
||||
new AccountVO(7L, "zhouxy07", "zhouxy07@qq.com", 0, 108L, LocalDateTime.of(2024, 1, 7, 13, 15), 0L),
|
||||
new AccountVO(8L, "zhouxy08", "zhouxy08@qq.com", 1, 108L, LocalDateTime.of(2024, 5, 8, 13, 15), 0L),
|
||||
new AccountVO(9L, "zhouxy09", "zhouxy09@qq.com", 1, 108L, LocalDateTime.of(2024, 5, 9, 13, 15), 0L),
|
||||
new AccountVO(10L, "zhouxy10", "zhouxy10@qq.com", 1, 108L, LocalDateTime.of(2024, 5, 10, 13, 15), 0L),
|
||||
new AccountVO(11L, "zhouxy11", "zhouxy11@qq.com", 1, 108L, LocalDateTime.of(2024, 5, 11, 13, 15), 0L),
|
||||
new AccountVO(12L, "zhouxy12", "zhouxy12@qq.com", 1, 108L, LocalDateTime.of(2024, 5, 12, 13, 15), 0L),
|
||||
new AccountVO(13L, "zhouxy13", "zhouxy13@qq.com", 1, 108L, LocalDateTime.of(2024, 5, 13, 13, 15), 0L),
|
||||
new AccountVO(14L, "zhouxy14", "zhouxy14@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 14, 13, 15), 0L),
|
||||
new AccountVO(15L, "zhouxy15", "zhouxy15@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 15, 13, 15), 0L),
|
||||
new AccountVO(16L, "zhouxy16", "zhouxy16@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 16, 13, 15), 0L),
|
||||
new AccountVO(17L, "zhouxy17", "zhouxy17@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 17, 13, 15), 0L),
|
||||
new AccountVO(18L, "zhouxy18", "zhouxy18@qq.com", 1, 108L, LocalDateTime.of(2024, 10, 18, 13, 15), 0L),
|
||||
new AccountVO(19L, "zhouxy19", "zhouxy19@qq.com", 1, 108L, LocalDateTime.of(2024, 11, 19, 13, 15), 0L),
|
||||
new AccountVO(20L, "zhouxy20", "zhouxy20@qq.com", 1, 108L, LocalDateTime.of(2024, 12, 20, 13, 15), 0L)
|
||||
);
|
||||
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
try (Statement statement = conn.createStatement()) {
|
||||
String ddl = "CREATE TABLE sys_account ("
|
||||
+ "\n" + " id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY"
|
||||
+ "\n" + " ,username VARCHAR(255) NOT NULL"
|
||||
+ "\n" + " ,email VARCHAR(255) NOT NULL"
|
||||
+ "\n" + " ,status VARCHAR(2) NOT NULL"
|
||||
+ "\n" + " ,create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP"
|
||||
+ "\n" + " ,created_by BIGINT NOT NULL"
|
||||
+ "\n" + " ,version BIGINT NOT NULL DEFAULT 0"
|
||||
+ "\n" + ")";
|
||||
statement.execute(ddl);
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO sys_account(id, username, email, status, create_time, created_by, version) VALUES"
|
||||
+ "\n" + "(?, ?, ?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement statement = conn.prepareStatement(sql)) {
|
||||
for (AccountVO a : data) {
|
||||
statement.setObject(1, a.getId());
|
||||
statement.setObject(2, a.getUsername());
|
||||
statement.setObject(3, a.getEmail());
|
||||
statement.setObject(4, a.getStatus());
|
||||
statement.setObject(5, a.getCreateTime());
|
||||
statement.setObject(6, a.getCreatedBy());
|
||||
statement.setObject(7, a.getVersion());
|
||||
statement.addBatch();
|
||||
}
|
||||
statement.executeBatch();
|
||||
statement.clearBatch();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static final String JSON_STR = "" +
|
||||
"{\n" +
|
||||
" \"size\": 15,\n" +
|
||||
" \"pageNum\": 3,\n" +
|
||||
" \"size\": 3,\n" +
|
||||
" \"orderBy\": [\"username-asc\"],\n" +
|
||||
" \"createTimeStart\": \"2024-05-06\",\n" +
|
||||
" \"createTimeEnd\": \"2024-07-06\",\n" +
|
||||
" \"updateTimeStart\": \"2024-08-06\",\n" +
|
||||
" \"updateTimeEnd\": \"2024-10-06\",\n" +
|
||||
" \"mobilePhone\": \"13169053215\"\n" +
|
||||
" \"createTimeEnd\": \"2030-07-06\"" +
|
||||
"}";
|
||||
|
||||
static final String WRONG_JSON_STR = "" +
|
||||
"{\n" +
|
||||
" \"pageNum\": 3,\n" +
|
||||
" \"size\": 3,\n" +
|
||||
" \"orderBy\": [\"status-asc\"],\n" +
|
||||
" \"createTimeStart\": \"2024-05-06\",\n" +
|
||||
" \"createTimeEnd\": \"2030-07-06\"" +
|
||||
"}";
|
||||
|
||||
@Test
|
||||
void testJackson() throws Exception {
|
||||
try {
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
om.registerModule(new JavaTimeModule());
|
||||
AccountQueryParams params = om.readValue(JSON_STR, AccountQueryParams.class);
|
||||
log.info(params.toString());
|
||||
ObjectMapper jackson = new ObjectMapper();
|
||||
jackson.registerModule(new JavaTimeModule());
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
AccountQueryParams params = jackson.readValue(JSON_STR, AccountQueryParams.class);
|
||||
PagingParams pagingParams = params.buildPagingParams();
|
||||
log.info("pagingParams: {}", pagingParams);
|
||||
|
||||
AccountQueries accountQueries = session.getMapper(AccountQueries.class);
|
||||
List<AccountVO> list = accountQueries.queryAccountList(params, pagingParams);
|
||||
long count = accountQueries.countAccount(params);
|
||||
PageResult<AccountVO> accountPageResult = PageResult.of(list, count);
|
||||
log.info(jackson.writeValueAsString(accountPageResult));
|
||||
|
||||
assertEquals(Lists.newArrayList(
|
||||
new AccountVO(14L, "zhouxy14", "zhouxy14@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 14, 13, 15), 0L),
|
||||
new AccountVO(15L, "zhouxy15", "zhouxy15@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 15, 13, 15), 0L),
|
||||
new AccountVO(16L, "zhouxy16", "zhouxy16@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 16, 13, 15), 0L)
|
||||
), accountPageResult.getContent());
|
||||
assertEquals(13, accountPageResult.getTotal());
|
||||
} catch (Exception e) {
|
||||
log.error("测试不通过", e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
AccountQueryParams queryParams = jackson.readValue(WRONG_JSON_STR, AccountQueryParams.class);
|
||||
assertThrows(IllegalArgumentException.class, () -> queryParams.buildPagingParams()); // NOSONAR
|
||||
}
|
||||
|
||||
static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
static final TypeAdapter<LocalDate> dateAdapter = new TypeAdapter<LocalDate>() {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate value) throws IOException {
|
||||
out.value(dateFormatter.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
return LocalDate.parse(in.nextString(), dateFormatter);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Test
|
||||
void testGson() throws Exception {
|
||||
try {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(LocalDate.class, dateAdapter)
|
||||
.create();
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(LocalDate.class, new TypeAdapter<LocalDate>() {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate value) throws IOException {
|
||||
out.value(dateFormatter.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
return LocalDate.parse(in.nextString(), dateFormatter);
|
||||
}
|
||||
|
||||
})
|
||||
.create();
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
AccountQueryParams params = gson.fromJson(JSON_STR, AccountQueryParams.class);
|
||||
log.info(params.toString());
|
||||
PagingParams pagingParams = params.buildPagingParams();
|
||||
log.info("pagingParams: {}", pagingParams);
|
||||
AccountQueries accountQueries = session.getMapper(AccountQueries.class);
|
||||
List<AccountVO> list = accountQueries.queryAccountList(params, pagingParams);
|
||||
long count = accountQueries.countAccount(params);
|
||||
PageResult<AccountVO> accountPageResult = PageResult.of(list, count);
|
||||
log.info(gson.toJson(accountPageResult));
|
||||
|
||||
assertEquals(Lists.newArrayList(
|
||||
new AccountVO(14L, "zhouxy14", "zhouxy14@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 14, 13, 15), 0L),
|
||||
new AccountVO(15L, "zhouxy15", "zhouxy15@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 15, 13, 15), 0L),
|
||||
new AccountVO(16L, "zhouxy16", "zhouxy16@qq.com", 1, 108L, LocalDateTime.of(2024, 8, 16, 13, 15), 0L)
|
||||
), accountPageResult.getContent());
|
||||
assertEquals(13, accountPageResult.getTotal());
|
||||
} catch (Exception e) {
|
||||
log.error("测试不通过", e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
AccountQueryParams queryParams = gson.fromJson(WRONG_JSON_STR, AccountQueryParams.class);
|
||||
assertThrows(IllegalArgumentException.class, () -> queryParams.buildPagingParams()); // NOSONAR
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,15 +242,7 @@ class AccountQueryParams extends PagingAndSortingQueryParams {
|
|||
private static final Map<String, String> PROPERTY_COLUMN_MAP = ImmutableMap.<String, String>builder()
|
||||
.put("id", "id")
|
||||
.put("username", "username")
|
||||
.put("email", "email")
|
||||
.put("mobilePhone", "mobile_phone")
|
||||
.put("status", "status")
|
||||
.put("nickname", "nickname")
|
||||
.put("sex", "sex")
|
||||
.put("createdBy", "created_by")
|
||||
.put("createTime", "create_time")
|
||||
.put("updatedBy", "updated_by")
|
||||
.put("updateTime", "update_time")
|
||||
.build();
|
||||
|
||||
public AccountQueryParams() {
|
||||
|
@ -131,17 +252,10 @@ class AccountQueryParams extends PagingAndSortingQueryParams {
|
|||
private @Getter @Setter Long id;
|
||||
private @Getter @Setter String username;
|
||||
private @Getter @Setter String email;
|
||||
private @Getter @Setter String mobilePhone;
|
||||
private @Getter @Setter Integer status;
|
||||
private @Getter @Setter String nickname;
|
||||
private @Getter @Setter Integer sex;
|
||||
private @Getter @Setter Long createdBy;
|
||||
private @Getter @Setter LocalDate createTimeStart;
|
||||
private @Setter LocalDate createTimeEnd;
|
||||
private @Getter @Setter Long updatedBy;
|
||||
private @Getter @Setter LocalDate updateTimeStart;
|
||||
private @Setter LocalDate updateTimeEnd;
|
||||
private @Getter @Setter Long roleId;
|
||||
|
||||
public LocalDate getCreateTimeEnd() {
|
||||
if (this.createTimeEnd == null) {
|
||||
|
@ -149,11 +263,26 @@ class AccountQueryParams extends PagingAndSortingQueryParams {
|
|||
}
|
||||
return this.createTimeEnd.plusDays(1);
|
||||
}
|
||||
|
||||
public LocalDate getUpdateTimeEnd() {
|
||||
if (this.updateTimeEnd == null) {
|
||||
return null;
|
||||
}
|
||||
return this.updateTimeEnd.plusDays(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
class AccountVO {
|
||||
private Long id;
|
||||
private String username;
|
||||
private String email;
|
||||
private Integer status;
|
||||
private Long createdBy;
|
||||
private LocalDateTime createTime;
|
||||
private Long version;
|
||||
}
|
||||
|
||||
interface AccountQueries {
|
||||
|
||||
List<AccountVO> queryAccountList(@Param("query") AccountQueryParams query,
|
||||
@Param("page") PagingParams page);
|
||||
|
||||
long countAccount(@Param("query") AccountQueryParams query);
|
||||
}
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.sql;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.sql.MyBatisSql.IN;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
class MyBatisSqlBuilderTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(MyBatisSqlBuilderTests.class);
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
// List<String> ids = Arrays.asList("2333", "4501477");
|
||||
MyBatisSql sql = MyBatisSql.newScriptSql()
|
||||
.SELECT("*")
|
||||
.FROM("test_table")
|
||||
.WHERE(IN("id", "ids"));
|
||||
log.info("sql: {}", sql);
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.commons.time;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
|
||||
|
@ -37,7 +38,7 @@ class QuarterTests {
|
|||
assertEquals(1, quarter.getValue());
|
||||
assertEquals("Q1", quarter.name());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
assertThrows(DateTimeException.class, () -> {
|
||||
Quarter.of(0);
|
||||
});
|
||||
|
||||
|
@ -84,7 +85,7 @@ class QuarterTests {
|
|||
assertEquals(2, quarter.getValue());
|
||||
assertEquals("Q2", quarter.name());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
assertThrows(DateTimeException.class, () -> {
|
||||
Quarter.of(5);
|
||||
});
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -925,9 +925,10 @@ public class AssertToolsTests {
|
|||
|
||||
// #region - Condition
|
||||
|
||||
static final class MyException extends RuntimeException {}
|
||||
|
||||
@Test
|
||||
void testCheckCondition() {
|
||||
class MyException extends RuntimeException {}
|
||||
|
||||
AssertTools.checkCondition(true, MyException::new);
|
||||
|
||||
|
|
|
@ -27,8 +27,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@Slf4j
|
||||
public class BigDecimalsTests {
|
||||
|
||||
// TODO 【优化】 检查测试用例
|
||||
|
||||
@Test
|
||||
void equalsValue_NullValues_ReturnsTrue() {
|
||||
assertTrue(BigDecimals.equalsValue(null, null));
|
||||
|
@ -176,7 +174,8 @@ public class BigDecimalsTests {
|
|||
BigDecimal bd1 = new BigDecimal("10");
|
||||
BigDecimal bd2 = new BigDecimal("20");
|
||||
BigDecimal bd3 = new BigDecimal("30");
|
||||
assertEquals(new BigDecimal("60"), BigDecimals.sum(bd1, bd2, bd3));
|
||||
BigDecimal bd4 = null;
|
||||
assertEquals(new BigDecimal("60"), BigDecimals.sum(bd1, bd2, bd3, bd4));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -192,6 +191,8 @@ public class BigDecimalsTests {
|
|||
|
||||
@Test
|
||||
void of_BlankString_ReturnsZero() {
|
||||
assertEquals(BigDecimal.ZERO, BigDecimals.of(null));
|
||||
assertEquals(BigDecimal.ZERO, BigDecimals.of(""));
|
||||
assertEquals(BigDecimal.ZERO, BigDecimals.of(" "));
|
||||
}
|
||||
|
||||
|
|
|
@ -17,96 +17,480 @@
|
|||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
import java.time.YearMonth;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||
import xyz.zhouxy.plusone.commons.time.YearQuarter;
|
||||
|
||||
class DateTimeToolsTests {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DateTimeToolsTests.class);
|
||||
|
||||
@Test
|
||||
void testToJoda() {
|
||||
LocalDateTime dt = LocalDateTime.of(2008, 8, 8, 20, 18, 59, 108000000);
|
||||
log.info("src: {}", dt);
|
||||
org.joda.time.LocalDateTime dt2 = DateTimeTools.toJodaLocalDateTime(dt);
|
||||
log.info("result: {}", dt2);
|
||||
org.joda.time.format.DateTimeFormatter f = org.joda.time.format.DateTimeFormat
|
||||
.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
// Java
|
||||
static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2024, 12, 29, 12, 58, 30, 333000000);
|
||||
static final LocalDate LOCAL_DATE = LOCAL_DATE_TIME.toLocalDate();
|
||||
static final LocalTime LOCAL_TIME = LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
assertEquals("2008-08-08 20:18:59.108", f.print(dt2));
|
||||
// Java - 2024-12-29 12:58:30.333333333 SystemDefaultZone
|
||||
static final ZoneId SYS_ZONE_ID = ZoneId.systemDefault();
|
||||
static final ZonedDateTime ZONED_DATE_TIME_WITH_SYS_ZONE = LOCAL_DATE_TIME.atZone(SYS_ZONE_ID);
|
||||
static final Instant INSTANT_WITH_SYS_ZONE = ZONED_DATE_TIME_WITH_SYS_ZONE.toInstant();
|
||||
static final long INSTANT_MILLIS = INSTANT_WITH_SYS_ZONE.toEpochMilli();
|
||||
|
||||
static final TimeZone SYS_TIME_ZONE = TimeZone.getDefault();
|
||||
static final Date SYS_DATE = Date.from(INSTANT_WITH_SYS_ZONE);
|
||||
static final Calendar SYS_CALENDAR = Calendar.getInstance(SYS_TIME_ZONE);
|
||||
static {
|
||||
SYS_CALENDAR.setTime(SYS_DATE);
|
||||
}
|
||||
|
||||
// Java - 2024-12-29 12:58:30.333333333 GMT+04:00
|
||||
static final ZoneId ZONE_ID = ZoneId.of("GMT+04:00");
|
||||
static final ZonedDateTime ZONED_DATE_TIME = LOCAL_DATE_TIME.atZone(ZONE_ID);
|
||||
static final Instant INSTANT = ZONED_DATE_TIME.toInstant();
|
||||
static final long MILLIS = INSTANT.toEpochMilli();
|
||||
|
||||
static final TimeZone TIME_ZONE = TimeZone.getTimeZone(ZONE_ID);
|
||||
static final Date DATE = Date.from(INSTANT);
|
||||
static final Calendar CALENDAR = Calendar.getInstance(TIME_ZONE);
|
||||
static {
|
||||
CALENDAR.setTime(DATE);
|
||||
}
|
||||
|
||||
// Joda
|
||||
static final org.joda.time.LocalDateTime JODA_LOCAL_DATE_TIME
|
||||
= new org.joda.time.LocalDateTime(2024, 12, 29, 12, 58, 30, 333);
|
||||
static final org.joda.time.LocalDate JODA_LOCAL_DATE = JODA_LOCAL_DATE_TIME.toLocalDate();
|
||||
static final org.joda.time.LocalTime JODA_LOCAL_TIME = JODA_LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
// Joda - 2024-12-29 12:58:30.333 SystemDefaultZone
|
||||
static final org.joda.time.DateTimeZone JODA_SYS_ZONE = org.joda.time.DateTimeZone.getDefault();
|
||||
static final org.joda.time.DateTime JODA_DATE_TIME_WITH_SYS_ZONE = JODA_LOCAL_DATE_TIME.toDateTime(JODA_SYS_ZONE);
|
||||
static final org.joda.time.Instant JODA_INSTANT_WITH_SYS_ZONE = JODA_DATE_TIME_WITH_SYS_ZONE.toInstant();
|
||||
static final long JODA_INSTANT_MILLIS = JODA_INSTANT_WITH_SYS_ZONE.getMillis();
|
||||
|
||||
// Joda - 2024-12-29 12:58:30.333 GMT+04:00
|
||||
static final org.joda.time.DateTimeZone JODA_ZONE = org.joda.time.DateTimeZone.forID("GMT+04:00");
|
||||
static final org.joda.time.DateTime JODA_DATE_TIME = JODA_LOCAL_DATE_TIME.toDateTime(JODA_ZONE);
|
||||
static final org.joda.time.Instant JODA_INSTANT = JODA_DATE_TIME.toInstant();
|
||||
static final long JODA_MILLIS = JODA_INSTANT.getMillis();
|
||||
|
||||
// ================================
|
||||
// #region - toDate
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toDate_timeMillis() {
|
||||
assertNotEquals(SYS_DATE, DATE);
|
||||
log.info("SYS_DATE: {}, DATE: {}", SYS_DATE, DATE);
|
||||
assertEquals(SYS_DATE, JODA_DATE_TIME_WITH_SYS_ZONE.toDate());
|
||||
assertEquals(SYS_DATE, DateTimeTools.toDate(INSTANT_MILLIS));
|
||||
assertEquals(DATE, JODA_DATE_TIME.toDate());
|
||||
assertEquals(DATE, DateTimeTools.toDate(MILLIS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToInstant() {
|
||||
ZonedDateTime dt = ZonedDateTime.of(2008, 1, 8, 10, 23, 50, 108000000, ZoneId.systemDefault());
|
||||
Instant instant = DateTimeTools.toInstant(dt.toInstant().toEpochMilli());
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-M-d HH:mm:ss.SSS");
|
||||
String str = formatter.format(instant.atZone(ZoneId.systemDefault()));
|
||||
log.info(str);
|
||||
assertEquals("08-1-8 10:23:50.108", str);
|
||||
void toDate_calendar() {
|
||||
assertEquals(SYS_DATE, DateTimeTools.toDate(SYS_CALENDAR));
|
||||
assertEquals(DATE, DateTimeTools.toDate(CALENDAR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToJodaDateTime() {
|
||||
ZonedDateTime dt = ZonedDateTime.of(2008, 1, 8, 10, 23, 50, 108000000, ZoneId.systemDefault());
|
||||
Instant instant = DateTimeTools.toInstant(dt.toInstant().toEpochMilli());
|
||||
|
||||
org.joda.time.format.DateTimeFormatter f = org.joda.time.format.DateTimeFormat
|
||||
.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(instant, ZoneId.of("+08:00"));
|
||||
log.info("jodaDateTime: {}", jodaDateTime);
|
||||
assertEquals("2008-01-08 10:23:50.108", f.print(jodaDateTime));
|
||||
|
||||
jodaDateTime = DateTimeTools.toJodaDateTime(instant, ZoneId.of("+02:00"));
|
||||
log.info("jodaDateTime: {}", jodaDateTime);
|
||||
assertEquals("2008-01-08 04:23:50.108", f.print(jodaDateTime));
|
||||
void toDate_instant() {
|
||||
assertEquals(SYS_DATE, DateTimeTools.toDate(INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(DATE, DateTimeTools.toDate(INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
java.time.Instant now = java.time.Instant.now();
|
||||
org.joda.time.DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York"));
|
||||
org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat
|
||||
.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
log.info(formatter.print(jodaDateTime));
|
||||
log.info(jodaDateTime.getZone().toString());
|
||||
log.info(jodaDateTime.toString());
|
||||
log.info("==========================================");
|
||||
org.joda.time.Instant instant = new org.joda.time.Instant(System.currentTimeMillis() - 500000);
|
||||
log.info(instant.toString());
|
||||
log.info(DateTimeTools.toJavaInstant(instant).toString());
|
||||
log.info(DateTimeTools.toZonedDateTime(instant, org.joda.time.DateTimeZone.forID("America/New_York"))
|
||||
.toString());
|
||||
void toDate_ZoneDateTime() {
|
||||
assertEquals(SYS_DATE, DateTimeTools.toDate(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(DATE, DateTimeTools.toDate(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToJodaInstant() {
|
||||
java.time.Instant javaInstant = java.time.Instant.now();
|
||||
log.info("javaInstant: {}", javaInstant);
|
||||
|
||||
org.joda.time.Instant jodaInstant = DateTimeTools.toJodaInstant(javaInstant);
|
||||
log.info("jodaInstant: {}", jodaInstant);
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
DateTimeFormatter formatter2 = formatter.withZone(ZoneId.systemDefault());
|
||||
log.info("{}", formatter);
|
||||
log.info("{}", formatter2);
|
||||
void toDate_LocalDateTimeAndZoneId() {
|
||||
assertEquals(SYS_DATE, DateTimeTools.toDate(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(DATE, DateTimeTools.toDate(LOCAL_DATE_TIME, ZONE_ID));
|
||||
assertEquals(SYS_DATE, DateTimeTools.toDate(LOCAL_DATE, LOCAL_TIME, SYS_ZONE_ID));
|
||||
assertEquals(DATE, DateTimeTools.toDate(LOCAL_DATE, LOCAL_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toDate
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toInstant_timeMillis() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toInstant(INSTANT_MILLIS));
|
||||
assertEquals(INSTANT, DateTimeTools.toInstant(MILLIS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toInstant_Date() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toInstant(SYS_DATE));
|
||||
assertEquals(INSTANT, DateTimeTools.toInstant(DATE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toInstant_Calendar() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toInstant(SYS_CALENDAR));
|
||||
assertEquals(INSTANT, DateTimeTools.toInstant(CALENDAR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toInstant_ZonedDateTime() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toInstant(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toInstant(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toInstant_LocalDateTimeAndZone() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toInstant(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(INSTANT, DateTimeTools.toInstant(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_TimeMillisAndZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(INSTANT_MILLIS, SYS_ZONE_ID));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(MILLIS, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_DateAndZoneId() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(SYS_DATE, SYS_ZONE_ID));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(DATE, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_DateAndTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(SYS_DATE, SYS_TIME_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(DATE, TIME_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_Calendar() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(SYS_CALENDAR));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(CALENDAR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_CalendarAndZoneId() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(SYS_CALENDAR, SYS_ZONE_ID));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(CALENDAR, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_CalendarAndTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(SYS_CALENDAR, SYS_TIME_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(CALENDAR, TIME_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_LocalDateTimeAndZoneId() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toLocalDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toLocalDateTime_TimeMillisAndZoneId() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(INSTANT_MILLIS, SYS_ZONE_ID));
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(MILLIS, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLocalDateTime_DateAndZoneId() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(SYS_DATE, SYS_ZONE_ID));
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(DATE, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLocalDateTime_DateAndTimeZone() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(SYS_DATE, SYS_TIME_ZONE));
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(DATE, TIME_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLocalDateTime_CalendarAndZoneId() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(SYS_CALENDAR, SYS_ZONE_ID));
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(CALENDAR, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLocalDateTime_CalendarAndTimeZone() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(SYS_CALENDAR, SYS_TIME_ZONE));
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(CALENDAR, TIME_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLocalDateTime_ZonedDateTimeAndZoneId() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(ZONED_DATE_TIME_WITH_SYS_ZONE, SYS_ZONE_ID));
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toLocalDateTime(ZONED_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toLocalDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaInstant_JavaInstant() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_ZonedDateTime() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaInstant() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaDateTime() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJavaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_ZonedDateTime() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_InstantAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(INSTANT_WITH_SYS_ZONE, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(INSTANT, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaDateTime() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaInstantAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_INSTANT_WITH_SYS_ZONE, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_INSTANT, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaLocalDateTime_JavaLocalDateTime() {
|
||||
assertEquals(JODA_LOCAL_DATE_TIME, DateTimeTools.toJodaLocalDateTime(LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaLocalDateTime_JodaLocalDateTime() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toJavaLocalDateTime(JODA_LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZondId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void convertJavaZoneIdAndJodaDateTimeZone() {
|
||||
assertEquals(SYS_ZONE_ID, DateTimeTools.toJavaZone(JODA_SYS_ZONE));
|
||||
assertEquals(ZONE_ID, DateTimeTools.toJavaZone(JODA_ZONE));
|
||||
assertEquals(JODA_SYS_ZONE, DateTimeTools.toJodaZone(SYS_ZONE_ID));
|
||||
assertEquals(JODA_ZONE, DateTimeTools.toJodaZone(ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - ZondId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - YearQuarter & Quarter
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void getQuarter() {
|
||||
YearQuarter expectedYearQuarter = YearQuarter.of(2024, 4);
|
||||
assertEquals(expectedYearQuarter, DateTimeTools.getQuarter(SYS_DATE));
|
||||
assertEquals(expectedYearQuarter, DateTimeTools.getQuarter(SYS_CALENDAR));
|
||||
assertEquals(Quarter.Q4, DateTimeTools.getQuarter(Month.DECEMBER));
|
||||
assertEquals(expectedYearQuarter, DateTimeTools.getQuarter(2024, Month.DECEMBER));
|
||||
assertEquals(expectedYearQuarter, DateTimeTools.getQuarter(YearMonth.of(2024, Month.DECEMBER)));
|
||||
assertEquals(expectedYearQuarter, DateTimeTools.getQuarter(LOCAL_DATE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - YearQuarter & Quarter
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void startDateOfYear() {
|
||||
assertEquals(LocalDate.of(2008, 1, 1), DateTimeTools.startDateOfYear(2008));
|
||||
assertEquals(LocalDate.of(2008, 12, 31), DateTimeTools.endDateOfYear(2008));
|
||||
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0),
|
||||
DateTimeTools.startOfNextDate(LOCAL_DATE));
|
||||
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0).atZone(SYS_ZONE_ID),
|
||||
DateTimeTools.startOfNextDate(LOCAL_DATE, SYS_ZONE_ID));
|
||||
assertEquals(LocalDateTime.of(2024, 12, 30, 0, 0, 0).atZone(ZONE_ID),
|
||||
DateTimeTools.startOfNextDate(LOCAL_DATE, ZONE_ID));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE);
|
||||
assertEquals(LOCAL_DATE.atStartOfDay(), localDateTimeRange.lowerEndpoint());
|
||||
assertTrue(localDateTimeRange.contains(LOCAL_DATE.atStartOfDay()));
|
||||
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
||||
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
||||
assertFalse(localDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay()));
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE, SYS_ZONE_ID);
|
||||
assertEquals(LOCAL_DATE.atStartOfDay().atZone(SYS_ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertTrue(zonedDateTimeRange.contains(LOCAL_DATE.atStartOfDay().atZone(SYS_ZONE_ID)));
|
||||
assertEquals(ZonedDateTime.of(LocalDate.of(2024, 12, 30).atStartOfDay(), SYS_ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
assertEquals(ZonedDateTime.of(LocalDate.of(2024, 12, 30).atStartOfDay(), SYS_ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
assertFalse(zonedDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay().atZone(SYS_ZONE_ID)));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - others
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toString
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void testToString() {
|
||||
assertEquals("04", DateTimeTools.toMonthString(Month.APRIL));
|
||||
assertEquals("04", DateTimeTools.toMonthString(Month.APRIL, true));
|
||||
assertEquals("4", DateTimeTools.toMonthString(Month.APRIL, false));
|
||||
assertEquals("2024", DateTimeTools.toYearString(2024));
|
||||
assertEquals("999999999", DateTimeTools.toYearString(Year.MAX_VALUE));
|
||||
assertEquals("-999999999", DateTimeTools.toYearString(Year.MIN_VALUE));
|
||||
assertThrows(DateTimeException.class, () -> DateTimeTools.toYearString(Year.MIN_VALUE - 1));
|
||||
assertThrows(DateTimeException.class, () -> DateTimeTools.toYearString(Year.MAX_VALUE + 1));
|
||||
|
||||
assertEquals("01", DateTimeTools.toMonthStringMM(1));
|
||||
assertEquals("02", DateTimeTools.toMonthStringMM(2));
|
||||
assertEquals("3", DateTimeTools.toMonthStringM(3));
|
||||
assertEquals("04", DateTimeTools.toMonthStringMM(Month.APRIL));
|
||||
assertEquals("05", DateTimeTools.toMonthStringMM(Month.MAY));
|
||||
assertEquals("6", DateTimeTools.toMonthStringM(Month.JUNE));
|
||||
|
||||
assertThrows(DateTimeException.class, () -> DateTimeTools.toMonthStringM(0));
|
||||
assertThrows(DateTimeException.class, () -> DateTimeTools.toMonthStringMM(0));
|
||||
assertThrows(DateTimeException.class, () -> DateTimeTools.toMonthStringM(13));
|
||||
assertThrows(DateTimeException.class, () -> DateTimeTools.toMonthStringMM(13));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toString
|
||||
// ================================
|
||||
}
|
||||
|
|
|
@ -1,3 +1,19 @@
|
|||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import cn.hutool.core.collection.ConcurrentHashSet;
|
||||
|
||||
public class IdGeneratorTests {
|
||||
|
||||
final ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10,
|
||||
0L, TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue<Runnable>());
|
||||
|
||||
@Test
|
||||
void testSnowflakeIdGenerator() { // NOSONAR
|
||||
final SnowflakeIdGenerator snowflake = new SnowflakeIdGenerator(0, 0);
|
||||
final Set<Long> ids = new ConcurrentHashSet<>();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
executor.execute(() -> {
|
||||
for (int j = 0; j < 50000; j++) {
|
||||
if (false == ids.add(snowflake.nextId())) {
|
||||
throw new RuntimeException("重复ID!");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = { 0L, 1L, 108L, 300L })
|
||||
void testIdWorker(long workerId) { // NOSONAR
|
||||
// 如果使用 new IdWorker(0L) 创建,会和下面的 IdGenerator#nextSnowflakeId 使用相同 workerId 的不同 IdWorker 实例,造成 ID 重复
|
||||
final IdWorker idWorker = IdGenerator.getSnowflakeIdGenerator(workerId);
|
||||
|
||||
final Set<Long> ids = new ConcurrentHashSet<>();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
executor.execute(() -> {
|
||||
for (int j = 0; j < 50000; j++) {
|
||||
if (false == ids.add(idWorker.nextId())) {
|
||||
throw new RuntimeException("重复ID!");
|
||||
}
|
||||
}
|
||||
});
|
||||
executor.execute(() -> {
|
||||
for (int j = 0; j < 50000; j++) {
|
||||
if (false == ids.add(IdGenerator.nextSnowflakeId(workerId))) {
|
||||
throw new RuntimeException("重复ID!");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToSimpleString() {
|
||||
UUID id = UUID.randomUUID();
|
||||
assertEquals(id.toString().replaceAll("-", ""),
|
||||
IdGenerator.toSimpleString(id));
|
||||
}
|
||||
|
||||
}
|
|
@ -24,8 +24,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
public
|
||||
class NumbersTests {
|
||||
|
||||
// TODO 【优化】 检查测试用例
|
||||
|
||||
@Test
|
||||
public void sum_ShortArray_ReturnsCorrectSum() {
|
||||
short[] numbers = {1, 2, 3, 4};
|
||||
|
|
|
@ -19,6 +19,7 @@ package xyz.zhouxy.plusone.commons.util;
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Optional;
|
||||
|
@ -28,11 +29,12 @@ import java.util.OptionalLong;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* {@link OptionalTools} 单元测试
|
||||
*/
|
||||
public
|
||||
class OptionalToolsTests {
|
||||
|
||||
// TODO 【优化】 检查测试用例
|
||||
|
||||
@Test
|
||||
void optionalOf_NullInteger_ReturnsEmptyOptionalInt() {
|
||||
OptionalInt result = OptionalTools.optionalOf((Integer) null);
|
||||
|
@ -112,8 +114,14 @@ class OptionalToolsTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
void orElseNull_NullOptional_ReturnsNull() {
|
||||
Object result = OptionalTools.orElseNull(Optional.ofNullable(null));
|
||||
void orElseNull_NullOptional_ThrowsNullPointerException() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> OptionalTools.orElseNull(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void orElseNull_EmptyOptional_ReturnsNull() {
|
||||
Object result = OptionalTools.orElseNull(Optional.empty());
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
|
@ -124,7 +132,13 @@ class OptionalToolsTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
void toInteger_NullOptionalInt_ReturnsNull() {
|
||||
void toInteger_NullOptionalInt_ThrowsNullPointerException() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> OptionalTools.toInteger(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toInteger_EmptyOptionalInt_ReturnsNull() {
|
||||
Integer result = OptionalTools.toInteger(OptionalInt.empty());
|
||||
assertNull(result);
|
||||
}
|
||||
|
@ -136,7 +150,13 @@ class OptionalToolsTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
void toLong_NullOptionalLong_ReturnsNull() {
|
||||
void toLong_NullOptionalLong_ThrowsNullPointerException() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> OptionalTools.toLong(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLong_EmptyOptionalLong_ReturnsNull() {
|
||||
Long result = OptionalTools.toLong(OptionalLong.empty());
|
||||
assertNull(result);
|
||||
}
|
||||
|
@ -148,7 +168,13 @@ class OptionalToolsTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
void toDouble_NullOptionalDouble_ReturnsNull() {
|
||||
void toDouble_NullOptionalDouble_ThrowsNullPointerException() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> OptionalTools.toDouble(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDouble_EmptyOptionalDouble_ReturnsNull() {
|
||||
Double result = OptionalTools.toDouble(OptionalDouble.empty());
|
||||
assertNull(result);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@SuppressWarnings("null")
|
||||
public class RandomToolsTests {
|
||||
|
||||
private static Random random;
|
||||
private static SecureRandom secureRandom;
|
||||
private static char[] sourceCharactersArray;
|
||||
private static String sourceCharactersString;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
random = new Random();
|
||||
secureRandom = new SecureRandom();
|
||||
sourceCharactersArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
|
||||
sourceCharactersString = "abcdefghijklmnopqrstuvwxyz";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomStr_NullRandom_ThrowsException() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> RandomTools.randomStr(null, sourceCharactersArray, 5));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> RandomTools.randomStr(null, sourceCharactersString, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomStr_NullSourceCharacters_ThrowsException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> RandomTools.randomStr(random, (char[]) null, 5));
|
||||
assertThrows(IllegalArgumentException.class, () -> RandomTools.randomStr(random, (String) null, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomStr_NegativeLength_ThrowsException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> RandomTools.randomStr(random, sourceCharactersArray, -1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomStr_ZeroLength_ReturnsEmptyString() {
|
||||
assertAll(
|
||||
() -> assertEquals("", RandomTools.randomStr(random, sourceCharactersArray, 0)),
|
||||
() -> assertEquals("", RandomTools.randomStr(random, sourceCharactersString, 0)),
|
||||
() -> assertEquals("", RandomTools.randomStr(secureRandom, sourceCharactersArray, 0)),
|
||||
() -> assertEquals("", RandomTools.randomStr(secureRandom, sourceCharactersString, 0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomStr_PositiveLength_ReturnsRandomString() {
|
||||
assertAll(
|
||||
() -> assertEquals(5, RandomTools.randomStr(random, sourceCharactersArray, 5).length()),
|
||||
() -> assertEquals(5, RandomTools.randomStr(random, sourceCharactersString, 5).length()),
|
||||
() -> assertEquals(5, RandomTools.randomStr(secureRandom, sourceCharactersArray, 5).length()),
|
||||
() -> assertEquals(5, RandomTools.randomStr(secureRandom, sourceCharactersString, 5).length()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomStr_ReturnsRandomString() {
|
||||
String result = RandomTools.randomStr(sourceCharactersArray, 5);
|
||||
assertEquals(5, result.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomStr_StringSourceCharacters_ReturnsRandomString() {
|
||||
String result = RandomTools.randomStr(sourceCharactersString, 5);
|
||||
assertEquals(5, result.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secureRandomStr_ReturnsRandomString() {
|
||||
String result = RandomTools.secureRandomStr(sourceCharactersArray, 5);
|
||||
assertEquals(5, result.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secureRandomStr_StringSourceCharacters_ReturnsRandomString() {
|
||||
String result = RandomTools.secureRandomStr(sourceCharactersString, 5);
|
||||
assertEquals(5, result.length());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public
|
||||
class RegexToolsTests {
|
||||
|
||||
@Test
|
||||
void getPattern_CachePatternTrue_ReturnsCachedPattern() {
|
||||
String pattern = "abc";
|
||||
Pattern cachedPattern = RegexTools.getPattern(pattern, true);
|
||||
Pattern patternFromCache = RegexTools.getPattern(pattern, true);
|
||||
assertSame(cachedPattern, patternFromCache, "Pattern should be cached");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPattern_CachePatternFalse_ReturnsNewPattern() {
|
||||
String pattern = "getPattern_CachePatternFalse_ReturnsNewPattern";
|
||||
Pattern pattern1 = RegexTools.getPattern(pattern, false);
|
||||
Pattern pattern2 = RegexTools.getPattern(pattern, false);
|
||||
assertNotSame(pattern1, pattern2, "Pattern should not be cached");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPattern_NullPattern_ThrowsException() {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
RegexTools.getPattern(null, true);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPatterns_CachePatternTrue_ReturnsCachedPatterns() {
|
||||
String[] patterns = {"abc", "def"};
|
||||
Pattern[] cachedPatterns = RegexTools.getPatterns(patterns, true);
|
||||
Pattern[] patternsFromCache = RegexTools.getPatterns(patterns, true);
|
||||
assertSame(cachedPatterns[0], patternsFromCache[0]);
|
||||
assertSame(cachedPatterns[1], patternsFromCache[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPatterns_CachePatternFalse_ReturnsNewPatterns() {
|
||||
String[] patterns = {"getPatterns_CachePatternFalse_ReturnsNewPatterns1", "getPatterns_CachePatternFalse_ReturnsNewPatterns2"};
|
||||
Pattern[] patterns1 = RegexTools.getPatterns(patterns, false);
|
||||
Pattern[] patterns2 = RegexTools.getPatterns(patterns, false);
|
||||
assertNotSame(patterns1[0], patterns2[0]);
|
||||
assertNotSame(patterns1[1], patterns2[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPatterns_NullPatterns_ThrowsException() {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
RegexTools.getPatterns(null, true);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_InputMatchesPattern_ReturnsTrue() {
|
||||
String pattern = "abc";
|
||||
Pattern compiledPattern = Pattern.compile(pattern);
|
||||
assertTrue(RegexTools.matches("abc", compiledPattern), "Input should match pattern");
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_InputDoesNotMatchPattern_ReturnsFalse() {
|
||||
String pattern = "abc";
|
||||
Pattern compiledPattern = Pattern.compile(pattern);
|
||||
assertFalse(RegexTools.matches("abcd", compiledPattern), "Input should not match pattern");
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_NullInput_ReturnsFalse() {
|
||||
String pattern = "abc";
|
||||
Pattern compiledPattern = Pattern.compile(pattern);
|
||||
assertFalse(RegexTools.matches(null, compiledPattern), "Null input should return false");
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesOne_InputMatchesOnePattern_ReturnsTrue() {
|
||||
String[] patterns = {"abc", "def"};
|
||||
Pattern[] compiledPatterns = new Pattern[patterns.length];
|
||||
for (int i = 0; i < patterns.length; i++) {
|
||||
compiledPatterns[i] = Pattern.compile(patterns[i]);
|
||||
}
|
||||
assertTrue(RegexTools.matchesOne("abc", compiledPatterns), "Input should match one pattern");
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesOne_InputDoesNotMatchAnyPattern_ReturnsFalse() {
|
||||
String[] patterns = {"abc", "def"};
|
||||
Pattern[] compiledPatterns = new Pattern[patterns.length];
|
||||
for (int i = 0; i < patterns.length; i++) {
|
||||
compiledPatterns[i] = Pattern.compile(patterns[i]);
|
||||
}
|
||||
assertFalse(RegexTools.matchesOne("xyz", compiledPatterns), "Input should not match any pattern");
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesAll_InputMatchesAllPatterns_ReturnsTrue() {
|
||||
String[] patterns = {"abc", "abc"};
|
||||
Pattern[] compiledPatterns = new Pattern[patterns.length];
|
||||
for (int i = 0; i < patterns.length; i++) {
|
||||
compiledPatterns[i] = Pattern.compile(patterns[i]);
|
||||
}
|
||||
assertTrue(RegexTools.matchesAll("abc", compiledPatterns), "Input should match all patterns");
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesAll_InputDoesNotMatchAllPatterns_ReturnsFalse() {
|
||||
String[] patterns = {"abc", "def"};
|
||||
Pattern[] compiledPatterns = new Pattern[patterns.length];
|
||||
for (int i = 0; i < patterns.length; i++) {
|
||||
compiledPatterns[i] = Pattern.compile(patterns[i]);
|
||||
}
|
||||
assertFalse(RegexTools.matchesAll("abc", compiledPatterns), "Input should not match all patterns");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMatcher_ValidInputAndPattern_ReturnsMatcher() {
|
||||
String pattern = "abc";
|
||||
Pattern compiledPattern = Pattern.compile(pattern);
|
||||
Matcher matcher = RegexTools.getMatcher("abc", compiledPattern);
|
||||
assertNotNull(matcher, "Matcher should not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMatcher_NullInput_ThrowsException() {
|
||||
String pattern = "abc";
|
||||
Pattern compiledPattern = Pattern.compile(pattern);
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
RegexTools.getMatcher(null, compiledPattern);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMatcher_NullPattern_ThrowsException() {
|
||||
final Pattern pattern = null;
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
RegexTools.getMatcher("abc", pattern);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"https://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
<configuration>
|
||||
<environments default="development">
|
||||
<environment id="development">
|
||||
<transactionManager type="JDBC"/>
|
||||
<dataSource type="POOLED">
|
||||
<property name="driver" value="org.h2.Driver"/>
|
||||
<property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE;MODE=MySQL"/>
|
||||
<property name="username" value="sa"/>
|
||||
<property name="password" value=""/>
|
||||
</dataSource>
|
||||
</environment>
|
||||
</environments>
|
||||
<mappers>
|
||||
<mapper resource="xyz/zhouxy/plusone/commons/model/dto/test/AccountQueries/AccountQueries.xml"/>
|
||||
</mappers>
|
||||
</configuration>
|
|
@ -0,0 +1,77 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="xyz.zhouxy.plusone.commons.model.dto.test.AccountQueries">
|
||||
|
||||
<resultMap id="accountVO" type="xyz.zhouxy.plusone.commons.model.dto.test.AccountVO">
|
||||
<id column="id" property="id" javaType="java.lang.Long" />
|
||||
<result column="username" property="username" />
|
||||
<result column="email" property="email" />
|
||||
<result column="status" property="status" />
|
||||
<result column="created_by" property="createdBy" />
|
||||
<result column="create_time" property="createTime" />
|
||||
<result column="version" property="version" />
|
||||
</resultMap>
|
||||
|
||||
<select id="queryAccountList" resultMap="accountVO">
|
||||
SELECT id, username, email, status, created_by, create_time, version
|
||||
FROM sys_account a
|
||||
<where>
|
||||
<if test="query.id != null">
|
||||
AND a.id = #{query.id}
|
||||
</if>
|
||||
<if test="query.username != null">
|
||||
AND a.username = #{query.username}
|
||||
</if>
|
||||
<if test="query.email != null">
|
||||
AND a.email = #{query.email}
|
||||
</if>
|
||||
<if test="query.status != null">
|
||||
AND a.status = #{query.status}
|
||||
</if>
|
||||
<if test="query.createdBy != null">
|
||||
AND a.created_by = #{query.createdBy}
|
||||
</if>
|
||||
<if test="query.createTimeStart != null">
|
||||
AND a.create_time >= #{query.createTimeStart}
|
||||
</if>
|
||||
<if test="query.createTimeEnd != null">
|
||||
AND a.create_time < #{query.createTimeEnd}
|
||||
</if>
|
||||
</where>
|
||||
ORDER BY
|
||||
<foreach collection="page.orderBy" index="i" item="property">
|
||||
${property.sqlSnippet},
|
||||
</foreach>
|
||||
id ASC
|
||||
LIMIT #{page.size} OFFSET #{page.offset}
|
||||
</select>
|
||||
|
||||
<select id="countAccount">
|
||||
SELECT count(*)
|
||||
FROM sys_account a
|
||||
<where>
|
||||
<if test="query.id != null">
|
||||
AND a.id = #{query.id}
|
||||
</if>
|
||||
<if test="query.username != null">
|
||||
AND a.username = #{query.username}
|
||||
</if>
|
||||
<if test="query.email != null">
|
||||
AND a.email = #{query.email}
|
||||
</if>
|
||||
<if test="query.status != null">
|
||||
AND a.status = #{query.status}
|
||||
</if>
|
||||
<if test="query.createdBy != null">
|
||||
AND a.created_by = #{query.createdBy}
|
||||
</if>
|
||||
<if test="query.createTimeStart != null">
|
||||
AND a.create_time >= #{query.createTimeStart}
|
||||
</if>
|
||||
<if test="query.createTimeEnd != null">
|
||||
AND a.create_time < #{query.createTimeEnd}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
</mapper>
|
Loading…
Reference in New Issue