001/**
002 * Copyright (C) 2006-2024 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.base.junit5.internal;
017
018import java.lang.annotation.Annotation;
019
020import org.junit.jupiter.api.extension.AfterAllCallback;
021import org.junit.jupiter.api.extension.BeforeAllCallback;
022import org.junit.jupiter.api.extension.ExtensionContext;
023import org.talend.sdk.component.junit.base.junit5.JUnit5InjectionSupport;
024import org.talend.sdk.component.junit.base.junit5.TempFolder;
025import org.talend.sdk.component.junit.base.junit5.TemporaryFolder;
026
027@Deprecated // part of jupiter 5.4 now
028public class TemporaryFolderExtension implements BeforeAllCallback, AfterAllCallback, JUnit5InjectionSupport {
029
030    private static final ExtensionContext.Namespace NAMESPACE =
031            ExtensionContext.Namespace.create(TemporaryFolderExtension.class.getName());
032
033    @Override
034    public void beforeAll(final ExtensionContext context) throws Exception {
035        final TemporaryFolder temporaryFolder = new TemporaryFolder();
036        context.getStore(NAMESPACE).put(TemporaryFolder.class.getName(), temporaryFolder);
037        temporaryFolder.create();
038    }
039
040    @Override
041    public void afterAll(final ExtensionContext context) {
042        TemporaryFolder.class.cast(context.getStore(NAMESPACE).get(TemporaryFolder.class.getName())).delete();
043    }
044
045    @Override
046    public boolean supports(final Class<?> type) {
047        return TemporaryFolder.class == type;
048    }
049
050    @Override
051    public Object findInstance(final ExtensionContext extensionContext, final Class<?> type) {
052        return extensionContext.getStore(NAMESPACE).get(TemporaryFolder.class.getName());
053    }
054
055    @Override
056    public Class<? extends Annotation> injectionMarker() {
057        return TempFolder.class;
058    }
059}