|
| 1 | +package com.metaobjects.conformance; |
| 2 | + |
| 3 | +import com.metaobjects.MetaDataException; |
| 4 | +import com.metaobjects.loader.InMemoryStringSource; |
| 5 | +import com.metaobjects.loader.LoaderOptions; |
| 6 | +import com.metaobjects.loader.MetaDataLoader; |
| 7 | +import com.metaobjects.loader.MetaDataSource; |
| 8 | +import com.metaobjects.registry.MetaDataRegistry; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertTrue; |
| 14 | +import static org.junit.Assert.fail; |
| 15 | + |
| 16 | +/** |
| 17 | + * Proves two loaders with different type registries in one JVM validate |
| 18 | + * independently (per-loader-registry design, success-criterion #3). A loader |
| 19 | + * whose registry knows {@code template.briefing} accepts it; a loader whose |
| 20 | + * registry does not rejects it — regardless of construction order, with no |
| 21 | + * global-singleton contamination. |
| 22 | + */ |
| 23 | +public class PerLoaderRegistryTest { |
| 24 | + |
| 25 | + // Structurally-valid canonical metadata declaring a template.briefing node |
| 26 | + // (mirrors fixtures/conformance/provider-extension-new-subtype-success input: |
| 27 | + // briefing requires @payloadRef -> an object.value, plus @author/@recipient). |
| 28 | + private static final String META = |
| 29 | + "{ \"metadata.root\": { \"package\": \"examples\", \"children\": [" |
| 30 | + + " { \"object.value\": { \"name\": \"BriefingNotes\", \"children\": [" |
| 31 | + + " { \"field.string\": { \"name\": \"summary\" } }," |
| 32 | + + " { \"field.string\": { \"name\": \"context\" } }" |
| 33 | + + " ] } }," |
| 34 | + + " { \"template.briefing\": { \"name\": \"DailyBriefing\"," |
| 35 | + + " \"@payloadRef\": \"BriefingNotes\"," |
| 36 | + + " \"@author\": \"Lorekeeper\", \"@recipient\": \"Council\" } }" |
| 37 | + + "] } }"; |
| 38 | + |
| 39 | + private static MetaDataLoader newLoader(String name, MetaDataRegistry reg) { |
| 40 | + LoaderOptions opts = LoaderOptions.create(false, false, true); |
| 41 | + MetaDataLoader loader = new MetaDataLoader(opts, MetaDataLoader.SUBTYPE_MANUAL, name); |
| 42 | + loader.setTypeRegistry(reg); |
| 43 | + loader.init(); |
| 44 | + return loader; |
| 45 | + } |
| 46 | + |
| 47 | + private static void load(MetaDataLoader loader) { |
| 48 | + loader.load(List.<MetaDataSource>of(new InMemoryStringSource(META, "meta.t.json"))); |
| 49 | + } |
| 50 | + |
| 51 | + /** Registry WITH briefing -> load succeeds (must not throw). */ |
| 52 | + private static void assertAccepts(MetaDataRegistry reg) { |
| 53 | + load(newLoader("with-briefing", reg)); |
| 54 | + } |
| 55 | + |
| 56 | + /** Registry WITHOUT briefing -> load fails (unknown subtype / not accepted). */ |
| 57 | + private static void assertRejects(MetaDataRegistry reg) { |
| 58 | + try { |
| 59 | + load(newLoader("without-briefing", reg)); |
| 60 | + fail("expected load to fail: template.briefing is unknown in this registry"); |
| 61 | + } catch (MetaDataException expected) { |
| 62 | + String m = String.valueOf(expected.getMessage()); |
| 63 | + assertTrue("expected an unknown-subtype/not-accepted failure, got: " + m, |
| 64 | + m.contains("UNKNOWN_SUBTYPE") |
| 65 | + || m.contains("does not accept child") |
| 66 | + || m.contains("briefing")); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void twoLoadersValidateIndependently_withFirst() { |
| 72 | + MetaDataRegistry withBriefing = MetaDataRegistry.createWithCoreProviders(); |
| 73 | + ConformanceTestProviders.BriefingTemplate.registerTypes(withBriefing); |
| 74 | + MetaDataRegistry withoutBriefing = MetaDataRegistry.createWithCoreProviders(); |
| 75 | + |
| 76 | + assertAccepts(withBriefing); |
| 77 | + assertRejects(withoutBriefing); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void twoLoadersValidateIndependently_withoutFirst() { |
| 82 | + // Reverse order: proves no ordering dependency / no global contamination. |
| 83 | + MetaDataRegistry withoutBriefing = MetaDataRegistry.createWithCoreProviders(); |
| 84 | + MetaDataRegistry withBriefing = MetaDataRegistry.createWithCoreProviders(); |
| 85 | + ConformanceTestProviders.BriefingTemplate.registerTypes(withBriefing); |
| 86 | + |
| 87 | + assertRejects(withoutBriefing); |
| 88 | + assertAccepts(withBriefing); |
| 89 | + } |
| 90 | +} |
0 commit comments