|
2 | 2 |
|
3 | 3 | import java.io.File;
|
4 | 4 | import java.io.FileInputStream;
|
| 5 | +import java.io.FileOutputStream; |
5 | 6 | import java.io.IOException;
|
6 | 7 | import java.util.LinkedList;
|
7 | 8 | import java.util.List;
|
|
10 | 11 | import com.google.common.collect.ImmutableList;
|
11 | 12 | import com.google.common.io.Files;
|
12 | 13 | import com.tngtech.archunit.lang.ArchRule;
|
| 14 | +import org.assertj.core.api.ThrowableAssert; |
13 | 15 | import org.junit.Before;
|
14 | 16 | import org.junit.Rule;
|
15 | 17 | import org.junit.Test;
|
@@ -38,6 +40,79 @@ public void setUp() throws Exception {
|
38 | 40 | "default.allowStoreCreation", String.valueOf(true)));
|
39 | 41 | }
|
40 | 42 |
|
| 43 | + @Test |
| 44 | + public void throws_exception_when_there_are_obsolete_entries_in_storedRules_files() throws Exception { |
| 45 | + // given |
| 46 | + store.save(defaultRule(), ImmutableList.of("first violation", "second violation")); |
| 47 | + Properties properties = readProperties(new File(configuredFolder, "stored.rules")); |
| 48 | + File ruleViolationsFile = new File(configuredFolder, properties.getProperty(defaultRule().getDescription())); |
| 49 | + assertThat(ruleViolationsFile.delete()).isTrue(); |
| 50 | + |
| 51 | + // when && then |
| 52 | + ThrowableAssert.ThrowingCallable storeInitialization = () -> store.initialize(propertiesOf( |
| 53 | + "default.path", configuredFolder.getAbsolutePath(), |
| 54 | + "default.allowStoreUpdate", String.valueOf(false))); |
| 55 | + assertThatThrownBy(storeInitialization) |
| 56 | + .isInstanceOf(StoreUpdateFailedException.class) |
| 57 | + .hasMessage("Failed to remove 1 obsolete stored rule(s). Updating frozen violations is disabled (enable by configuration freeze.store.default.allowStoreUpdate=true)"); |
| 58 | + assertThat(store.contains(defaultRule())).isTrue(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void deletes_obsolete_entries_from_storedRules_files() throws Exception { |
| 63 | + // given |
| 64 | + store.save(defaultRule(), ImmutableList.of("first violation", "second violation")); |
| 65 | + Properties properties = readProperties(new File(configuredFolder, "stored.rules")); |
| 66 | + File ruleViolationsFile = new File(configuredFolder, properties.getProperty(defaultRule().getDescription())); |
| 67 | + assertThat(ruleViolationsFile.delete()).isTrue(); |
| 68 | + |
| 69 | + // when |
| 70 | + store.initialize(propertiesOf("default.path", configuredFolder.getAbsolutePath())); |
| 71 | + |
| 72 | + // then |
| 73 | + assertThat(store.contains(defaultRule())).isFalse(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void throws_exception_when_there_are_unreferenced_in_store_directory() throws Exception { |
| 78 | + // given |
| 79 | + store.save(defaultRule(), ImmutableList.of("first violation", "second violation")); |
| 80 | + File propertiesFile = new File(configuredFolder, "stored.rules"); |
| 81 | + Properties properties = readProperties(propertiesFile); |
| 82 | + File ruleViolationsFile = new File(configuredFolder, properties.getProperty(defaultRule().getDescription())); |
| 83 | + assertThat(ruleViolationsFile).exists(); |
| 84 | + properties.remove(defaultRule().getDescription()); |
| 85 | + storeProperties(propertiesFile, properties); |
| 86 | + |
| 87 | + // when && then |
| 88 | + ThrowableAssert.ThrowingCallable storeInitialization = () -> store.initialize(propertiesOf( |
| 89 | + "default.path", configuredFolder.getAbsolutePath(), |
| 90 | + "default.allowStoreUpdate", String.valueOf(false))); |
| 91 | + assertThatThrownBy(storeInitialization) |
| 92 | + .isInstanceOf(StoreUpdateFailedException.class) |
| 93 | + .hasMessage("Failed to remove 1 unreferenced rule files. Updating frozen store is disabled (enable by configuration freeze.store.default.allowStoreUpdate=true)"); |
| 94 | + assertThat(ruleViolationsFile).exists(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void deletes_files_not_referenced_in_storedRules() throws Exception { |
| 99 | + // given |
| 100 | + store.save(defaultRule(), ImmutableList.of("first violation", "second violation")); |
| 101 | + File propertiesFile = new File(configuredFolder, "stored.rules"); |
| 102 | + Properties properties = readProperties(propertiesFile); |
| 103 | + File ruleViolationsFile = new File(configuredFolder, properties.getProperty(defaultRule().getDescription())); |
| 104 | + assertThat(ruleViolationsFile).exists(); |
| 105 | + properties.remove(defaultRule().getDescription()); |
| 106 | + storeProperties(propertiesFile, properties); |
| 107 | + |
| 108 | + // when |
| 109 | + store.initialize(propertiesOf("default.path", configuredFolder.getAbsolutePath())); |
| 110 | + |
| 111 | + // then |
| 112 | + assertThat(store.contains(defaultRule())).isFalse(); |
| 113 | + assertThat(ruleViolationsFile).doesNotExist(); |
| 114 | + } |
| 115 | + |
41 | 116 | @Test
|
42 | 117 | public void reports_unknown_rule_as_unstored() {
|
43 | 118 | assertThat(store.contains(defaultRule())).as("store contains random rule").isFalse();
|
@@ -126,6 +201,12 @@ private Properties readProperties(File file) throws IOException {
|
126 | 201 | return properties;
|
127 | 202 | }
|
128 | 203 |
|
| 204 | + private static void storeProperties(File propertiesFile, Properties properties) throws IOException { |
| 205 | + try (FileOutputStream outputStream = new FileOutputStream(propertiesFile)) { |
| 206 | + properties.store(outputStream, ""); |
| 207 | + } |
| 208 | + } |
| 209 | + |
129 | 210 | private Properties propertiesOf(String... keyValuePairs) {
|
130 | 211 | Properties result = new Properties();
|
131 | 212 | LinkedList<String> keyValues = new LinkedList<>(asList(keyValuePairs));
|
|
0 commit comments