View Javadoc
1   /*
2    *    Copyright 2009-2024 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.Assertions;
21  import org.junit.jupiter.api.Test;
22  
23  class ErrorContextTest {
24  
25    private static final String SOME_FILE_XML = "somefile.xml";
26    private static final String SOME_ACTIVITY = "some activity";
27    private static final String SOME_OBJECT = "some object";
28    private static final String MORE_INFO = "Here's more info.";
29    private static final String EXCEPTION_MESSAGE = "test";
30  
31    @Test
32    void shouldShowProgressiveErrorContextBuilding() {
33      ErrorContext context = ErrorContext.instance();
34  
35      context.resource(SOME_FILE_XML).activity(SOME_ACTIVITY).object(SOME_OBJECT).message(MORE_INFO);
36      String contextString = context.toString();
37      Assertions.assertTrue(contextString.contains("### " + MORE_INFO));
38      Assertions.assertTrue(contextString.contains("### The error may exist in " + SOME_FILE_XML));
39      Assertions.assertTrue(contextString.contains("### The error may involve " + SOME_OBJECT));
40      Assertions.assertTrue(contextString.contains("### The error occurred while " + SOME_ACTIVITY));
41      context.reset();
42  
43      context.activity(SOME_ACTIVITY).object(SOME_OBJECT).message(MORE_INFO);
44      contextString = context.toString();
45      Assertions.assertTrue(contextString.contains("### " + MORE_INFO));
46      Assertions.assertTrue(contextString.contains("### The error occurred while " + SOME_ACTIVITY));
47      context.reset();
48  
49      context.object(SOME_OBJECT).message(MORE_INFO);
50      contextString = context.toString();
51      Assertions.assertTrue(contextString.contains("### " + MORE_INFO));
52      Assertions.assertTrue(contextString.contains("### The error may involve " + SOME_OBJECT));
53      context.reset();
54  
55      context.message(MORE_INFO);
56      contextString = context.toString();
57      Assertions.assertTrue(contextString.contains("### " + MORE_INFO));
58      context.reset();
59  
60      context.cause(new Exception(EXCEPTION_MESSAGE));
61      contextString = context.toString();
62      Assertions.assertTrue(contextString.contains("### Cause: java.lang.Exception: " + EXCEPTION_MESSAGE));
63      context.reset();
64  
65    }
66  
67    @Test
68    void verifyStoreRecall() throws Exception {
69      ErrorContext outer = ErrorContext.instance();
70      ErrorContext inner = ErrorContext.instance().store();
71      assertEquals(inner, ErrorContext.instance());
72      ErrorContext recalled = ErrorContext.instance().recall();
73      assertEquals(outer, recalled);
74      assertEquals(outer, ErrorContext.instance());
75    }
76  }