|
Java example source code file (TypeTokenSubtypeTest.java)
This example Java source code file (TypeTokenSubtypeTest.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Learn more about this Java project at its project page.
The TypeTokenSubtypeTest.java Java example source code
/*
* Copyright (C) 2016 The Guava 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 com.google.common.reflect;
import junit.framework.TestCase;
import java.util.Comparator;
import java.util.List;
@AndroidIncompatible // lots of failures, possibly some related to bad equals() implementations?
public class TypeTokenSubtypeTest extends TestCase {
public void testOwnerTypeSubtypes() throws Exception {
new OwnerTypeSubtypingTests().testAllDeclarations();
}
public void testWildcardSubtypes() throws Exception {
new WildcardSubtypingTests().testAllDeclarations();
}
public void testSubtypeOfInnerClass_nonStaticAnonymousClass() {
TypeToken<?> supertype = new TypeToken.Shop>() {};
Class<?> subclass = new Mall().new Shop() {}.getClass();
assertTrue(TypeToken.of(subclass).isSubtypeOf(supertype));
}
public void testSubtypeOfInnerClass_nonStaticAnonymousClass_typeParameterOfOwnerTypeNotMatch() {
TypeToken<?> supertype = new TypeToken.Shop>() {};
Class<?> subclass = new Mall().new Shop() {}.getClass();
assertFalse(TypeToken.of(subclass).isSubtypeOf(supertype));
}
public void testSubtypeOfInnerClass_nonStaticAnonymousClass_typeParameterOfInnerTypeNotMatch() {
TypeToken<?> supertype = new TypeToken.Shop>() {};
Class<?> subclass = new Mall().new Shop() {}.getClass();
assertFalse(TypeToken.of(subclass).isSubtypeOf(supertype));
}
public static void testSubtypeOfInnerClass_staticAnonymousClass() {
TypeToken<?> supertype = new TypeToken.Shop>() {};
Class<?> subclass = new Mall().new Shop() {}.getClass();
assertTrue(TypeToken.of(subclass).isSubtypeOf(supertype));
}
public static void testSubtypeOfStaticAnonymousClass() {
Class<?> superclass = new Mall().new Shop() {}.getClass();
assertTrue(TypeToken.of(superclass).isSubtypeOf(superclass));
assertFalse(TypeToken.of(new Mall<Outdoor>().new Shop() {}.getClass())
.isSubtypeOf(superclass));
}
public void testSubtypeOfNonStaticAnonymousClass() {
Class<?> superclass = new Mall().new Shop() {}.getClass();
assertTrue(TypeToken.of(superclass).isSubtypeOf(superclass));
assertFalse(TypeToken.of(new Mall<Outdoor>().new Shop() {}.getClass())
.isSubtypeOf(superclass));
}
private static class OwnerTypeSubtypingTests extends SubtypeTester {
@TestSubtype
public Mall<Outdoor>.Shop innerTypeIsSubtype(
Mall<Outdoor>.Retailer retailer) {
return isSubtype(retailer);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public Mall<Outdoor>.Shop innerTypeIsSubtype_supertypeWithWildcard(
Mall<Outdoor>.Retailer retailer) {
return isSubtype(retailer);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public Mall<?>.Shop innerTypeIsSubtype_withWildcards(
Mall<? extends Indoor>.Retailer retailer) {
return isSubtype(retailer);
}
@TestSubtype
public Mall<Outdoor>.Shop ownerTypeIsSubtype(
Outlet<Outdoor>.Shop shop) {
return isSubtype(shop);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public Mall<? extends Outdoor>.Shop ownerTypeIsSubtype_supertypeWithWildcard(
Outlet<Outdoor>.Shop shop) {
return isSubtype(shop);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public Mall<?>.Shop ownerTypeIsSubtype_withWildcards(
Outlet<? extends Outdoor>.Shop shop) {
return isSubtype(shop);
}
@TestSubtype
public Mall<Outdoor>.Shop bothOwnerTypeAndInnerTypeAreSubtypes(
Outlet<Outdoor>.Retailer retailer) {
return isSubtype(retailer);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public Mall<? super Outdoor>.Shop
bothOwnerTypeAndInnerTypeAreSubtypes_supertypeWithWildcard(
Outlet<Outdoor>.Retailer retailer) {
return isSubtype(retailer);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public Mall<? super Outdoor>.Shop
bothOwnerTypeAndInnerTypeAreSubtypes_withWildcards(
Outlet<Outdoor>.Retailer retailer) {
return isSubtype(retailer);
}
@TestSubtype
public Mall<Outdoor>.Shop ownerTypeDoesNotMatch(
Mall<Indoor>.Shop shop) {
return notSubtype(shop);
}
@TestSubtype
public Mall<Outdoor>.Shop ownerTypeDoesNotMatch_subtypeWithWildcard(
Mall<? extends Outdoor>.Shop shop) {
return notSubtype(shop);
}
@TestSubtype
public Mall<? extends Outdoor>.Shop ownerTypeDoesNotMatch_supertypeWithWildcard(
Mall<?>.Shop shop) {
return notSubtype(shop);
}
@TestSubtype
public Mall<Outdoor>.Retailer innerTypeDoesNotMatch(
Mall<Outdoor>.Shop shop) {
return notSubtype(shop);
}
@TestSubtype
public Mall<Outdoor>.Shop innerTypeDoesNotMatch_subtypeWithWildcard(
Mall<Outdoor>.Shop shop) {
return notSubtype(shop);
}
@TestSubtype
public Mall<Outdoor>.Shop innerTypeDoesNotMatch_supertypeWithWildcard(
Mall<Outdoor>.Shop shop) {
return notSubtype(shop);
}
@TestSubtype(suppressGetSubtype = true)
public ConsumerFacing<Electronics> supertypeIsNestedClass(
Mall<Indoor>.Retailer shop) {
return isSubtype(shop);
}
@TestSubtype
public ConsumerFacing<Grocery> nestedClassIsNotSupertypeDueToTypeParameter(
Mall<Indoor>.Shop shop) {
return notSubtype(shop);
}
@TestSubtype
public ConsumerFacing<Grocery> nestedClassIsNotSupertype(
Mall<Indoor>.Shop shop) {
return notSubtype(shop);
}
@TestSubtype(suppressGetSubtype = true)
public Comparator<Electronics> supertypeIsTopLevelClass(
Mall<Indoor>.Retailer shop) {
return isSubtype(shop);
}
@TestSubtype
public Comparator<Electronics> topLevelClassIsNotSupertypeDueToTypeParameter(
Mall<Indoor>.Retailer shop) {
return notSubtype(shop);
}
@TestSubtype
public Comparator<Electronics> topLevelClassIsNotSupertype(
Mall<Indoor>.Shop shop) {
return notSubtype(shop);
}
}
private static class WildcardSubtypingTests extends SubtypeTester {
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public <T> Iterable supertypeWithWildcardUpperBound(List list) {
return isSubtype(list);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public <T> Iterable supertypeWithWildcardUpperBound_notMatch(List list) {
return notSubtype(list);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public <T> Iterable supertypeWithWildcardULowerBound(List list) {
return isSubtype(list);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public <T> Iterable supertypeWithWildcardULowerBound_notMatch(List list) {
return notSubtype(list);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public <T> Iterable wildcardsMatchByUpperBound(List list) {
return isSubtype(list);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public <T> Iterable wildCardsDoNotMatchByUpperBound(List list) {
return notSubtype(list);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public <T> Iterable wildcardsMatchByLowerBound(
List<? super CharSequence> list) {
return isSubtype(list);
}
@TestSubtype(suppressGetSupertype = true, suppressGetSubtype = true)
public <T> Iterable wildCardsDoNotMatchByLowerBound(
List<? super String> list) {
return notSubtype(list);
}
}
// TODO(benyu): migrate all subtyping tests from TypeTokenTest to this class using SubtypeTester.
private interface Outdoor {}
private interface Indoor {}
private interface Grocery {}
private interface Electronics {}
private interface ConsumerFacing<T> {}
private static class Mall<T> {
class Shop<ProductT> {}
abstract class Retailer<ProductT> extends Shop
implements Comparator<ProductT>, ConsumerFacing {}
}
private static class Outlet<T> extends Mall {}
}
Other Java examples (source code examples)
Here is a short list of links related to this Java TypeTokenSubtypeTest.java source code file:
|