1 /* 2 * Copyright (C) 2020 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 com.android.build.config; 18 19 import java.io.ByteArrayOutputStream; 20 import java.io.PrintStream; 21 import java.io.UnsupportedEncodingException; 22 import java.nio.charset.StandardCharsets; 23 24 /** 25 * Errors for testing. 26 */ 27 public class TestErrors extends ErrorReporter { 28 29 public static final int ERROR_CODE = 1; 30 31 public final Category ERROR = new Category(ERROR_CODE, true, Level.ERROR, 32 "An error."); 33 34 public static final int WARNING_CODE = 2; 35 36 public final Category WARNING = new Category(WARNING_CODE, true, Level.WARNING, 37 "A warning."); 38 39 public static final int HIDDEN_CODE = 3; 40 41 public final Category HIDDEN = new Category(HIDDEN_CODE, true, Level.HIDDEN, 42 "A hidden warning."); 43 44 public static final int ERROR_FIXED_CODE = 4; 45 46 public final Category ERROR_FIXED = new Category(ERROR_FIXED_CODE, false, Level.ERROR, 47 "An error that can't have its level changed."); 48 assertHasEntry(Errors.Category category)49 public void assertHasEntry(Errors.Category category) { 50 assertHasEntry(category, this); 51 } 52 getErrorMessages()53 public String getErrorMessages() { 54 return getErrorMessages(this); 55 } 56 assertHasEntry(Errors.Category category, ErrorReporter errors)57 public static void assertHasEntry(Errors.Category category, ErrorReporter errors) { 58 StringBuilder found = new StringBuilder(); 59 for (Errors.Entry entry: errors.getEntries()) { 60 if (entry.getCategory() == category) { 61 return; 62 } 63 found.append(' '); 64 found.append(entry.getCategory().getCode()); 65 } 66 throw new AssertionError("No error category " + category.getCode() + " found." 67 + " Found category codes were:" + found); 68 } 69 getErrorMessages(ErrorReporter errors)70 public static String getErrorMessages(ErrorReporter errors) { 71 final ByteArrayOutputStream stream = new ByteArrayOutputStream(); 72 try { 73 errors.printErrors(new PrintStream(stream, true, StandardCharsets.UTF_8.name())); 74 } catch (UnsupportedEncodingException ex) { 75 // utf-8 is always supported 76 } 77 return new String(stream.toByteArray(), StandardCharsets.UTF_8); 78 } 79 } 80 81