mirror of
https://github.com/google/guice.git
synced 2024-04-21 12:32:36 +00:00
This required upgrading some test dependencies, because the tests need some impls of the APIs which only exist in newer releases. This also removes the struts2 extension from the maven & bazel build (although it keeps the now-unused source), because struts2 has no release that supports jakarta. For users that want to continue using the struts2 extension, please continue to use the Guice 6.0 release (instead of the 7.0+ line, which will only support the jakarta dependencies). Fixes #1383. (I'll cut the 6.0 candidates/releases from before this commit, and if we need to make further 6.0 changes, I'll manually merge those into the 6.0 branch.) PiperOrigin-RevId: 529504991
64 lines
2.2 KiB
Java
64 lines
2.2 KiB
Java
/*
|
|
* Copyright (C) 2019 Google Inc.
|
|
*
|
|
* 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 com.google.inject.daggeradapter;
|
|
|
|
import static com.google.inject.daggeradapter.BindingSubject.assertThat;
|
|
|
|
import com.google.inject.Binding;
|
|
import com.google.inject.Guice;
|
|
import com.google.inject.Injector;
|
|
import com.google.inject.Key;
|
|
import com.google.inject.TypeLiteral;
|
|
import dagger.BindsOptionalOf;
|
|
import dagger.Module;
|
|
import java.lang.annotation.Retention;
|
|
import java.lang.annotation.RetentionPolicy;
|
|
import java.util.Optional;
|
|
import jakarta.inject.Qualifier;
|
|
import junit.framework.TestCase;
|
|
|
|
/** Tests of {@link BindsOptionalOf} support in {@link DaggerAdapter}. */
|
|
|
|
public class OptionalBindingsTest extends TestCase {
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Qualifier
|
|
@interface TestQualifier {}
|
|
|
|
@Module
|
|
interface BasicModule {
|
|
@BindsOptionalOf
|
|
String optionalString();
|
|
|
|
@BindsOptionalOf
|
|
@TestQualifier
|
|
Integer optionalQualifiedInteger();
|
|
}
|
|
|
|
public void testBinds() {
|
|
Injector injector = Guice.createInjector(DaggerAdapter.from(BasicModule.class));
|
|
|
|
Binding<Optional<String>> optionalBinding = injector.getBinding(new Key<Optional<String>>() {});
|
|
assertThat(optionalBinding).hasProvidedValueThat().isEqualTo(Optional.empty());
|
|
assertThat(optionalBinding).hasSource(BasicModule.class, "optionalString");
|
|
|
|
Binding<Optional<Integer>> qualifiedOptionalBinding =
|
|
injector.getBinding(Key.get(new TypeLiteral<Optional<Integer>>() {}, TestQualifier.class));
|
|
assertThat(qualifiedOptionalBinding).hasProvidedValueThat().isEqualTo(Optional.empty());
|
|
assertThat(qualifiedOptionalBinding).hasSource(BasicModule.class, "optionalQualifiedInteger");
|
|
}
|
|
}
|