mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 14:35:52 +01:00
Migrating testcase to ScalaTest
This commit is contained in:
@@ -2,16 +2,17 @@ package gitbucket.core.api
|
|||||||
|
|
||||||
import gitbucket.core.util.RepositoryName
|
import gitbucket.core.util.RepositoryName
|
||||||
|
|
||||||
import org.specs2.mutable.Specification
|
//import org.specs2.mutable.Specification
|
||||||
import org.json4s.jackson.JsonMethods.{pretty, parse}
|
import org.json4s.jackson.JsonMethods.{pretty, parse}
|
||||||
import org.json4s._
|
import org.json4s._
|
||||||
import org.specs2.matcher._
|
//import org.specs2.matcher._
|
||||||
|
import org.scalatest.FunSuite
|
||||||
|
|
||||||
import java.util.{Calendar, TimeZone, Date}
|
import java.util.{Calendar, TimeZone, Date}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class JsonFormatSpec extends Specification {
|
class JsonFormatSpec extends FunSuite {
|
||||||
val date1 = {
|
val date1 = {
|
||||||
val d = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
|
val d = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
|
||||||
d.set(2011,3,14,16,0,49)
|
d.set(2011,3,14,16,0,49)
|
||||||
@@ -374,67 +375,58 @@ class JsonFormatSpec extends Specification {
|
|||||||
}
|
}
|
||||||
}"""
|
}"""
|
||||||
|
|
||||||
def beFormatted(json2Arg:String) = new Matcher[String] {
|
def assertJson(resultJson: String, expectJson: String) = {
|
||||||
def apply[S <: String](e: Expectable[S]) = {
|
import java.util.regex.Pattern
|
||||||
import java.util.regex.Pattern
|
val json2 = Pattern.compile("""^\s*//.*$""", Pattern.MULTILINE).matcher(expectJson).replaceAll("")
|
||||||
val json2 = Pattern.compile("""^\s*//.*$""", Pattern.MULTILINE).matcher(json2Arg).replaceAll("")
|
val js2 = try {
|
||||||
val js2 = try{
|
parse(json2)
|
||||||
parse(json2)
|
} catch {
|
||||||
}catch{
|
case e: com.fasterxml.jackson.core.JsonParseException => {
|
||||||
case e:com.fasterxml.jackson.core.JsonParseException => {
|
val p = java.lang.Math.max(e.getLocation.getCharOffset() - 10, 0).toInt
|
||||||
val p = java.lang.Math.max(e.getLocation.getCharOffset()-10,0).toInt
|
val message = json2.substring(p, java.lang.Math.min(p + 100, json2.length))
|
||||||
val message = json2.substring(p,java.lang.Math.min(p+100,json2.length))
|
throw new com.fasterxml.jackson.core.JsonParseException(message + e.getMessage, e.getLocation)
|
||||||
throw new com.fasterxml.jackson.core.JsonParseException(message + e.getMessage , e.getLocation)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val js1 = parse(e.value)
|
|
||||||
result(js1 == js2,
|
|
||||||
"expected",
|
|
||||||
{
|
|
||||||
val diff = js2 diff js1
|
|
||||||
s"${pretty(js1)} is not ${pretty(js2)} \n\n ${pretty(Extraction.decompose(diff)(org.json4s.DefaultFormats))}"
|
|
||||||
},
|
|
||||||
e)
|
|
||||||
}
|
}
|
||||||
|
val js1 = parse(resultJson)
|
||||||
|
assert(js1 === js2)
|
||||||
}
|
}
|
||||||
"JsonFormat" should {
|
|
||||||
"apiUser" in {
|
test("apiUser") {
|
||||||
JsonFormat(apiUser) must beFormatted(apiUserJson)
|
assertJson(JsonFormat(apiUser), apiUserJson)
|
||||||
}
|
}
|
||||||
"repository" in {
|
test("repository") {
|
||||||
JsonFormat(repository) must beFormatted(repositoryJson)
|
assertJson(JsonFormat(repository), repositoryJson)
|
||||||
}
|
}
|
||||||
"apiPushCommit" in {
|
test("apiPushCommit") {
|
||||||
JsonFormat(apiPushCommit) must beFormatted(apiPushCommitJson)
|
assertJson(JsonFormat(apiPushCommit), apiPushCommitJson)
|
||||||
}
|
}
|
||||||
"apiComment" in {
|
test("apiComment") {
|
||||||
JsonFormat(apiComment) must beFormatted(apiCommentJson)
|
assertJson(JsonFormat(apiComment), apiCommentJson)
|
||||||
JsonFormat(apiCommentPR) must beFormatted(apiCommentPRJson)
|
assertJson(JsonFormat(apiCommentPR), apiCommentPRJson)
|
||||||
}
|
}
|
||||||
"apiCommitListItem" in {
|
test("apiCommitListItem") {
|
||||||
JsonFormat(apiCommitListItem) must beFormatted(apiCommitListItemJson)
|
assertJson(JsonFormat(apiCommitListItem), apiCommitListItemJson)
|
||||||
}
|
}
|
||||||
"apiCommitStatus" in {
|
test("apiCommitStatus") {
|
||||||
JsonFormat(apiCommitStatus) must beFormatted(apiCommitStatusJson)
|
assertJson(JsonFormat(apiCommitStatus), apiCommitStatusJson)
|
||||||
}
|
}
|
||||||
"apiCombinedCommitStatus" in {
|
test("apiCombinedCommitStatus") {
|
||||||
JsonFormat(apiCombinedCommitStatus) must beFormatted(apiCombinedCommitStatusJson)
|
assertJson(JsonFormat(apiCombinedCommitStatus), apiCombinedCommitStatusJson)
|
||||||
}
|
}
|
||||||
"apiLabel" in {
|
test("apiLabel") {
|
||||||
JsonFormat(apiLabel) must beFormatted(apiLabelJson)
|
assertJson(JsonFormat(apiLabel), apiLabelJson)
|
||||||
}
|
}
|
||||||
"apiIssue" in {
|
test("apiIssue") {
|
||||||
JsonFormat(apiIssue) must beFormatted(apiIssueJson)
|
assertJson(JsonFormat(apiIssue), apiIssueJson)
|
||||||
JsonFormat(apiIssuePR) must beFormatted(apiIssuePRJson)
|
assertJson(JsonFormat(apiIssuePR), apiIssuePRJson)
|
||||||
}
|
}
|
||||||
"apiPullRequest" in {
|
test("apiPullRequest") {
|
||||||
JsonFormat(apiPullRequest) must beFormatted(apiPullRequestJson)
|
assertJson(JsonFormat(apiPullRequest), apiPullRequestJson)
|
||||||
}
|
}
|
||||||
"apiPullRequestReviewComment" in {
|
test("apiPullRequestReviewComment") {
|
||||||
JsonFormat(apiPullRequestReviewComment) must beFormatted(apiPullRequestReviewCommentJson)
|
assertJson(JsonFormat(apiPullRequestReviewComment), apiPullRequestReviewCommentJson)
|
||||||
}
|
}
|
||||||
"apiBranchProtection" in {
|
test("apiBranchProtection") {
|
||||||
JsonFormat(apiBranchProtection) must beFormatted(apiBranchProtectionJson)
|
assertJson(JsonFormat(apiBranchProtection), apiBranchProtectionJson)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,74 +5,74 @@ import java.util.Date
|
|||||||
import gitbucket.core.model.Account
|
import gitbucket.core.model.Account
|
||||||
import gitbucket.core.service.{SystemSettingsService, RequestCache}
|
import gitbucket.core.service.{SystemSettingsService, RequestCache}
|
||||||
import gitbucket.core.controller.Context
|
import gitbucket.core.controller.Context
|
||||||
import org.specs2.mutable._
|
import org.mockito.Mockito._
|
||||||
import org.specs2.mock.Mockito
|
|
||||||
import SystemSettingsService.SystemSettings
|
import SystemSettingsService.SystemSettings
|
||||||
import javax.servlet.http.HttpServletRequest
|
import javax.servlet.http.HttpServletRequest
|
||||||
import play.twirl.api.Html
|
import play.twirl.api.Html
|
||||||
|
import org.scalatest.FunSpec
|
||||||
|
|
||||||
class AvatarImageProviderSpec extends Specification with Mockito {
|
class AvatarImageProviderSpec extends FunSpec {
|
||||||
|
|
||||||
val request = mock[HttpServletRequest]
|
val request = mock(classOf[HttpServletRequest])
|
||||||
request.getRequestURL returns new StringBuffer("http://localhost:8080/path.html")
|
when(request.getRequestURL).thenReturn(new StringBuffer("http://localhost:8080/path.html"))
|
||||||
request.getRequestURI returns "/path.html"
|
when(request.getRequestURI).thenReturn("/path.html")
|
||||||
request.getContextPath returns ""
|
when(request.getContextPath).thenReturn("")
|
||||||
|
|
||||||
"getAvatarImageHtml" should {
|
describe("getAvatarImageHtml") {
|
||||||
"show Gravatar image for no image account if gravatar integration is enabled" in {
|
it("should show Gravatar image for no image account if gravatar integration is enabled") {
|
||||||
implicit val context = Context(createSystemSettings(true), None, request)
|
implicit val context = Context(createSystemSettings(true), None, request)
|
||||||
val provider = new AvatarImageProviderImpl(Some(createAccount(None)))
|
val provider = new AvatarImageProviderImpl(Some(createAccount(None)))
|
||||||
|
|
||||||
provider.toHtml("user", 32).toString mustEqual
|
assert(provider.toHtml("user", 32).toString ==
|
||||||
"<img src=\"https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=32&d=retro&r=g\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />"
|
"<img src=\"https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=32&d=retro&r=g\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />")
|
||||||
}
|
}
|
||||||
|
|
||||||
"show uploaded image even if gravatar integration is enabled" in {
|
it("should show uploaded image even if gravatar integration is enabled") {
|
||||||
implicit val context = Context(createSystemSettings(true), None, request)
|
implicit val context = Context(createSystemSettings(true), None, request)
|
||||||
val provider = new AvatarImageProviderImpl(Some(createAccount(Some("icon.png"))))
|
val provider = new AvatarImageProviderImpl(Some(createAccount(Some("icon.png"))))
|
||||||
|
|
||||||
provider.toHtml("user", 32).toString mustEqual
|
assert(provider.toHtml("user", 32).toString ==
|
||||||
"<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />"
|
"<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />")
|
||||||
}
|
}
|
||||||
|
|
||||||
"show local image for no image account if gravatar integration is disabled" in {
|
it("should show local image for no image account if gravatar integration is disabled") {
|
||||||
implicit val context = Context(createSystemSettings(false), None, request)
|
implicit val context = Context(createSystemSettings(false), None, request)
|
||||||
val provider = new AvatarImageProviderImpl(Some(createAccount(None)))
|
val provider = new AvatarImageProviderImpl(Some(createAccount(None)))
|
||||||
|
|
||||||
provider.toHtml("user", 32).toString mustEqual
|
assert(provider.toHtml("user", 32).toString ==
|
||||||
"<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />"
|
"<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />")
|
||||||
}
|
}
|
||||||
|
|
||||||
"show Gravatar image for specified mail address if gravatar integration is enabled" in {
|
it("should show Gravatar image for specified mail address if gravatar integration is enabled") {
|
||||||
implicit val context = Context(createSystemSettings(true), None, request)
|
implicit val context = Context(createSystemSettings(true), None, request)
|
||||||
val provider = new AvatarImageProviderImpl(None)
|
val provider = new AvatarImageProviderImpl(None)
|
||||||
|
|
||||||
provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual
|
assert(provider.toHtml("user", 20, "hoge@hoge.com").toString ==
|
||||||
"<img src=\"https://www.gravatar.com/avatar/4712f9b0e63f56ad952ad387eaa23b9c?s=20&d=retro&r=g\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />"
|
"<img src=\"https://www.gravatar.com/avatar/4712f9b0e63f56ad952ad387eaa23b9c?s=20&d=retro&r=g\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />")
|
||||||
}
|
}
|
||||||
|
|
||||||
"show unknown image for unknown user if gravatar integration is enabled" in {
|
it("should show unknown image for unknown user if gravatar integration is enabled") {
|
||||||
implicit val context = Context(createSystemSettings(true), None, request)
|
implicit val context = Context(createSystemSettings(true), None, request)
|
||||||
val provider = new AvatarImageProviderImpl(None)
|
val provider = new AvatarImageProviderImpl(None)
|
||||||
|
|
||||||
provider.toHtml("user", 20).toString mustEqual
|
assert(provider.toHtml("user", 20).toString ==
|
||||||
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />"
|
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />")
|
||||||
}
|
}
|
||||||
|
|
||||||
"show unknown image for specified mail address if gravatar integration is disabled" in {
|
it("should show unknown image for specified mail address if gravatar integration is disabled") {
|
||||||
implicit val context = Context(createSystemSettings(false), None, request)
|
implicit val context = Context(createSystemSettings(false), None, request)
|
||||||
val provider = new AvatarImageProviderImpl(None)
|
val provider = new AvatarImageProviderImpl(None)
|
||||||
|
|
||||||
provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual
|
assert(provider.toHtml("user", 20, "hoge@hoge.com").toString ==
|
||||||
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />"
|
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />")
|
||||||
}
|
}
|
||||||
|
|
||||||
"add tooltip if it's enabled" in {
|
it("should add tooltip if it's enabled") {
|
||||||
implicit val context = Context(createSystemSettings(false), None, request)
|
implicit val context = Context(createSystemSettings(false), None, request)
|
||||||
val provider = new AvatarImageProviderImpl(None)
|
val provider = new AvatarImageProviderImpl(None)
|
||||||
|
|
||||||
provider.toHtml("user", 20, "hoge@hoge.com", true).toString mustEqual
|
assert(provider.toHtml("user", 20, "hoge@hoge.com", true).toString ==
|
||||||
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" data-toggle=\"tooltip\" title=\"user\"/>"
|
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" data-toggle=\"tooltip\" title=\"user\"/>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,68 +1,68 @@
|
|||||||
package gitbucket.core.view
|
package gitbucket.core.view
|
||||||
|
|
||||||
import gitbucket.core.util.ControlUtil
|
import gitbucket.core.util.ControlUtil
|
||||||
import org.specs2.mutable._
|
|
||||||
import ControlUtil._
|
import ControlUtil._
|
||||||
|
import org.scalatest.FunSpec
|
||||||
|
|
||||||
class PaginationSpec extends Specification {
|
class PaginationSpec extends FunSpec {
|
||||||
|
|
||||||
"max" should {
|
describe("max") {
|
||||||
"return max page number" in {
|
it("should return max page number") {
|
||||||
val pagination = Pagination(1, 100, 10, 6)
|
val pagination = Pagination(1, 100, 10, 6)
|
||||||
pagination.max mustEqual 10
|
assert(pagination.max == 10)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
"omitLeft and omitRight" should {
|
describe("omitLeft and omitRight") {
|
||||||
"return true if pagination links at their side will be omitted" in {
|
it("should return true if pagination links at their side will be omitted") {
|
||||||
defining(Pagination(1, 100, 10, 6)){ pagination =>
|
defining(Pagination(1, 100, 10, 6)){ pagination =>
|
||||||
pagination.omitLeft mustEqual false
|
assert(pagination.omitLeft == false)
|
||||||
pagination.omitRight mustEqual true
|
assert(pagination.omitRight == true)
|
||||||
}
|
}
|
||||||
defining(Pagination(9, 100, 10, 6)){ pagination =>
|
defining(Pagination(9, 100, 10, 6)){ pagination =>
|
||||||
pagination.omitLeft mustEqual true
|
assert(pagination.omitLeft == true)
|
||||||
pagination.omitRight mustEqual false
|
assert(pagination.omitRight == false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
"visibleFor" should {
|
describe("visibleFor") {
|
||||||
"return true for visible pagination links" in {
|
it("should return true for visible pagination links") {
|
||||||
defining(Pagination(1, 100, 10, 6)){ pagination =>
|
defining(Pagination(1, 100, 10, 6)){ pagination =>
|
||||||
pagination.visibleFor(1) mustEqual true
|
assert(pagination.visibleFor(1) == true)
|
||||||
pagination.visibleFor(2) mustEqual true
|
assert(pagination.visibleFor(2) == true)
|
||||||
pagination.visibleFor(3) mustEqual true
|
assert(pagination.visibleFor(3) == true)
|
||||||
pagination.visibleFor(4) mustEqual true
|
assert(pagination.visibleFor(4) == true)
|
||||||
pagination.visibleFor(5) mustEqual true
|
assert(pagination.visibleFor(5) == true)
|
||||||
pagination.visibleFor(6) mustEqual false
|
assert(pagination.visibleFor(6) == false)
|
||||||
pagination.visibleFor(7) mustEqual false
|
assert(pagination.visibleFor(7) == false)
|
||||||
pagination.visibleFor(8) mustEqual false
|
assert(pagination.visibleFor(8) == false)
|
||||||
pagination.visibleFor(9) mustEqual false
|
assert(pagination.visibleFor(9) == false)
|
||||||
pagination.visibleFor(10) mustEqual true
|
assert(pagination.visibleFor(10) == true)
|
||||||
}
|
}
|
||||||
defining(Pagination(5, 100, 10, 6)){ pagination =>
|
defining(Pagination(5, 100, 10, 6)){ pagination =>
|
||||||
pagination.visibleFor(1) mustEqual true
|
assert(pagination.visibleFor(1) == true)
|
||||||
pagination.visibleFor(2) mustEqual false
|
assert(pagination.visibleFor(2) == false)
|
||||||
pagination.visibleFor(3) mustEqual false
|
assert(pagination.visibleFor(3) == false)
|
||||||
pagination.visibleFor(4) mustEqual true
|
assert(pagination.visibleFor(4) == true)
|
||||||
pagination.visibleFor(5) mustEqual true
|
assert(pagination.visibleFor(5) == true)
|
||||||
pagination.visibleFor(6) mustEqual true
|
assert(pagination.visibleFor(6) == true)
|
||||||
pagination.visibleFor(7) mustEqual false
|
assert(pagination.visibleFor(7) == false)
|
||||||
pagination.visibleFor(8) mustEqual false
|
assert(pagination.visibleFor(8) == false)
|
||||||
pagination.visibleFor(9) mustEqual false
|
assert(pagination.visibleFor(9) == false)
|
||||||
pagination.visibleFor(10) mustEqual true
|
assert(pagination.visibleFor(10) == true)
|
||||||
}
|
}
|
||||||
defining(Pagination(8, 100, 10, 6)){ pagination =>
|
defining(Pagination(8, 100, 10, 6)){ pagination =>
|
||||||
pagination.visibleFor(1) mustEqual true
|
assert(pagination.visibleFor(1) == true)
|
||||||
pagination.visibleFor(2) mustEqual false
|
assert(pagination.visibleFor(2) == false)
|
||||||
pagination.visibleFor(3) mustEqual false
|
assert(pagination.visibleFor(3) == false)
|
||||||
pagination.visibleFor(4) mustEqual false
|
assert(pagination.visibleFor(4) == false)
|
||||||
pagination.visibleFor(5) mustEqual false
|
assert(pagination.visibleFor(5) == false)
|
||||||
pagination.visibleFor(6) mustEqual true
|
assert(pagination.visibleFor(6) == true)
|
||||||
pagination.visibleFor(7) mustEqual true
|
assert(pagination.visibleFor(7) == true)
|
||||||
pagination.visibleFor(8) mustEqual true
|
assert(pagination.visibleFor(8) == true)
|
||||||
pagination.visibleFor(9) mustEqual true
|
assert(pagination.visibleFor(9) == true)
|
||||||
pagination.visibleFor(10) mustEqual true
|
assert(pagination.visibleFor(10) == true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user