Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JDK Modules Export to JUnit Test to run Integration Tests via IDE #864

Merged
merged 7 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [2023.3.3]

### `Tests` enhancements
- Add JDK Export Properties to JUnit Tests to run Integration Tests [#864](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/864)
- Load Properties from Files and Environment [#864](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/864)

## [2023.3.2]

### `Project Import` enhancements
Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
- Rustam Burmenskyi
- Oleksandr Dihtiar
- Andrei Lisetskii
- Eugeni Kalenchuk
- Eugeni Kalenchuk
- Stefan Kruk
13 changes: 10 additions & 3 deletions src/com/intellij/idea/plugin/hybris/common/HybrisConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ object HybrisConstants {
const val PROPERTY_OPTIONAL_CONFIG_DIR = "hybris.optional.config.dir"
const val PROPERTY_LANG_PACKS = "lang.packs"
const val PROPERTY_IMPEX_HEADER_REPLACEMENT = "impex.header.replacement"
const val PROPERTY_ENV_PROPERTY_PREFIX = "env.properties.prefix"

const val PROPERTY_STANDALONE_JDKMODULESEXPORTS = "standalone.jdkmodulesexports";

const val DEFAULT_LANGUAGE_ISOCODE = "en"

Expand Down Expand Up @@ -301,13 +304,17 @@ object HybrisConstants {
const val LIB_DIRECTORY = "lib"
const val BIN_DIRECTORY = "bin"
const val RESOURCES_DIRECTORY = "resources"
const val LOCAL_PROPERTIES = "local.properties"
const val PROJECT_PROPERTIES = "project.properties"
const val LOCAL_PROPERTIES_FILE = "local.properties"
const val PROJECT_PROPERTIES_FILE = "project.properties"
const val ENV_PROPERTIES_FILE = "env.properties"
const val ADVANCED_PROPERTIES_FILE = "advanced.properties"
const val SPRING_WEB_FILE_SET_NAME = "web application context"
const val APPLICATION_CONTEXT_SPRING_FILES = "application-context"
const val ADDITIONAL_WEB_SPRING_CONFIG_FILES = "additionalWebSpringConfigs"
const val GLOBAL_CONTEXT_SPRING_FILES = "global-context"
const val HYBRIS_CONFIG_DIR_KEY = "HYBRIS_CONFIG_DIR"
const val HYBRIS_CONFIG_DIR_ENV = "HYBRIS_CONFIG_DIR"
const val HYBRIS_RUNTIME_PROPERTIES_ENV = "HYBRIS_RUNTIME_PROPERTIES"
const val HYBRIS_OPT_CONFIG_DIR_ENV = "HYBRIS_OPT_CONFIG_DIR"
const val HYBRIS_API_VERSION_KEY = "version.api"
const val HYBRIS_VERSION_KEY = "version"
const val JAVADOC_FALLBACK_URL = "https://help.sap.com/docs/SAP_COMMERCE/c5613bd3cc9942efb74d017b40eb0892/179bbc9b35274d7ca784e46b3beb40b2.html"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ private String getDebugPort(
) {
final var commonIdeaService = CommonIdeaService.getInstance();
final ConfigModuleDescriptor configDescriptor = hybrisProjectDescriptor.getConfigHybrisModuleDescriptor();
String port = findPortProperty(configDescriptor, HybrisConstants.LOCAL_PROPERTIES, cache);
String port = findPortProperty(configDescriptor, HybrisConstants.LOCAL_PROPERTIES_FILE, cache);

if (port != null) {
return port;
}
final PlatformModuleDescriptor platformDescriptor = commonIdeaService.getPlatformDescriptor(hybrisProjectDescriptor);

if (platformDescriptor != null) {
port = findPortProperty(platformDescriptor, HybrisConstants.PROJECT_PROPERTIES, cache);
port = findPortProperty(platformDescriptor, HybrisConstants.PROJECT_PROPERTIES_FILE, cache);

if (port != null) {
return port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ private static String findBuildCompilerProperty(
final ConfigModuleDescriptor configDescriptor = descriptor.getConfigHybrisModuleDescriptor();

if (configDescriptor != null) {
propertyFiles.add(new File(configDescriptor.getModuleRootDirectory(), HybrisConstants.LOCAL_PROPERTIES));
propertyFiles.add(new File(configDescriptor.getModuleRootDirectory(), HybrisConstants.LOCAL_PROPERTIES_FILE));
}
final PlatformModuleDescriptor platformDescriptor = descriptor.getPlatformHybrisModuleDescriptor();
propertyFiles.add(new File(platformDescriptor.getModuleRootDirectory(), HybrisConstants.ADVANCED_PROPERTIES));
propertyFiles.add(new File(platformDescriptor.getModuleRootDirectory(), HybrisConstants.PROJECT_PROPERTIES));
propertyFiles.add(new File(platformDescriptor.getModuleRootDirectory(), HybrisConstants.PROJECT_PROPERTIES_FILE));

return cache.findPropertyInFiles(propertyFiles, HybrisConstants.PROPERTY_BUILD_COMPILER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class DefaultSpringConfigurator : SpringConfigurator {
moduleDescriptor.addSpringFile(advancedProperties.absolutePath)

hybrisProjectDescriptor.configHybrisModuleDescriptor
?.let { File(it.moduleRootDirectory, HybrisConstants.LOCAL_PROPERTIES) }
?.let { File(it.moduleRootDirectory, HybrisConstants.LOCAL_PROPERTIES_FILE) }
?.let { moduleDescriptor.addSpringFile(it.absolutePath) }
}

Expand Down Expand Up @@ -120,7 +120,7 @@ class DefaultSpringConfigurator : SpringConfigurator {
moduleDescriptor: YRegularModuleDescriptor
) {
val projectProperties = Properties()
val propFile = File(moduleDescriptor.moduleRootDirectory, HybrisConstants.PROJECT_PROPERTIES)
val propFile = File(moduleDescriptor.moduleRootDirectory, HybrisConstants.PROJECT_PROPERTIES_FILE)
moduleDescriptor.addSpringFile(propFile.absolutePath)
try {
projectProperties.load(propFile.inputStream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private File getExpectedConfigDir(final PlatformModuleDescriptor platformHybrisM
if (!expectedConfigDir.isDirectory()) {
return null;
}
final File propertiesFile = new File(expectedConfigDir, HybrisConstants.LOCAL_PROPERTIES);
final File propertiesFile = new File(expectedConfigDir, HybrisConstants.LOCAL_PROPERTIES_FILE);
if (!propertiesFile.exists()) {
return expectedConfigDir;
}
Expand All @@ -266,7 +266,7 @@ private File getExpectedConfigDir(final PlatformModuleDescriptor platformHybrisM
return expectedConfigDir;
}

String hybrisConfig = (String) properties.get(HybrisConstants.HYBRIS_CONFIG_DIR_KEY);
String hybrisConfig = (String) properties.get(HybrisConstants.HYBRIS_CONFIG_DIR_ENV);
if (hybrisConfig == null) {
return expectedConfigDir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean isConfigModule(@NotNull final File file) {
Validate.notNull(file);

return new File(file, HybrisConstants.LOCAL_EXTENSIONS_XML).isFile()
&& new File(file, HybrisConstants.LOCAL_PROPERTIES).isFile();
&& new File(file, HybrisConstants.LOCAL_PROPERTIES_FILE).isFile();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ interface PropertiesService {
@JvmStatic
fun getInstance(project: Project): PropertiesService? = project.getService(PropertiesService::class.java)
}

fun getProperty(key: String): IProperty?
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
package com.intellij.idea.plugin.hybris.properties.impl

import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.common.HybrisConstants.PROPERTY_ENV_PROPERTY_PREFIX
import com.intellij.idea.plugin.hybris.common.HybrisConstants.ADVANCED_PROPERTIES_FILE
import com.intellij.idea.plugin.hybris.common.HybrisConstants.ENV_PROPERTIES_FILE
import com.intellij.idea.plugin.hybris.common.HybrisConstants.HYBRIS_OPT_CONFIG_DIR_ENV
import com.intellij.idea.plugin.hybris.common.HybrisConstants.HYBRIS_RUNTIME_PROPERTIES_ENV
import com.intellij.idea.plugin.hybris.common.HybrisConstants.LOCAL_PROPERTIES_FILE
import com.intellij.idea.plugin.hybris.common.HybrisConstants.PROJECT_PROPERTIES_FILE
import com.intellij.idea.plugin.hybris.common.yExtensionName
import com.intellij.idea.plugin.hybris.properties.PropertiesService
import com.intellij.lang.properties.IProperty
Expand Down Expand Up @@ -47,8 +54,7 @@ class PropertiesServiceImpl(val project: Project) : PropertiesService {
private val optionalPropertiesFilePattern = Pattern.compile("([1-9]\\d)-(\\w*)\\.properties")

override fun getLanguages(): Set<String> {
val languages = findMacroProperty(HybrisConstants.PROPERTY_LANG_PACKS)
?.value
val languages = findProperty(HybrisConstants.PROPERTY_LANG_PACKS)
?.split(",")
?.map { it.trim() }
?: emptyList()
Expand All @@ -64,14 +70,17 @@ class PropertiesServiceImpl(val project: Project) : PropertiesService {
override fun containsLanguage(language: String, supportedLanguages: Set<String>) = supportedLanguages
.contains(language.lowercase())

override fun findAutoCompleteProperties(query: String): List<IProperty> = findAllProperties()
.filter { it.key != null && it.key!!.contains(query) || query.isBlank() }

override fun getProperty(key: String): IProperty? = findAllProperties()
.firstOrNull { it.key != null && it.key == key }
override fun findProperty(key: String): String? = findAllProperties()
.filter { it.key == key }
.map { it.value }
.firstOrNull()

override fun findAutoCompleteProperties(query: String): List<IProperty> = findAllIProperties()
.filter { it.key != null && it.key!!.contains(query) || query.isBlank() }

override fun findMacroProperty(query: String): IProperty? {
val allProps = findAllProperties()
val allProps = findAllIProperties()
if (allProps.isEmpty()) {
return null;
}
Expand All @@ -83,49 +92,8 @@ class PropertiesServiceImpl(val project: Project) : PropertiesService {
return filteredProps.reduce { one, two -> if (one.key!!.length > two.key!!.length) one else two }
}

override fun findProperty(query: String): String? = findMacroProperty(query)
?.value
?.replace("\\", "")

private fun resolvePropertyValue(value: String?): String {
return resolvePropertyValue(value, HashMap())
}

private fun resolvePropertyValue(value: String?, resolvedProperties: MutableMap<String, String>): String {
if (value == null) {
return ""
}
var index = 0
val sb = StringBuilder()
while (index != -1) {
val startIndex = value.indexOf(nestedPropertyPrefix, index)
val endIndex = value.indexOf(nestedPropertySuffix, startIndex)
if (startIndex != -1 && endIndex != -1) {
sb.append(value, index, startIndex)
val propertyKey = value.substring(startIndex + nestedPropertyPrefix.length, endIndex)
var resolvedValue: String? = resolvedProperties[propertyKey]
if (resolvedValue != null) {
sb.append(resolvedValue)
} else {
val property = findMacroProperty(propertyKey)
if (property != null) {
resolvedValue = resolvePropertyValue(property.value)
sb.append(resolvedValue)
resolvedProperties[propertyKey] = resolvedValue
} else {
sb.append(nestedPropertyPrefix).append(propertyKey).append(nestedPropertySuffix)
}
}
index = endIndex + 1
} else {
sb.append(value, index, value.length)
index = startIndex
}
}
return sb.toString()
}

private fun findAllProperties(): List<IProperty> {
private fun findAllIProperties(): List<IProperty> {
val result = LinkedHashMap<String, IProperty>()
val configModule = obtainConfigModule() ?: return emptyList()
val platformModule = obtainPlatformModule() ?: return emptyList()
Expand All @@ -134,61 +102,116 @@ class PropertiesServiceImpl(val project: Project) : PropertiesService {
var advancedPropsFile: PropertiesFile? = null
var localPropsFile: PropertiesFile? = null

var propertiesFiles = ArrayList<PropertiesFile>();

// Ignore Order and production.properties for now as `developer.mode` should be set to true for development anyway
FileTypeIndex.getFiles(PropertiesFileType.INSTANCE, scope)
.mapNotNull { PsiManager.getInstance(project).findFile(it) }
.mapNotNull { it as? PropertiesFile }
.forEach { file ->
when (file.name) {
"env.properties" -> envPropsFile = file
"advanced.properties" -> advancedPropsFile = file
"local.properties" -> localPropsFile = file
else -> {
for (property in file.properties) {
if (property.key != null) {
result[property.key!!] = property
}
}
}
ENV_PROPERTIES_FILE -> envPropsFile = file
ADVANCED_PROPERTIES_FILE -> advancedPropsFile = file
LOCAL_PROPERTIES_FILE -> localPropsFile = file
PROJECT_PROPERTIES_FILE -> propertiesFiles.add(file)
}
}
addPropertyFile(result, envPropsFile)
addPropertyFile(result, advancedPropsFile)
addPropertyFile(result, localPropsFile)

val optDir = result[HybrisConstants.PROPERTY_OPTIONAL_CONFIG_DIR]
addOptionalConfiguration(result, optDir)
envPropsFile?.let { propertiesFiles.add(0, it) }
advancedPropsFile?.let { propertiesFiles.add(1, it) }
localPropsFile?.let { propertiesFiles.add(it) }

return ArrayList(result.values)
}
propertiesFiles.forEach { file -> addPropertyFile(result, file) }

loadHybrisRuntimeProperties(result)
loadHybrisOptionalConfigDir(result)

return ArrayList(result.values)
}

private fun addOptionalConfiguration(result: LinkedHashMap<String, IProperty>, optDir: IProperty?) {
if (optDir == null) {
return
private fun findAllProperties(): LinkedHashMap<String, String> = findAllIProperties()
.filter { it.value != null && it.key != null }
.associateTo(LinkedHashMap()) { it.key!! to it.value!! }
.let { properties ->
addEnvironmentProperties(properties)
properties.filter { it.value.contains(nestedPropertyPrefix) }
.forEach { replacePlaceholder(properties, it.key, HashSet<String>()) }
return properties
}
val dir = File(optDir.value)
if (!dir.isDirectory) {
return

private fun addEnvironmentProperties(properties: MutableMap<String, String>) {
properties[PROPERTY_ENV_PROPERTY_PREFIX]?.let { prefix ->
System.getenv()
.filter { it.key.startsWith(prefix) }
.forEach() {
val envPropertyKey = it.key.substring(prefix.length)
val key = envPropertyKey.replace("__", "##")
.replace("_",".")
.replace("##","_")
properties[key] = it.value
}
}
val matchedFiles = dir.listFiles { _, name -> optionalPropertiesFilePattern.matcher(name).matches() }
?: return
val propertyFiles = TreeMap<String, File>()
Arrays.stream(matchedFiles).forEach { file -> propertyFiles[file.name] = file }

propertyFiles.values.forEach { file ->
val virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file)
if (virtualFile == null || !virtualFile.exists()) {
return
}
val psiFile = PsiManager.getInstance(project).findFile(virtualFile)
if (psiFile is PropertiesFile) {
addPropertyFile(result, psiFile as PropertiesFile?)
}

private fun replacePlaceholder(result: LinkedHashMap<String, String>, key: String, visitedProperties: MutableSet<String>) {

var lastIndex = 0

val value = result[key] ?: ""
var replacedValue = value

while (true) {
val startIndex = value.indexOf(nestedPropertyPrefix, lastIndex)
val endIndex = value.indexOf(nestedPropertySuffix, startIndex + 1)
lastIndex = endIndex + nestedPropertyPrefix.length

if (startIndex == -1 || endIndex == -1)
break

val placeHolder = value.substring(startIndex, endIndex + nestedPropertySuffix.length)
val nestedKey = placeHolder.substring(nestedPropertyPrefix.length, placeHolder.length - nestedPropertySuffix.length)
if (visitedProperties.contains(nestedKey))
continue
visitedProperties.add(nestedKey)
val nestedValue: String? = result[nestedKey]
nestedValue?.let {
var newValue = it
if (it.contains(nestedPropertyPrefix)) {
replacePlaceholder(result, nestedKey, visitedProperties)
newValue = result[nestedKey] ?: ""
}

if (!newValue.contains(nestedPropertyPrefix)) {
replacedValue = replacedValue.replace(placeHolder, newValue)
}
}

}
result[key] = replacedValue;
}

private fun addPropertyFile(result: LinkedHashMap<String, IProperty>, propertiesFile: PropertiesFile?) {
private fun loadHybrisOptionalConfigDir(result: MutableMap<String, IProperty>) = (System.getenv(HYBRIS_OPT_CONFIG_DIR_ENV)
?: result[HybrisConstants.PROPERTY_OPTIONAL_CONFIG_DIR]?.value)
?.let { File(it) }
?.takeIf { it.isDirectory }
?.listFiles { _, name -> optionalPropertiesFilePattern.matcher(name).matches() }
?.associateByTo(TreeMap()) { it.name }
?.values
?.mapNotNull { toPropertiesFile(it) }
?.forEach { addPropertyFile(result, it) }

private fun loadHybrisRuntimeProperties(result: MutableMap<String, IProperty>) = System.getenv(HYBRIS_RUNTIME_PROPERTIES_ENV)
?.takeIf { it.isNotBlank() }
?.let { File(it) }
?.let { toPropertiesFile(it) }
?.let { addPropertyFile(result, it) }

private fun toPropertiesFile(file: File) = LocalFileSystem.getInstance().findFileByIoFile(file)
?.takeIf { it.exists() }
?.let { PsiManager.getInstance(project).findFile(it) }
?.let { it as? PropertiesFile }

private fun addPropertyFile(result: MutableMap<String, IProperty>, propertiesFile: PropertiesFile?) {
if (propertiesFile == null) {
return
}
Expand All @@ -202,9 +225,9 @@ class PropertiesServiceImpl(val project: Project) : PropertiesService {
private fun createSearchScope(configModule: Module, platformModule: Module): GlobalSearchScope {
val projectPropertiesScope = GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.everythingScope(project), PropertiesFileType.INSTANCE)
.filter { it.name == "project.properties" }
val envPropertiesScope = platformModule.moduleContentScope.filter { it.name == "env.properties" }
val advancedPropertiesScope = platformModule.moduleContentScope.filter { it.name == "advanced.properties" }
val localPropertiesScope = configModule.moduleContentScope.filter { it.name == "local.properties" }
val envPropertiesScope = platformModule.moduleContentScope.filter { it.name == ENV_PROPERTIES_FILE }
val advancedPropertiesScope = platformModule.moduleContentScope.filter { it.name == ADVANCED_PROPERTIES_FILE }
val localPropertiesScope = configModule.moduleContentScope.filter { it.name == LOCAL_PROPERTIES_FILE }

return projectPropertiesScope.or(envPropertiesScope.or(advancedPropertiesScope.or(localPropertiesScope)))
}
Expand Down
Loading