|
| 1 | +package com.metaobjects.mojo; |
| 2 | + |
| 3 | +import com.metaobjects.MetaDataException; |
| 4 | +import com.metaobjects.loader.MetaDataLoader; |
| 5 | +import org.apache.maven.project.MavenProject; |
| 6 | +import org.junit.Test; |
| 7 | +import org.mockito.Mockito; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.Comparator; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +import static org.junit.Assert.assertTrue; |
| 19 | +import static org.junit.Assert.fail; |
| 20 | + |
| 21 | +/** |
| 22 | + * A {@code <loader>} naming {@code <sourceDir>} and NOTHING else must load that |
| 23 | + * directory's metadata. |
| 24 | + * |
| 25 | + * <p>It used to load nothing at all — and say nothing about it. The precedence |
| 26 | + * ladder consults the port-neutral config only when the pom names NEITHER |
| 27 | + * {@code <sourceDir>} nor {@code <sources>}, so naming {@code <sourceDir>} alone |
| 28 | + * took the pom-owns-the-concern branch with an empty source list: the generators |
| 29 | + * ran against an empty model, wrote zero files, and the build reported SUCCESS. |
| 30 | + * |
| 31 | + * <p>Which matters because {@code <sourceDir>} alone is exactly what the shipped |
| 32 | + * adopter guidance teaches, and it is also the remedy 0.24.0's own |
| 33 | + * {@code ERR_COLLECTION_NOT_FOUND} prints ("declare {@code <sourceDir>}/{@code |
| 34 | + * <sources>} explicitly") — so following the fix for the silent-empty model put |
| 35 | + * you straight back into a silent-empty model. |
| 36 | + * |
| 37 | + * <p>Two neighbouring cases are pinned alongside it, because the fix makes this |
| 38 | + * branch tolerant and both are ways it could go quiet again: a {@code <sourceDir>} |
| 39 | + * that does not exist (already loud, an {@code IllegalArgumentException} naming the |
| 40 | + * path), and one that exists but holds no metadata at all. |
| 41 | + * |
| 42 | + * <p>Paths here are ABSOLUTE on purpose — {@code <sourceDir>} resolves against the |
| 43 | + * process working directory, not the module basedir, so a relative path in a test |
| 44 | + * would be measuring the runner's cwd rather than the mojo. |
| 45 | + */ |
| 46 | +public class SourceDirWithoutSourcesTest { |
| 47 | + |
| 48 | + private static final String WIDGET_JSON = """ |
| 49 | + { |
| 50 | + "metadata.root": { |
| 51 | + "package": "sourcedir::only", |
| 52 | + "children": [ |
| 53 | + { "object.entity": { "name": "Widget", "children": [ |
| 54 | + { "field.long": { "name": "id" } } |
| 55 | + ] } } |
| 56 | + ] |
| 57 | + } |
| 58 | + } |
| 59 | + """; |
| 60 | + |
| 61 | + /** A pom naming <sourceDir> and no <sources> — the documented shape. */ |
| 62 | + private MetaDataGeneratorMojo mojoWithSourceDirOnly(Path basedir, String sourceDir) { |
| 63 | + MavenProject mavenProject = Mockito.mock(MavenProject.class); |
| 64 | + Mockito.when(mavenProject.getBasedir()).thenReturn(basedir.toFile()); |
| 65 | + |
| 66 | + MetaDataGeneratorMojo mojo = new MetaDataGeneratorMojo(); |
| 67 | + mojo.project = mavenProject; |
| 68 | + mojo.setLoader(LoaderParam.builder("sourcedir-only-test") |
| 69 | + .withClassname("com.metaobjects.loader.MetaDataLoader") |
| 70 | + .withSourceDir(sourceDir) |
| 71 | + .build()); |
| 72 | + mojo.setGenerators(Collections.emptyList()); |
| 73 | + mojo.setGlobals(Collections.emptyMap()); |
| 74 | + return mojo; |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void sourceDirAloneLoadsThatDirectorysMetadata() throws IOException { |
| 79 | + Path root = Files.createTempDirectory("mo-mojo-srcdir-").toAbsolutePath().normalize(); |
| 80 | + try { |
| 81 | + Path metaDir = root.resolve("src/main/metaobjects"); |
| 82 | + Files.createDirectories(metaDir); |
| 83 | + Files.write(metaDir.resolve("meta.widget.json"), WIDGET_JSON.getBytes(StandardCharsets.UTF_8)); |
| 84 | + |
| 85 | + MetaDataGeneratorMojo mojo = mojoWithSourceDirOnly(root, metaDir.toString()); |
| 86 | + MetaDataLoader loaded = mojo.createLoader(mojo.createProjectClassLoader()); |
| 87 | + |
| 88 | + assertEquals("a <sourceDir> with no <sources> must load the directory, not nothing", |
| 89 | + 1, loaded.getMetaObjects().size()); |
| 90 | + assertEquals("Widget", loaded.getMetaObjects().get(0).getShortName()); |
| 91 | + } finally { |
| 92 | + deleteRecursive(root); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void aSourceDirThatDoesNotExistFailsRatherThanLoadingNothing() throws IOException { |
| 98 | + Path root = Files.createTempDirectory("mo-mojo-srcdir-missing-").toAbsolutePath().normalize(); |
| 99 | + Path typo = root.resolve("src/main/typo"); |
| 100 | + try { |
| 101 | + MetaDataGeneratorMojo mojo = mojoWithSourceDirOnly(root, typo.toString()); |
| 102 | + try { |
| 103 | + MetaDataLoader loaded = mojo.createLoader(mojo.createProjectClassLoader()); |
| 104 | + fail("expected a failure naming the missing directory, got a loader with " |
| 105 | + + loaded.getMetaObjects().size() + " object(s)"); |
| 106 | + } catch (RuntimeException expected) { |
| 107 | + assertTrue("the message must name the path that did not resolve, got: " |
| 108 | + + expected.getMessage(), |
| 109 | + expected.getMessage().contains(typo.toString())); |
| 110 | + } |
| 111 | + } finally { |
| 112 | + deleteRecursive(root); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void aSourceDirHoldingNoMetadataFailsRatherThanLoadingNothing() throws IOException { |
| 118 | + Path root = Files.createTempDirectory("mo-mojo-srcdir-empty-").toAbsolutePath().normalize(); |
| 119 | + try { |
| 120 | + Path metaDir = root.resolve("src/main/metaobjects"); |
| 121 | + Files.createDirectories(metaDir); |
| 122 | + |
| 123 | + MetaDataGeneratorMojo mojo = mojoWithSourceDirOnly(root, metaDir.toString()); |
| 124 | + try { |
| 125 | + MetaDataLoader loaded = mojo.createLoader(mojo.createProjectClassLoader()); |
| 126 | + fail("expected a failure naming the empty directory, got a loader with " |
| 127 | + + loaded.getMetaObjects().size() + " object(s)"); |
| 128 | + } catch (MetaDataException expected) { |
| 129 | + assertTrue("the message must name the directory that held nothing, got: " |
| 130 | + + expected.getMessage(), |
| 131 | + expected.getMessage().contains(metaDir.toString())); |
| 132 | + } |
| 133 | + } finally { |
| 134 | + deleteRecursive(root); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static void deleteRecursive(Path root) throws IOException { |
| 139 | + if (!Files.exists(root)) return; |
| 140 | + try (Stream<Path> walk = Files.walk(root)) { |
| 141 | + walk.sorted(Comparator.reverseOrder()).forEach(p -> p.toFile().delete()); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments