Skip to content

Commit

Permalink
IJMP-1522 getResource has been replaced to getResourceAsStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Katsiaryna Tsytsenia authored and Katsiaryna Tsytsenia committed May 7, 2024
1 parent adb8e9d commit 790ada8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.util.ReflectionUtil
import org.apache.commons.io.FileUtils
import org.zowe.explorer.config.ConfigService
import org.zowe.explorer.config.connect.*
import org.zowe.explorer.config.connect.ui.zosmf.ConnectionDialogState
Expand All @@ -43,9 +41,7 @@ import org.zowe.explorer.zowe.ZOWE_CONFIG_NAME
import org.zowe.kotlinsdk.annotations.ZVersion
import org.zowe.kotlinsdk.zowe.config.ZoweConfig
import org.zowe.kotlinsdk.zowe.config.parseConfigJson
import java.io.File
import java.net.URI
import java.net.URL
import java.io.*
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.nio.file.*
Expand Down Expand Up @@ -73,8 +69,8 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
/**
* This function is required for testing purposes
*/
private fun getResourceUrl(strPath: String): URL? {
return ZoweConfigServiceImpl::class.java.classLoader?.getResource(strPath)
private fun getResourceStream(strPath: String): InputStream? {
return ZoweConfigServiceImpl::class.java.classLoader?.getResourceAsStream(strPath)
}
}

Expand Down Expand Up @@ -280,12 +276,14 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
val jsonFileName = "${myProject.basePath}/${ZOWE_CONFIG_NAME}"
val charset: Charset = StandardCharsets.UTF_8

val pathSourceConfig = getResourceUrl("files/${ZOWE_CONFIG_NAME}")
val pathSourceSchema = getResourceUrl("files/zowe.schema.json")
val inputStreamSourceConfig = getResourceStream("files/${ZOWE_CONFIG_NAME}")
val inputStreamSourceSchema = getResourceStream("files/zowe.schema.json")

val f: File = File(schemaFileName)
if (!f.exists()) {
FileUtils.copyURLToFile(pathSourceSchema, File(schemaFileName))
FileOutputStream(f, false).use {
inputStreamSourceSchema?.transferTo(it)
}
}

val urlRegex =
Expand All @@ -300,14 +298,11 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
port = matcher.group(4).substring(1)
}

val array = pathSourceConfig.toString().split("!")
val fs: FileSystem = FileSystems.newFileSystem(URI.create(array[0]), HashMap<String, String?>())
var content = String(Files.readAllBytes(fs.getPath(array[1])), charset)
fs.close()
content = content.replace("<PORT>".toRegex(), port)
content = content.replace("<HOST>".toRegex(), "\"$host\"")
content = content.replace("<SSL>".toRegex(), (!state.isAllowSsl).toString())
Files.write(Paths.get(jsonFileName), content.toByteArray(charset))
var content = inputStreamSourceConfig?.readAllBytes()?.let { String(it, charset)}
content = content?.replace("<PORT>".toRegex(), port)
content = content?.replace("<HOST>".toRegex(), "\"$host\"")
content = content?.replace("<SSL>".toRegex(), (!state.isAllowSsl).toString())
content?.toByteArray(charset)?.let { Files.write(Paths.get(jsonFileName), it) }

runWriteAction {
val configCredentialsMap = mutableMapOf<String, Any?>()
Expand Down
11 changes: 0 additions & 11 deletions src/test/kotlin/org/zowe/explorer/config/ZoweConfigTestSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,6 @@ class ZoweConfigTestSpec : WithApplicationShouldSpec({
every { ZoweConfigService.getInstance(mockedProject) } returns ZoweConfigServiceImpl(mockedProject)
}

mockkObject(ZoweConfigServiceImpl)
every { ZoweConfigServiceImpl.Companion["getResourceUrl"](any() as String) } answers {
val p = ZoweConfigTestSpec::class.java.classLoader?.getResource(firstArg<String>())?.path
if (firstArg<String>().contains(ZOWE_CONFIG_NAME)) {
URL("jar:file:/path/to/jar!" + p.toString())
} else {
URL("file:" + p)
}

}

var checkFilePath = false
var checkUser = false
var checkPass = false
Expand Down

0 comments on commit 790ada8

Please sign in to comment.