package jnpf.fileStorage.dms; import jnpf.dms.fonts.DmsWatermarkFonts; import jnpf.exception.DataException; import org.junit.Assume; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import java.awt.Font; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Locale; import static org.junit.Assert.*; public class DmsWatermarkFontsTest { @Rule public TemporaryFolder temporary = new TemporaryFolder(); @Test public void unsupportedAndCorruptFilesNeverBecomeOptionsOrSystemFallbacks() throws Exception { Files.writeString(temporary.newFile("old.fon").toPath(), "legacy bitmap font"); Files.writeString(temporary.newFile("invalid.ttf").toPath(), "not a font"); DmsWatermarkFonts fonts = new DmsWatermarkFonts(temporary.getRoot().getAbsolutePath()); assertTrue(fonts.options().isEmpty()); assertThrows(DataException.class, () -> fonts.choose("SansSerif", "ABC", 24)); } @Test public void missingDirectoryHasAnExplicitError() { DmsWatermarkFonts fonts = new DmsWatermarkFonts(temporary.getRoot() + "/missing"); assertThrows(DataException.class, fonts::options); } @Test public void physicalFontNamesAndLocalizedAliasesComeFromTheConfiguredFile() throws Exception { Path source = List.of(Path.of("/System/Library/Fonts/Supplemental/Arial.ttf"), Path.of("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"), Path.of("C:/Windows/Fonts/arial.ttf")) .stream().filter(Files::isRegularFile).findFirst().orElse(null); Assume.assumeNotNull(source); Files.copy(source, temporary.getRoot().toPath().resolve("renamed.TTF")); Font physical = Font.createFonts(source.toFile())[0]; DmsWatermarkFonts fonts = new DmsWatermarkFonts(temporary.getRoot().getAbsolutePath()); assertEquals(1, fonts.options().size()); assertEquals(physical.getFamily(Locale.ENGLISH), fonts.options().get(0).family()); assertEquals(physical.getFamily(Locale.SIMPLIFIED_CHINESE), fonts.options().get(0).displayName()); Font chosen = fonts.choose(physical.getFamily(Locale.SIMPLIFIED_CHINESE), "Watermark", 28); assertEquals(physical.getFontName(Locale.ENGLISH), chosen.getFontName(Locale.ENGLISH)); assertEquals(28F, chosen.getSize2D(), 0F); assertThrows(DataException.class, () -> fonts.choose("Font not in directory", "Watermark", 28)); } @Test public void collectionsAreLoadedByFamilyRatherThanFileName() throws Exception { Path source = Path.of("/System/Library/Fonts/Helvetica.ttc"); Assume.assumeTrue(Files.isRegularFile(source)); Files.copy(source, temporary.getRoot().toPath().resolve("collection.ttc")); DmsWatermarkFonts fonts = new DmsWatermarkFonts(temporary.getRoot().getAbsolutePath()); assertFalse(fonts.options().isEmpty()); for (DmsWatermarkFonts.Option option : fonts.options()) { assertEquals(option.family(), fonts.choose(option.family(), "ABC", 24).getFamily(Locale.ENGLISH)); } } }