Skip to content

Commit

Permalink
refactor jsonDiff to avoid code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
squito committed Mar 19, 2015
1 parent 73f1378 commit 7fd156a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
35 changes: 35 additions & 0 deletions core/src/test/scala/org/apache/spark/JsonTestUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark

import org.json4s.JsonMethods
import org.json4s._
import org.json4s.jackson.JsonMethods

trait JsonTestUtils {
def assertValidDataInJson(validateJson: JValue, expectedJson: JValue) {
val Diff(c, a, d) = validateJson diff expectedJson
val validatePretty = JsonMethods.pretty(validateJson)
val expectedPretty = JsonMethods.pretty(expectedJson)
val errorMessage = s"Expected:\n$expectedPretty\nFound:\n$validatePretty"
import org.scalactic.TripleEquals._
assert(c === JNothing, s"$errorMessage\nChanged:\n${JsonMethods.pretty(c)}")
assert(a === JNothing, s"$errorMessage\nAdded:\n${JsonMethods.pretty(a)}")
assert(d === JNothing, s"$errorMessage\nDeleted:\n${JsonMethods.pretty(d)}")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ import org.json4s._
import org.json4s.jackson.JsonMethods
import org.scalatest.FunSuite

import org.apache.spark.deploy.DeployMessages.{MasterStateResponse, WorkerStateResponse}
import org.apache.spark.deploy.master.{ApplicationInfo, DriverInfo, RecoveryState, WorkerInfo}
import org.apache.spark.{JsonTestUtils, SparkConf}
import org.apache.spark.deploy.DeployMessages.WorkerStateResponse
import org.apache.spark.deploy.worker.{DriverRunner, ExecutorRunner}
import org.apache.spark.SparkConf

class JsonProtocolSuite extends FunSuite {
class JsonProtocolSuite extends FunSuite with JsonTestUtils {

test("writeApplicationDescription") {
val output = JsonProtocol.writeApplicationDescription(createAppDesc())
Expand Down Expand Up @@ -88,15 +87,6 @@ class JsonProtocolSuite extends FunSuite {
}
}

def assertValidDataInJson(validateJson: JValue, expectedJson: JValue) {
val Diff(c, a, d) = validateJson diff expectedJson
val validatePretty = JsonMethods.pretty(validateJson)
val expectedPretty = JsonMethods.pretty(expectedJson)
val errorMessage = s"Expected:\n$expectedPretty\nFound:\n$validatePretty"
assert(c === JNothing, s"$errorMessage\nChanged:\n${JsonMethods.pretty(c)}")
assert(a === JNothing, s"$errorMessage\nAdded:\n${JsonMethods.pretty(a)}")
assert(d === JNothing, s"$errorMessage\nDelected:\n${JsonMethods.pretty(d)}")
}
}

object JsonConstants {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ import java.net.{HttpURLConnection, URL}
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}

import org.apache.commons.io.{FileUtils, IOUtils}
import org.json4s._
import org.json4s.jackson.JsonMethods
import org.mockito.Mockito.when
import org.scalatest.{BeforeAndAfter, FunSuite, Matchers}
import org.scalatest.mock.MockitoSugar

import org.apache.spark.{SecurityManager, SparkConf}
import org.apache.spark.{JsonTestUtils, SecurityManager, SparkConf}
import org.apache.spark.ui.SparkUI

class HistoryServerSuite extends FunSuite with BeforeAndAfter with Matchers with MockitoSugar {
class HistoryServerSuite extends FunSuite with BeforeAndAfter with Matchers with MockitoSugar
with JsonTestUtils {

private val logDir = new File("src/test/resources/spark-events")
private val expRoot = new File("src/test/resources/HistoryServerExpectations/")
Expand Down Expand Up @@ -102,7 +101,7 @@ class HistoryServerSuite extends FunSuite with BeforeAndAfter with Matchers with
import org.json4s.jackson.JsonMethods._
val jsonAst = parse(json)
val expAst = parse(exp)
HistoryServerSuite.assertValidDataInJson(jsonAst, expAst)
assertValidDataInJson(jsonAst, expAst)
}
}

Expand Down Expand Up @@ -243,15 +242,4 @@ object HistoryServerSuite {
else throw new RuntimeException("got code: " + code + " when getting " + path + " w/ error: " + error)
}

def assertValidDataInJson(validateJson: JValue, expectedJson: JValue) {
val Diff(c, a, d) = validateJson diff expectedJson
val validatePretty = JsonMethods.pretty(validateJson)
val expectedPretty = JsonMethods.pretty(expectedJson)
val errorMessage = s"Expected:\n$expectedPretty\nFound:\n$validatePretty"
import org.scalactic.TripleEquals._
assert(c === JNothing, s"$errorMessage\nChanged:\n${JsonMethods.pretty(c)}")
assert(a === JNothing, s"$errorMessage\nAdded:\n${JsonMethods.pretty(a)}")
assert(d === JNothing, s"$errorMessage\nDeleted:\n${JsonMethods.pretty(d)}")
}
}

0 comments on commit 7fd156a

Please sign in to comment.