1 /*
2  * Copyright (C) 2017 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.libcore.timezone.util;
18 
19 import org.junit.Test;
20 
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 
25 import com.android.libcore.timezone.testing.TestUtils;
26 import com.android.libcore.timezone.util.Errors.HaltExecutionException;
27 
28 public class ErrorsTest {
29 
30     @Test
warnings()31     public void warnings() {
32         Errors errors = new Errors();
33         assertTrue(errors.isEmpty());
34         assertFalse(errors.hasError());
35         assertFalse(errors.hasFatal());
36 
37         errors.addWarning("Hello");
38         assertFalse(errors.isEmpty());
39         assertFalse(errors.hasError());
40         assertFalse(errors.hasFatal());
41 
42         TestUtils.assertContains(errors.asString(), "Hello");
43     }
44 
45     @Test
error()46     public void error() {
47         Errors errors = new Errors();
48         assertTrue(errors.isEmpty());
49         assertFalse(errors.hasError());
50         assertFalse(errors.hasFatal());
51 
52         errors.addError("Hello");
53         assertFalse(errors.isEmpty());
54         assertTrue(errors.hasError());
55         assertFalse(errors.hasFatal());
56 
57         TestUtils.assertContains(errors.asString(), "Hello");
58     }
59 
60     @Test
fatal()61     public void fatal() {
62         Errors errors = new Errors();
63         assertTrue(errors.isEmpty());
64         assertFalse(errors.hasError());
65         assertFalse(errors.hasFatal());
66 
67         try {
68             throw errors.addFatalAndHalt("Hello");
69         } catch (HaltExecutionException expected) {}
70 
71         assertFalse(errors.isEmpty());
72         assertTrue(errors.hasError());
73         assertTrue(errors.hasFatal());
74 
75         TestUtils.assertContains(errors.asString(), "Hello");
76     }
77 
78     @Test
scope()79     public void scope() {
80         Errors errors = new Errors();
81 
82         errors.addWarning("Hello");
83 
84         errors.pushScope("Monty Python");
85         errors.addError("John Cleese");
86 
87         errors.pushScope("Holy grail");
88         try {
89             errors.addFatalAndHalt("Silly place");
90             fail();
91         } catch (HaltExecutionException expected) {}
92         errors.popScope();
93 
94         errors.addError("Michael Palin");
95 
96         errors.pushScope("Parrot sketch");
97         try {
98             errors.addFatalAndHalt("Fjords");
99             fail();
100         } catch (HaltExecutionException expected) {}
101         errors.popScope();
102 
103         String[] lines = errors.asString().split("\n");
104 
105         String line0 = lines[0];
106         TestUtils.assertContains(line0, "Hello");
107         TestUtils.assertAbsent(line0, "Monty Python");
108 
109         String line1 = lines[1];
110         TestUtils.assertContains(line1, "Monty Python");
111         TestUtils.assertAbsent(line1, "Holy grail");
112         TestUtils.assertContains(line1, "John Cleese");
113 
114         String line2 = lines[2];
115         TestUtils.assertContains(line2, "Monty Python", "Holy grail");
116         TestUtils.assertAbsent(line2, "Parrot sketch");
117         TestUtils.assertContains(line2, "Silly place");
118 
119         String line3 = lines[3];
120         TestUtils.assertContains(line3, "Monty Python");
121         TestUtils.assertAbsent(line3, "Holy grail");
122         TestUtils.assertContains(line3, "Michael Palin");
123 
124         String line4 = lines[4];
125         TestUtils.assertContains(line4, "Monty Python", "Parrot sketch");
126         TestUtils.assertAbsent(line4, "Holy grail");
127         TestUtils.assertContains(line4, "Fjords");
128     }
129 
130 }
131