From 689ed6b3b786fa48ee55bff02c2f150267ffdc41 Mon Sep 17 00:00:00 2001 From: Miroslav Pokorny Date: Mon, 6 Jan 2025 14:29:02 +1100 Subject: [PATCH] SpreadsheetMetadataEnvironmentContext ignores wrapped EnvironmentContext values --- .../spreadsheet/meta/SpreadsheetMetadata.java | 23 ++------- ...SpreadsheetMetadataEnvironmentContext.java | 16 +++++-- ...adsheetMetadataEnvironmentContextTest.java | 48 +++++++++++++++++-- 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/main/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadata.java b/src/main/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadata.java index 9acdc65fb..50ee4371a 100644 --- a/src/main/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadata.java +++ b/src/main/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadata.java @@ -36,7 +36,6 @@ import walkingkooka.datetime.DateTimeContexts; import walkingkooka.datetime.HasNow; import walkingkooka.environment.EnvironmentContext; -import walkingkooka.environment.EnvironmentContexts; import walkingkooka.locale.HasLocale; import walkingkooka.math.DecimalNumberContext; import walkingkooka.math.HasDecimalNumberContext; @@ -611,28 +610,16 @@ final DecimalNumberContext decimalNumberContext0() { // EnvironmentContext............................................................................................... /** - * Getter that returns a {@link EnvironmentContext} view o this {@link SpreadsheetMetadata} combined with the given {@link EnvironmentContext}. + * Getter that returns a {@link EnvironmentContext} view o this {@link SpreadsheetMetadata} using the given {@link EnvironmentContext} + * for the time and user. */ public final EnvironmentContext environmentContext(final EnvironmentContext context) { - Objects.requireNonNull(context, "context"); - - if (null == this.environmentContext) { - this.environmentContext = SpreadsheetMetadataEnvironmentContext.with(this); - } - return EnvironmentContexts.collection( - Lists.of( - this.environmentContext, - context - ), - context + return SpreadsheetMetadataEnvironmentContext.with( + this, + context ); } - /** - * Cached {@link SpreadsheetMetadataEnvironmentContext}. - */ - private EnvironmentContext environmentContext; - // ExpressionFunctionProvider....................................................................................... /** diff --git a/src/main/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadataEnvironmentContext.java b/src/main/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadataEnvironmentContext.java index 4576ddabd..0c925298e 100644 --- a/src/main/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadataEnvironmentContext.java +++ b/src/main/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadataEnvironmentContext.java @@ -36,14 +36,18 @@ */ final class SpreadsheetMetadataEnvironmentContext implements EnvironmentContext { - static SpreadsheetMetadataEnvironmentContext with(final SpreadsheetMetadata metadata) { + static SpreadsheetMetadataEnvironmentContext with(final SpreadsheetMetadata metadata, + final EnvironmentContext context) { return new SpreadsheetMetadataEnvironmentContext( - Objects.requireNonNull(metadata, "metadata") + Objects.requireNonNull(metadata, "metadata"), + Objects.requireNonNull(context, "context") ); } - private SpreadsheetMetadataEnvironmentContext(final SpreadsheetMetadata metadata) { + private SpreadsheetMetadataEnvironmentContext(final SpreadsheetMetadata metadata, + final EnvironmentContext context) { this.metadata = metadata; + this.context = context; } @Override @@ -98,14 +102,16 @@ public Set> environmentValueNames() { @Override public LocalDateTime now() { - throw new UnsupportedOperationException(); + return this.context.now(); } @Override public Optional user() { - throw new UnsupportedOperationException(); + return this.context.user(); } + private final EnvironmentContext context; + @Override public String toString() { return this.metadata.toString(); diff --git a/src/test/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadataEnvironmentContextTest.java b/src/test/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadataEnvironmentContextTest.java index daab6b3b5..89189e994 100644 --- a/src/test/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadataEnvironmentContextTest.java +++ b/src/test/java/walkingkooka/spreadsheet/meta/SpreadsheetMetadataEnvironmentContextTest.java @@ -23,23 +23,54 @@ import walkingkooka.environment.EnvironmentContextTesting2; import walkingkooka.environment.EnvironmentContexts; import walkingkooka.environment.EnvironmentValueName; +import walkingkooka.net.email.EmailAddress; import walkingkooka.spreadsheet.SpreadsheetId; import walkingkooka.spreadsheet.SpreadsheetName; import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.Locale; +import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertThrows; public final class SpreadsheetMetadataEnvironmentContextTest implements EnvironmentContextTesting2, ToStringTesting { + private final static LocalDateTime NOW = LocalDateTime.of( + 1999, + 12, + 31, + 12, + 58 + ); + + private final static EmailAddress USER = EmailAddress.parse("user@example.com"); + + private final static EnvironmentContext CONTEXT = EnvironmentContexts.empty( + () -> NOW, + Optional.of(USER) + ); + @Test - public void testWithNullFails() { + public void testWithNullMetadataFails() { assertThrows( NullPointerException.class, - () -> SpreadsheetMetadataEnvironmentContext.with(null) + () -> SpreadsheetMetadataEnvironmentContext.with( + null, + CONTEXT + ) + ); + } + + @Test + public void testWithNullEnvironmentContextFails() { + assertThrows( + NullPointerException.class, + () -> SpreadsheetMetadataEnvironmentContext.with( + SpreadsheetMetadata.EMPTY, + null + ) ); } @@ -97,13 +128,22 @@ public void testEnvironmentValueNames() { @Override public void testUser() { - throw new UnsupportedOperationException(); + this.userAndCheck(USER); + } + + @Test + public void testNow() { + this.checkEquals( + this.createContext().now(), + NOW + ); } @Override public SpreadsheetMetadataEnvironmentContext createContext() { return SpreadsheetMetadataEnvironmentContext.with( - SpreadsheetMetadataTesting.METADATA_EN_AU + SpreadsheetMetadataTesting.METADATA_EN_AU, + CONTEXT ); }