2016-04-12 22:34:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 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.gson.internal;
|
|
|
|
|
2023-01-31 20:20:54 +01:00
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
2022-12-22 15:04:16 +01:00
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
2016-04-12 22:34:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test unsafe allocator instantiation
|
|
|
|
* @author Ugljesa Jovanovic
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
public final class UnsafeAllocatorInstantiationTest {
|
2016-04-12 22:34:16 +02:00
|
|
|
|
|
|
|
public interface Interface {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static abstract class AbstractClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ConcreteClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-17 18:05:18 +02:00
|
|
|
* Ensure that an {@link AssertionError} is thrown when trying
|
2016-04-12 22:34:16 +02:00
|
|
|
* to instantiate an interface
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-04-17 18:05:18 +02:00
|
|
|
public void testInterfaceInstantiation() throws Exception {
|
2016-04-12 22:34:16 +02:00
|
|
|
try {
|
2022-09-30 00:59:47 +02:00
|
|
|
UnsafeAllocator.INSTANCE.newInstance(Interface.class);
|
2016-04-12 22:34:16 +02:00
|
|
|
fail();
|
2022-04-17 18:05:18 +02:00
|
|
|
} catch (AssertionError e) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(e).hasMessageThat().startsWith("UnsafeAllocator is used for non-instantiable type");
|
2016-04-12 22:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-17 18:05:18 +02:00
|
|
|
* Ensure that an {@link AssertionError} is thrown when trying
|
2016-04-12 22:34:16 +02:00
|
|
|
* to instantiate an abstract class
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-04-17 18:05:18 +02:00
|
|
|
public void testAbstractClassInstantiation() throws Exception {
|
2016-04-12 22:34:16 +02:00
|
|
|
try {
|
2022-09-30 00:59:47 +02:00
|
|
|
UnsafeAllocator.INSTANCE.newInstance(AbstractClass.class);
|
2016-04-12 22:34:16 +02:00
|
|
|
fail();
|
2022-04-17 18:05:18 +02:00
|
|
|
} catch (AssertionError e) {
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(e).hasMessageThat().startsWith("UnsafeAllocator is used for non-instantiable type");
|
2016-04-12 22:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that no exception is thrown when trying to instantiate a concrete class
|
|
|
|
*/
|
2022-12-22 15:04:16 +01:00
|
|
|
@Test
|
2022-04-17 18:05:18 +02:00
|
|
|
public void testConcreteClassInstantiation() throws Exception {
|
2022-09-30 00:59:47 +02:00
|
|
|
ConcreteClass instance = UnsafeAllocator.INSTANCE.newInstance(ConcreteClass.class);
|
2023-01-31 20:20:54 +01:00
|
|
|
assertThat(instance).isNotNull();
|
2016-04-12 22:34:16 +02:00
|
|
|
}
|
|
|
|
}
|