001/**
002 * Copyright (C) 2006-2022 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.junit.environment;
017
018import static java.util.stream.Collectors.toList;
019
020import java.lang.annotation.Annotation;
021import java.util.Collection;
022import java.util.stream.Stream;
023
024public abstract class BaseEnvironmentProvider implements EnvironmentProvider {
025
026    @Override
027    public final AutoCloseable start(final Class<?> clazz, final Annotation[] annotations) {
028        final AutoCloseable autoCloseable = Stream
029                .of(annotations)
030                .filter(a -> a.annotationType() == EnvironmentConfigurations.class)
031                .findFirst()
032                .map(config -> {
033                    final EnvironmentConfigurations configurations = EnvironmentConfigurations.class.cast(config);
034                    return Stream
035                            .of(configurations.value())
036                            .filter(c -> c.environment().equals(getName())
037                                    || c.environment().equals(getName().replace("Environment", "")))
038                            .findFirst()
039                            .map(conf -> {
040                                final Collection<Runnable> releases = Stream.of(conf.systemProperties()).map(p -> {
041                                    final String old = System.getProperty(p.key());
042                                    System.setProperty(p.key(), p.value());
043                                    return (Runnable) () -> {
044                                        if (old == null) {
045                                            System.clearProperty(p.key());
046                                        } else {
047                                            System.clearProperty(p.value());
048                                        }
049                                    };
050                                }).collect(toList());
051                                return (AutoCloseable) () -> releases.forEach(Runnable::run);
052                            })
053                            .orElseGet(() -> () -> {
054
055                            });
056                })
057                .orElse(() -> {
058                });
059        final AutoCloseable closeable = doStart(clazz, annotations);
060        return () -> {
061            try {
062                if (closeable != null) {
063                    closeable.close();
064                }
065            } finally {
066                autoCloseable.close();
067            }
068        };
069    }
070
071    public String getName() {
072        return getClass().getSimpleName().replace("Environment", "");
073    }
074
075    protected abstract AutoCloseable doStart(Class<?> clazz, Annotation[] annotations);
076}