Improve Batches
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_ServletContextAccess.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * * 
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * * 
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * *
21  ******************************************************************************/
22
23 package org.onap.aaf.cadi.test;
24
25 import static org.junit.Assert.*;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.aaf.cadi.PropAccess;
30 import org.onap.aaf.cadi.ServletContextAccess;
31 import org.onap.aaf.cadi.Access.Level;
32 import org.onap.aaf.cadi.PropAccess.LogIt;
33
34 import static org.mockito.Mockito.*;
35 import static org.hamcrest.CoreMatchers.*;
36
37 import java.lang.reflect.Field;
38
39 import java.io.ByteArrayInputStream;
40 import java.io.PrintStream;
41 import java.util.Enumeration;
42 import java.util.Properties;
43 import java.util.StringTokenizer;
44
45 import javax.servlet.FilterConfig;
46 import javax.servlet.ServletContext;
47
48 @SuppressWarnings("unused")
49 public class JU_ServletContextAccess {
50
51     private FilterConfig filter_mock;
52     Enumeration<String> enumeration;
53     
54     private class CustomEnumeration implements Enumeration<String> {
55         private int idx = 0;
56         private final String[] elements = {"This", "is", "a", "test"};
57         @Override
58         public String nextElement() {
59             return idx >= elements.length ? null : elements[idx++];
60         }
61         @Override
62         public boolean hasMoreElements() {
63             return idx < elements.length;
64         }
65     }
66
67     @Before
68     public void setup() {
69         enumeration = new CustomEnumeration();
70         filter_mock = mock(FilterConfig.class);
71         when(filter_mock.getInitParameterNames()).thenReturn(enumeration);
72     }
73     
74
75     @Test
76     public void logTest() throws Exception {
77         ServletContext sc_mock = mock(ServletContext.class);
78         when(filter_mock.getServletContext()).thenReturn(sc_mock);
79         ServletContextAccess sca = new ServletContextAccess(filter_mock);
80
81         sca.log(Level.DEBUG);
82
83         sca.setLogLevel(Level.DEBUG);
84         sca.log(Level.DEBUG);
85
86         try {
87             sca.log(new Exception("This exception was thrown intentionally, please ignore it"));
88         } catch (Exception e) {
89             fail("Should have thrown an exception");
90         }
91     }
92
93     @Test
94     public void contextTest() {
95         ServletContext sc_mock = mock(ServletContext.class);
96         when(filter_mock.getServletContext()).thenReturn(sc_mock);
97         ServletContextAccess sca = new ServletContextAccess(filter_mock);
98         assertThat(sca.context(), instanceOf(ServletContext.class));
99     }
100
101 }