|
| 1 | +// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.settings |
| 5 | + |
| 6 | +import com.intellij.util.xmlb.XmlSerializer |
| 7 | +import org.assertj.core.api.Assertions.assertThat |
| 8 | +import org.jdom.output.XMLOutputter |
| 9 | +import org.junit.Before |
| 10 | +import org.junit.Test |
| 11 | +import software.aws.toolkits.jetbrains.utils.xmlElement |
| 12 | + |
| 13 | +class LspSettingsTest { |
| 14 | + private lateinit var lspSettings: LspSettings |
| 15 | + |
| 16 | + @Before |
| 17 | + fun setUp() { |
| 18 | + lspSettings = LspSettings() |
| 19 | + lspSettings.loadState(LspConfiguration()) |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `artifact path is empty by default`() { |
| 24 | + assertThat(lspSettings.getArtifactPath()).isEmpty() |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `artifact path can be set`() { |
| 29 | + lspSettings.setArtifactPath("test\\lsp.js") |
| 30 | + assertThat(lspSettings.getArtifactPath()).isNotEmpty() |
| 31 | + assertThat(lspSettings.getArtifactPath()).isEqualTo("test\\lsp.js") |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `artifact path cannot be null`() { |
| 36 | + lspSettings.setArtifactPath(null) |
| 37 | + assertThat(lspSettings.getArtifactPath()).isEmpty() |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `serialize settings to ensure backwards compatibility`() { |
| 42 | + val element = xmlElement( |
| 43 | + """ |
| 44 | + <component name="LspSettings"> |
| 45 | + </component> |
| 46 | + """.trimIndent() |
| 47 | + ) |
| 48 | + lspSettings.setArtifactPath("temp\\lsp.js") |
| 49 | + |
| 50 | + XmlSerializer.serializeInto(lspSettings.state, element) |
| 51 | + |
| 52 | + val actual = XMLOutputter().outputString(element) |
| 53 | + |
| 54 | + val expected = "<component name=\"LspSettings\">\n" + |
| 55 | + "<option name=\"artifactPath\" value=\"temp\\lsp.js\" /></component>" |
| 56 | + |
| 57 | + assertThat(actual).isEqualTo(expected) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `deserialize empty settings to ensure backwards compatibility`() { |
| 62 | + val element = xmlElement( |
| 63 | + """ |
| 64 | + <component name="LspSettings"> |
| 65 | + </component> |
| 66 | + """ |
| 67 | + ) |
| 68 | + val actual = XmlSerializer.deserialize(element, LspConfiguration::class.java) |
| 69 | + assertThat(actual.artifactPath).isEmpty() |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `deserialize existing settings to ensure backwards compatibility`() { |
| 74 | + val element = xmlElement( |
| 75 | + """ |
| 76 | + <component name="LspSettings"> |
| 77 | + <option name="artifactPath" value='temp\lsp.js'/> |
| 78 | + </component> |
| 79 | + """.trimIndent() |
| 80 | + ) |
| 81 | + val actual = XmlSerializer.deserialize(element, LspConfiguration::class.java) |
| 82 | + assertThat(actual.artifactPath).isNotEmpty() |
| 83 | + assertThat(actual.artifactPath).isEqualTo("temp\\lsp.js") |
| 84 | + } |
| 85 | +} |
0 commit comments