View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
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    *       https://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  package org.apache.ibatis.executor;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import org.junit.jupiter.api.Test;
21  
22  class ErrorContextTest {
23  
24    @Test
25    void shouldShowProgressiveErrorContextBuilding() {
26      ErrorContext context = ErrorContext.instance();
27      context.resource("somefile.xml").activity("some activity").object("some object").message("Here's more info.");
28      context.toString().startsWith("### The error occurred in somefile.xml.");
29      context.reset();
30  
31      context.activity("some activity").object("some object").message("Here's more info.");
32      context.toString().startsWith("### The error occurred while some activity.");
33      context.reset();
34  
35      context.object("some object").message("Here's more info.");
36      context.toString().startsWith("### Check some object.");
37      context.reset();
38  
39      context.message("Here's more info.");
40      context.toString().startsWith("### Here's more info.");
41      context.reset();
42  
43      context.cause(new Exception("test"));
44      context.toString().startsWith("### Cause: java.lang.Exception: test");
45      context.reset();
46  
47    }
48  
49    @Test
50    void verifyStoreRecall() throws Exception {
51      ErrorContext outer = ErrorContext.instance();
52      ErrorContext inner = ErrorContext.instance().store();
53      assertEquals(inner, ErrorContext.instance());
54      ErrorContext recalled = ErrorContext.instance().recall();
55      assertEquals(outer, recalled);
56      assertEquals(outer, ErrorContext.instance());
57    }
58  }