1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5 * https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 package org.eluder.coveralls.maven.plugin.util;
26
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.util.Arrays;
31 import java.util.Iterator;
32
33 import org.junit.jupiter.api.Assertions;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.io.CleanupMode;
36 import org.junit.jupiter.api.io.TempDir;
37
38 /**
39 * The Class ExistingFilesTest.
40 */
41 class ExistingFilesTest {
42
43 /** The folder. */
44 @TempDir(cleanup = CleanupMode.ON_SUCCESS)
45 public Path folder;
46
47 /**
48 * Adds the all for null.
49 */
50 @Test
51 void addAllForNull() {
52 final var existingFiles = new ExistingFiles();
53 Assertions.assertThrows(NullPointerException.class, () -> existingFiles.addAll(null));
54 }
55
56 /**
57 * Adds the for null.
58 */
59 @Test
60 void addForNull() {
61 final var existingFiles = new ExistingFiles();
62 Assertions.assertThrows(NullPointerException.class, () -> existingFiles.add(null));
63 }
64
65 /**
66 * Adds the for existing.
67 *
68 * @throws IOException
69 * Signals that an I/O exception has occurred.
70 */
71 @Test
72 void addForExisting() throws IOException {
73 final var f = Files.createFile(this.folder.resolve("f")).toFile();
74 final var iter = new ExistingFiles().add(f).add(f).iterator();
75 ExistingFilesTest.assertSize(iter, 1);
76 }
77
78 /**
79 * Adds the for directory.
80 *
81 * @throws IOException
82 * Signals that an I/O exception has occurred.
83 */
84 @Test
85 void addForDirectory() throws IOException {
86 final var d = Files.createDirectory(this.folder.resolve("d")).toFile();
87 final var iter = new ExistingFiles().add(d).iterator();
88 ExistingFilesTest.assertSize(iter, 0);
89 }
90
91 /**
92 * Creates the for null.
93 */
94 @Test
95 void createForNull() {
96 final var iter = ExistingFiles.create(null).iterator();
97 ExistingFilesTest.assertSize(iter, 0);
98 }
99
100 /**
101 * Creates the for multiple files.
102 *
103 * @throws IOException
104 * Signals that an I/O exception has occurred.
105 */
106 @Test
107 void createForMultipleFiles() throws IOException {
108 final var f1 = Files.createFile(this.folder.resolve("f1")).toFile();
109 final var f2 = Files.createFile(this.folder.resolve("f2")).toFile();
110 final var iter = ExistingFiles.create(Arrays.asList(f1, f2)).iterator();
111 ExistingFilesTest.assertSize(iter, 2);
112 }
113
114 /**
115 * Assert size.
116 *
117 * @param iter
118 * the iter
119 * @param size
120 * the size
121 */
122 private static void assertSize(Iterator<?> iter, int size) {
123 for (var i = 0; i < size; i++) {
124 iter.next();
125 }
126 Assertions.assertFalse(iter.hasNext());
127 }
128
129 }