REM:
1. 测试Mybatis枚举使用方法
This commit is contained in:
HuangXin 2020-07-29 19:29:32 +08:00
parent c6cac8c320
commit fe0c0faf3e
5 changed files with 264 additions and 113 deletions

View File

@ -0,0 +1,22 @@
package com.dispose.common;
/**
* The interface Base enum.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
public interface BaseEnum {
/**
* Gets value.
*
* @return the value
*/
Integer getValue();
/**
* Gets description.
*
* @return the description
*/
String getDescription();
}

View File

@ -0,0 +1,125 @@
package com.dispose.common;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
/**
* The type Common enum handler.
*
* @param <E> the type parameter
* @author <huangxin@cmhi.chinamoblie.com>
*/
public final class CommonEnumHandler<E extends BaseEnum> extends BaseTypeHandler<E> {
/**
* The Enum type.
*/
private final Class<E> enumType;
/**
* The Enums.
*/
private List<E> enums;
/**
* Instantiates a new Common enum handler.
*
* @param type the type
*/
public CommonEnumHandler(Class<E> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.enumType = type;
this.enums = Arrays.asList(type.getEnumConstants());
}
/**
* Sets non null parameter.
*
* @param preparedStatement the prepared statement
* @param i the
* @param e the e
* @param jdbcType the jdbc type
* @throws SQLException the sql exception
*/
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, E e, JdbcType jdbcType) throws SQLException {
preparedStatement.setInt(i, e.getValue());
}
/**
* Gets nullable result.
*
* @param resultSet the result set
* @param s the s
* @return the nullable result
* @throws SQLException the sql exception
*/
@Override
public E getNullableResult(ResultSet resultSet, String s) throws SQLException {
if (resultSet.getObject(s) == null) {
return null;
}
int val = resultSet.getInt(s);
return locateEnumStatus(val);
}
/**
* Gets nullable result.
*
* @param resultSet the result set
* @param index the index
* @return the nullable result
* @throws SQLException the sql exception
*/
@Override
public E getNullableResult(ResultSet resultSet, int index) throws SQLException {
if (resultSet.getObject(index) == null) {
return null;
}
int val = resultSet.getInt(index);
return locateEnumStatus(val);
}
/**
* Gets nullable result.
*
* @param callableStatement the callable statement
* @param index the index
* @return the nullable result
* @throws SQLException the sql exception
*/
@Override
public E getNullableResult(CallableStatement callableStatement, int index) throws SQLException {
if (callableStatement.getObject(index) == null) {
return null;
}
int val = callableStatement.getInt(index);
return locateEnumStatus(val);
}
/**
* Locate enum status e.
*
* @param index the index
* @return the e
*/
private E locateEnumStatus(int index) {
for (E e : enums) {
if (e.getValue() == index) {
return e;
}
}
throw new IllegalArgumentException(enumType.getName() + " unknown enumerated type index:" + index);
}
}

View File

@ -1,11 +1,12 @@
package com.dispose.common;
/**
* The enum Dispose object type.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
public enum DisposeObjectType {
public enum DisposeObjectType implements BaseEnum {
/**
* The Ip.
*/
@ -23,11 +24,11 @@ public enum DisposeObjectType {
/**
* The Code.
*/
private final int code;
private Integer value;
/**
* The Readme.
*/
private final String readme;
private String description;
/**
* Instantiates a new Dispose object type.
@ -36,8 +37,8 @@ public enum DisposeObjectType {
* @param readme the readme
*/
DisposeObjectType(int code, String readme) {
this.code = code;
this.readme = readme;
this.value = code;
this.description = readme;
}
/**
@ -45,8 +46,9 @@ public enum DisposeObjectType {
*
* @return the code
*/
public int getCode() {
return this.code;
@Override
public Integer getValue() {
return this.value;
}
/**
@ -54,7 +56,8 @@ public enum DisposeObjectType {
*
* @return the readme
*/
public String getReadme() {
return this.readme;
@Override
public String getDescription() {
return this.description;
}
}

View File

@ -1,5 +1,6 @@
package com.dispose.pojo.entity;
import com.dispose.common.DisposeObjectType;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
@ -55,7 +56,7 @@ public class DisposeCapacity {
/**
* The Object type.
*/
private Integer objectType;
private DisposeObjectType objectType;
/**
* The Ip type.

View File

@ -59,7 +59,7 @@ public class DisposeCapacityMapperTest {
.deviceId(1L)
.ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
.capacityType(DisposeCapacityType.CLEANUP.getCode())
.objectType(DisposeObjectType.IP.getCode())
.objectType(DisposeObjectType.IP)
.protectIp("")
.build());
@ -67,7 +67,7 @@ public class DisposeCapacityMapperTest {
.deviceId(1L)
.ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
.capacityType(DisposeCapacityType.BLOCKING.getCode())
.objectType(DisposeObjectType.IP.getCode())
.objectType(DisposeObjectType.URL)
.protectIp("")
.build());
@ -80,105 +80,105 @@ public class DisposeCapacityMapperTest {
log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList));
}
/**
* A 2 del dispose capacity.
*
* @throws JsonProcessingException the json processing exception
*/
@Test
public void a2_delDisposeCapacity() throws JsonProcessingException {
List<DisposeCapacity> newCapList = new ArrayList<>();
newCapList.add(DisposeCapacity.builder()
.deviceId(1L)
.ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
.capacityType(DisposeCapacityType.CLEANUP.getCode())
.objectType(DisposeObjectType.IP.getCode())
.protectIp("")
.build());
newCapList.add(DisposeCapacity.builder()
.deviceId(1L)
.ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
.capacityType(DisposeCapacityType.BLOCKING.getCode())
.objectType(DisposeObjectType.IP.getCode())
.protectIp("")
.build());
int items = disposeCapacityMapper.addNewDisposeCapacity(newCapList);
Assert.assertEquals(items, newCapList.size());
List<DisposeCapacity> capList = disposeCapacityMapper.selectAll();
log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList));
items = disposeCapacityMapper.delDisposeCapacity(capList.get(0).getDeviceId(),
capList.get(0).getCapacityType());
Assert.assertEquals(items, 1);
capList = disposeCapacityMapper.selectAll();
Assert.assertEquals(capList.size(), 1);
log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList));
}
@Test
public void a3_delDeviceDisposeCapacity() {
List<DisposeCapacity> newCapList = new ArrayList<>();
newCapList.add(DisposeCapacity.builder()
.deviceId(1L)
.ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
.capacityType(DisposeCapacityType.CLEANUP.getCode())
.objectType(DisposeObjectType.IP.getCode())
.protectIp("")
.build());
newCapList.add(DisposeCapacity.builder()
.deviceId(1L)
.ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
.capacityType(DisposeCapacityType.BLOCKING.getCode())
.objectType(DisposeObjectType.IP.getCode())
.protectIp("")
.build());
Assert.assertEquals(disposeCapacityMapper.addNewDisposeCapacity(newCapList), newCapList.size());
Assert.assertEquals(disposeCapacityMapper.delDeviceDisposeCapacity(newCapList.get(0).getDeviceId()),
newCapList.size());
Assert.assertEquals(0, disposeCapacityMapper.selectAll().size());
}
@Test
public void b1_getDeviceDisposeCapacity() throws JsonProcessingException {
List<DisposeCapacity> newCapList = new ArrayList<>();
newCapList.add(DisposeCapacity.builder()
.deviceId(1L)
.ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
.capacityType(DisposeCapacityType.CLEANUP.getCode())
.objectType(DisposeObjectType.IP.getCode())
.protectIp("")
.build());
newCapList.add(DisposeCapacity.builder()
.deviceId(1L)
.ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
.capacityType(DisposeCapacityType.BLOCKING.getCode())
.objectType(DisposeObjectType.IP.getCode())
.protectIp("")
.build());
Assert.assertEquals(disposeCapacityMapper.addNewDisposeCapacity(newCapList), newCapList.size());
List<DisposeCapacity> capList = disposeCapacityMapper.getDeviceDisposeCapacity(newCapList.get(0).getDeviceId());
Assert.assertEquals(capList.size(), newCapList.size());
log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList));
}
// /**
// * A 2 del dispose capacity.
// *
// * @throws JsonProcessingException the json processing exception
// */
// @Test
// public void a2_delDisposeCapacity() throws JsonProcessingException {
// List<DisposeCapacity> newCapList = new ArrayList<>();
//
// newCapList.add(DisposeCapacity.builder()
// .deviceId(1L)
// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
// .capacityType(DisposeCapacityType.CLEANUP)
// .objectType(DisposeObjectType.IP)
// .protectIp("")
// .build());
//
// newCapList.add(DisposeCapacity.builder()
// .deviceId(1L)
// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
// .capacityType(DisposeCapacityType.BLOCKING)
// .objectType(DisposeObjectType.IP)
// .protectIp("")
// .build());
//
// int items = disposeCapacityMapper.addNewDisposeCapacity(newCapList);
//
// Assert.assertEquals(items, newCapList.size());
//
// List<DisposeCapacity> capList = disposeCapacityMapper.selectAll();
//
// log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList));
//
// items = disposeCapacityMapper.delDisposeCapacity(capList.get(0).getDeviceId(),
// capList.get(0).getCapacityType().getCode());
//
// Assert.assertEquals(items, 1);
//
// capList = disposeCapacityMapper.selectAll();
//
// Assert.assertEquals(capList.size(), 1);
//
// log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList));
// }
//
// @Test
// public void a3_delDeviceDisposeCapacity() {
// List<DisposeCapacity> newCapList = new ArrayList<>();
//
// newCapList.add(DisposeCapacity.builder()
// .deviceId(1L)
// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
// .capacityType(DisposeCapacityType.CLEANUP)
// .objectType(DisposeObjectType.IP)
// .protectIp("")
// .build());
//
// newCapList.add(DisposeCapacity.builder()
// .deviceId(1L)
// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
// .capacityType(DisposeCapacityType.BLOCKING)
// .objectType(DisposeObjectType.IP)
// .protectIp("")
// .build());
//
// Assert.assertEquals(disposeCapacityMapper.addNewDisposeCapacity(newCapList), newCapList.size());
//
// Assert.assertEquals(disposeCapacityMapper.delDeviceDisposeCapacity(newCapList.get(0).getDeviceId()),
// newCapList.size());
//
// Assert.assertEquals(0, disposeCapacityMapper.selectAll().size());
// }
//
// @Test
// public void b1_getDeviceDisposeCapacity() throws JsonProcessingException {
// List<DisposeCapacity> newCapList = new ArrayList<>();
//
// newCapList.add(DisposeCapacity.builder()
// .deviceId(1L)
// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
// .capacityType(DisposeCapacityType.CLEANUP)
// .objectType(DisposeObjectType.IP)
// .protectIp("")
// .build());
//
// newCapList.add(DisposeCapacity.builder()
// .deviceId(1L)
// .ipType(IpAddrType.IPV4.getCode() | IpAddrType.IPV6.getCode())
// .capacityType(DisposeCapacityType.BLOCKING)
// .objectType(DisposeObjectType.IP)
// .protectIp("")
// .build());
//
// Assert.assertEquals(disposeCapacityMapper.addNewDisposeCapacity(newCapList), newCapList.size());
//
// List<DisposeCapacity> capList = disposeCapacityMapper.getDeviceDisposeCapacity(newCapList.get(0).getDeviceId());
//
// Assert.assertEquals(capList.size(), newCapList.size());
//
// log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(capList));
// }
}