Search in sources :

Example 26 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class ExtensibleDynamicObjectTest method parentMethodsAreInherited.

@Test
public void parentMethodsAreInherited() {
    Bean parent = new Bean();
    Convention convention = parent.extensibleDynamicObject.getConvention();
    convention.getPlugins().put("convention", new ConventionBean());
    Bean bean = new Bean();
    bean.setParent(parent.getAsDynamicObject());
    DynamicObject inherited = bean.getInheritable();
    assertTrue(inherited.hasMethod("conventionMethod", "a", "b"));
    assertThat(inherited.invokeMethod("conventionMethod", "a", "b"), equalTo((Object) "convention:a.b"));
}
Also used : Convention(org.gradle.api.plugins.Convention) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Test(org.junit.Test)

Example 27 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class ExtensibleDynamicObjectTest method inheritedConventionPropertiesTrackChanges.

@Test
public void inheritedConventionPropertiesTrackChanges() {
    Bean bean = new Bean();
    DynamicObject inherited = bean.getInheritable();
    assertFalse(inherited.hasProperty("conventionProperty"));
    Convention convention = bean.extensibleDynamicObject.getConvention();
    ConventionBean conventionBean = new ConventionBean();
    conventionBean.setConventionProperty("value");
    convention.getPlugins().put("convention", conventionBean);
    assertTrue(inherited.hasProperty("conventionProperty"));
    assertThat(inherited.getProperty("conventionProperty"), equalTo((Object) "value"));
}
Also used : Convention(org.gradle.api.plugins.Convention) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Test(org.junit.Test)

Example 28 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class ExtensibleDynamicObjectTest method inheritedAdditionalObjectPropertiesTrackChanges.

@Test
public void inheritedAdditionalObjectPropertiesTrackChanges() {
    Bean other = new Bean();
    other.defineProperty("other", "value");
    Bean bean = new Bean();
    DynamicObject inherited = bean.getInheritable();
    assertFalse(inherited.hasProperty("other"));
    bean.extensibleDynamicObject.addObject(other.getAsDynamicObject(), ExtensibleDynamicObject.Location.BeforeConvention);
    assertTrue(inherited.hasProperty("other"));
    assertThat(inherited.getProperty("other"), equalTo((Object) "value"));
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Test(org.junit.Test)

Example 29 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class ExtensibleDynamicObjectTest method doesNotIncludeToStringInSetPropertyErrorMessageWhenItIsNotImplemented.

@Test
public void doesNotIncludeToStringInSetPropertyErrorMessageWhenItIsNotImplemented() {
    DynamicObject bean = new ExtensibleDynamicObject(new Object(), Object.class, ThreadGlobalInstantiator.getOrCreate());
    try {
        bean.setProperty("unknown", "value");
        fail();
    } catch (MissingPropertyException e) {
        assertThat(e.getMessage(), equalTo("Could not set unknown property 'unknown' for object of type java.lang.Object."));
    }
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Test(org.junit.Test)

Example 30 with DynamicObject

use of org.gradle.internal.metaobject.DynamicObject in project gradle by gradle.

the class AsmBackedClassGeneratorTest method doesNotAddSetterMethodsForPropertyWhoseTypeIsPropertyWhenTheSetterMethodsAlreadyExist.

@Test
public void doesNotAddSetterMethodsForPropertyWhoseTypeIsPropertyWhenTheSetterMethodsAlreadyExist() throws Exception {
    DefaultProviderFactory providerFactory = new DefaultProviderFactory();
    BeanWithProperty bean = generator.newInstance(BeanWithProperty.class, TestUtil.objectFactory());
    DynamicObject dynamicObject = ((DynamicObjectAware) bean).getAsDynamicObject();
    dynamicObject.setProperty("prop2", "value");
    assertEquals("[value]", bean.getProp2().get());
    dynamicObject.setProperty("prop2", 12);
    assertEquals("[12]", bean.getProp2().get());
    dynamicObject.setProperty("prop2", providerFactory.provider(new Callable<String>() {

        int count;

        @Override
        public String call() throws Exception {
            return "[" + String.valueOf(++count) + "]";
        }
    }));
    assertEquals("[1]", bean.getProp2().get());
    assertEquals("[2]", bean.getProp2().get());
}
Also used : BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) DefaultProviderFactory(org.gradle.api.internal.provider.DefaultProviderFactory) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Aggregations

DynamicObject (org.gradle.internal.metaobject.DynamicObject)45 BeanDynamicObject (org.gradle.internal.metaobject.BeanDynamicObject)40 Test (org.junit.Test)37 Convention (org.gradle.api.plugins.Convention)10 Callable (java.util.concurrent.Callable)5 DynamicObjectAware (org.gradle.api.internal.DynamicObjectAware)5 DefaultProviderFactory (org.gradle.api.internal.provider.DefaultProviderFactory)5 ExtensibleDynamicObject (org.gradle.internal.extensibility.ExtensibleDynamicObject)4 Closure (groovy.lang.Closure)3 MissingPropertyException (groovy.lang.MissingPropertyException)3 MissingMethodException (groovy.lang.MissingMethodException)2 File (java.io.File)2 Map (java.util.Map)2 ClassLoaderScope (org.gradle.api.internal.initialization.ClassLoaderScope)2 DynamicInvokeResult (org.gradle.internal.metaobject.DynamicInvokeResult)2 Predicate (com.google.common.base.Predicate)1 DirectoryTree (org.gradle.api.file.DirectoryTree)1 ExtensibleDynamicObject (org.gradle.api.internal.ExtensibleDynamicObject)1 ScriptHandlerInternal (org.gradle.api.internal.initialization.ScriptHandlerInternal)1