|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p/> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p/> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.tez.analyzer; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.FileAlreadyExistsException; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
| 27 | + |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class TestCSVResult { |
| 34 | + |
| 35 | + private Path testDir; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUp() throws IOException { |
| 39 | + testDir = Paths.get(System.getProperty("user.dir") + "/test"); |
| 40 | + Files.createDirectories(testDir); |
| 41 | + } |
| 42 | + |
| 43 | + @After |
| 44 | + public void tearDown() throws IOException { |
| 45 | + if (Files.exists(testDir)) { |
| 46 | + Files.walk(testDir) |
| 47 | + .sorted(java.util.Comparator.reverseOrder()) |
| 48 | + .forEach(path -> path.toFile().delete()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testDumpToFileWritesContent() throws Exception { |
| 54 | + Path dir = Files.createDirectories(testDir.resolve("test1")); |
| 55 | + Path out = dir.resolve("out.csv"); |
| 56 | + |
| 57 | + CSVResult result = new CSVResult(new String[]{"h1", "h2"}); |
| 58 | + result.addRecord(new String[]{"a", "b"}); |
| 59 | + result.dumpToFile(out.toString()); |
| 60 | + |
| 61 | + String content = Files.readString(out, StandardCharsets.UTF_8); |
| 62 | + Assert.assertEquals("h1,h2\na,b\n", content); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testDumpToFileRejectsExistingFile() throws Exception { |
| 67 | + Path out = testDir.resolve("existing.csv"); |
| 68 | + Files.createFile(out); |
| 69 | + |
| 70 | + CSVResult result = new CSVResult(new String[]{"x"}); |
| 71 | + |
| 72 | + try { |
| 73 | + result.dumpToFile(out.toString()); |
| 74 | + Assert.fail("Expected FileAlreadyExistsException when output file already exists"); |
| 75 | + } catch (FileAlreadyExistsException e) { |
| 76 | + Assert.assertTrue(e.getMessage().contains(out.toString())); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testDumpToFileRejectsMissingParentDir() throws Exception { |
| 82 | + Path missingParent = testDir.resolve("no-such-dir"); |
| 83 | + Path out = missingParent.resolve("out.csv"); |
| 84 | + |
| 85 | + CSVResult result = new CSVResult(new String[]{"x"}); |
| 86 | + |
| 87 | + try { |
| 88 | + result.dumpToFile(out.toString()); |
| 89 | + Assert.fail("Expected IOException when parent directory does not exist"); |
| 90 | + } catch (IOException e) { |
| 91 | + Assert.assertTrue(e.getMessage().contains("Parent directory does not exist")); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test(expected = IllegalArgumentException.class) |
| 96 | + public void testDumpToFileRejectsNullFileName() throws Exception { |
| 97 | + new CSVResult(new String[]{"x"}).dumpToFile(null); |
| 98 | + } |
| 99 | + |
| 100 | + @Test(expected = IllegalArgumentException.class) |
| 101 | + public void testDumpToFileRejectsEmptyFileName() throws Exception { |
| 102 | + new CSVResult(new String[]{"x"}).dumpToFile(""); |
| 103 | + } |
| 104 | + |
| 105 | + @Test(expected = IllegalArgumentException.class) |
| 106 | + public void testDumpToFileRejectsBlankFileName() throws Exception { |
| 107 | + new CSVResult(new String[]{"x"}).dumpToFile(" "); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testDumpToFileNestedDirectory() throws Exception { |
| 112 | + Path nested = testDir.resolve("a").resolve("b"); |
| 113 | + Files.createDirectories(nested); |
| 114 | + |
| 115 | + Path out = nested.resolve("nested.csv"); |
| 116 | + |
| 117 | + CSVResult result = new CSVResult(new String[]{"h"}); |
| 118 | + result.addRecord(new String[]{"r"}); |
| 119 | + result.dumpToFile(out.toString()); |
| 120 | + |
| 121 | + Assert.assertEquals("h\nr\n", Files.readString(out, StandardCharsets.UTF_8)); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testDumpToFileNoWriteUpwardDirPath() throws Exception { |
| 126 | + String relativePath = testDir + "/../../out.csv"; |
| 127 | + CSVResult result = new CSVResult(new String[]{"h"}); |
| 128 | + |
| 129 | + try { |
| 130 | + result.dumpToFile(relativePath); |
| 131 | + Assert.fail("Expected IOException due to moving upwards from pwd"); |
| 132 | + } catch (IOException e) { |
| 133 | + Assert.assertTrue(e.getMessage().contains(Paths.get(relativePath).toAbsolutePath().normalize().toString())); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments