Skip to content

Commit d26952a

Browse files
author
jack
committedFeb 8, 2018
1 parent c025997 commit d26952a

31 files changed

+982
-0
lines changed
 

‎.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.gradle
2+
/build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
nbproject/private/
21+
build/
22+
nbbuild/
23+
dist/
24+
nbdist/
25+
.nb-gradle/

‎app.sql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE SCHEMA `demo_testing_and_deploy` DEFAULT CHARACTER SET utf8 ;
2+

‎build.gradle

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
buildscript {
2+
ext {
3+
kotlinVersion = '1.2.20'
4+
springBootVersion = '2.0.0.RC1'
5+
}
6+
repositories {
7+
mavenLocal() // 使用本地仓库
8+
mavenCentral()
9+
maven { url "https://repo.spring.io/snapshot" }
10+
maven { url "https://repo.spring.io/milestone" }
11+
}
12+
dependencies {
13+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
14+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
15+
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
16+
classpath('com.easy.springboot:demo_gradle_plugin_kor:1.0-SNAPSHOT') // 添加插件依赖
17+
}
18+
}
19+
20+
apply plugin: 'kotlin'
21+
apply plugin: 'kotlin-spring'
22+
apply plugin: 'eclipse'
23+
apply plugin: 'org.springframework.boot'
24+
apply plugin: 'io.spring.dependency-management'
25+
apply plugin: 'com.springboot.kor' //应用插件,名称对应到com.springboot.kor.properties属性文件名
26+
27+
28+
group = 'com.easy.springboot'
29+
version = '0.0.1-SNAPSHOT'
30+
sourceCompatibility = 1.8
31+
32+
compileKotlin {
33+
// freeCompilerArgs = ["-Xjsr305=strict"]
34+
kotlinOptions.jvmTarget = "1.8"
35+
}
36+
compileTestKotlin {
37+
// freeCompilerArgs = ["-Xjsr305=strict"]
38+
kotlinOptions.jvmTarget = "1.8"
39+
}
40+
41+
repositories {
42+
mavenCentral()
43+
maven { url "https://repo.spring.io/snapshot" }
44+
maven { url "https://repo.spring.io/milestone" }
45+
}
46+
47+
48+
dependencies {
49+
compile('org.springframework.boot:spring-boot-starter-actuator')
50+
compile('org.springframework.boot:spring-boot-starter-data-jpa')
51+
compile('org.springframework.boot:spring-boot-starter-freemarker')
52+
compile('org.springframework.boot:spring-boot-starter-web')
53+
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
54+
compile("org.jetbrains.kotlin:kotlin-reflect")
55+
runtime('org.springframework.boot:spring-boot-devtools')
56+
runtime('mysql:mysql-connector-java')
57+
compile('org.springframework.boot:spring-boot-starter-test')
58+
59+
}
60+
61+
// 自定义 kor 插件的输入参数
62+
korArgs {
63+
entity = 'Demo'
64+
}
65+

‎gradle/wrapper/gradle-wrapper.jar

53.4 KB
Binary file not shown.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Tue Feb 06 12:27:20 CET 2018
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-bin.zip

‎gradlew

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/usr/bin/env sh
2+
3+
##############################################################################
4+
##
5+
## Gradle start up script for UN*X
6+
##
7+
##############################################################################
8+
9+
# Attempt to set APP_HOME
10+
# Resolve links: $0 may be a link
11+
PRG="$0"
12+
# Need this for relative symlinks.
13+
while [ -h "$PRG" ] ; do
14+
ls=`ls -ld "$PRG"`
15+
link=`expr "$ls" : '.*-> \(.*\)$'`
16+
if expr "$link" : '/.*' > /dev/null; then
17+
PRG="$link"
18+
else
19+
PRG=`dirname "$PRG"`"/$link"
20+
fi
21+
done
22+
SAVED="`pwd`"
23+
cd "`dirname \"$PRG\"`/" >/dev/null
24+
APP_HOME="`pwd -P`"
25+
cd "$SAVED" >/dev/null
26+
27+
APP_NAME="Gradle"
28+
APP_BASE_NAME=`basename "$0"`
29+
30+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31+
DEFAULT_JVM_OPTS=""
32+
33+
# Use the maximum available, or set MAX_FD != -1 to use that value.
34+
MAX_FD="maximum"
35+
36+
warn ( ) {
37+
echo "$*"
38+
}
39+
40+
die ( ) {
41+
echo
42+
echo "$*"
43+
echo
44+
exit 1
45+
}
46+
47+
# OS specific support (must be 'true' or 'false').
48+
cygwin=false
49+
msys=false
50+
darwin=false
51+
nonstop=false
52+
case "`uname`" in
53+
CYGWIN* )
54+
cygwin=true
55+
;;
56+
Darwin* )
57+
darwin=true
58+
;;
59+
MINGW* )
60+
msys=true
61+
;;
62+
NONSTOP* )
63+
nonstop=true
64+
;;
65+
esac
66+
67+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68+
69+
# Determine the Java command to use to start the JVM.
70+
if [ -n "$JAVA_HOME" ] ; then
71+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72+
# IBM's JDK on AIX uses strange locations for the executables
73+
JAVACMD="$JAVA_HOME/jre/sh/java"
74+
else
75+
JAVACMD="$JAVA_HOME/bin/java"
76+
fi
77+
if [ ! -x "$JAVACMD" ] ; then
78+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79+
80+
Please set the JAVA_HOME variable in your environment to match the
81+
location of your Java installation."
82+
fi
83+
else
84+
JAVACMD="java"
85+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86+
87+
Please set the JAVA_HOME variable in your environment to match the
88+
location of your Java installation."
89+
fi
90+
91+
# Increase the maximum file descriptors if we can.
92+
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93+
MAX_FD_LIMIT=`ulimit -H -n`
94+
if [ $? -eq 0 ] ; then
95+
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96+
MAX_FD="$MAX_FD_LIMIT"
97+
fi
98+
ulimit -n $MAX_FD
99+
if [ $? -ne 0 ] ; then
100+
warn "Could not set maximum file descriptor limit: $MAX_FD"
101+
fi
102+
else
103+
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104+
fi
105+
fi
106+
107+
# For Darwin, add options to specify how the application appears in the dock
108+
if $darwin; then
109+
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110+
fi
111+
112+
# For Cygwin, switch paths to Windows format before running java
113+
if $cygwin ; then
114+
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115+
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116+
JAVACMD=`cygpath --unix "$JAVACMD"`
117+
118+
# We build the pattern for arguments to be converted via cygpath
119+
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120+
SEP=""
121+
for dir in $ROOTDIRSRAW ; do
122+
ROOTDIRS="$ROOTDIRS$SEP$dir"
123+
SEP="|"
124+
done
125+
OURCYGPATTERN="(^($ROOTDIRS))"
126+
# Add a user-defined pattern to the cygpath arguments
127+
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128+
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129+
fi
130+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
131+
i=0
132+
for arg in "$@" ; do
133+
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134+
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135+
136+
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137+
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138+
else
139+
eval `echo args$i`="\"$arg\""
140+
fi
141+
i=$((i+1))
142+
done
143+
case $i in
144+
(0) set -- ;;
145+
(1) set -- "$args0" ;;
146+
(2) set -- "$args0" "$args1" ;;
147+
(3) set -- "$args0" "$args1" "$args2" ;;
148+
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149+
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150+
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151+
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152+
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153+
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154+
esac
155+
fi
156+
157+
# Escape application args
158+
save ( ) {
159+
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160+
echo " "
161+
}
162+
APP_ARGS=$(save "$@")
163+
164+
# Collect all arguments for the java command, following the shell quoting and substitution rules
165+
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166+
167+
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168+
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169+
cd "$(dirname "$0")"
170+
fi
171+
172+
exec "$JAVACMD" "$@"

‎gradlew.bat

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
@if "%DEBUG%" == "" @echo off
2+
@rem ##########################################################################
3+
@rem
4+
@rem Gradle startup script for Windows
5+
@rem
6+
@rem ##########################################################################
7+
8+
@rem Set local scope for the variables with windows NT shell
9+
if "%OS%"=="Windows_NT" setlocal
10+
11+
set DIRNAME=%~dp0
12+
if "%DIRNAME%" == "" set DIRNAME=.
13+
set APP_BASE_NAME=%~n0
14+
set APP_HOME=%DIRNAME%
15+
16+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17+
set DEFAULT_JVM_OPTS=
18+
19+
@rem Find java.exe
20+
if defined JAVA_HOME goto findJavaFromJavaHome
21+
22+
set JAVA_EXE=java.exe
23+
%JAVA_EXE% -version >NUL 2>&1
24+
if "%ERRORLEVEL%" == "0" goto init
25+
26+
echo.
27+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28+
echo.
29+
echo Please set the JAVA_HOME variable in your environment to match the
30+
echo location of your Java installation.
31+
32+
goto fail
33+
34+
:findJavaFromJavaHome
35+
set JAVA_HOME=%JAVA_HOME:"=%
36+
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37+
38+
if exist "%JAVA_EXE%" goto init
39+
40+
echo.
41+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42+
echo.
43+
echo Please set the JAVA_HOME variable in your environment to match the
44+
echo location of your Java installation.
45+
46+
goto fail
47+
48+
:init
49+
@rem Get command-line arguments, handling Windows variants
50+
51+
if not "%OS%" == "Windows_NT" goto win9xME_args
52+
53+
:win9xME_args
54+
@rem Slurp the command line arguments.
55+
set CMD_LINE_ARGS=
56+
set _SKIP=2
57+
58+
:win9xME_args_slurp
59+
if "x%~1" == "x" goto execute
60+
61+
set CMD_LINE_ARGS=%*
62+
63+
:execute
64+
@rem Setup the command line
65+
66+
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67+
68+
@rem Execute Gradle
69+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70+
71+
:end
72+
@rem End local scope for the variables with windows NT shell
73+
if "%ERRORLEVEL%"=="0" goto mainEnd
74+
75+
:fail
76+
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77+
rem the _cmd.exe /c_ return code!
78+
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79+
exit /b 1
80+
81+
:mainEnd
82+
if "%OS%"=="Windows_NT" endlocal
83+
84+
:omega
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.easy.springboot.demo_testing_and_deploy
2+
3+
import com.easy.springboot.demo_testing_and_deploy.dao.RoleDao
4+
import com.easy.springboot.demo_testing_and_deploy.dao.UserDao
5+
import com.easy.springboot.demo_testing_and_deploy.entity.Role
6+
import com.easy.springboot.demo_testing_and_deploy.entity.User
7+
import org.springframework.boot.ApplicationRunner
8+
import org.springframework.boot.autoconfigure.SpringBootApplication
9+
import org.springframework.boot.builder.SpringApplicationBuilder
10+
import org.springframework.context.support.beans
11+
12+
@SpringBootApplication
13+
open class DemoTestingAndDeployApplication
14+
15+
fun main(args: Array<String>) {
16+
17+
SpringApplicationBuilder().initializers(
18+
beans {
19+
bean {
20+
ApplicationRunner {
21+
try {
22+
val roleDao = ref<RoleDao>()
23+
// 普通用户角色
24+
val roleUser = Role()
25+
roleUser.role = "ROLE_USER"
26+
val r1 = roleDao.save(roleUser)
27+
28+
// 超级管理员角色
29+
val roleAdmin = Role()
30+
roleAdmin.role = "ROLE_ADMIN"
31+
val r2 = roleDao.save(roleAdmin)
32+
33+
val userDao = ref<UserDao>()
34+
// 普通用户
35+
val user = User()
36+
user.username = "user"
37+
user.password = "user"
38+
val userRoles = setOf(r1)
39+
user.roles = userRoles
40+
userDao.save(user)
41+
42+
// 超级管理员用户
43+
val admin = User()
44+
admin.username = "admin"
45+
admin.password = "admin"
46+
val adminRoles = setOf(r1, r2)
47+
admin.roles = adminRoles
48+
userDao.save(admin)
49+
50+
} catch (e: Exception) {
51+
}
52+
}
53+
}
54+
}
55+
).sources(DemoTestingAndDeployApplication::class.java).run(*args)
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.easy.springboot.demo_testing_and_deploy.controller
2+
3+
import org.springframework.web.bind.annotation.GetMapping
4+
import org.springframework.web.bind.annotation.RequestParam
5+
import org.springframework.web.bind.annotation.RestController
6+
7+
@RestController
8+
class ClassUtilController {
9+
10+
@GetMapping("/classinfo")
11+
fun classinfo(@RequestParam("classname") classname: String): List<MethodInfo> {
12+
val result = mutableListOf<MethodInfo>()
13+
14+
Class.forName(classname).declaredMethods.forEach {
15+
16+
val paramsList = mutableListOf<MethodParam>()
17+
it.parameters.forEach {
18+
paramsList.add(MethodParam(it.name, it.type.canonicalName))
19+
}
20+
21+
result.add(
22+
MethodInfo(
23+
methodName = it.name,
24+
params = paramsList,
25+
returnType = it.returnType.canonicalName
26+
)
27+
)
28+
}
29+
return result
30+
}
31+
}
32+
33+
data class MethodInfo(var methodName: String, var params: List<MethodParam>, var returnType: String)
34+
data class MethodParam(var name: String, var type: String)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
package com.easy.springboot.demo_testing_and_deploy.controller
3+
import com.easy.springboot.demo_testing_and_deploy.dao.RoleDao
4+
import com.easy.springboot.demo_testing_and_deploy.entity.Role
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.web.bind.annotation.GetMapping
7+
import org.springframework.web.bind.annotation.RequestMapping
8+
import org.springframework.web.bind.annotation.RestController
9+
import org.springframework.web.bind.annotation.PathVariable
10+
import javax.servlet.http.HttpServletRequest
11+
/**
12+
* Created by Kor on 2018-02-08 12:44:14. author: 东海陈光剑
13+
*/
14+
@RestController
15+
@RequestMapping("/role")
16+
class RoleController {
17+
@Autowired lateinit var RoleDao: RoleDao
18+
@GetMapping(value = ["/"])
19+
fun role(request: HttpServletRequest): List<Role>{
20+
return RoleDao.findAll()
21+
}
22+
@GetMapping(value = ["/{id}"])
23+
fun getOne(@PathVariable("id") id:Long): Role {
24+
return RoleDao.getOne(id)
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
package com.easy.springboot.demo_testing_and_deploy.controller
3+
import com.easy.springboot.demo_testing_and_deploy.dao.UserDao
4+
import com.easy.springboot.demo_testing_and_deploy.entity.User
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.web.bind.annotation.GetMapping
7+
import org.springframework.web.bind.annotation.RequestMapping
8+
import org.springframework.web.bind.annotation.RestController
9+
import org.springframework.web.bind.annotation.PathVariable
10+
import javax.servlet.http.HttpServletRequest
11+
/**
12+
* Created by Kor on 2018-02-08 12:44:04. author: 东海陈光剑
13+
*/
14+
@RestController
15+
@RequestMapping("/user")
16+
class UserController {
17+
@Autowired lateinit var UserDao: UserDao
18+
@GetMapping(value = ["/"])
19+
fun user(request: HttpServletRequest): List<User>{
20+
return UserDao.findAll()
21+
}
22+
@GetMapping(value = ["/{id}"])
23+
fun getOne(@PathVariable("id") id:Long): User {
24+
return UserDao.getOne(id)
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.easy.springboot.demo_testing_and_deploy.dao
2+
3+
import com.easy.springboot.demo_testing_and_deploy.entity.Role
4+
import org.springframework.data.jpa.repository.JpaRepository
5+
6+
/**
7+
* Created by Kor on 2018-02-08 12:44:14. author: 东海陈光剑
8+
*/
9+
interface RoleDao : JpaRepository<Role, Long> {
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.easy.springboot.demo_testing_and_deploy.dao
2+
3+
import com.easy.springboot.demo_testing_and_deploy.entity.User
4+
import org.springframework.data.jpa.repository.JpaRepository
5+
6+
/**
7+
* Created by Kor on 2018-02-08 12:44:04. author: 东海陈光剑
8+
*/
9+
interface UserDao : JpaRepository<User, Long> {
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.easy.springboot.demo_testing_and_deploy.entity
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat
4+
import java.util.*
5+
import javax.persistence.Entity
6+
import javax.persistence.GeneratedValue
7+
import javax.persistence.GenerationType
8+
import javax.persistence.Id
9+
10+
/**
11+
* Created by Kor on 2018-02-08 12:44:14. author: 东海陈光剑
12+
*/
13+
@Entity
14+
class Role {
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.IDENTITY)
17+
var id: Long = -1
18+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
19+
var gmtCreate = Date()
20+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
21+
var gmtModify = Date()
22+
var isDeleted = 0
23+
24+
var role = "ROLE_USER"
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.easy.springboot.demo_testing_and_deploy.entity
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat
4+
import java.util.*
5+
import javax.persistence.*
6+
7+
/**
8+
* Created by Kor on 2018-02-08 12:44:04. author: 东海陈光剑
9+
*/
10+
@Entity
11+
class User {
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
var id: Long = -1
15+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
16+
var gmtCreate = Date()
17+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
18+
var gmtModify = Date()
19+
var isDeleted = 0
20+
21+
@Column(unique = true, length = 100)
22+
var username = ""
23+
var password = ""
24+
@ManyToMany(targetEntity = Role::class, fetch = FetchType.EAGER)
25+
lateinit var roles: Set<Role>
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
package com.easy.springboot.demo_testing_and_deploy.service
3+
4+
import com.easy.springboot.demo_testing_and_deploy.entity.Role
5+
6+
/**
7+
* Created by Kor on 2018-02-08 12:44:14. author: 东海陈光剑
8+
*/
9+
interface RoleService {
10+
fun findAll():List<Role>
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.easy.springboot.demo_testing_and_deploy.service
2+
3+
import com.easy.springboot.demo_testing_and_deploy.entity.User
4+
5+
/**
6+
* Created by Kor on 2018-02-08 12:44:04. author: 东海陈光剑
7+
*/
8+
interface UserService {
9+
fun findAll(): List<User>
10+
fun getOne(id:Long):User?
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.easy.springboot.demo_testing_and_deploy.service.impl
2+
3+
import com.easy.springboot.demo_testing_and_deploy.dao.RoleDao
4+
import com.easy.springboot.demo_testing_and_deploy.entity.Role
5+
import com.easy.springboot.demo_testing_and_deploy.service.RoleService
6+
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.stereotype.Service
8+
9+
/**
10+
* Created by Kor on 2018-02-08 12:44:14. author: 东海陈光剑
11+
*/
12+
@Service
13+
class RoleServiceImpl : RoleService {
14+
@Autowired lateinit var roleDao: RoleDao
15+
override fun findAll(): List<Role> {
16+
return roleDao.findAll()
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.easy.springboot.demo_testing_and_deploy.service.impl
2+
3+
import com.easy.springboot.demo_testing_and_deploy.dao.UserDao
4+
import com.easy.springboot.demo_testing_and_deploy.entity.User
5+
import com.easy.springboot.demo_testing_and_deploy.service.UserService
6+
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.stereotype.Service
8+
9+
/**
10+
* Created by Kor on 2018-02-08 12:44:04. author: 东海陈光剑
11+
*/
12+
@Service
13+
class UserServiceImpl : UserService {
14+
@Autowired lateinit var userDao: UserDao
15+
override fun findAll(): List<User> {
16+
return userDao.findAll()
17+
}
18+
19+
override fun getOne(id: Long): User? {
20+
return userDao.getOne(id)
21+
}
22+
}

‎src/main/resources/application-daily.properties

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
spring.application.name=demo_testing_and_deploy
2+
info.app.name=demo_testing_and_deploy
3+
info.app.version=v1.0.0
4+
info.app.description=demo_testing_and_deploy
5+
server.port=8012
6+
#mysql
7+
spring.datasource.url=jdbc:mysql://localhost:3306/demo_testing_and_deploy?useUnicode=true&characterEncoding=UTF8&useSSL=false
8+
spring.datasource.username=root
9+
spring.datasource.password=root
10+
spring.datasource.driverClassName=com.mysql.jdbc.Driver
11+
# Specify the DBMS
12+
spring.jpa.database=MYSQL
13+
# Show or not log for each sql query
14+
spring.jpa.show-sql=true
15+
# Hibernate ddl auto (create, create-drop, update)
16+
#spring.jpa.hibernate.ddl-auto=create-drop
17+
spring.jpa.hibernate.ddl-auto=update
18+
# stripped before adding them to the entity manager)
19+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
20+
#logging
21+
logging.level.root=info
22+
logging.level.org.springframework.web=info
23+
logging.path=${user.home}/logs
24+
logging.file.max-history=30
25+
logging.file.max-size=2MB
26+
#Endpoints in Spring Boot 2.0
27+
#http://127.0.0.1:${server.port}/actuator
28+
management.endpoints.enabled-by-default=true
29+
management.endpoints.web.expose=*
30+
#management.endpoint.beans.enabled=false
31+
#management.endpoint.info.enabled=false
32+
#management.endpoint.health.enabled=false
33+
#management.endpoints.web.enabled=true
34+
#management.endpoints.jmx.enabled=true
35+
#management.endpoints.web.exclude=env

‎src/main/resources/application-prod.properties

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.profiles.active=dev

‎src/main/resources/banner.png

150 KB
Loading

‎src/main/resources/banner.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
${AnsiColor.GREEN}
2+
_____ _ ____ _ ___ ___
3+
/ ____| (_) | _ \ | | |__ \ / _ \
4+
| (___ _ __ _ __ _ _ __ __ _ | |_) | ___ ___ | |_ ) || | | |
5+
\___ \| '_ \| '__| | '_ \ / _` | | _ < / _ \ / _ \| __| / / | | | |
6+
____) | |_) | | | | | | | (_| | | |_) | (_) | (_) | |_ / /_ | |_| |
7+
|_____/| .__/|_| |_|_| |_|\__, | |____/ \___/ \___/ \__| |____(_)___/
8+
| | __/ |
9+
|_| |___/
10+
11+
${AnsiColor.BRIGHT_RED}
12+
Application : ${spring.application.name}
13+
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}

‎src/main/resources/logback-spring.xml

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<springProperty scope="context"
4+
name="logging.file"
5+
source="logging.file"/>
6+
7+
<springProperty scope="context"
8+
name="logging.path"
9+
source="logging.path"/>
10+
11+
<springProperty scope="context"
12+
name="logging.level.root"
13+
source="logging.level.root"/>
14+
15+
<springProperty scope="context"
16+
name="spring.application.name"
17+
source="spring.application.name"/>
18+
19+
<springProperty scope="context"
20+
name="logging.file.max-size"
21+
source="logging.file.max-size"/>
22+
23+
<springProperty scope="context"
24+
name="logging.file.max-history"
25+
source="logging.file.max-history"/>
26+
27+
<property name="LOG_FILE"
28+
value="${logging.path:-.}/${logging.file:-${spring.application.name:-spring}.log}"/>
29+
30+
<property name="MAX_SIZE"
31+
value="${logging.file.max-size:-10MB}"/>
32+
33+
34+
<property name="MAX_HISTORY"
35+
value="${logging.file.max-history:-0}"/>
36+
37+
38+
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
39+
<conversionRule conversionWord="wex"
40+
converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
41+
<conversionRule conversionWord="wEx"
42+
converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
43+
44+
<property name="CONSOLE_LOG_PATTERN"
45+
value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
46+
<property name="FILE_LOG_PATTERN"
47+
value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
48+
49+
<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
50+
<logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>
51+
<logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
52+
<logger name="org.apache.sshd.common.util.SecurityUtils" level="WARN"/>
53+
<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
54+
<logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/>
55+
<logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
56+
57+
<!-- show parameters for hibernate sql 专为 Hibernate 定制 -->
58+
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE"/>
59+
<logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG"/>
60+
<logger name="org.hibernate.SQL" level="DEBUG"/>
61+
<logger name="org.hibernate.engine.QueryParameters" level="DEBUG"/>
62+
<logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG"/>
63+
64+
<!--myibatis log configure-->
65+
<logger name="com.apache.ibatis" level="TRACE"/>
66+
<logger name="java.sql.Connection" level="DEBUG"/>
67+
<logger name="java.sql.Statement" level="DEBUG"/>
68+
<logger name="java.sql.PreparedStatement" level="DEBUG"/>
69+
70+
<!--<include resource="org/springframework/boot/logging/logback/base.xml"/>-->
71+
72+
<appender name="FILE"
73+
class="ch.qos.logback.core.rolling.RollingFileAppender">
74+
<encoder>
75+
<pattern>${FILE_LOG_PATTERN}</pattern>
76+
</encoder>
77+
<file>${LOG_FILE}</file>
78+
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
79+
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
80+
<maxFileSize>${MAX_SIZE}</maxFileSize>
81+
<maxHistory>${MAX_HISTORY}</maxHistory>
82+
</rollingPolicy>
83+
</appender>
84+
85+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
86+
<encoder>
87+
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
88+
<charset>utf8</charset>
89+
</encoder>
90+
</appender>
91+
92+
<root level="${logging.level.root}">
93+
<appender-ref ref="CONSOLE"/>
94+
<appender-ref ref="FILE"/>
95+
</root>
96+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.easy.springboot.demo_testing_and_deploy
2+
3+
import org.hamcrest.Matchers
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.springframework.boot.test.context.SpringBootTest
7+
import org.springframework.test.context.junit4.SpringRunner
8+
import kotlin.reflect.KClass
9+
import kotlin.reflect.full.declaredFunctions
10+
11+
@RunWith(SpringRunner::class)
12+
@SpringBootTest
13+
class DemoTestingAndDeployApplicationTests {
14+
15+
@Test
16+
fun contextLoads() {
17+
18+
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.easy.springboot.demo_testing_and_deploy.controller
2+
3+
import org.hamcrest.Matchers
4+
import org.junit.Before
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.springframework.beans.factory.annotation.Autowired
8+
import org.springframework.boot.test.context.SpringBootTest
9+
import org.springframework.http.MediaType
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
11+
import org.springframework.test.web.servlet.MockMvc
12+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
13+
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
14+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
15+
import org.springframework.test.web.servlet.setup.MockMvcBuilders
16+
import org.springframework.web.context.WebApplicationContext
17+
18+
19+
@RunWith(SpringJUnit4ClassRunner::class)
20+
@SpringBootTest
21+
class UserControllerTest {
22+
23+
@Autowired
24+
lateinit var context: WebApplicationContext
25+
lateinit var mvc: MockMvc
26+
@Before
27+
fun setUp() {
28+
mvc = MockMvcBuilders.webAppContextSetup(context).build()
29+
}
30+
31+
@Test
32+
fun testFetchUser1() {
33+
val output = """
34+
// 20180208214800
35+
// http://127.0.0.1:8012/user/1
36+
37+
{
38+
"id": 1,
39+
"gmtCreate": "2018-02-08 12:58:14",
40+
"gmtModify": "2018-02-08 12:58:14",
41+
"username": "user",
42+
"password": "user",
43+
"roles": [
44+
{
45+
"id": 1,
46+
"gmtCreate": "2018-02-08 12:58:14",
47+
"gmtModify": "2018-02-08 12:58:14",
48+
"role": "ROLE_USER"
49+
}
50+
]
51+
}
52+
"""
53+
54+
mvc.perform(MockMvcRequestBuilders.get("/user/1")
55+
.contentType(MediaType.APPLICATION_JSON_UTF8)
56+
.accept(MediaType.APPLICATION_JSON))
57+
.andExpect(MockMvcResultMatchers.status().isOk)
58+
.andDo(MockMvcResultHandlers.print())
59+
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("""
60+
"username":"user"
61+
""".trimIndent())))
62+
.andDo {
63+
println("it.request.method=${it.request.method}")
64+
println("it.response.contentAsString=${it.response.contentAsString}")
65+
}
66+
.andExpect(MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(1)))
67+
.andExpect(MockMvcResultMatchers.jsonPath("$.roles[0].role", Matchers.equalTo("ROLE_USER")))
68+
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.easy.springboot.demo_testing_and_deploy.dao
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.boot.test.context.SpringBootTest
8+
import org.springframework.test.context.junit4.SpringRunner
9+
10+
@RunWith(SpringRunner::class)
11+
@SpringBootTest
12+
class UserDaoTest {
13+
@Autowired lateinit var userDao: UserDao
14+
15+
@Test
16+
fun testFindAll() {
17+
Assert.assertTrue(userDao.findAll().size == 2)
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.easy.springboot.demo_testing_and_deploy.service
2+
3+
import com.easy.springboot.demo_testing_and_deploy.dao.UserDao
4+
import com.easy.springboot.demo_testing_and_deploy.entity.Role
5+
import com.easy.springboot.demo_testing_and_deploy.entity.User
6+
import com.easy.springboot.demo_testing_and_deploy.service.impl.UserServiceImpl
7+
import com.fasterxml.jackson.databind.ObjectMapper
8+
import org.junit.Assert
9+
import org.junit.Before
10+
import org.junit.Test
11+
import org.junit.runner.RunWith
12+
import org.mockito.InjectMocks
13+
import org.mockito.Mock
14+
import org.mockito.Mockito.`when`
15+
import org.mockito.MockitoAnnotations
16+
import org.mockito.junit.MockitoJUnitRunner
17+
18+
19+
@RunWith(MockitoJUnitRunner::class)
20+
class MockUserServiceTest {
21+
@Mock
22+
lateinit var mockUserDao: UserDao // mock 一个DAO层的接口
23+
@InjectMocks
24+
lateinit var userService: UserServiceImpl// Mock一个 Service 的实现类,用 @InjectMocks。注意这里是实现类 UserServiceImpl
25+
26+
@Before
27+
fun setUp() {
28+
// initMocks 必须,否则 @Mock 注解无效
29+
MockitoAnnotations.initMocks(this)
30+
}
31+
32+
@Test
33+
fun testGetOne() {
34+
val mockUser = User()
35+
mockUser.id = 101
36+
mockUser.username = "mockUser"
37+
mockUser.password = "123456"
38+
39+
val roles = mutableSetOf<Role>()
40+
val r1 = Role()
41+
r1.role = "ROLE_USER"
42+
val r2 = Role()
43+
r1.role = "ROLE_ADMIN"
44+
roles.add(r1)
45+
roles.add(r2)
46+
mockUser.roles = roles
47+
//模拟 UserDao对象
48+
`when`(mockUserDao.getOne(1)).thenReturn(mockUser)
49+
50+
val u = userService.getOne(1)
51+
println(ObjectMapper().writeValueAsString(u))
52+
Assert.assertTrue(u?.password == "123456")
53+
}
54+
55+
56+
}
57+
58+
59+
/**
60+
*
61+
* Mockito 主要用于 service 层的 mock 测试。mock 的对象一般是对 DAO 层的依赖; 另外就是别人的Service实现类;
62+
* 官方文档上是这么描述的:
63+
64+
mock()/@Mock: create mockspy()/@Spy: partial mocking, real methods are invoked but still can be verified and stubbed
65+
optionally specify how it should behave via Answer/ReturnValues/MockSettings
66+
67+
when()/given() to specify how a mock should behave
68+
If the provided answers don’t fit your needs, write one yourself extending the Answerinterface
69+
70+
@InjectMocks: automatically inject mocks/spies fields annotated with @Spy or @Mock -- 这句话理解意思是它会把上下文中你标记为@Spy和@Mock的对象都自动注解进去。是不是就相当于把实现类中的私有成员属性(比如ReportMediaDayMapper的依赖)给偷梁换柱了
71+
72+
verify(): to check methods were called with given arguments
73+
can use flexible argument matching, for example any expression via the any()
74+
or capture what arguments where called using @Captor instead
75+
76+
* */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.easy.springboot.demo_testing_and_deploy.service
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.boot.test.context.SpringBootTest
8+
import org.springframework.test.context.junit4.SpringRunner
9+
10+
11+
@RunWith(SpringRunner::class)
12+
@SpringBootTest
13+
class UserServiceTest {
14+
15+
@Autowired lateinit var userService: UserService
16+
17+
@Test
18+
fun testFindAll() {
19+
Assert.assertTrue(userService.findAll().size == 2)
20+
}
21+
22+
}

0 commit comments

Comments
 (0)
Please sign in to comment.