Improve Batches
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / config / test / JU_Get.java
1 /*******************************************************************************
2  * * org.onap.aaf
3  * * ===========================================================================
4  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  * * ===========================================================================
6  * * Licensed under the Apache License, Version 2.0 (the "License");
7  * * you may not use this file except in compliance with the License.
8  * * You may obtain a copy of the License at
9  * *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  * *
12  *  * Unless required by applicable law or agreed to in writing, software
13  * * distributed under the License is distributed on an "AS IS" BASIS,
14  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * * See the License for the specific language governing permissions and
16  * * limitations under the License.
17  * * ============LICENSE_END====================================================
18  * *
19  * *
20  ******************************************************************************/
21
22 package org.onap.aaf.cadi.config.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import org.junit.*;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
30
31 import org.onap.aaf.cadi.PropAccess;
32 import org.onap.aaf.cadi.config.Get;
33
34 public class JU_Get {
35
36     private String defaultVal = "some default value";
37
38     private ByteArrayOutputStream outStream;
39
40     private TestBean tb;
41
42     @Before
43     public void setup() {
44         outStream = new ByteArrayOutputStream();
45         System.setOut(new PrintStream(outStream));
46     }
47
48     @After
49     public void tearDown() {
50         System.setOut(System.out);
51     }
52
53     @Test
54     public void beanTest() {
55         tb = new TestBean();
56         tb.setProperty1("prop1");
57
58         Get.Bean testBean = new Get.Bean(tb);
59         assertThat(testBean.get("property1", defaultVal, true), is("prop1"));
60         assertThat(testBean.get("property2", defaultVal, true), is(defaultVal));
61         assertThat(testBean.get("thrower", defaultVal, true), is(defaultVal));
62     }
63
64     @Test
65     public void nullTest() {
66         assertThat(Get.NULL.get("name", defaultVal, true), is(defaultVal));
67     }
68
69     @Test
70     public void accessTest() {
71
72         PropAccess access = new PropAccess();
73         access.setProperty("tag", "value");
74         Get.AccessGet accessGet = new Get.AccessGet(access);
75
76         assertThat(accessGet.get("tag", defaultVal, true), is("value"));
77         outStream.reset();
78
79         assertThat(accessGet.get("not a real tag", defaultVal, true), is(defaultVal));
80         outStream.reset();
81
82         assertThat(accessGet.get("not a real tag", null, true), is(nullValue()));
83
84         outStream.reset();
85
86         assertThat(accessGet.get("tag", defaultVal, false), is("value"));
87         assertThat(outStream.toString(), is(""));
88     }
89
90     public class TestBean implements java.io.Serializable {
91
92         private static final long serialVersionUID = 1L;
93         private String property1 = null;
94         private String property2 = null;
95         @SuppressWarnings("unused")
96         private String thrower = null;
97
98         public TestBean() { } 
99         public String getProperty1() { return property1; }
100         public void setProperty1(final String value) { this.property1 = value; }
101         public String getProperty2() { return property2; }
102         public void setProperty2(final String value) { this.property2 = value; }
103         public String getThrower() throws Exception { throw new Exception(); }
104         public void setThrower(final String value) { this.thrower = value; }
105
106     }
107 }