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=========================================================
20 package org.onap.policy.apex.core.infrastructure.java.classes;
22 import static org.junit.Assert.*;
25 import java.io.IOException;
26 import java.io.InputStream;
28 import java.util.TreeSet;
30 import org.junit.Test;
31 import org.mockito.Mockito;
33 public class ClassUtilsTest {
36 public void testGetClassNames() throws IOException {
37 InputStream input = null;
38 ClassUtils.getClassNames();
39 assertEquals(new TreeSet<>(), ClassUtils.processJar(input));
43 public void testProcessFileName() {
44 assertEquals("testing.txt",ClassUtils.processFileName("testing.txt"));
45 assertNull(ClassUtils.processFileName(null));
46 assertEquals("",ClassUtils.processFileName("/classes/"));
50 public void testProcessDir() throws Exception {
51 File mockFile = Mockito.mock(File.class);
52 File mockChildFile = 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);
57 File[] files = {mockChildFile};
58 Mockito.when(mockFile.listFiles()).thenReturn(files);
59 Mockito.when(mockChildFile.getName()).thenReturn("test.class");
60 Mockito.when(mockChildFile.getAbsolutePath()).thenReturn("/test/");
61 assertEquals(Set.of(".test."),ClassUtils.processDir(mockFile, "Here"));
62 Mockito.when(mockChildFile.getName()).thenReturn("test.class");
63 assertEquals(Set.of(".test."),ClassUtils.processDir(mockFile, "Here"));
64 Mockito.when(mockChildFile.getName()).thenReturn("$test.class");
65 assertEquals(new TreeSet<>(),ClassUtils.processDir(mockFile, "Here"));