|
| 1 | +package com.metaobjects.database; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonParser; |
| 6 | +import com.metaobjects.MetaDataException; |
| 7 | +import com.metaobjects.config.NeutralConfig; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.Comparator; |
| 15 | +import java.util.List; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertFalse; |
| 20 | +import static org.junit.Assert.assertNull; |
| 21 | +import static org.junit.Assert.assertThrows; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | + |
| 24 | +/** |
| 25 | + * {@code naming.collections} names the pluralizer an object without {@code @collectionName} |
| 26 | + * takes its collection name from. Java generates nothing from it yet, but a project shared with |
| 27 | + * a port that does carries the key, so Java reads it, refuses an unknown name like |
| 28 | + * {@code columns} and {@code tables}, and keeps the selected name on the loaded naming. |
| 29 | + */ |
| 30 | +public class CollectionNamingTest { |
| 31 | + |
| 32 | + @After |
| 33 | + public void reset() { |
| 34 | + NamingStrategies.resetForTesting(); |
| 35 | + } |
| 36 | + |
| 37 | + private static PhysicalNaming readNaming(String naming) throws IOException { |
| 38 | + Path dir = Files.createTempDirectory("mo-collection-naming-"); |
| 39 | + try { |
| 40 | + Files.createDirectory(dir.resolve(".metaobjects")); |
| 41 | + Files.writeString(dir.resolve(".metaobjects").resolve("config.json"), |
| 42 | + "{ \"schema_version\": 1, \"naming\": " + naming + " }"); |
| 43 | + return NeutralConfig.read(dir).orElseThrow().getPhysicalNaming(); |
| 44 | + } finally { |
| 45 | + try (Stream<Path> paths = Files.walk(dir)) { |
| 46 | + paths.sorted(Comparator.reverseOrder()).forEach(p -> p.toFile().delete()); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void theBuiltInsPluralizeOrKeepTheName() { |
| 53 | + assertEquals(List.of("standard", "none"), NamingStrategies.BUILT_IN_PLURALIZERS); |
| 54 | + assertEquals("standard", NamingStrategies.DEFAULT_PLURALIZER); |
| 55 | + assertEquals("CaseHistories", NamingStrategies.pluralizer("standard").apply("CaseHistory")); |
| 56 | + assertEquals("CaseHistory", NamingStrategies.pluralizer("none").apply("CaseHistory")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void standardMatchesTheSharedNamingConformanceCorpus() throws IOException { |
| 61 | + Path repoRoot = Path.of(System.getProperty("user.dir")).resolve("../../..").normalize(); |
| 62 | + Path fixture = repoRoot.resolve("fixtures/naming-conformance/already-plural-pluralize.json"); |
| 63 | + JsonArray cases = JsonParser.parseString(Files.readString(fixture)).getAsJsonObject().getAsJsonArray("cases"); |
| 64 | + assertFalse(cases.isEmpty()); |
| 65 | + for (int i = 0; i < cases.size(); i++) { |
| 66 | + JsonObject c = cases.get(i).getAsJsonObject(); |
| 67 | + String name = c.get("name").getAsString(); |
| 68 | + assertEquals(name, c.get("apiPlural").getAsString(), NamingStrategies.standardPluralize(name)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void theConfigReadsABuiltIn() throws IOException { |
| 74 | + assertEquals("none", readNaming("{ \"collections\": \"none\" }").collections()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void absentNamesNoPluralizer() throws IOException { |
| 79 | + assertNull(readNaming("{ \"columns\": \"snake_case\" }").collections()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void anUnknownPluralizerIsRefusedNamingTheKind() { |
| 84 | + MetaDataException ex = assertThrows(MetaDataException.class, |
| 85 | + () -> readNaming("{ \"collections\": \"latin\" }")); |
| 86 | + assertTrue(ex.getMessage(), ex.getMessage().contains("\"naming.collections\" names \"latin\"")); |
| 87 | + assertTrue(ex.getMessage(), ex.getMessage().contains("Java port")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void anApplicationPluralizerIsSelectable() throws IOException { |
| 92 | + NamingStrategies.registerPluralizer("java-test-shout", n -> n.toUpperCase() + "S"); |
| 93 | + assertTrue(NamingStrategies.registeredPluralizers().contains("java-test-shout")); |
| 94 | + assertEquals("java-test-shout", readNaming("{ \"collections\": \"java-test-shout\" }").collections()); |
| 95 | + NamingStrategies.resetForTesting(); |
| 96 | + assertFalse(NamingStrategies.isPluralizer("java-test-shout")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void aBuiltInPluralizerCannotBeReplaced() { |
| 101 | + assertThrows(IllegalArgumentException.class, () -> NamingStrategies.registerPluralizer("none", n -> n)); |
| 102 | + assertThrows(IllegalArgumentException.class, () -> NamingStrategies.registerPluralizer("standard", n -> n)); |
| 103 | + } |
| 104 | +} |
0 commit comments