Search in sources :

Example 6 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testOverridingDefaultTemplateViaConfig.

public void testOverridingDefaultTemplateViaConfig() throws Exception {
    ConfigObject config = new ConfigSlurper().parse("grails.sitemesh.default.layout='otherApplication'");
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MapPropertySource("grails", config));
    GrailsWebRequest webRequest = buildMockRequest(new PropertySourcesConfig(propertySources));
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/application.gsp", "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");
    appCtx.registerMockResource("/grails-app/views/layouts/otherApplication.gsp", "<html><body><h1>Other Default Layout</h1><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class FooController {\n" + "def controllerName = 'foo'\n" + "def actionUri = '/foo/fooAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/otherApplication.gsp", d.getPage());
    assertEquals("otherApplication", d.getName());
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) MapPropertySource(org.springframework.core.env.MapPropertySource) ServletContext(javax.servlet.ServletContext) MutablePropertySources(org.springframework.core.env.MutablePropertySources) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) ConfigObject(groovy.util.ConfigObject) ConfigSlurper(groovy.util.ConfigSlurper)

Example 7 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByLayoutPropertyInController.

public void testDecoratedByLayoutPropertyInController() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest(null);
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/test.gsp", "<html><body><g:layoutBody /></body></html>");
    appCtx.registerMockResource("/grails-app/views/layouts/mylayout.gsp", "<html><body><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class TestController {\n" + "def controllerName = 'test'\n" + "def actionUri = '/test/testAction'\n" + "static layout = 'mylayout'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Test title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/mylayout.gsp", d.getPage());
    assertEquals("mylayout", d.getName());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 8 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByControllerConvention.

public void testDecoratedByControllerConvention() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest(null);
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/test.gsp", "<html><body><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class TestController {\n" + "def controllerName = 'test'\n" + "def actionUri = '/test/testAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Test title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/test.gsp", d.getPage());
    assertEquals("test", d.getName());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 9 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class ConstrainedPropertyTests method testConstraintBuilder.

@SuppressWarnings("rawtypes")
public void testConstraintBuilder() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class groovyClass = gcl.parseClass("class TestClass {\n" + "Long id\n" + "Long version\n" + "String login\n" + "String other\n" + "String email\n" + "static constraints = {\n" + "login(size:5..15,nullable:false,blank:false)\n" + "other(blank:false,size:5..15,nullable:false)\n" + "email(email:true)\n" + "}\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
    ga.initialise();
    new MappingContextBuilder(ga).build(groovyClass);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "TestClass");
    Map constrainedProperties = domainClass.getConstrainedProperties();
    assert constrainedProperties.size() == 3;
    grails.gorm.validation.ConstrainedProperty loginConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("login");
    Collection appliedConstraints = loginConstraint.getAppliedConstraints();
    assertTrue(appliedConstraints.size() == 3);
    // Check the order of the constraints for the 'login' property...
    int index = 0;
    String[] constraintNames = new String[] { "size", "nullable", "blank" };
    for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
        Constraint c = (Constraint) iter.next();
        assertEquals(constraintNames[index], c.getName());
        index++;
    }
    // ...and for the 'other' property.
    appliedConstraints = ((grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("other")).getAppliedConstraints();
    index = 0;
    constraintNames = new String[] { "blank", "size", "nullable" };
    for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
        Constraint c = (Constraint) iter.next();
        assertEquals(constraintNames[index], c.getName());
        index++;
    }
    grails.gorm.validation.ConstrainedProperty emailConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("email");
    assertEquals(2, emailConstraint.getAppliedConstraints().size());
    GroovyObject go = (GroovyObject) groovyClass.newInstance();
    go.setProperty("email", "rubbish_email");
    Errors errors = new BindException(go, "TestClass");
    emailConstraint.validate(go, go.getProperty("email"), errors);
    assertTrue(errors.hasErrors());
    go.setProperty("email", "valid@email.com");
    errors = new BindException(go, "TestClass");
    emailConstraint.validate(go, go.getProperty("email"), errors);
    assertFalse(errors.hasErrors());
}
Also used : GrailsDomainClass(grails.core.GrailsDomainClass) BindException(org.springframework.validation.BindException) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Errors(org.springframework.validation.Errors) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsApplication(grails.core.GrailsApplication) Iterator(java.util.Iterator) Collection(java.util.Collection) GrailsDomainClass(grails.core.GrailsDomainClass) Map(java.util.Map)

Example 10 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class ConstraintsEvaluatingPropertyTests method testNullableConstraint.

/**
     * Test that static constraints work
     */
@SuppressWarnings("rawtypes")
public void testNullableConstraint() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    gcl.parseClass("package org.grails.validation\n" + "@grails.persistence.Entity class Book {\n" + "   Long id\n" + "   Long version\n" + "   String title\n" + "   String description\n" + "   Author author\n" + "   Author assistent\n" + "   Set chapters\n" + "   Map remarks\n" + "   static hasMany = [chapters:Chapter]\n" + "   static constraints = {\n" + "      description(nullable: true)\n" + "      assistent(nullable: true)\n" + "   }\n" + "}\n" + "@grails.persistence.Entity class Author {\n" + "   Long id\n" + "   Long version\n" + "   String name\n" + "}\n" + "@grails.persistence.Entity class Chapter {\n" + "   Long id\n" + "   Long version\n" + "   String text\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses());
    ga.initialise();
    new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
    GrailsDomainClass bookClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "org.grails.validation.Book");
    Map constraints = bookClass.getConstrainedProperties();
    Constrained p = (Constrained) constraints.get("title");
    assertFalse("Title property should be required", p.isNullable());
    p = (Constrained) constraints.get("description");
    assertTrue("Description property should be optional", p.isNullable());
    p = (Constrained) constraints.get("author");
    assertFalse("Author property should be required", p.isNullable());
    p = (Constrained) constraints.get("assistent");
    assertTrue("Assistent property should be optional", p.isNullable());
    // Test that Collections and Maps are nullable by default
    p = (Constrained) constraints.get("chapters");
    assertTrue("Chapters property should be optional", p.isNullable());
    p = (Constrained) constraints.get("remarks");
    assertTrue("Remarks property should be optional", p.isNullable());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Constrained(grails.gorm.validation.Constrained) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Map(java.util.Map)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)136 File (java.io.File)28 GroovyObject (groovy.lang.GroovyObject)19 Test (org.junit.jupiter.api.Test)18 IOException (java.io.IOException)15 HashMap (java.util.HashMap)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Map (java.util.Map)12 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 BuildException (org.apache.tools.ant.BuildException)9 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)9 ClassNode (org.codehaus.groovy.ast.ClassNode)9 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)8 ArtefactHandler (grails.core.ArtefactHandler)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7