Skip to content

Commit

Permalink
Bump minimum required Java from 17 to 21
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed Sep 24, 2024
1 parent 6ed1ae8 commit d46baac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
* Bump minimum required Java from 17 to 21
* Bump version of Gradle used to build projects from 8.9 to 8.10.2
* Bump VS Code engine from 1.92.0 to 1.93.0

Expand Down
9 changes: 5 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { workspace } from "vscode";
export async function findJavaExecutable(): Promise<IJavaRuntime> {
const javaHomeProperty: string = 'vividus-studio.java-home';
const userJavaHome: string = workspace.getConfiguration().get(javaHomeProperty) as string;
const minJavaVersion: number = 21;

if (userJavaHome != null) {
const runtime: IJavaRuntime = await getRuntime(userJavaHome, { withVersion: true }) as IJavaRuntime;
Expand All @@ -12,21 +13,21 @@ export async function findJavaExecutable(): Promise<IJavaRuntime> {
throw new Error(`Unable to find Java at location specified by ${javaHomeProperty} user property: ${userJavaHome}`);
}

if (runtime.version?.major as number < 17) {
if (runtime.version?.major as number < minJavaVersion) {
throw new Error(`The ${javaHomeProperty} user property points to Java ${runtime.version?.java_version} installation,`
+ ` but Java 17 or higher is required`)
+ ` but Java ${minJavaVersion} or higher is required`)
}

return runtime;
}

const runtime: IJavaRuntime = (await findRuntimes({ withVersion: true })).find(runtime => {
const version: IJavaVersion = runtime.version as IJavaVersion;
return version.major >= 17
return version.major >= minJavaVersion
}) as IJavaRuntime;

if (!runtime) {
throw new Error('Unable to find Java 17 or higher installation');
throw new Error(`Unable to find Java ${minJavaVersion} or higher installation`);
}

return runtime;
Expand Down
34 changes: 17 additions & 17 deletions src/test/suite/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,43 @@ suite('Utils', () => {
sinon.restore()
})

const java13: IJavaRuntime = {
const java17: IJavaRuntime = {
homedir: 'home_dir',
version: {
java_version: '13.0.2',
major: 13
java_version: '17.0.2',
major: 17
}
}

const java17: IJavaRuntime = {
const java21: IJavaRuntime = {
homedir: 'home_dir',
version: {
java_version: '17.0.2',
major: 17
java_version: '21.0.4',
major: 21
}
}

const jdkUtilsModule = require('jdk-utils')

test('Should return Java 17 installation pointed by vividus-studio.java-home property', async () => {
const getRuntimeStub: SinonStub = sinon.stub(jdkUtilsModule, 'getRuntime').returns(java17)
test('Should return Java 21 installation pointed by vividus-studio.java-home property', async () => {
const getRuntimeStub: SinonStub = sinon.stub(jdkUtilsModule, 'getRuntime').returns(java21)

sinon.stub(workspace, 'getConfiguration').returns(<WorkspaceConfiguration>{
get: (section: string) => { return 'home_dir' }
});

assert.equal(java17, await findJavaExecutable())
assert.equal(java21, await findJavaExecutable())
assert.equal(true, getRuntimeStub.calledWith('home_dir', { withVersion: true }))
})

test('Should return Java 17 system installation', async () => {
const getRuntimeStub: SinonStub = sinon.stub(jdkUtilsModule, 'findRuntimes').returns([java17])
test('Should return Java 21 system installation', async () => {
const getRuntimeStub: SinonStub = sinon.stub(jdkUtilsModule, 'findRuntimes').returns([java21])

sinon.stub(workspace, 'getConfiguration').returns(<WorkspaceConfiguration>{
get: (section: string) => { return null }
});

assert.equal(java17, await findJavaExecutable())
assert.equal(java21, await findJavaExecutable())
assert.equal(true, getRuntimeStub.calledWith({ withVersion: true }))
})

Expand All @@ -66,27 +66,27 @@ suite('Utils', () => {
})

test('Should fail if vividus-studio.java-home property points to old Java versions', async () => {
const getRuntimeStub: SinonStub = sinon.stub(jdkUtilsModule, 'getRuntime').returns(java13)
const getRuntimeStub: SinonStub = sinon.stub(jdkUtilsModule, 'getRuntime').returns(java17)

sinon.stub(workspace, 'getConfiguration').returns(<WorkspaceConfiguration>{
get: (section: string) => { return 'home_dir' }
});

await assert.rejects(findJavaExecutable(),
{ message: 'The vividus-studio.java-home user property points to Java 13.0.2 installation, but Java 17 or higher is required' })
{ message: 'The vividus-studio.java-home user property points to Java 17.0.2 installation, but Java 21 or higher is required' })
assert.equal(true, getRuntimeStub.calledOnce)
assert.equal(true, getRuntimeStub.calledWith('home_dir', { withVersion: true }))
})

test('Should fail if there is no Java 17 installed in the system', async () => {
const getRuntimeStub: SinonStub = sinon.stub(jdkUtilsModule, 'findRuntimes').returns([java13])
test('Should fail if there is no Java 21 installed in the system', async () => {
const getRuntimeStub: SinonStub = sinon.stub(jdkUtilsModule, 'findRuntimes').returns([java17])

sinon.stub(workspace, 'getConfiguration').returns(<WorkspaceConfiguration>{
get: (section: string) => { return null }
});

await assert.rejects(findJavaExecutable(),
{ message: 'Unable to find Java 17 or higher installation' })
{ message: 'Unable to find Java 21 or higher installation' })
assert.equal(true, getRuntimeStub.calledOnce)
assert.equal(true, getRuntimeStub.calledWith({ withVersion: true }))
})
Expand Down

0 comments on commit d46baac

Please sign in to comment.