| 1 |
/* |
| 2 |
* Copyright 2018-2020 The JSpecify Authors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
package org.jspecify.annotations; |
| 17 |
|
| 18 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 |
import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 |
import static org.junit.jupiter.api.Assumptions.assumeFalse; |
| 21 |
import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 22 |
|
| 23 |
import java.lang.annotation.ElementType; |
| 24 |
import java.lang.annotation.Target; |
| 25 |
import java.util.Arrays; |
| 26 |
import java.util.HashSet; |
| 27 |
import java.util.Set; |
| 28 |
import java.util.stream.Collectors; |
| 29 |
import org.junit.jupiter.api.BeforeEach; |
| 30 |
import org.junit.jupiter.api.DisplayName; |
| 31 |
import org.junit.jupiter.api.Nested; |
| 32 |
import org.junit.jupiter.api.Test; |
| 33 |
|
| 34 |
@DisplayName("@NullMarked") |
| 35 |
class NullMarkedTest { |
| 36 |
static boolean isJava8() { |
| 37 |
String version = System.getProperty("java.version"); |
| 38 |
return version.startsWith("1.8"); |
| 39 |
} |
| 40 |
|
| 41 |
static Set<String> loadTargets() { |
| 42 |
return Arrays.stream(NullMarked.class.getAnnotation(Target.class).value()) |
| 43 |
.map(ElementType::toString) |
| 44 |
.collect(Collectors.toSet()); |
| 45 |
} |
| 46 |
|
| 47 |
@Nested |
| 48 |
@DisplayName("with Java 8") |
| 49 |
static class WithJava8 { |
| 50 |
@BeforeEach |
| 51 |
void assumeJava8() { |
| 52 |
assumeTrue(isJava8()); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
void onlyBasicReflectionWorks() { |
| 57 |
Object unused = NullMarked.class.getMethods(); |
| 58 |
/* |
| 59 |
* But reading the *annotations* on NullMarked would result in an exception: Those annotations |
| 60 |
* include @Target, which refers to MODULE, which doesn't exist under Java 8. |
| 61 |
* |
| 62 |
* (It happens to fail with ArrayStoreException.) |
| 63 |
*/ |
| 64 |
assertThrows(ArrayStoreException.class, NullMarked.class::getAnnotations); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@Nested |
| 69 |
@DisplayName("with Java 9+") |
| 70 |
static class WithJava9OrLater { |
| 71 |
@BeforeEach |
| 72 |
void assumeJava9OrLater() { |
| 73 |
assumeFalse(isJava8()); |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
void annotationIncludesModuleAsTarget() { |
| 78 |
Set<String> targets = loadTargets(); |
| 79 |
assertEquals( |
| 80 |
new HashSet<String>(Arrays.asList("TYPE", "METHOD", "CONSTRUCTOR", "PACKAGE", "MODULE")), |
| 81 |
targets); |
| 82 |
} |
| 83 |
} |
| 84 |
} |