1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.server.wm.intent; 18 19 import org.json.JSONException; 20 21 /** 22 * Exception to report differences in the {@link Persistence.StateDump} when verifying a 23 * {@link Persistence.TestCase} using the {@linkLaunchRunner#verify} method 24 */ 25 public class StateComparisonException extends RuntimeException { 26 private Persistence.StateDump mExpected; 27 private Persistence.StateDump mActual; 28 private String stage; 29 StateComparisonException(Persistence.StateDump expected, Persistence.StateDump actual, String stage)30 public StateComparisonException(Persistence.StateDump expected, 31 Persistence.StateDump actual, String stage) { 32 mExpected = expected; 33 mActual = actual; 34 this.stage = stage; 35 } 36 37 38 @Override getMessage()39 public String getMessage() { 40 try { 41 String newLine = System.lineSeparator(); 42 return new StringBuilder() 43 .append(newLine) 44 .append(stage) 45 .append(newLine) 46 .append("Expected:") 47 .append(newLine) 48 .append(mExpected.toJson().toString(2)) 49 .append(newLine) 50 .append(newLine) 51 .append("Actual:") 52 .append(newLine) 53 .append(mActual.toJson().toString(2)) 54 .append(newLine) 55 .append(newLine) 56 .toString(); 57 } catch (JSONException e) { 58 e.printStackTrace(); 59 return "json parse exception during error message creation"; 60 } 61 } 62 assertEndStatesEqual(Persistence.StateDump expected, Persistence.StateDump actual)63 public static void assertEndStatesEqual(Persistence.StateDump expected, 64 Persistence.StateDump actual) { 65 compareAndThrow(expected, actual, "Different endSates"); 66 } 67 assertInitialStateEqual(Persistence.StateDump expected, Persistence.StateDump actual)68 public static void assertInitialStateEqual(Persistence.StateDump expected, 69 Persistence.StateDump actual) { 70 compareAndThrow(expected, actual, "Different initial states"); 71 } 72 compareAndThrow(Persistence.StateDump expected, Persistence.StateDump actual, String stage)73 private static void compareAndThrow(Persistence.StateDump expected, 74 Persistence.StateDump actual, String stage) { 75 if (!expected.equals(actual)) { 76 throw new StateComparisonException(expected, actual, stage); 77 } 78 } 79 } 80