Skip to content

Instantly share code, notes, and snippets.

@stella6767
Created August 18, 2022 05:15
Show Gist options
  • Save stella6767/d0477b67ecbcdf3b115f878487c811be to your computer and use it in GitHub Desktop.
Save stella6767/d0477b67ecbcdf3b115f878487c811be to your computer and use it in GitHub Desktop.
코틀린 고차함수 테스트
object CustomAopObject {
val log = KotlinLogging.logger { }
/**
* 커리함수테스트
*/
fun <T> manageTransaction(method: () -> T): T? {
try {
println("began transaction") //트랜잭션 시작
val result = method.invoke()
println("commit") //트랜잭션 커밋
return result
} catch (e: Exception) {
println("rollback")
return null
}
}
fun <T> addTryCatch(method: () -> T) {
//todo 반환타입이 없는 경우, 쓰겠다.
try {
method.invoke()
} catch (e: Exception) {
log.error { e.stackTraceToString() }
}
}
fun <T> addTryCatchWithReturn(method: () -> T): T {
try {
return method.invoke()
} catch (e: Exception) {
log.error("aop try catch with Return error ", e)
throw RuntimeException(e)
}
}
fun <T : AuditingEntity> saveEntity(entity: T, em: EntityManager): AuditingEntity? {
Assert.notNull(entity, "Entity must not be null.")
return if (entity.id == 0L) {
em.persist(entity)
entity
} else {
em.merge(entity)
}
}
// hard delete
inline fun <reified T : AuditingEntity> deleteEntity(entity: T, em:EntityManager) {
Assert.notNull(entity, "Entity must not be null.")
// if (entity.id == 0L) {
// return
// }
// val type = ProxyUtils.getUserClass(entity)
// val existing = em.find(type, entity.id) as T ?: return
// em.remove(if (em.contains(entity)) entity else em.merge(entity))
em.remove(entity)
}
}
@Test
fun customAopTest() {
//doSometing3()
val higerOrderTest = higerOrderTest()
higerOrderTest.invoke()
}
fun doSometing(): String? = CustomAopObject.manageTransaction {
println("query 실행")
"repository 결과"
}
fun doSometing2(): String? = CustomAopObject.manageTransaction {
println("query2 실행")
"repository 결과2"
}
fun doSometing3() = CustomAopObject.addTryCatch {
println("query3 실행")
throw RuntimeException("forced error")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment