001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.utils.internal;
021
022import org.apache.commons.lang.StringUtils;
023import org.junit.rules.ExternalResource;
024import org.junit.rules.TemporaryFolder;
025import org.junit.runner.Description;
026import org.junit.runners.model.Statement;
027import org.sonar.api.utils.TempFolder;
028
029import javax.annotation.Nullable;
030
031import java.io.File;
032import java.io.IOException;
033
034/**
035 * Implementation of {@link org.sonar.api.utils.TempFolder} to be used
036 * only in JUnit tests. It wraps {@link org.junit.rules.TemporaryFolder}.
037 * <br>
038 * Example:
039 * <pre>
040 * public class MyTest {
041 *   &#064;@org.junit.Rule
042 *   public JUnitTempFolder temp = new JUnitTempFolder();
043 *
044 *   &#064;@org.junit.Test
045 *   public void myTest() throws Exception {
046 *     File dir = temp.newDir();
047 *     // ...
048 *   }
049 * }
050 * </pre>
051 *
052 * @since 5.1
053 */
054public class JUnitTempFolder extends ExternalResource implements TempFolder {
055
056  private final TemporaryFolder junit = new TemporaryFolder();
057
058  @Override
059  public Statement apply(Statement base, Description description) {
060    return junit.apply(base, description);
061  }
062
063  @Override
064  protected void before() throws Throwable {
065    junit.create();
066  }
067
068  @Override
069  protected void after() {
070    junit.delete();
071  }
072
073  @Override
074  public File newDir() {
075    try {
076      return junit.newFolder();
077    } catch (IOException e) {
078      throw new IllegalStateException("Fail to create temp dir", e);
079    }
080  }
081
082  @Override
083  public File newDir(String name) {
084    try {
085      return junit.newFolder(name);
086    } catch (IOException e) {
087      throw new IllegalStateException("Fail to create temp dir", e);
088    }
089  }
090
091  @Override
092  public File newFile() {
093    try {
094      return junit.newFile();
095    } catch (IOException e) {
096      throw new IllegalStateException("Fail to create temp file", e);
097    }
098  }
099
100  @Override
101  public File newFile(@Nullable String prefix, @Nullable String suffix) {
102    try {
103      return junit.newFile(StringUtils.defaultString(prefix) + "-" + StringUtils.defaultString(suffix));
104    } catch (IOException e) {
105      throw new IllegalStateException("Fail to create temp file", e);
106    }
107  }
108}