/* * Copyright 2014 Google Inc. All rights reserved. * * 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.inferred.freebuilder.processor; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import org.inferred.freebuilder.FreeBuilder; import org.inferred.freebuilder.processor.util.feature.FeatureSet; import org.inferred.freebuilder.processor.util.testing.BehaviorTestRunner.Shared; import org.inferred.freebuilder.processor.util.testing.BehaviorTester; import org.inferred.freebuilder.processor.util.testing.ParameterizedBehaviorTestFactory; import org.inferred.freebuilder.processor.util.testing.SourceBuilder; import org.inferred.freebuilder.processor.util.testing.TestBuilder; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; import org.junit.runners.Parameterized.UseParametersRunnerFactory; import java.util.List; import javax.tools.JavaFileObject; @RunWith(Parameterized.class) @UseParametersRunnerFactory(ParameterizedBehaviorTestFactory.class) public class OptionalMapperMethodTest { @Parameters(name = "{1}: {0}") public static List<Object[]> parameters() { ImmutableList.Builder<Object[]> parameters = ImmutableList.builder(); for (FeatureSet features : FeatureSets.WITH_LAMBDAS) { parameters.add(new Object[] { features, java.util.Optional.class }); } for (FeatureSet features : FeatureSets.WITH_GUAVA_AND_LAMBDAS) { parameters.add(new Object[] { features, com.google.common.base.Optional.class }); } return parameters.build(); } private static JavaFileObject optionalIntegerType(Class<?> optionalType) { return new SourceBuilder() .addLine("package com.example;") .addLine("@%s", FreeBuilder.class) .addLine("public interface DataType {") .addLine(" %s<Integer> getProperty();", optionalType) .addLine("") .addLine(" public static class Builder extends DataType_Builder {}") .addLine("}") .build(); } @Rule public final ExpectedException thrown = ExpectedException.none(); @Shared public BehaviorTester behaviorTester; @Parameter(value = 0) public FeatureSet features; @Parameter(value = 1) public Class<?> optionalType; @Test public void mapReplacesValueToBeReturnedFromGetter() { behaviorTester .with(new Processor(features)) .with(optionalIntegerType(optionalType)) .with(new TestBuilder() .addLine("com.example.DataType value = new com.example.DataType.Builder()") .addLine(" .setProperty(11)") .addLine(" .mapProperty(a -> a + 3)") .addLine(" .build();") .addLine("assertEquals(14, (int) value.getProperty().get());") .build()) .runTest(); } @Test public void mapDelegatesToSetterForValidation() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("property must be non-negative"); behaviorTester .with(new Processor(features)) .with(new SourceBuilder() .addLine("package com.example;") .addLine("@%s", FreeBuilder.class) .addLine("public interface DataType {") .addLine(" %s<Integer> getProperty();", optionalType) .addLine("") .addLine(" public static class Builder extends DataType_Builder {") .addLine(" @Override public Builder setProperty(int property) {") .addLine(" %s.checkArgument(property >= 0, \"property must be non-negative\");", Preconditions.class) .addLine(" return super.setProperty(property);") .addLine(" }") .addLine(" }") .addLine("}") .build()) .with(new TestBuilder() .addLine("new com.example.DataType.Builder()") .addLine(" .setProperty(11)") .addLine(" .mapProperty(a -> -3);") .build()) .runTest(); } @Test public void mapThrowsNpeIfMapperIsNull() { thrown.expect(NullPointerException.class); behaviorTester .with(new Processor(features)) .with(optionalIntegerType(optionalType)) .with(new TestBuilder() .addLine("new com.example.DataType.Builder()") .addLine(" .setProperty(11)") .addLine(" .mapProperty(null);") .build()) .runTest(); } @Test public void mapThrowsNpeIfMapperIsNullAndPropertyIsEmpty() { thrown.expect(NullPointerException.class); behaviorTester .with(new Processor(features)) .with(optionalIntegerType(optionalType)) .with(new TestBuilder() .addLine("new com.example.DataType.Builder()") .addLine(" .mapProperty(null);") .build()) .runTest(); } @Test public void mapAllowsNullReturn() { behaviorTester .with(new Processor(features)) .with(optionalIntegerType(optionalType)) .with(new TestBuilder() .addLine("com.example.DataType value = new com.example.DataType.Builder()") .addLine(" .setProperty(11)") .addLine(" .mapProperty(a -> null)") .addLine(" .build();") .addLine("assertFalse(value.getProperty().isPresent());") .build()) .runTest(); } @Test public void mapSkipsMapperIfPropertyIsEmpty() { behaviorTester .with(new Processor(features)) .with(optionalIntegerType(optionalType)) .with(new TestBuilder() .addLine("com.example.DataType value = new com.example.DataType.Builder()") .addLine(" .mapProperty(a -> { fail(\"mapper called\"); return null; })") .addLine(" .build();") .addLine("assertFalse(value.getProperty().isPresent());") .build()) .runTest(); } @Test public void mapReplacesValueToBeReturnedFromPrefixlessGetter() { behaviorTester .with(new Processor(features)) .with(new SourceBuilder() .addLine("package com.example;") .addLine("@%s", FreeBuilder.class) .addLine("public interface DataType {") .addLine(" %s<Integer> property();", optionalType) .addLine("") .addLine(" public static class Builder extends DataType_Builder {}") .addLine("}") .build()) .with(new TestBuilder() .addLine("com.example.DataType value = new com.example.DataType.Builder()") .addLine(" .property(11)") .addLine(" .mapProperty(a -> a + 3)") .addLine(" .build();") .addLine("assertEquals(14, (int) value.property().get());") .build()) .runTest(); } }