2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.infrastructure.java.classes;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
27 import java.io.IOException;
28 import java.io.InputStream;
30 import java.util.TreeSet;
31 import org.junit.Test;
32 import org.mockito.Mockito;
34 public class ClassUtilsTest {
37 public void testGetClassNames() throws IOException {
38 InputStream input = null;
39 ClassUtils.getClassNames();
40 assertEquals(new TreeSet<>(), ClassUtils.processJar(input));
44 public void testProcessFileName() {
45 assertEquals("testing.txt", ClassUtils.processFileName("testing.txt"));
46 assertNull(ClassUtils.processFileName(null));
47 assertEquals("", ClassUtils.processFileName("/classes/"));
51 public void testProcessDir() throws Exception {
52 File mockFile = Mockito.mock(File.class);
53 Mockito.when(mockFile.isDirectory()).thenReturn(false);
54 assertEquals(new TreeSet<>(), ClassUtils.processDir(mockFile, "Here"));
55 assertEquals(new TreeSet<>(), ClassUtils.processDir(null, "Test"));
56 Mockito.when(mockFile.isDirectory()).thenReturn(true);
58 File mockChildFile = Mockito.mock(File.class);
59 File[] files = {mockChildFile};
60 Mockito.when(mockFile.listFiles()).thenReturn(files);
61 Mockito.when(mockChildFile.getName()).thenReturn("test.class");
62 Mockito.when(mockChildFile.getAbsolutePath()).thenReturn("/test/");
63 assertEquals(Set.of(".test."), ClassUtils.processDir(mockFile, "Here"));
64 Mockito.when(mockChildFile.getName()).thenReturn("test.class");
65 assertEquals(Set.of(".test."), ClassUtils.processDir(mockFile, "Here"));
66 Mockito.when(mockChildFile.getName()).thenReturn("$test.class");
67 assertEquals(new TreeSet<>(), ClassUtils.processDir(mockFile, "Here"));