|
| 1 | +package io.sentry.android.gradle.util |
| 2 | + |
| 3 | +import java.io.StringWriter |
| 4 | +import java.util.Properties |
| 5 | +import kotlin.test.assertEquals |
| 6 | +import kotlin.test.assertFalse |
| 7 | +import kotlin.test.assertTrue |
| 8 | +import org.junit.Test |
| 9 | + |
| 10 | +class PropertiesUtilTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + fun `store writes properties without timestamp`() { |
| 14 | + val props = Properties() |
| 15 | + props.setProperty("key1", "value1") |
| 16 | + props.setProperty("key2", "value2") |
| 17 | + |
| 18 | + val writer = StringWriter() |
| 19 | + PropertiesUtil.store(props, writer, "Test comment") |
| 20 | + val result = writer.toString() |
| 21 | + |
| 22 | + // should not contain date patterns that Java Properties.store() adds |
| 23 | + assertFalse(result.contains("Mon ") || result.contains("Tue ") || result.contains("Wed ")) |
| 24 | + assertFalse(result.contains("Thu ") || result.contains("Fri ") || result.contains("Sat ")) |
| 25 | + assertFalse(result.contains("Sun ")) |
| 26 | + // should contain properties |
| 27 | + assertTrue(result.contains("key1=value1")) |
| 28 | + assertTrue(result.contains("key2=value2")) |
| 29 | + // should contain comment |
| 30 | + assertTrue(result.contains("# Test comment")) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `store writes properties in sorted order`() { |
| 35 | + val props = Properties() |
| 36 | + props.setProperty("zebra", "last") |
| 37 | + props.setProperty("apple", "first") |
| 38 | + props.setProperty("middle", "mid") |
| 39 | + |
| 40 | + val writer = StringWriter() |
| 41 | + PropertiesUtil.store(props, writer) |
| 42 | + val result = writer.toString() |
| 43 | + |
| 44 | + val appleIndex = result.indexOf("apple=first") |
| 45 | + val middleIndex = result.indexOf("middle=mid") |
| 46 | + val zebraIndex = result.indexOf("zebra=last") |
| 47 | + |
| 48 | + assertTrue(appleIndex < middleIndex) |
| 49 | + assertTrue(middleIndex < zebraIndex) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `store works without comment`() { |
| 54 | + val props = Properties() |
| 55 | + props.setProperty("key", "value") |
| 56 | + |
| 57 | + val writer = StringWriter() |
| 58 | + PropertiesUtil.store(props, writer) |
| 59 | + val result = writer.toString() |
| 60 | + |
| 61 | + assertFalse(result.contains("#")) |
| 62 | + assertEquals("key=value\n", result) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `store produces identical output for same properties`() { |
| 67 | + val props = Properties() |
| 68 | + props.setProperty("io.sentry.build-tool", "gradle") |
| 69 | + props.setProperty("io.sentry.ProguardUuids", "test-uuid") |
| 70 | + |
| 71 | + val writer1 = StringWriter() |
| 72 | + PropertiesUtil.store(props, writer1, "Generated by sentry-android-gradle-plugin") |
| 73 | + |
| 74 | + val writer2 = StringWriter() |
| 75 | + PropertiesUtil.store(props, writer2, "Generated by sentry-android-gradle-plugin") |
| 76 | + |
| 77 | + assertEquals(writer1.toString(), writer2.toString()) |
| 78 | + } |
| 79 | +} |
0 commit comments