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 org.junit.Assert; 20 import org.junit.Test; 21 22 import java.util.HashSet; 23 import java.util.List; 24 25 public class ErrorReporterTest { 26 /** 27 * Test that errors can be recorded and retrieved. 28 */ 29 @Test testAdding()30 public void testAdding() { 31 TestErrors errors = new TestErrors(); 32 33 errors.ERROR.add(new Position("a", 12), "Errrororrrr"); 34 35 Assert.assertTrue(errors.hadWarningOrError()); 36 Assert.assertTrue(errors.hadError()); 37 38 List<TestErrors.Entry> entries = errors.getEntries(); 39 Assert.assertEquals(1, entries.size()); 40 41 TestErrors.Entry entry = entries.get(0); 42 Assert.assertEquals(errors.ERROR, entry.getCategory()); 43 Assert.assertEquals("a", entry.getPosition().getFile()); 44 Assert.assertEquals(12, entry.getPosition().getLine()); 45 Assert.assertEquals("Errrororrrr", entry.getMessage()); 46 47 Assert.assertNotEquals("", errors.getErrorMessages()); 48 } 49 50 /** 51 * Test that not adding an error doesn't record errors. 52 */ 53 @Test testNoError()54 public void testNoError() { 55 TestErrors errors = new TestErrors(); 56 57 Assert.assertFalse(errors.hadWarningOrError()); 58 Assert.assertFalse(errors.hadError()); 59 Assert.assertEquals("", errors.getErrorMessages()); 60 } 61 62 /** 63 * Test that not adding a warning doesn't record errors. 64 */ 65 @Test testWarning()66 public void testWarning() { 67 TestErrors errors = new TestErrors(); 68 69 errors.WARNING.add("Waaaaarninggggg"); 70 71 Assert.assertTrue(errors.hadWarningOrError()); 72 Assert.assertFalse(errors.hadError()); 73 Assert.assertNotEquals("", errors.getErrorMessages()); 74 } 75 76 /** 77 * Test that hidden warnings don't report. 78 */ 79 @Test testHidden()80 public void testHidden() { 81 TestErrors errors = new TestErrors(); 82 83 errors.HIDDEN.add("Hidddeennn"); 84 85 Assert.assertFalse(errors.hadWarningOrError()); 86 Assert.assertFalse(errors.hadError()); 87 Assert.assertEquals("", errors.getErrorMessages()); 88 } 89 90 /** 91 * Test changing an error level. 92 */ 93 @Test testSetLevel()94 public void testSetLevel() { 95 TestErrors errors = new TestErrors(); 96 Assert.assertEquals(TestErrors.Level.ERROR, errors.ERROR.getLevel()); 97 98 errors.ERROR.setLevel(TestErrors.Level.WARNING); 99 100 Assert.assertEquals(TestErrors.Level.WARNING, errors.ERROR.getLevel()); 101 } 102 103 /** 104 * Test that changing a fixed error fails. 105 */ 106 @Test testSetLevelFails()107 public void testSetLevelFails() { 108 TestErrors errors = new TestErrors(); 109 Assert.assertEquals(TestErrors.Level.ERROR, errors.ERROR_FIXED.getLevel()); 110 111 boolean exceptionThrown = false; 112 try { 113 errors.ERROR_FIXED.setLevel(TestErrors.Level.WARNING); 114 } catch (RuntimeException ex) { 115 exceptionThrown = true; 116 } 117 118 Assert.assertTrue(exceptionThrown); 119 Assert.assertEquals(TestErrors.Level.ERROR, errors.ERROR_FIXED.getLevel()); 120 } 121 } 122