1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
23 package org.onap.aaf.cadi.test;
25 import static org.junit.Assert.*;
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;
34 import static org.mockito.Mockito.*;
35 import static org.hamcrest.CoreMatchers.*;
37 import java.lang.reflect.Field;
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;
45 import javax.servlet.FilterConfig;
46 import javax.servlet.ServletContext;
48 @SuppressWarnings("unused")
49 public class JU_ServletContextAccess {
51 private FilterConfig filter_mock;
52 Enumeration<String> enumeration;
54 private class CustomEnumeration implements Enumeration<String> {
56 private final String[] elements = {"This", "is", "a", "test"};
58 public String nextElement() {
59 return idx >= elements.length ? null : elements[idx++];
62 public boolean hasMoreElements() {
63 return idx < elements.length;
69 enumeration = new CustomEnumeration();
70 filter_mock = mock(FilterConfig.class);
71 when(filter_mock.getInitParameterNames()).thenReturn(enumeration);
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);
83 sca.setLogLevel(Level.DEBUG);
87 sca.log(new Exception("This exception was thrown intentionally, please ignore it"));
88 } catch (Exception e) {
89 fail("Should have thrown an exception");
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));