@Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { Map<String, Object> attributes = metadata .getAnnotationAttributes(ConditionalOnJava.class.getName()); Range range = (Range) attributes.get("range"); JavaVersion version = (JavaVersion) attributes.get("value"); return getMatchOutcome(range, JVM_VERSION, version); }
@Test public void boundsTests() throws Exception { testBounds(Range.EQUAL_OR_NEWER, JavaVersion.SEVEN, JavaVersion.SIX, true); testBounds(Range.EQUAL_OR_NEWER, JavaVersion.SEVEN, JavaVersion.SEVEN, true); testBounds(Range.EQUAL_OR_NEWER, JavaVersion.SEVEN, JavaVersion.EIGHT, false); testBounds(Range.OLDER_THAN, JavaVersion.SEVEN, JavaVersion.SIX, false); testBounds(Range.OLDER_THAN, JavaVersion.SEVEN, JavaVersion.SEVEN, false); testBounds(Range.OLDER_THAN, JavaVersion.SEVEN, JavaVersion.EIGHT, true); }
@Test public void equalOrNewerMessage() throws Exception { ConditionOutcome outcome = this.condition.getMatchOutcome(Range.EQUAL_OR_NEWER, JavaVersion.SEVEN, JavaVersion.SIX); assertThat(outcome.getMessage()) .isEqualTo("Required JVM version " + "1.6 or newer found 1.7"); }
@Test public void olderThanMessage() throws Exception { ConditionOutcome outcome = this.condition.getMatchOutcome(Range.OLDER_THAN, JavaVersion.SEVEN, JavaVersion.SIX); assertThat(outcome.getMessage()) .isEqualTo("Required JVM version " + "older than 1.6 found 1.7"); }
private String getJavaVersion(Class<?>... hiddenClasses) throws Exception { URL[] urls = ((URLClassLoader) getClass().getClassLoader()).getURLs(); URLClassLoader classLoader = new ClassHidingClassLoader(urls, hiddenClasses); Class<?> javaVersionClass = classLoader .loadClass(ConditionalOnJava.JavaVersion.class.getName()); Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, "getJavaVersion"); Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null); classLoader.close(); return javaVersion.toString(); }
@Test public void equalOrNewerMessage() throws Exception { ConditionOutcome outcome = this.condition.getMatchOutcome(Range.EQUAL_OR_NEWER, JavaVersion.SEVEN, JavaVersion.SIX); assertThat(outcome.getMessage(), equalTo("Required JVM version " + "1.6 or newer found 1.7")); }
@Test public void olderThanMessage() throws Exception { ConditionOutcome outcome = this.condition.getMatchOutcome(Range.OLDER_THAN, JavaVersion.SEVEN, JavaVersion.SIX); assertThat(outcome.getMessage(), equalTo("Required JVM version " + "older than 1.6 found 1.7")); }
@Bean @ConditionalOnMissingBean(name = "websocketContainerCustomizer") @ConditionalOnJava(JavaVersion.SEVEN) public TomcatWebSocketContainerCustomizer websocketContainerCustomizer() { return new TomcatWebSocketContainerCustomizer(); }
protected ConditionOutcome getMatchOutcome(Range range, JavaVersion runningVersion, JavaVersion version) { boolean match = runningVersion.isWithin(range, version); return new ConditionOutcome(match, getMessage(range, runningVersion, version)); }
private String getMessage(Range range, JavaVersion runningVersion, JavaVersion version) { String expected = String.format( range == Range.EQUAL_OR_NEWER ? "%s or newer" : "older than %s", version); return "Required JVM version " + expected + " found " + runningVersion; }
private void testBounds(Range range, JavaVersion runningVersion, JavaVersion version, boolean expected) { ConditionOutcome outcome = this.condition.getMatchOutcome(range, runningVersion, version); assertThat(outcome.isMatch()).as(outcome.getMessage()).isEqualTo(expected); }
private void testBounds(Range range, JavaVersion runningVersion, JavaVersion version, boolean expected) { ConditionOutcome outcome = this.condition.getMatchOutcome(range, runningVersion, version); assertThat(outcome.getMessage(), outcome.isMatch(), equalTo(expected)); }