Skip to content

Commit

Permalink
Prepare for 2024.1: Move Jdk Mock creation to testsdkcompat (#5968)
Browse files Browse the repository at this point in the history
2. The IdeaTestUtil.getMockJdk no longer returns an instance of MockJdk. It returns a regular JavaSdk instead.
Even more, the MockSdk is officially deprecated. Hence, in 2024.1 and newer we need to Use the regular SDKs
in tests and modify them with SdkModificator

JetBrains/intellij-community@7f05dd0#diff-5fb6bb2bf3104221b2ee04a10cdcf4424ea108b3f541e6b7eb828c721ea8817fL183
  • Loading branch information
tpasternak authored Jan 22, 2024
1 parent 0bda2bd commit c4440bd
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@
*/
package com.google.idea.blaze.java.sync.projectstructure;

import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.stream;

import com.google.common.collect.ImmutableMap;
import com.google.idea.blaze.base.BlazeIntegrationTestCase;
import com.google.idea.blaze.java.sync.sdk.BlazeJdkProvider;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.MockSdk;
import com.intellij.openapi.projectRoots.impl.UnknownSdkType;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.util.containers.MultiMap;
import java.io.File;
import java.util.Comparator;
import java.util.Optional;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.util.Comparator;
import java.util.Optional;

import static com.google.common.truth.Truth.assertThat;
import static com.google.idea.java.JavaSdkCompat.getNonJavaMockSdk;
import static com.google.idea.java.JavaSdkCompat.getUniqueMockJdk;
import static java.util.Arrays.stream;

/** Integration tests for {@link Jdks}. */
@RunWith(JUnit4.class)
public class JdksTest extends BlazeIntegrationTestCase {
Expand Down Expand Up @@ -116,6 +115,7 @@ public void testChooseHigherJdkForHigherLevel() {
assertThat(chosenSdk).isEqualTo(jdk8);
}

/** #api233 remove test */
@Test
public void testChooseDifferentSdkIfCurrentNotJdk() {
Sdk currentSdk = getNonJavaMockSdk();
Expand Down Expand Up @@ -300,19 +300,4 @@ private void registerJdkProvider(ImmutableMap<LanguageLevel, Sdk> jdkProvider) {
.orElse(null));
}

private static Sdk getUniqueMockJdk(LanguageLevel languageLevel) {
MockSdk jdk = (MockSdk) IdeaTestUtil.getMockJdk(languageLevel.toJavaVersion());
jdk.setName(jdk.getName() + "." + jdk.hashCode());
jdk.setHomePath(jdk.getHomePath() + "." + jdk.hashCode());
return jdk;
}

private static Sdk getNonJavaMockSdk() {
return new MockSdk(
/* name= */ "",
/* homePath= */ "",
/* versionString= */ "",
/* roots= */ MultiMap.empty(),
UnknownSdkType.getInstance(""));
}
}
12 changes: 12 additions & 0 deletions testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ java_library(
"android-studio-2022.2": glob([
"testcompat/v222/com/google/idea/sdkcompat/**/*.java",
"testcompat/v222/com/google/idea/testing/**/*.java",
"testcompat/v222/com/google/idea/java/**/*.java",
]),
"android-studio-2022.3": glob([
"testcompat/v223/com/google/idea/sdkcompat/**/*.java",
"testcompat/v223/com/google/idea/testing/**/*.java",
"testcompat/v223/com/google/idea/java/**/*.java",
]),
"android-studio-2023.1": glob([
"testcompat/v231/com/google/idea/sdkcompat/**/*.java",
"testcompat/v231/com/google/idea/testing/**/*.java",
"testcompat/v231/com/google/idea/java/**/*.java",
]),
"android-studio-2023.2": glob([
"testcompat/v232/com/google/idea/sdkcompat/**/*.java",
"testcompat/v232/com/google/idea/testing/**/*.java",
"testcompat/v232/com/google/idea/java/**/*.java",
]),
"clion-2021.3": glob([
"testcompat/v213/com/google/idea/sdkcompat/**/*.java",
Expand Down Expand Up @@ -100,34 +104,42 @@ java_library(
"intellij-2022.3": glob([
"testcompat/v223/com/google/idea/sdkcompat/**/*.java",
"testcompat/v223/com/google/idea/testing/**/*.java",
"testcompat/v223/com/google/idea/java/**/*.java",
]),
"intellij-ue-2022.3": glob([
"testcompat/v223/com/google/idea/sdkcompat/**/*.java",
"testcompat/v223/com/google/idea/testing/**/*.java",
"testcompat/v223/com/google/idea/java/**/*.java",
]),
"intellij-2023.1": glob([
"testcompat/v231/com/google/idea/sdkcompat/**/*.java",
"testcompat/v231/com/google/idea/testing/**/*.java",
"testcompat/v231/com/google/idea/java/**/*.java",
]),
"intellij-ue-2023.1": glob([
"testcompat/v231/com/google/idea/sdkcompat/**/*.java",
"testcompat/v231/com/google/idea/testing/**/*.java",
"testcompat/v231/com/google/idea/java/**/*.java",
]),
"intellij-2023.2": glob([
"testcompat/v232/com/google/idea/sdkcompat/**/*.java",
"testcompat/v232/com/google/idea/testing/**/*.java",
"testcompat/v232/com/google/idea/java/**/*.java",
]),
"intellij-ue-2023.2": glob([
"testcompat/v232/com/google/idea/sdkcompat/**/*.java",
"testcompat/v232/com/google/idea/testing/**/*.java",
"testcompat/v232/com/google/idea/java/**/*.java",
]),
"intellij-2023.3": glob([
"testcompat/v233/com/google/idea/sdkcompat/**/*.java",
"testcompat/v233/com/google/idea/testing/**/*.java",
"testcompat/v233/com/google/idea/java/**/*.java",
]),
"intellij-ue-2023.3": glob([
"testcompat/v233/com/google/idea/sdkcompat/**/*.java",
"testcompat/v233/com/google/idea/testing/**/*.java",
"testcompat/v233/com/google/idea/java/**/*.java",
]),
"default": [],
}),
Expand Down
48 changes: 48 additions & 0 deletions testing/testcompat/v223/com/google/idea/java/JavaSdkCompat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 The Bazel Authors. All rights reserved.
*
* 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.idea.java;

import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.MockSdk;
import com.intellij.openapi.projectRoots.impl.UnknownSdkType;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.util.containers.MultiMap;

/**
* Provides SDK compatibility shims for base plugin API classes, available to all IDEs during
* test-time.
*/
public final class JavaSdkCompat {
private JavaSdkCompat() {}
/** #api233 to inline */
public static Sdk getUniqueMockJdk(LanguageLevel languageLevel) {
MockSdk jdk = (MockSdk) IdeaTestUtil.getMockJdk(languageLevel.toJavaVersion());
jdk.setName(jdk.getName() + "." + jdk.hashCode());
jdk.setHomePath(jdk.getHomePath() + "." + jdk.hashCode());
return jdk;
}

/** #api233 to remove */
public static Sdk getNonJavaMockSdk() {
return new MockSdk(
/* name= */ "",
/* homePath= */ "",
/* versionString= */ "",
/* roots= */ MultiMap.empty(),
UnknownSdkType.getInstance(""));
}
}
48 changes: 48 additions & 0 deletions testing/testcompat/v231/com/google/idea/java/JavaSdkCompat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 The Bazel Authors. All rights reserved.
*
* 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.idea.java;

import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.MockSdk;
import com.intellij.openapi.projectRoots.impl.UnknownSdkType;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.util.containers.MultiMap;

/**
* Provides SDK compatibility shims for base plugin API classes, available to all IDEs during
* test-time.
*/
public final class JavaSdkCompat {
private JavaSdkCompat() {}
/** #api233 to inline */
public static Sdk getUniqueMockJdk(LanguageLevel languageLevel) {
MockSdk jdk = (MockSdk) IdeaTestUtil.getMockJdk(languageLevel.toJavaVersion());
jdk.setName(jdk.getName() + "." + jdk.hashCode());
jdk.setHomePath(jdk.getHomePath() + "." + jdk.hashCode());
return jdk;
}

/** #api233 to remove */
public static Sdk getNonJavaMockSdk() {
return new MockSdk(
/* name= */ "",
/* homePath= */ "",
/* versionString= */ "",
/* roots= */ MultiMap.empty(),
UnknownSdkType.getInstance(""));
}
}
48 changes: 48 additions & 0 deletions testing/testcompat/v232/com/google/idea/java/JavaSdkCompat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 The Bazel Authors. All rights reserved.
*
* 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.idea.java;

import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.MockSdk;
import com.intellij.openapi.projectRoots.impl.UnknownSdkType;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.util.containers.MultiMap;

/**
* Provides SDK compatibility shims for base plugin API classes, available to all IDEs during
* test-time.
*/
public final class JavaSdkCompat {
private JavaSdkCompat() {}
/** #api233 to inline */
public static Sdk getUniqueMockJdk(LanguageLevel languageLevel) {
MockSdk jdk = (MockSdk) IdeaTestUtil.getMockJdk(languageLevel.toJavaVersion());
jdk.setName(jdk.getName() + "." + jdk.hashCode());
jdk.setHomePath(jdk.getHomePath() + "." + jdk.hashCode());
return jdk;
}

/** #api233 to remove */
public static Sdk getNonJavaMockSdk() {
return new MockSdk(
/* name= */ "",
/* homePath= */ "",
/* versionString= */ "",
/* roots= */ MultiMap.empty(),
UnknownSdkType.getInstance(""));
}
}
48 changes: 48 additions & 0 deletions testing/testcompat/v233/com/google/idea/java/JavaSdkCompat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 The Bazel Authors. All rights reserved.
*
* 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.idea.java;

import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.MockSdk;
import com.intellij.openapi.projectRoots.impl.UnknownSdkType;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.util.containers.MultiMap;

/**
* Provides SDK compatibility shims for base plugin API classes, available to all IDEs during
* test-time.
*/
public final class JavaSdkCompat {
private JavaSdkCompat() {}
/** #api233 to inline */
public static Sdk getUniqueMockJdk(LanguageLevel languageLevel) {
MockSdk jdk = (MockSdk) IdeaTestUtil.getMockJdk(languageLevel.toJavaVersion());
jdk.setName(jdk.getName() + "." + jdk.hashCode());
jdk.setHomePath(jdk.getHomePath() + "." + jdk.hashCode());
return jdk;
}

/** #api233 to remove */
public static Sdk getNonJavaMockSdk() {
return new MockSdk(
/* name= */ "",
/* homePath= */ "",
/* versionString= */ "",
/* roots= */ MultiMap.empty(),
UnknownSdkType.getInstance(""));
}
}

0 comments on commit c4440bd

Please sign in to comment.