Add test case of a typed Builder (Builder<T>). Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f95a85b8 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f95a85b8 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f95a85b8
Branch: refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure Commit: f95a85b8dc87704a678f70e4f6c5061c67cf26c4 Parents: ba677d8 Author: Gary Gregory <ggreg...@apache.org> Authored: Tue Aug 9 17:21:56 2016 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Tue Aug 9 17:21:56 2016 -0700 ---------------------------------------------------------------------- .../ValidatingPluginWithTypedBuilder.java | 70 +++++++++++++++++++ .../ValidatingPluginWithTypedBuilderTest.java | 71 ++++++++++++++++++++ 2 files changed, 141 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f95a85b8/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPluginWithTypedBuilder.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPluginWithTypedBuilder.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPluginWithTypedBuilder.java new file mode 100644 index 0000000..9ebb85e --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPluginWithTypedBuilder.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.logging.log4j.core.config.plugins.validation; + +import java.util.Objects; + +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; +import org.apache.logging.log4j.core.config.plugins.PluginFactory; +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; + +/** + * + */ +@Plugin(name = "ValidatingPluginWithTypedBuilder", category = "Test") +public class ValidatingPluginWithTypedBuilder { + + private final String name; + + public ValidatingPluginWithTypedBuilder(final String name) { + this.name = Objects.requireNonNull(name, "name"); + } + + public String getName() { + return name; + } + + @PluginFactory + public static ValidatingPluginWithTypedBuilder newValidatingPlugin( + @Required(message = "The name given by the factory is null") final String name) { + return new ValidatingPluginWithTypedBuilder(name); + } + + @PluginBuilderFactory + public static Builder<Integer> newBuilder() { + return new Builder<>(); + } + + public static class Builder<T> implements org.apache.logging.log4j.core.util.Builder<ValidatingPluginWithTypedBuilder> { + + @PluginBuilderAttribute + @Required(message = "The name given by the builder is null") + private String name; + + public Builder<T> withName(final String name) { + this.name = name; + return this; + } + + @Override + public ValidatingPluginWithTypedBuilder build() { + return new ValidatingPluginWithTypedBuilder(name); + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f95a85b8/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidatingPluginWithTypedBuilderTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidatingPluginWithTypedBuilderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidatingPluginWithTypedBuilderTest.java new file mode 100644 index 0000000..ae236b4 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidatingPluginWithTypedBuilderTest.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.logging.log4j.core.config.plugins.validation.validators; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.apache.logging.log4j.core.config.Node; +import org.apache.logging.log4j.core.config.NullConfiguration; +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder; +import org.apache.logging.log4j.core.config.plugins.util.PluginManager; +import org.apache.logging.log4j.core.config.plugins.util.PluginType; +import org.apache.logging.log4j.core.config.plugins.validation.ValidatingPluginWithTypedBuilder; +import org.junit.Before; +import org.junit.Test; + +public class ValidatingPluginWithTypedBuilderTest { + + private PluginType<ValidatingPluginWithTypedBuilder> plugin; + private Node node; + + @SuppressWarnings("unchecked") + @Before + public void setUp() throws Exception { + final PluginManager manager = new PluginManager("Test"); + manager.collectPlugins(); + plugin = (PluginType<ValidatingPluginWithTypedBuilder>) manager + .getPluginType("ValidatingPluginWithTypedBuilder"); + assertNotNull("Rebuild this module to make sure annotaion processing kicks in.", plugin); + node = new Node(null, "Validator", plugin); + } + + @Test + public void testNullDefaultValue() throws Exception { + // @formatter:off + final ValidatingPluginWithTypedBuilder validatingPlugin = (ValidatingPluginWithTypedBuilder) + new PluginBuilder(plugin). + withConfiguration(new NullConfiguration()). + withConfigurationNode(node).build(); + // @formatter:on + assertNull(validatingPlugin); + } + + @Test + public void testNonNullValue() throws Exception { + node.getAttributes().put("name", "foo"); + // @formatter:off + final ValidatingPluginWithTypedBuilder validatingPlugin = (ValidatingPluginWithTypedBuilder) + new PluginBuilder(plugin). + withConfiguration(new NullConfiguration()). + withConfigurationNode(node).build(); + // @formatter:on + assertNotNull(validatingPlugin); + assertEquals("foo", validatingPlugin.getName()); + } +}