|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.ofbiz.base.container; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.net.MalformedURLException; |
| 25 | +import java.net.URL; |
| 26 | +import java.net.URLClassLoader; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.nio.file.Paths; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +import org.apache.ofbiz.base.component.ComponentConfig; |
| 35 | +import org.apache.ofbiz.base.start.StartupException; |
| 36 | +import org.junit.After; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Ignore; |
| 39 | +import org.junit.Test; |
| 40 | + |
| 41 | + |
| 42 | +public class ComponentContainerTest { |
| 43 | + private static final Path ORDER_CONFIG = Paths.get("applications", "order", "config"); |
| 44 | + private static final Path ACCOUNTING_CONFIG = Paths.get("applications", "accounting", "config"); |
| 45 | + private static final Path[] CONFIGS = {ORDER_CONFIG, ACCOUNTING_CONFIG}; |
| 46 | + |
| 47 | + private Path ofbizHome = Paths.get("testsdata", "ComponentContainerTest").toAbsolutePath().normalize(); |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() throws IOException { |
| 51 | + cleanUp(); |
| 52 | + for (Path cfg : CONFIGS) { |
| 53 | + Files.createDirectory(ofbizHome.resolve(cfg)); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @After |
| 58 | + public void cleanUp() throws IOException { |
| 59 | + for (Path cfg : CONFIGS) { |
| 60 | + Files.deleteIfExists(ofbizHome.resolve(cfg)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Ignore("Dependency declarations do not impact the components order") |
| 65 | + @Test |
| 66 | + public void testCheckDependencyForComponent() throws ContainerException, StartupException, MalformedURLException { |
| 67 | + ComponentContainer containerObj = new ComponentContainer(); |
| 68 | + containerObj.init("component-container", ofbizHome); |
| 69 | + |
| 70 | + List<String> loadedComponents = ComponentConfig.components() |
| 71 | + .map(c -> c.getGlobalName()) |
| 72 | + .collect(Collectors.toList()); |
| 73 | + // we can cast ContextClassLoader since ComponentContainer.loadClassPathForAllComponents has called |
| 74 | + // setContextClassLoader with an URLClassLoader instance |
| 75 | + URL[] classpath = ((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURLs(); |
| 76 | + List<URL> actualClasspath = Arrays.asList(classpath); |
| 77 | + List<URL> expectedClasspath = Arrays.asList( |
| 78 | + ofbizHome.resolve(ORDER_CONFIG).toUri().toURL(), |
| 79 | + ofbizHome.resolve(ACCOUNTING_CONFIG).toUri().toURL()); |
| 80 | + |
| 81 | + assertEquals(expectedClasspath, actualClasspath); |
| 82 | + assertEquals(Arrays.asList("order", "accounting"), loadedComponents); |
| 83 | + } |
| 84 | +} |
0 commit comments