Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
Update AbstractGrailsTagTests: test with Grails 2022.0.0 snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
rainboyan committed Nov 10, 2022
1 parent b9e78b5 commit 7c7ac3a
Showing 1 changed file with 90 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
package org.grails.web.taglib

import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory

import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import com.opensymphony.module.sitemesh.RequestConstants
import org.springframework.beans.BeansException
import org.springframework.beans.factory.config.AutowireCapableBeanFactory
import org.springframework.beans.factory.config.BeanFactoryPostProcessor
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
import org.springframework.beans.factory.support.RootBeanDefinition
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext
import org.springframework.core.io.DescriptiveResource
import org.springframework.context.ApplicationContext
import org.springframework.context.MessageSource
import org.springframework.context.support.StaticMessageSource
import org.springframework.core.convert.support.DefaultConversionService
import org.springframework.core.io.Resource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
import org.springframework.mock.web.MockServletContext
import org.springframework.ui.context.Theme
import org.springframework.ui.context.ThemeSource
import org.springframework.ui.context.support.SimpleTheme
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.servlet.DispatcherServlet
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
import org.springframework.web.servlet.support.JstlUtils
import org.springframework.web.servlet.theme.SessionThemeResolver
import org.w3c.dom.Document

import grails.build.support.MetaClassRegistryCleaner
import grails.core.DefaultGrailsApplication
import grails.core.GrailsApplication
Expand All @@ -11,17 +46,31 @@ import grails.util.Holders
import grails.util.Metadata
import grails.web.pages.GroovyPagesUriService
import org.grails.buffer.FastStringWriter
import org.grails.commons.CodecArtefactHandler
import org.grails.config.PropertySourcesConfig
import org.grails.core.artefact.ControllerArtefactHandler
import org.grails.core.artefact.gsp.TagLibArtefactHandler
import org.grails.encoder.Encoder
import org.grails.encoder.impl.HTML4Codec
import org.grails.encoder.impl.HTMLJSCodec
import org.grails.encoder.impl.JavaScriptCodec
import org.grails.encoder.impl.RawCodec
import org.grails.gsp.GroovyPage
import org.grails.gsp.GroovyPageMetaInfo
import org.grails.gsp.GroovyPageTemplate
import org.grails.gsp.GroovyPagesTemplateEngine
import org.grails.gsp.compiler.SitemeshPreprocessor
import org.grails.plugins.DefaultGrailsPlugin
import org.grails.plugins.MockGrailsPluginManager
import org.grails.plugins.codecs.CodecsPluginConfiguration
import org.grails.plugins.codecs.HTMLCodec
import org.grails.plugins.codecs.URLCodec
import org.grails.plugins.core.CoreConfiguration
import org.grails.plugins.databinding.DataBindingConfiguration
import org.grails.plugins.web.controllers.ControllersPluginConfiguration
import org.grails.plugins.web.mime.MimeTypesConfiguration
import org.grails.spring.beans.GrailsApplicationAwareBeanPostProcessor
import org.grails.spring.beans.PluginManagerAwareBeanPostProcessor
import org.grails.taglib.GroovyPageAttributes
import org.grails.taglib.TagOutput
import org.grails.taglib.encoder.OutputContextLookupHelper
Expand All @@ -37,45 +86,23 @@ import org.grails.web.sitemesh.GSPSitemeshPage
import org.grails.web.sitemesh.GrailsHTMLPageParser
import org.grails.web.sitemesh.GrailsLayoutView
import org.grails.web.util.GrailsApplicationAttributes
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.springframework.beans.factory.config.AutowireCapableBeanFactory
import org.springframework.beans.factory.support.RootBeanDefinition
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
import org.springframework.context.ApplicationContext
import org.springframework.context.MessageSource
import org.springframework.context.support.StaticMessageSource
import org.springframework.core.convert.support.DefaultConversionService
import org.springframework.core.io.Resource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
import org.springframework.mock.web.MockServletContext
import org.springframework.ui.context.Theme
import org.springframework.ui.context.ThemeSource
import org.springframework.ui.context.support.SimpleTheme
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.servlet.DispatcherServlet
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
import org.springframework.web.servlet.support.JstlUtils
import org.springframework.web.servlet.theme.SessionThemeResolver
import org.w3c.dom.Document

import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory

import static org.junit.jupiter.api.Assertions.*

abstract class AbstractGrailsTagTests {

static final Class[] DEFAULT_AUTO_CONFIGURATIONS = [
CoreConfiguration,
CodecsPluginConfiguration,
ControllersPluginConfiguration,
DataBindingConfiguration,
MimeTypesConfiguration]

MockServletContext servletContext
GrailsWebRequest webRequest
MockHttpServletRequest request
MockHttpServletResponse response
ApplicationContext ctx
AnnotationConfigServletWebApplicationContext ctx
def originalHandler
ApplicationContext appCtx
GrailsApplication ga
Expand Down Expand Up @@ -240,57 +267,72 @@ info.app.name: ${getClass().name}
mockManager.registerProvidedArtefacts(grailsApplication)

def mockControllerClass = gcl.parseClass("class MockController { def index = {} } ")
ctx = new AnnotationConfigServletWebServerApplicationContext()
grailsApplication.addArtefact(ControllerArtefactHandler.TYPE, mockControllerClass)
grailsApplication.addArtefact(CodecArtefactHandler.TYPE, HTMLCodec)
grailsApplication.addArtefact(CodecArtefactHandler.TYPE, HTML4Codec)
grailsApplication.addArtefact(CodecArtefactHandler.TYPE, JavaScriptCodec)
grailsApplication.addArtefact(CodecArtefactHandler.TYPE, HTMLJSCodec)
grailsApplication.addArtefact(CodecArtefactHandler.TYPE, URLCodec)
grailsApplication.addArtefact(CodecArtefactHandler.TYPE, RawCodec)

ctx = new AnnotationConfigServletWebApplicationContext()
ctx.setServletContext(new MockServletContext())
ctx.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource))
ctx.refresh()
ctx.servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, ctx)

grailsApplication.setApplicationContext(ctx)

ctx.beanFactory.registerSingleton(GrailsApplication.APPLICATION_ID, grailsApplication)
ctx.beanFactory.registerSingleton("pluginManager", mockManager)

grailsApplication.addArtefact(ControllerArtefactHandler.TYPE, mockControllerClass)

messageSource = ctx.getBean("messageSource", MessageSource)

ctx.beanFactory.registerSingleton("classLoader", gcl)
ctx.beanFactory.registerSingleton("grailsLinkGenerator", new DefaultLinkGenerator("http://localhost:8080"))
ctx.beanFactory.registerSingleton("manager", mockManager)
ctx.beanFactory.registerSingleton("conversionService", new DefaultConversionService())
ctx.beanFactory.registerSingleton(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService())
ctx.register(DEFAULT_AUTO_CONFIGURATIONS)
ctx.refresh()
ctx.servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, ctx)

grailsApplication.setApplicationContext(ctx)

messageSource = ctx.getBean("messageSource", MessageSource)

onInitMockBeans()

def dependantPluginClasses = []
List<Class> dependantPluginClasses = []
dependantPluginClasses << gcl.loadClass("org.grails.plugins.CoreGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.CodecsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.codecs.CodecsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.domain.DomainClassGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.i18n.I18nGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.mapping.UrlMappingsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.controllers.ControllersGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.GroovyPagesGrailsPlugin")

def dependentPlugins = dependantPluginClasses.collect { new DefaultGrailsPlugin(it, grailsApplication)}
List<DefaultGrailsPlugin> dependentPlugins = dependantPluginClasses.collect {
new DefaultGrailsPlugin(it, new DescriptiveResource(it.toString()), grailsApplication)
}

dependentPlugins.each { mockManager.registerMockPlugin(it); it.manager = mockManager }
mockManager.registerProvidedArtefacts(grailsApplication)
mockManager.setApplicationContext(ctx)

def springConfig = new WebRuntimeSpringConfiguration(ctx)

webRequest = GrailsWebMockUtil.bindMockWebRequest(ctx)
onInit()
try {
JstlUtils.exposeLocalizationContext webRequest.getRequest(), null
} catch (Throwable e) {
// ignore
} catch (Throwable ignore) {
}

servletContext = webRequest.servletContext
Holders.servletContext = servletContext
Holders.addApplicationDiscoveryStrategy(new ServletEnvironmentGrailsApplicationDiscoveryStrategy(servletContext));

springConfig.servletContext = servletContext
springConfig.registerPostProcessor(new BeanFactoryPostProcessor() {
@Override
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.addBeanPostProcessor(new GrailsApplicationAwareBeanPostProcessor(grailsApplication))
beanFactory.addBeanPostProcessor(new PluginManagerAwareBeanPostProcessor(mockManager))
}
})

dependentPlugins*.doWithRuntimeConfiguration(springConfig)

Expand Down Expand Up @@ -478,7 +520,7 @@ info.app.name: ${getClass().name}
def preprocessor=new SitemeshPreprocessor()
preprocessor.addGspSitemeshCapturing(template)
}

String applyLayout(String layout, String template, Map params=[:]) {
def gspSiteMeshPage = new GSPSitemeshPage()
request.setAttribute(GrailsLayoutView.GSP_SITEMESH_PAGE, gspSiteMeshPage)
Expand Down

0 comments on commit 7c7ac3a

Please sign in to comment.