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; 017 018import static java.util.Optional.ofNullable; 019 020import java.io.File; 021import java.io.IOException; 022import java.util.stream.Stream; 023 024// backport the junit4 rule of the same name to junit 5 025@Deprecated // part of jupiter 5.4 now 026public class TemporaryFolder { 027 028 private File folder; 029 030 public void create() throws IOException { 031 folder = createTemporaryFolderIn(null); 032 } 033 034 public File newFile(final String fileName) throws IOException { 035 final File file = new File(getRoot(), fileName); 036 if (!file.createNewFile()) { 037 throw new IOException("a file with the name \'" + fileName + "\' already exists in the test folder"); 038 } 039 return file; 040 } 041 042 public File newFile() throws IOException { 043 return File.createTempFile("talendjunit", null, getRoot()); 044 } 045 046 public File newFolder(final String... folderNames) throws IOException { 047 File file = getRoot(); 048 for (int i = 0; i < folderNames.length; i++) { 049 String folderName = folderNames[i]; 050 validateFolderName(folderName); 051 file = new File(file, folderName); 052 if (!file.mkdir() && isLastElementInArray(i, folderNames)) { 053 throw new IOException("a folder with the name \'" + folderName + "\' already exists"); 054 } 055 } 056 return file; 057 } 058 059 private void validateFolderName(final String folderName) throws IOException { 060 File tempFile = new File(folderName); 061 if (tempFile.getParent() != null) { 062 String errorMsg = "Folder name cannot consist of multiple path components separated by a file separator." 063 + " Please use newFolder('MyParentFolder','MyFolder') to create hierarchies of folders"; 064 throw new IOException(errorMsg); 065 } 066 } 067 068 private boolean isLastElementInArray(final int index, final String[] array) { 069 return index == array.length - 1; 070 } 071 072 public File newFolder() throws IOException { 073 return createTemporaryFolderIn(getRoot()); 074 } 075 076 private File createTemporaryFolderIn(final File parentFolder) throws IOException { 077 final File createdFolder = File.createTempFile("junit", "", parentFolder); 078 createdFolder.delete(); 079 createdFolder.mkdir(); 080 return createdFolder; 081 } 082 083 public File getRoot() { 084 if (folder == null) { 085 throw new IllegalStateException("the temporary folder has not yet been created"); 086 } 087 return folder; 088 } 089 090 public void delete() { 091 ofNullable(folder).ifPresent(this::recursiveDelete); 092 } 093 094 private void recursiveDelete(final File file) { 095 final File[] files = file.listFiles(); 096 if (files != null) { 097 Stream.of(files).forEach(this::recursiveDelete); 098 } 099 file.delete(); 100 } 101}