Skip to content

Commit

Permalink
Add Unit test for persistence module. (#12768)
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion authored Oct 21, 2024
1 parent 2b178be commit 6ebe0f7
Show file tree
Hide file tree
Showing 31 changed files with 4,426 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public boolean checkMasterWritable() {
return result == 0;
}
} catch (CannotGetJdbcConnectionException e) {
LOGGER.error("[db-error] " + e.toString(), e);
LOGGER.error("[db-error] " + e, e);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public NJdbcException(String msg, Throwable cause) {
public NJdbcException(Throwable cause) {
super("", cause);
}

public String getOriginExceptionName() {
return originExceptionName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @author boyan
* @date 2010-5-6
*/
public class EmbeddedPaginationHelperImpl<E> implements PaginationHelper {
public class EmbeddedPaginationHelperImpl<E> implements PaginationHelper<E> {

private final DatabaseOperate databaseOperate;

Expand Down Expand Up @@ -59,66 +59,27 @@ public Page<E> fetchPage(final String sqlCountRows, final String sqlFetchRows, f
@Override
public Page<E> fetchPage(final String sqlCountRows, final String sqlFetchRows, Object[] args, final int pageNo,
final int pageSize, final Long lastMaxId, final RowMapper rowMapper) {
if (pageNo <= 0 || pageSize <= 0) {
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero");
}

// Query the total number of current records
Integer rowCountInt = databaseOperate.queryOne(sqlCountRows, args, Integer.class);
if (rowCountInt == null) {
throw new IllegalArgumentException("fetchPageLimit error");
}

// Count pages
int pageCount = rowCountInt / pageSize;
if (rowCountInt > pageSize * pageCount) {
pageCount++;
}

// Create Page object
final Page<E> page = new Page<>();
page.setPageNumber(pageNo);
page.setPagesAvailable(pageCount);
page.setTotalCount(rowCountInt);

if (pageNo > pageCount) {
return page;
}

List<E> result = databaseOperate.queryMany(sqlFetchRows, args, rowMapper);
for (E item : result) {
page.getPageItems().add(item);
}
return page;
return doFetchPage(sqlCountRows, args, sqlFetchRows, args, pageNo, pageSize, rowMapper);
}

@Override
public Page<E> fetchPageLimit(final String sqlCountRows, final String sqlFetchRows, final Object[] args,
final int pageNo, final int pageSize, final RowMapper rowMapper) {
if (pageNo <= 0 || pageSize <= 0) {
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero");
}
// Query the total number of current records
Integer rowCountInt = databaseOperate.queryOne(sqlCountRows, Integer.class);
if (rowCountInt == null) {
throw new IllegalArgumentException("fetchPageLimit error");
}

// Count pages
int pageCount = rowCountInt / pageSize;
if (rowCountInt > pageSize * pageCount) {
pageCount++;
}

return doFetchPage(sqlCountRows, null, sqlFetchRows, args, pageNo, pageSize, rowMapper);
}

@Override
public Page<E> fetchPageLimit(final String sqlCountRows, final Object[] args1, final String sqlFetchRows,
final Object[] args2, final int pageNo, final int pageSize, final RowMapper rowMapper) {
return doFetchPage(sqlCountRows, args1, sqlFetchRows, args2, pageNo, pageSize, rowMapper);
}

@Override
public Page<E> fetchPageLimit(final String sqlFetchRows, final Object[] args, final int pageNo, final int pageSize,
final RowMapper rowMapper) {
checkPageInfo(pageNo, pageSize);
// Create Page object
final Page<E> page = new Page<>();
page.setPageNumber(pageNo);
page.setPagesAvailable(pageCount);
page.setTotalCount(rowCountInt);

if (pageNo > pageCount) {
return page;
}

List<E> result = databaseOperate.queryMany(sqlFetchRows, args, rowMapper);
for (E item : result) {
Expand All @@ -128,13 +89,38 @@ public Page<E> fetchPageLimit(final String sqlCountRows, final String sqlFetchRo
}

@Override
public Page<E> fetchPageLimit(final String sqlCountRows, final Object[] args1, final String sqlFetchRows,
final Object[] args2, final int pageNo, final int pageSize, final RowMapper rowMapper) {
public Page fetchPageLimit(MapperResult countMapperResult, MapperResult mapperResult, int pageNo, int pageSize,
RowMapper rowMapper) {
return fetchPageLimit(countMapperResult.getSql(), countMapperResult.getParamList().toArray(),
mapperResult.getSql(), mapperResult.getParamList().toArray(), pageNo, pageSize, rowMapper);
}

@Override
public void updateLimit(final String sql, final Object[] args) {
EmbeddedStorageContextHolder.addSqlContext(sql, args);
try {
databaseOperate.update(EmbeddedStorageContextHolder.getCurrentSqlContext());
} finally {
EmbeddedStorageContextHolder.cleanAllContext();
}
}

private void checkPageInfo(final int pageNo, final int pageSize) {
if (pageNo <= 0 || pageSize <= 0) {
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero");
}
}

private Page<E> doFetchPage(final String sqlCountRows, final Object[] countAgrs, final String sqlFetchRows,
final Object[] fetchArgs, final int pageNo, final int pageSize, final RowMapper rowMapper) {
checkPageInfo(pageNo, pageSize);
// Query the total number of current records
Integer rowCountInt = databaseOperate.queryOne(sqlCountRows, args1, Integer.class);
Integer rowCountInt = null;
if (null != countAgrs) {
rowCountInt = databaseOperate.queryOne(sqlCountRows, countAgrs, Integer.class);
} else {
rowCountInt = databaseOperate.queryOne(sqlCountRows, Integer.class);
}
if (rowCountInt == null) {
throw new IllegalArgumentException("fetchPageLimit error");
}
Expand All @@ -155,44 +141,10 @@ public Page<E> fetchPageLimit(final String sqlCountRows, final Object[] args1, f
return page;
}

List<E> result = databaseOperate.queryMany(sqlFetchRows, args2, rowMapper);
for (E item : result) {
page.getPageItems().add(item);
}
return page;
}

@Override
public Page<E> fetchPageLimit(final String sqlFetchRows, final Object[] args, final int pageNo, final int pageSize,
final RowMapper rowMapper) {
if (pageNo <= 0 || pageSize <= 0) {
throw new IllegalArgumentException("pageNo and pageSize must be greater than zero");
}
// Create Page object
final Page<E> page = new Page<>();

List<E> result = databaseOperate.queryMany(sqlFetchRows, args, rowMapper);
List<E> result = databaseOperate.queryMany(sqlFetchRows, fetchArgs, rowMapper);
for (E item : result) {
page.getPageItems().add(item);
}
return page;
}

@Override
public Page fetchPageLimit(MapperResult countMapperResult, MapperResult mapperResult, int pageNo, int pageSize,
RowMapper rowMapper) {
return fetchPageLimit(countMapperResult.getSql(), countMapperResult.getParamList().toArray(),
mapperResult.getSql(), mapperResult.getParamList().toArray(), pageNo, pageSize, rowMapper);
}

@Override
public void updateLimit(final String sql, final Object[] args) {
EmbeddedStorageContextHolder.addSqlContext(sql, args);
try {
databaseOperate.update(EmbeddedStorageContextHolder.getCurrentSqlContext());
} finally {
EmbeddedStorageContextHolder.cleanAllContext();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -152,26 +151,4 @@ default Boolean blockUpdate(BiConsumer<Boolean, Throwable> consumer) {
}
}

/**
* data modify transaction The SqlContext to be executed in the current thread will be executed and automatically
* cleared.
*
* @return is success
*/
default CompletableFuture<Boolean> futureUpdate() {
try {
CompletableFuture<Boolean> future = new CompletableFuture<>();
update(EmbeddedStorageContextHolder.getCurrentSqlContext(), (o, throwable) -> {
if (Objects.nonNull(throwable)) {
future.completeExceptionally(throwable);
return;
}
future.complete(o);
});
return future;
} finally {
EmbeddedStorageContextHolder.cleanAllContext();
}
}

}
Loading

0 comments on commit 6ebe0f7

Please sign in to comment.