org.gradle.api.internal.ConventionAwareHelperTest Maven / Gradle / Ivy
/*
* Copyright 2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal;
import org.gradle.api.InvalidUserDataException;
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider;
import org.gradle.util.TestTask;
import org.gradle.util.TestUtil;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.util.List;
import java.util.concurrent.Callable;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static org.gradle.util.WrapUtil.toList;
import static org.gradle.util.WrapUtil.toMap;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.*;
public class ConventionAwareHelperTest {
ConventionAwareHelper conventionAware;
TestTask testTask;
@Rule
public TestNameTestDirectoryProvider temporaryFolder = new TestNameTestDirectoryProvider();
@Before public void setUp() {
testTask = TestUtil.create(temporaryFolder).task(TestTask.class);
conventionAware = new ConventionAwareHelper(testTask);
}
@Test
public void canMapPropertiesUsingClosure() {
conventionAware.map("list1", TestUtil.toClosure("{ ['a'] }"));
assertThat(conventionAware.getConventionValue(null, "list1", false), equalTo((Object) toList("a")));
conventionAware.map("list1", TestUtil.toClosure("{ convention -> [convention] }"));
assertThat(conventionAware.getConventionValue(null, "list1", false), equalTo((Object) toList(conventionAware.getConvention())));
conventionAware.map("list1", TestUtil.toClosure("{ convention, object -> [convention, object] }"));
assertThat(conventionAware.getConventionValue(null, "list1", false), equalTo((Object) toList(conventionAware.getConvention(), testTask)));
}
@Test
public void canMapPropertiesUsingCallable() {
Callable callable = new Callable() {
public Object call() throws Exception {
return toList("a");
}
};
conventionAware.map("list1", callable);
assertThat(conventionAware.getConventionValue(null, "list1", false), equalTo((Object) toList("a")));
}
@Test
public void canSetMappingUsingDynamicProperty() {
TestUtil.call("{ it.list1 = { ['a'] } }", conventionAware);
assertThat(conventionAware.getConventionValue(null, "list1", false), equalTo((Object) toList("a")));
}
@Test (expected = InvalidUserDataException.class) public void cannotMapUnknownProperty() {
conventionAware.map("unknownProp", new Callable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy