From 71083f709896cc09eeebc3aeeff53d7b1b93e7f3 Mon Sep 17 00:00:00 2001 From: Dean Wette Date: Wed, 12 Jul 2023 02:49:54 -0500 Subject: [PATCH] Move `RewriteMapProvider` example to docs examples and add test. (#565) * Move `RewriteMapProvider` example to docs examples and add test. closes #202 * fix code smell --- .../templates/soy/soyfeaturesrenaming.adoc | 52 +-------------- .../docs/soy/RewriteMapProvider.groovy | 65 ++++++++++++++++++ .../docs/soy/RewriteMapProviderSpec.groovy | 29 ++++++++ .../test/resources/styles/renaming-map.json | 5 ++ .../micronaut/docs/soy/RewriteMapProvider.kt | 65 ++++++++++++++++++ .../docs/soy/RewriteMapProviderTest.kt | 30 +++++++++ .../test/resources/styles/renaming-map.json | 5 ++ .../docs/soy/RewriteMapProvider.java | 66 +++++++++++++++++++ .../docs/soy/RewriteMapProviderTest.java | 30 +++++++++ .../test/resources/styles/renaming-map.json | 5 ++ 10 files changed, 301 insertions(+), 51 deletions(-) create mode 100644 test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProvider.groovy create mode 100644 test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProviderSpec.groovy create mode 100644 test-suite-groovy/src/test/resources/styles/renaming-map.json create mode 100644 test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProvider.kt create mode 100644 test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProviderTest.kt create mode 100644 test-suite-kotlin/src/test/resources/styles/renaming-map.json create mode 100644 test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProvider.java create mode 100644 test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProviderTest.java create mode 100644 test-suite/src/test/resources/styles/renaming-map.json diff --git a/src/main/docs/guide/views/templates/soy/soyfeaturesrenaming.adoc b/src/main/docs/guide/views/templates/soy/soyfeaturesrenaming.adoc index 8fdce5a1b..0b85f2d0e 100644 --- a/src/main/docs/guide/views/templates/soy/soyfeaturesrenaming.adoc +++ b/src/main/docs/guide/views/templates/soy/soyfeaturesrenaming.adoc @@ -53,58 +53,8 @@ must be a string literal (variables cannot be used): --srcs [...templates...]; The last step is to provide the renaming map to Micronaut Views Soy: -```java -@Singleton -public class RewriteMapProvider implements SoyNamingMapProvider { - /** Filename for the JSON class renaming map. */ - private static final String RENAMING_MAP_NAME = "renaming-map.json"; - /** Naming map to use for classes. */ - private static SoyCssRenamingMap CSS_RENAMING_MAP = null; - - /** - * Load JSON embedded in a JAR resource into a naming map for Soy rendering. - * - * @param mapPath URL to the JAR resource we should load. - */ - @Nullable - private static Map loadMapFromJSON(URL mapPath) { - try { - return new ObjectMapper().readValue(mapPath, new TypeReference>() { }); - } catch (Throwable thr) { - //handle `JsonMappingException` and `IOException`, if, for instance, you're using Jackson - throw new RuntimeException(thr); - } - } - - static { - final URL mapPath = - SoyRenderConfigProvider.class.getClassLoader().getResource(RENAMING_MAP_NAME); - if (mapPath != null) { - // load the renaming map - final Map cssRenamingMap = loadMapFromJSON(mapPath); - if (renamingMapRaw != null) { - CSS_RENAMING_MAP = new SoyCssRenamingMap() { - @Nullable @Override - public String get(@NotNull String className) { - // (or whatever logic you need to rewrite the class) - return cssRenamingMap.get(className); - } - }; - } - } - } - - /** - * Provide a CSS renaming map to Soy/Micronaut. - * - * @return Inflated Soy CSS renaming map. - */ - @Nullable @Override public SoyCssRenamingMap cssRenamingMap() { - return CSS_RENAMING_MAP; - } -} -``` +snippet::io.micronaut.docs.soy.RewriteMapProvider[tags="clazz"] Then, your output will be renamed. Referencing the Soy template sample above, output would look something like this: ```html diff --git a/test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProvider.groovy b/test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProvider.groovy new file mode 100644 index 000000000..541e6042a --- /dev/null +++ b/test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProvider.groovy @@ -0,0 +1,65 @@ +package io.micronaut.docs.soy + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper +import com.google.template.soy.shared.SoyCssRenamingMap +import io.micronaut.context.annotation.Requires +import io.micronaut.core.annotation.Nullable +import io.micronaut.views.soy.SoyNamingMapProvider +import jakarta.inject.Singleton + +import javax.validation.constraints.NotNull + +@Requires(property = "spec.name", value = "RewriteMapProviderSpec") +//tag::clazz[] +@Singleton +class RewriteMapProvider implements SoyNamingMapProvider { + /** Filename for the JSON class renaming map. */ + private static final String RENAMING_MAP_NAME = "styles/renaming-map.json" + + /** Naming map to use for classes. */ + private static SoyCssRenamingMap CSS_RENAMING_MAP = null + + /** + * Load JSON embedded in a JAR resource into a naming map for Soy rendering. + * + * @param mapPath URL to the JAR resource we should load. + */ + @Nullable + private static Map loadMapFromJSON(URL mapPath) { + try { + return new ObjectMapper().readValue(mapPath, new TypeReference>() { }) + } catch (Throwable thr) { + //handle `JsonMappingException` and `IOException`, if, for instance, you're using Jackson + throw new RuntimeException(thr) + } + } + + static { + final URL mapPath = + RewriteMapProvider.class.getClassLoader().getResource(RENAMING_MAP_NAME) + if (mapPath != null) { + // load the renaming map + final Map cssRenamingMap = loadMapFromJSON(mapPath) + if (cssRenamingMap != null) { + CSS_RENAMING_MAP = new SoyCssRenamingMap() { + @Nullable @Override + public String get(@NotNull String className) { + // (or whatever logic you need to rewrite the class) + return cssRenamingMap.get(className) + } + } + } + } + } + + /** + * Provide a CSS renaming map to Soy/Micronaut. + * + * @return Inflated Soy CSS renaming map. + */ + @Nullable @Override SoyCssRenamingMap cssRenamingMap() { + return CSS_RENAMING_MAP + } +} +//end::clazz[] diff --git a/test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProviderSpec.groovy b/test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProviderSpec.groovy new file mode 100644 index 000000000..7205edb79 --- /dev/null +++ b/test-suite-groovy/src/test/groovy/io/micronaut/docs/soy/RewriteMapProviderSpec.groovy @@ -0,0 +1,29 @@ +package io.micronaut.docs.soy + +import com.google.template.soy.shared.SoyCssRenamingMap +import io.micronaut.context.BeanContext +import io.micronaut.context.annotation.Property +import io.micronaut.test.extensions.spock.annotation.MicronautTest +import io.micronaut.views.soy.SoyNamingMapProvider +import jakarta.inject.Inject +import spock.lang.Specification + +@Property(name = "spec.name", value = "RewriteMapProviderSpec") +@MicronautTest +class RewriteMapProviderSpec extends Specification { + + @Inject + BeanContext beanContext; + + def "test read renaming map file"() { + when: + SoyNamingMapProvider rewriteMapProvider = beanContext.getBean(SoyNamingMapProvider.class); + + then: + rewriteMapProvider + SoyCssRenamingMap soyCssRenamingMap = rewriteMapProvider.cssRenamingMap(); + soyCssRenamingMap.get('dialog') == 'a' + soyCssRenamingMap.get('content') == 'b' + soyCssRenamingMap.get('title') == 'c' + } +} diff --git a/test-suite-groovy/src/test/resources/styles/renaming-map.json b/test-suite-groovy/src/test/resources/styles/renaming-map.json new file mode 100644 index 000000000..eddadfc55 --- /dev/null +++ b/test-suite-groovy/src/test/resources/styles/renaming-map.json @@ -0,0 +1,5 @@ +{ + "dialog": "a", + "content": "b", + "title": "c" +} diff --git a/test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProvider.kt b/test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProvider.kt new file mode 100644 index 000000000..cb4190469 --- /dev/null +++ b/test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProvider.kt @@ -0,0 +1,65 @@ +package io.micronaut.docs.soy + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper +import com.google.template.soy.shared.SoyCssRenamingMap +import io.micronaut.context.annotation.Requires +import io.micronaut.core.annotation.Nullable +import io.micronaut.views.soy.SoyNamingMapProvider +import jakarta.inject.Singleton +import java.net.URL + +@Requires(property = "spec.name", value = "RewriteMapProviderTest") +//tag::clazz[] +@Singleton +class RewriteMapProvider : SoyNamingMapProvider { + + /** + * Provide a CSS renaming map to Soy/Micronaut. + * + * @return Inflated Soy CSS renaming map. + */ + @Nullable + override fun cssRenamingMap(): SoyCssRenamingMap { + return CSS_RENAMING_MAP!! + } + + companion object { + /** Filename for the JSON class renaming map. */ + private const val RENAMING_MAP_NAME = "styles/renaming-map.json" + + /** Naming map to use for classes. */ + private var CSS_RENAMING_MAP: SoyCssRenamingMap? = null + + /** + * Load JSON embedded in a JAR resource into a naming map for Soy rendering. + * + * @param mapPath URL to the JAR resource we should load. + */ + @Nullable + private fun loadMapFromJSON(mapPath: URL): Map? { + return try { + ObjectMapper().readValue?>( + mapPath, + object : TypeReference?>() {}) + } catch (thr: Throwable) { + //handle `JsonMappingException` and `IOException`, if, for instance, you're using Jackson + throw RuntimeException(thr) + } + } + + init { + val mapPath = RewriteMapProvider::class.java.classLoader.getResource(RENAMING_MAP_NAME) + if (mapPath != null) { + // load the renaming map + val cssRenamingMap = loadMapFromJSON(mapPath) + if (cssRenamingMap != null) { + CSS_RENAMING_MAP = + SoyCssRenamingMap { className -> // (or whatever logic you need to rewrite the class) + cssRenamingMap[className] + } + } + } + } + } +} diff --git a/test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProviderTest.kt b/test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProviderTest.kt new file mode 100644 index 000000000..28d00a6fa --- /dev/null +++ b/test-suite-kotlin/src/test/kotlin/io/micronaut/docs/soy/RewriteMapProviderTest.kt @@ -0,0 +1,30 @@ +package io.micronaut.docs.soy + +import io.micronaut.context.BeanContext +import io.micronaut.context.annotation.Property +import io.micronaut.test.extensions.junit5.annotation.MicronautTest +import io.micronaut.views.soy.SoyNamingMapProvider +import jakarta.inject.Inject +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + + +@Property(name = "spec.name", value = "RewriteMapProviderTest") +@MicronautTest +internal class RewriteMapProviderTest { + + @Inject + lateinit var beanContext: BeanContext + + @Test + fun testReadRenamingMapFile() { + val rewriteMapProvider = beanContext.getBean(SoyNamingMapProvider::class.java) + + Assertions.assertNotNull(rewriteMapProvider) + val soyCssRenamingMap = rewriteMapProvider.cssRenamingMap() + Assertions.assertEquals("a", soyCssRenamingMap["dialog"]) + Assertions.assertEquals("b", soyCssRenamingMap["content"]) + Assertions.assertEquals("c", soyCssRenamingMap["title"] ) + } +} + diff --git a/test-suite-kotlin/src/test/resources/styles/renaming-map.json b/test-suite-kotlin/src/test/resources/styles/renaming-map.json new file mode 100644 index 000000000..eddadfc55 --- /dev/null +++ b/test-suite-kotlin/src/test/resources/styles/renaming-map.json @@ -0,0 +1,5 @@ +{ + "dialog": "a", + "content": "b", + "title": "c" +} diff --git a/test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProvider.java b/test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProvider.java new file mode 100644 index 000000000..bfb436ed0 --- /dev/null +++ b/test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProvider.java @@ -0,0 +1,66 @@ +package io.micronaut.docs.soy; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.template.soy.shared.SoyCssRenamingMap; +import io.micronaut.context.annotation.Requires; +import io.micronaut.core.annotation.Nullable; +import io.micronaut.views.soy.SoyNamingMapProvider; +import jakarta.inject.Singleton; + +import javax.validation.constraints.NotNull; +import java.net.URL; +import java.util.Map; + +@Requires(property = "spec.name", value = "RewriteMapProviderTest") +//tag::clazz[] +@Singleton +public class RewriteMapProvider implements SoyNamingMapProvider { + /** Filename for the JSON class renaming map. */ + private static final String RENAMING_MAP_NAME = "styles/renaming-map.json"; + + /** Naming map to use for classes. */ + private static SoyCssRenamingMap CSS_RENAMING_MAP = null; + + /** + * Load JSON embedded in a JAR resource into a naming map for Soy rendering. + * + * @param mapPath URL to the JAR resource we should load. + */ + @Nullable + private static Map loadMapFromJSON(URL mapPath) { + try { + return new ObjectMapper().readValue(mapPath, new TypeReference>() { }); + } catch (Throwable thr) { + //handle `JsonMappingException` and `IOException`, if, for instance, you're using Jackson + throw new RuntimeException(thr); + } + } + + static { + final URL mapPath = RewriteMapProvider.class.getClassLoader().getResource(RENAMING_MAP_NAME); + if (mapPath != null) { + // load the renaming map + final Map cssRenamingMap = loadMapFromJSON(mapPath); + if (cssRenamingMap != null) { + CSS_RENAMING_MAP = new SoyCssRenamingMap() { + @Nullable @Override + public String get(@NotNull String className) { + // (or whatever logic you need to rewrite the class) + return cssRenamingMap.get(className); + } + }; + } + } + } + + /** + * Provide a CSS renaming map to Soy/Micronaut. + * + * @return Inflated Soy CSS renaming map. + */ + @Nullable @Override public SoyCssRenamingMap cssRenamingMap() { + return CSS_RENAMING_MAP; + } +} +//end::clazz[] diff --git a/test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProviderTest.java b/test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProviderTest.java new file mode 100644 index 000000000..d3201bb5b --- /dev/null +++ b/test-suite/src/test/java/io/micronaut/docs/soy/RewriteMapProviderTest.java @@ -0,0 +1,30 @@ +package io.micronaut.docs.soy; + +import com.google.template.soy.shared.SoyCssRenamingMap; +import io.micronaut.context.BeanContext; +import io.micronaut.context.annotation.Property; +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; +import io.micronaut.views.soy.SoyNamingMapProvider; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@Property(name = "spec.name", value = "RewriteMapProviderTest") +@MicronautTest +class RewriteMapProviderTest { + + @Inject + BeanContext beanContext; + + @Test + void testReadRenamingMapFile() { + SoyNamingMapProvider rewriteMapProvider = beanContext.getBean(SoyNamingMapProvider.class); + assertNotNull(rewriteMapProvider); + SoyCssRenamingMap soyCssRenamingMap = rewriteMapProvider.cssRenamingMap(); + assertEquals("a", soyCssRenamingMap.get("dialog")); + assertEquals("b", soyCssRenamingMap.get("content")); + assertEquals("c", soyCssRenamingMap.get("title")); + } +} diff --git a/test-suite/src/test/resources/styles/renaming-map.json b/test-suite/src/test/resources/styles/renaming-map.json new file mode 100644 index 000000000..eddadfc55 --- /dev/null +++ b/test-suite/src/test/resources/styles/renaming-map.json @@ -0,0 +1,5 @@ +{ + "dialog": "a", + "content": "b", + "title": "c" +}