|
| 1 | +package org.jfrog.build.api.util; |
| 2 | + |
| 3 | +import org.apache.commons.lang.SystemUtils; |
| 4 | +import org.apache.commons.lang.exception.ExceptionUtils; |
| 5 | +import org.testng.annotations.AfterMethod; |
| 6 | +import org.testng.annotations.Test; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | + |
| 12 | +import static org.jfrog.build.api.util.CommonUtils.handleJavaTmpdirProperty; |
| 13 | +import static org.testng.Assert.assertEquals; |
| 14 | +import static org.testng.Assert.assertThrows; |
| 15 | +import static org.testng.FileAssert.fail; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author yahavi |
| 19 | + **/ |
| 20 | +@Test |
| 21 | +public class CreateTempDirTests { |
| 22 | + |
| 23 | + private static final String JAVA_IO_TMPDIR_PROP = "java.io.tmpdir"; |
| 24 | + private static final String OLD_JAVA_IO_TMPDIR = System.getProperty(JAVA_IO_TMPDIR_PROP); |
| 25 | + |
| 26 | + @AfterMethod |
| 27 | + public void setUp() { |
| 28 | + System.setProperty(JAVA_IO_TMPDIR_PROP, OLD_JAVA_IO_TMPDIR); |
| 29 | + } |
| 30 | + |
| 31 | + public void createTempDirPropertyExist() { |
| 32 | + String oldJavaIoTmpdir = System.getProperty(JAVA_IO_TMPDIR_PROP); |
| 33 | + try { |
| 34 | + handleJavaTmpdirProperty(); |
| 35 | + assertEquals(System.getProperty(JAVA_IO_TMPDIR_PROP), oldJavaIoTmpdir); |
| 36 | + } catch (RuntimeException e) { |
| 37 | + fail(ExceptionUtils.getRootCauseMessage(e)); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public void createTempDirMissingProperty() { |
| 42 | + System.setProperty(JAVA_IO_TMPDIR_PROP, ""); |
| 43 | + if (SystemUtils.IS_OS_WINDOWS) { |
| 44 | + assertThrows(RuntimeException.class, CommonUtils::handleJavaTmpdirProperty); |
| 45 | + return; |
| 46 | + } |
| 47 | + try { |
| 48 | + handleJavaTmpdirProperty(); |
| 49 | + assertEquals(System.getProperty(JAVA_IO_TMPDIR_PROP), "/tmp"); |
| 50 | + } catch (RuntimeException e) { |
| 51 | + fail(ExceptionUtils.getRootCauseMessage(e)); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public void createTempDirMissingDir() { |
| 56 | + Path currentDir = Paths.get("not-exist-dir").toAbsolutePath(); |
| 57 | + System.setProperty(JAVA_IO_TMPDIR_PROP, currentDir.toString()); |
| 58 | + try { |
| 59 | + handleJavaTmpdirProperty(); |
| 60 | + assertEquals(System.getProperty(JAVA_IO_TMPDIR_PROP), currentDir.toString()); |
| 61 | + } catch (RuntimeException e) { |
| 62 | + fail(ExceptionUtils.getRootCauseMessage(e)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments