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 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
24 import static org.junit.Assert.*;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.aaf.cadi.PropAccess;
29 import org.onap.aaf.cadi.ServletContextAccess;
30 import org.onap.aaf.cadi.Access.Level;
31 import org.onap.aaf.cadi.PropAccess.LogIt;
33 import static org.mockito.Mockito.*;
34 import static org.hamcrest.CoreMatchers.*;
36 import java.lang.reflect.Field;
38 import java.io.ByteArrayInputStream;
39 import java.io.PrintStream;
40 import java.util.Enumeration;
41 import java.util.Properties;
42 import java.util.StringTokenizer;
44 import javax.servlet.FilterConfig;
45 import javax.servlet.ServletContext;
47 @SuppressWarnings("unused")
48 public class JU_ServletContextAccess {
50 private FilterConfig filter_mock;
51 Enumeration<String> enumeration;
53 private class CustomEnumeration implements Enumeration<String> {
55 private final String[] elements = {"This", "is", "a", "test"};
57 public String nextElement() {
58 return idx >= elements.length ? null : elements[idx++];
61 public boolean hasMoreElements() {
62 return idx < elements.length;
68 enumeration = new CustomEnumeration();
69 filter_mock = mock(FilterConfig.class);
70 when(filter_mock.getInitParameterNames()).thenReturn(enumeration);
74 public void ConstructorTest() throws Exception {
75 ServletContextAccess sca = new ServletContextAccess(filter_mock);
79 public void logTest() throws Exception {
80 ServletContext sc_mock = mock(ServletContext.class);
81 when(filter_mock.getServletContext()).thenReturn(sc_mock);
82 ServletContextAccess sca = new ServletContextAccess(filter_mock);
86 sca.setLogLevel(Level.DEBUG);
90 sca.log(new Exception("This exception was thrown intentionally, please ignore it"));
91 } catch(Exception e) {
92 fail("Should have thrown an exception");
97 public void contextTest() {
98 ServletContext sc_mock = mock(ServletContext.class);
99 when(filter_mock.getServletContext()).thenReturn(sc_mock);
100 ServletContextAccess sca = new ServletContextAccess(filter_mock);
101 assertThat(sca.context(), instanceOf(ServletContext.class));