Skip to content

Commit 7fd156a

Browse files
committed
refactor jsonDiff to avoid code duplication
1 parent 73f1378 commit 7fd156a

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.spark
18+
19+
import org.json4s.JsonMethods
20+
import org.json4s._
21+
import org.json4s.jackson.JsonMethods
22+
23+
trait JsonTestUtils {
24+
def assertValidDataInJson(validateJson: JValue, expectedJson: JValue) {
25+
val Diff(c, a, d) = validateJson diff expectedJson
26+
val validatePretty = JsonMethods.pretty(validateJson)
27+
val expectedPretty = JsonMethods.pretty(expectedJson)
28+
val errorMessage = s"Expected:\n$expectedPretty\nFound:\n$validatePretty"
29+
import org.scalactic.TripleEquals._
30+
assert(c === JNothing, s"$errorMessage\nChanged:\n${JsonMethods.pretty(c)}")
31+
assert(a === JNothing, s"$errorMessage\nAdded:\n${JsonMethods.pretty(a)}")
32+
assert(d === JNothing, s"$errorMessage\nDeleted:\n${JsonMethods.pretty(d)}")
33+
}
34+
35+
}

core/src/test/scala/org/apache/spark/deploy/JsonProtocolSuite.scala

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ import org.json4s._
2525
import org.json4s.jackson.JsonMethods
2626
import org.scalatest.FunSuite
2727

28-
import org.apache.spark.deploy.DeployMessages.{MasterStateResponse, WorkerStateResponse}
29-
import org.apache.spark.deploy.master.{ApplicationInfo, DriverInfo, RecoveryState, WorkerInfo}
28+
import org.apache.spark.{JsonTestUtils, SparkConf}
29+
import org.apache.spark.deploy.DeployMessages.WorkerStateResponse
3030
import org.apache.spark.deploy.worker.{DriverRunner, ExecutorRunner}
31-
import org.apache.spark.SparkConf
3231

33-
class JsonProtocolSuite extends FunSuite {
32+
class JsonProtocolSuite extends FunSuite with JsonTestUtils {
3433

3534
test("writeApplicationDescription") {
3635
val output = JsonProtocol.writeApplicationDescription(createAppDesc())
@@ -88,15 +87,6 @@ class JsonProtocolSuite extends FunSuite {
8887
}
8988
}
9089

91-
def assertValidDataInJson(validateJson: JValue, expectedJson: JValue) {
92-
val Diff(c, a, d) = validateJson diff expectedJson
93-
val validatePretty = JsonMethods.pretty(validateJson)
94-
val expectedPretty = JsonMethods.pretty(expectedJson)
95-
val errorMessage = s"Expected:\n$expectedPretty\nFound:\n$validatePretty"
96-
assert(c === JNothing, s"$errorMessage\nChanged:\n${JsonMethods.pretty(c)}")
97-
assert(a === JNothing, s"$errorMessage\nAdded:\n${JsonMethods.pretty(a)}")
98-
assert(d === JNothing, s"$errorMessage\nDelected:\n${JsonMethods.pretty(d)}")
99-
}
10090
}
10191

10292
object JsonConstants {

core/src/test/scala/org/apache/spark/deploy/history/HistoryServerSuite.scala

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ import java.net.{HttpURLConnection, URL}
2121
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
2222

2323
import org.apache.commons.io.{FileUtils, IOUtils}
24-
import org.json4s._
25-
import org.json4s.jackson.JsonMethods
2624
import org.mockito.Mockito.when
2725
import org.scalatest.{BeforeAndAfter, FunSuite, Matchers}
2826
import org.scalatest.mock.MockitoSugar
2927

30-
import org.apache.spark.{SecurityManager, SparkConf}
28+
import org.apache.spark.{JsonTestUtils, SecurityManager, SparkConf}
3129
import org.apache.spark.ui.SparkUI
3230

33-
class HistoryServerSuite extends FunSuite with BeforeAndAfter with Matchers with MockitoSugar {
31+
class HistoryServerSuite extends FunSuite with BeforeAndAfter with Matchers with MockitoSugar
32+
with JsonTestUtils {
3433

3534
private val logDir = new File("src/test/resources/spark-events")
3635
private val expRoot = new File("src/test/resources/HistoryServerExpectations/")
@@ -102,7 +101,7 @@ class HistoryServerSuite extends FunSuite with BeforeAndAfter with Matchers with
102101
import org.json4s.jackson.JsonMethods._
103102
val jsonAst = parse(json)
104103
val expAst = parse(exp)
105-
HistoryServerSuite.assertValidDataInJson(jsonAst, expAst)
104+
assertValidDataInJson(jsonAst, expAst)
106105
}
107106
}
108107

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

246-
def assertValidDataInJson(validateJson: JValue, expectedJson: JValue) {
247-
val Diff(c, a, d) = validateJson diff expectedJson
248-
val validatePretty = JsonMethods.pretty(validateJson)
249-
val expectedPretty = JsonMethods.pretty(expectedJson)
250-
val errorMessage = s"Expected:\n$expectedPretty\nFound:\n$validatePretty"
251-
import org.scalactic.TripleEquals._
252-
assert(c === JNothing, s"$errorMessage\nChanged:\n${JsonMethods.pretty(c)}")
253-
assert(a === JNothing, s"$errorMessage\nAdded:\n${JsonMethods.pretty(a)}")
254-
assert(d === JNothing, s"$errorMessage\nDeleted:\n${JsonMethods.pretty(d)}")
255-
}
256245
}
257-

0 commit comments

Comments
 (0)