1 package com.github.mikkoi.maven.plugins.enforcer.rule.propertyusage.configuration; 2 3 import org.apache.maven.plugin.logging.SystemStreamLog; 4 import org.junit.Rule; 5 import org.junit.Test; 6 import org.junit.rules.TemporaryFolder; 7 8 import java.io.File; 9 import java.nio.file.Paths; 10 import java.util.Arrays; 11 import java.util.Collection; 12 import java.util.Collections; 13 import java.util.stream.Collectors; 14 15 import static com.github.mikkoi.maven.plugins.enforcer.rule.propertyusage.configuration.FileSpecs.absoluteCwdAndFile; 16 import static org.junit.Assert.assertEquals; 17 18 public class FileSpecsTest { 19 20 @Rule 21 public TemporaryFolder testDir = new TemporaryFolder(); 22 23 @Test 24 public void getFilenamesFromFileSpecs() throws Exception { 25 SystemStreamLog slog = new SystemStreamLog(); 26 File tempFile = testDir.newFile(); 27 Collection<String> files = Arrays.asList( 28 "src/test/resources/properties-dir/**/*.properties", 29 "src/test/resources/app1.properties", 30 tempFile.getAbsolutePath() 31 ); 32 Collection<String> expected = Arrays.asList( 33 absoluteCwdAndFile("src/test/resources/properties-dir/sub-app-props1.properties"), 34 absoluteCwdAndFile("src/test/resources/properties-dir/sub-app-props2.properties"), 35 absoluteCwdAndFile("src/test/resources/app1.properties"), 36 tempFile.getAbsolutePath() 37 ); 38 expected = expected.stream().sorted().collect(Collectors.toSet()); 39 slog.debug("current dir:"+ Paths.get("").toAbsolutePath().toString()); 40 Collection<String> filenames = FileSpecs.getAbsoluteFilenames(files, Paths.get("").toAbsolutePath(), slog); 41 filenames = filenames.stream().sorted().collect(Collectors.toSet()); 42 assertEquals("Filenames are as expected.", expected, filenames); 43 } 44 45 @Test 46 public void getFilenamesFromFileSpecs_noFiles() throws Exception { 47 SystemStreamLog slog = new SystemStreamLog(); 48 Collection<String> files = Arrays.asList( 49 absoluteCwdAndFile("src/test/resources/no-exist-dir/*.properties"), 50 absoluteCwdAndFile("src/test/resources/no-way.properties") 51 ); 52 Collection<String> expected = Collections.emptySet(); 53 expected = expected.stream().sorted().collect(Collectors.toSet()); 54 Collection<String> filenames = FileSpecs.getAbsoluteFilenames(files, Paths.get("").toAbsolutePath(), slog); 55 filenames = filenames.stream().sorted().collect(Collectors.toSet()); 56 assertEquals("Filenames are as expected (none).", expected, filenames); 57 } 58 }