Sync the latest code changes
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / util / AAIConfigCommandLinePropGetterTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.util;
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.security.Permission;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.aai.AAISetup;
32
33 public class AAIConfigCommandLinePropGetterTest extends AAISetup {
34
35         private SecurityManager m;
36         private TestSecurityManager sm;
37
38         @Before
39         public void setUp() 
40         {
41                 m = System.getSecurityManager();
42                 sm = new TestSecurityManager();
43                 System.setSecurityManager(sm);
44         }
45
46         @After
47         public void tearDown()
48         {   
49                 System.setSecurityManager(m);
50         }
51
52         @Test
53         public void testMainNoArgs() {
54                 try {
55                         AAIConfigCommandLinePropGetter.main(new String[] {});
56                 } catch (SecurityException se) {
57                         // assert main method ends with System.exit(0)
58                         assertEquals("0", se.getMessage());
59                 }
60         }
61         
62         @Test
63         public void testMainReadProp() {
64                 try {
65                         AAIConfigCommandLinePropGetter.main(new String[] {"aai.primary.filetransfer.serverlist"});
66                 } catch (SecurityException se) {
67                         // assert main method ends with System.exit(0)
68                         assertEquals("0", se.getMessage());
69                 }
70         }
71         
72         @Test
73         public void testMainOneArg() {
74                 try {
75                         AAIConfigCommandLinePropGetter.main(new String[] {"one"});
76                 } catch (SecurityException se) {
77                         // assert main method ends with System.exit(0)
78                         assertEquals("0", se.getMessage());
79                 }
80         }
81         
82         @Test
83         public void testMainMoreThanOneArg() {
84                 try {
85                         AAIConfigCommandLinePropGetter.main(new String[] {"one", "two"});
86                 } catch (SecurityException se) {
87                         // assert main method ends with System.exit(0)
88                         assertEquals("0", se.getMessage());
89                 }
90         }
91 }
92
93 class TestSecurityManager extends SecurityManager {
94         @Override 
95         public void checkPermission(Permission permission) {            
96                 if ("exitVM".equals(permission.getName())) 
97                 {
98                         throw new SecurityException("System.exit attempted and blocked.");
99                 }
100         }
101         @Override 
102         public void checkExit(int status) {
103                 throw new SecurityException(Integer.toString(status));
104         }
105 }