|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Damian Malczewski |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in all |
| 11 | + * copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: MIT |
| 14 | + */ |
| 15 | +package io.github.problem4j.core; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +/** |
| 25 | + * Some of the tests in this class may appear trivial or unnecessary. They are intentionally |
| 26 | + * included to explore and validate the behavior of various code coverage analysis tools. These |
| 27 | + * tests help ensure that the coverage reports correctly reflect different execution paths, edge |
| 28 | + * cases, and instrumentation scenarios. |
| 29 | + */ |
| 30 | +class AbstractProblemContextTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void givenEmptyContext_whenCreated_thenContextIsEmpty() { |
| 34 | + ProblemContext context = new AbstractProblemContext() {}; |
| 35 | + |
| 36 | + assertThat(context.toMap()).isEmpty(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void givenEmptyContext_whenPuttingValue_thenValueIsStored() { |
| 41 | + ProblemContext context = new AbstractProblemContext() {}; |
| 42 | + |
| 43 | + context.put("key1", "value1"); |
| 44 | + |
| 45 | + assertThat(context.get("key1")).isEqualTo("value1"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void givenContextWithValue_whenCheckingContainsKey_thenReturnsTrue() { |
| 50 | + ProblemContext context = new AbstractProblemContext() {}; |
| 51 | + context.put("key1", "value1"); |
| 52 | + |
| 53 | + assertThat(context.containsKey("key1")).isTrue(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void givenContextWithoutKey_whenCheckingContainsKey_thenReturnsFalse() { |
| 58 | + ProblemContext context = new AbstractProblemContext() {}; |
| 59 | + |
| 60 | + assertThat(context.containsKey("nonexistent")).isFalse(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void givenContextWithValue_whenGettingNonexistentKey_thenReturnsNull() { |
| 65 | + ProblemContext context = new AbstractProblemContext() {}; |
| 66 | + context.put("key1", "value1"); |
| 67 | + |
| 68 | + assertThat(context.get("nonexistent")).isNull(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void givenEmptyContext_whenPuttingNullValue_thenKeyIsNotAdded() { |
| 73 | + ProblemContext context = new AbstractProblemContext() {}; |
| 74 | + |
| 75 | + context.put("key1", null); |
| 76 | + |
| 77 | + assertThat(context.containsKey("key1")).isFalse(); |
| 78 | + assertThat(context.get("key1")).isNull(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void givenContextWithValue_whenPuttingNullValue_thenKeyIsRemoved() { |
| 83 | + ProblemContext context = new AbstractProblemContext() {}; |
| 84 | + context.put("key1", "value1"); |
| 85 | + |
| 86 | + context.put("key1", null); |
| 87 | + |
| 88 | + assertThat(context.containsKey("key1")).isFalse(); |
| 89 | + assertThat(context.get("key1")).isNull(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void givenEmptyContext_whenPuttingMultipleValues_thenAllValuesAreStored() { |
| 94 | + ProblemContext context = new AbstractProblemContext() {}; |
| 95 | + |
| 96 | + context.put("key1", "value1").put("key2", "value2").put("key3", "value3"); |
| 97 | + |
| 98 | + assertThat(context.get("key1")).isEqualTo("value1"); |
| 99 | + assertThat(context.get("key2")).isEqualTo("value2"); |
| 100 | + assertThat(context.get("key3")).isEqualTo("value3"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void givenEmptyContext_whenPuttingValue_thenReturnsSelfForChaining() { |
| 105 | + ProblemContext context = new AbstractProblemContext() {}; |
| 106 | + |
| 107 | + ProblemContext result = context.put("key1", "value1"); |
| 108 | + |
| 109 | + assertThat(result).isSameAs(context); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void givenContextWithValue_whenCalling_toMap_thenReturnsImmutableMap() { |
| 114 | + ProblemContext context = new AbstractProblemContext() {}; |
| 115 | + context.put("key1", "value1"); |
| 116 | + |
| 117 | + Map<String, String> map = context.toMap(); |
| 118 | + |
| 119 | + assertThat(map).containsEntry("key1", "value1"); |
| 120 | + assertThat(map).isUnmodifiable(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void givenContextWithValue_whenModifyingReturnedMap_thenContextIsNotAffected() { |
| 125 | + ProblemContext context = new AbstractProblemContext() {}; |
| 126 | + context.put("key1", "value1"); |
| 127 | + |
| 128 | + Map<String, String> map = context.toMap(); |
| 129 | + assertThat(map).isUnmodifiable(); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void givenContextWithValues_whenUpdatingExistingValue_thenValueIsReplaced() { |
| 134 | + ProblemContext context = new AbstractProblemContext() {}; |
| 135 | + context.put("key1", "value1"); |
| 136 | + |
| 137 | + context.put("key1", "newValue"); |
| 138 | + |
| 139 | + assertThat(context.get("key1")).isEqualTo("newValue"); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void givenMapWithValues_whenCreatingContextFromMap_thenValuesAreCopied() { |
| 144 | + Map<String, String> sourceMap = new HashMap<>(); |
| 145 | + sourceMap.put("key1", "value1"); |
| 146 | + sourceMap.put("key2", "value2"); |
| 147 | + |
| 148 | + ProblemContext context = new AbstractProblemContext(sourceMap) {}; |
| 149 | + |
| 150 | + assertThat(context.get("key1")).isEqualTo("value1"); |
| 151 | + assertThat(context.get("key2")).isEqualTo("value2"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void givenMapWithValues_whenCreatingContextFromMap_thenChangesToSourceMapDontAffectContext() { |
| 156 | + Map<String, String> sourceMap = new HashMap<>(); |
| 157 | + sourceMap.put("key1", "value1"); |
| 158 | + |
| 159 | + ProblemContext context = new AbstractProblemContext(sourceMap) {}; |
| 160 | + sourceMap.put("key2", "value2"); |
| 161 | + |
| 162 | + assertThat(context.containsKey("key2")).isFalse(); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + void givenContextWithValues_whenCreatingContextFromContext_thenValuesAreCopied() { |
| 167 | + ProblemContext source = new AbstractProblemContext() {}; |
| 168 | + source.put("key1", "value1"); |
| 169 | + source.put("key2", "value2"); |
| 170 | + |
| 171 | + ProblemContext context = new AbstractProblemContext(source) {}; |
| 172 | + |
| 173 | + assertThat(context.get("key1")).isEqualTo("value1"); |
| 174 | + assertThat(context.get("key2")).isEqualTo("value2"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void |
| 179 | + givenContextWithValues_whenCreatingContextFromContext_thenChangesToSourceDontAffectContext() { |
| 180 | + ProblemContext source = new AbstractProblemContext() {}; |
| 181 | + source.put("key1", "value1"); |
| 182 | + |
| 183 | + ProblemContext context = new AbstractProblemContext(source) {}; |
| 184 | + source.put("key2", "value2"); |
| 185 | + |
| 186 | + assertThat(context.containsKey("key2")).isFalse(); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + void |
| 191 | + givenContextWithValues_whenCreatingContextFromContext_thenChangesToNewContextDontAffectSource() { |
| 192 | + ProblemContext source = new AbstractProblemContext() {}; |
| 193 | + source.put("key1", "value1"); |
| 194 | + |
| 195 | + ProblemContext context = new AbstractProblemContext(source) {}; |
| 196 | + context.put("key2", "value2"); |
| 197 | + |
| 198 | + assertThat(source.containsKey("key2")).isFalse(); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void givenEmptyMap_whenCreatingContext_thenContextIsEmpty() { |
| 203 | + ProblemContext context = new AbstractProblemContext(Collections.emptyMap()) {}; |
| 204 | + |
| 205 | + assertThat(context.toMap()).isEmpty(); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + void givenTwoContextsWithSameValues_whenComparingEquality_thenTheyAreEqual() { |
| 210 | + ProblemContext context1 = new AbstractProblemContext() {}; |
| 211 | + context1.put("key1", "value1"); |
| 212 | + |
| 213 | + ProblemContext context2 = new AbstractProblemContext() {}; |
| 214 | + context2.put("key1", "value1"); |
| 215 | + |
| 216 | + assertThat(context1).isEqualTo(context2); |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + void givenTwoContextsWithDifferentValues_whenComparingEquality_thenTheyAreNotEqual() { |
| 221 | + ProblemContext context1 = new AbstractProblemContext() {}; |
| 222 | + context1.put("key1", "value1"); |
| 223 | + |
| 224 | + ProblemContext context2 = new AbstractProblemContext() {}; |
| 225 | + context2.put("key1", "value2"); |
| 226 | + |
| 227 | + assertThat(context1).isNotEqualTo(context2); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + void givenContextAndItself_whenComparingEquality_thenTheyAreEqual() { |
| 232 | + ProblemContext context = new AbstractProblemContext() {}; |
| 233 | + context.put("key1", "value1"); |
| 234 | + |
| 235 | + assertThat(context).isEqualTo(context); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + void givenContextAndNull_whenComparingEquality_thenTheyAreNotEqual() { |
| 240 | + ProblemContext context = new AbstractProblemContext() {}; |
| 241 | + |
| 242 | + assertThat(context).isNotEqualTo(null); |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + void givenContextAndNonContextObject_whenComparingEquality_thenTheyAreNotEqual() { |
| 247 | + ProblemContext context = new AbstractProblemContext() {}; |
| 248 | + context.put("key1", "value1"); |
| 249 | + |
| 250 | + assertThat(context).isNotEqualTo("not a context"); |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + void givenTwoContextsWithSameValues_whenComparingHashCode_thenHashCodesAreEqual() { |
| 255 | + ProblemContext context1 = new AbstractProblemContext() {}; |
| 256 | + context1.put("key1", "value1"); |
| 257 | + |
| 258 | + ProblemContext context2 = new AbstractProblemContext() {}; |
| 259 | + context2.put("key1", "value1"); |
| 260 | + |
| 261 | + assertThat(context1.hashCode()).isEqualTo(context2.hashCode()); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + void givenContextWithValues_whenCallingToString_thenReturnsMapRepresentation() { |
| 266 | + ProblemContext context = new AbstractProblemContext() {}; |
| 267 | + context.put("key1", "value1"); |
| 268 | + |
| 269 | + String result = context.toString(); |
| 270 | + |
| 271 | + assertThat(result).contains("key1"); |
| 272 | + assertThat(result).contains("value1"); |
| 273 | + } |
| 274 | + |
| 275 | + @Test |
| 276 | + void givenEmptyContext_whenCallingToString_thenReturnsEmptyMapRepresentation() { |
| 277 | + ProblemContext context = new AbstractProblemContext() {}; |
| 278 | + |
| 279 | + String result = context.toString(); |
| 280 | + |
| 281 | + assertThat(result).isEqualTo("{}"); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + void givenContextWithMultipleValues_whenCallingToString_thenReturnsAllValues() { |
| 286 | + ProblemContext context = new AbstractProblemContext() {}; |
| 287 | + context.put("key1", "value1"); |
| 288 | + context.put("key2", "value2"); |
| 289 | + |
| 290 | + String result = context.toString(); |
| 291 | + |
| 292 | + assertThat(result).contains("key1"); |
| 293 | + assertThat(result).contains("value1"); |
| 294 | + assertThat(result).contains("key2"); |
| 295 | + assertThat(result).contains("value2"); |
| 296 | + } |
| 297 | + |
| 298 | + @Test |
| 299 | + void givenContextWithEmptyStringValue_whenPuttingValue_thenValueIsStored() { |
| 300 | + ProblemContext context = new AbstractProblemContext() {}; |
| 301 | + |
| 302 | + context.put("key1", ""); |
| 303 | + |
| 304 | + assertThat(context.get("key1")).isEqualTo(""); |
| 305 | + } |
| 306 | + |
| 307 | + @Test |
| 308 | + void givenContextWithWhitespaceValue_whenPuttingValue_thenValueIsStored() { |
| 309 | + ProblemContext context = new AbstractProblemContext() {}; |
| 310 | + |
| 311 | + context.put("key1", " "); |
| 312 | + |
| 313 | + assertThat(context.get("key1")).isEqualTo(" "); |
| 314 | + } |
| 315 | +} |
0 commit comments