-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathPropertiesFileUtilsTest.java
206 lines (178 loc) · 8.93 KB
/
PropertiesFileUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.opendatakit.builder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.opendatakit.provider.KeyValueStoreColumns.TABLE_ID;
import androidx.documentfile.provider.DocumentFile;
import org.junit.Before;
import org.junit.Test;
import org.opendatakit.database.data.KeyValueStoreEntry;
import org.opendatakit.database.data.OrderedColumns;
import org.opendatakit.exception.ServicesAvailabilityException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PropertiesFileUtilsTest {
private String appName;
private String tableId;
private OrderedColumns orderedDefns;
private List<KeyValueStoreEntry> kvsEntries;
private File definitionCsv;
private File propertiesCsv;
@Before
public void setUp() {
appName = "TestApp";
tableId = "TestTable";
kvsEntries = new ArrayList<>();
definitionCsv = new File("definition.csv");
propertiesCsv = new File("properties.csv");
}
@Test
public void testWritePropertiesIntoCsv_NullAppName() throws ServicesAvailabilityException {
// Test writing properties into CSV with null appName
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(null, tableId, orderedDefns, kvsEntries, DocumentFile.fromFile(definitionCsv), DocumentFile.fromFile(propertiesCsv));
assertFalse(success);
}
@Test
public void testWritePropertiesIntoCsv_NullTableId() throws ServicesAvailabilityException {
// Test writing properties into CSV with null tableId
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(appName, null, orderedDefns, kvsEntries, DocumentFile.fromFile(definitionCsv), DocumentFile.fromFile(propertiesCsv));
assertFalse(success);
}
@Test
public void testWritePropertiesIntoCsv_NullOrderedDefns() throws ServicesAvailabilityException {
// Test writing properties into CSV with null orderedDefns
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(appName, tableId, null, kvsEntries, DocumentFile.fromFile(definitionCsv), DocumentFile.fromFile(propertiesCsv));
assertFalse(success);
}
@Test
public void testWritePropertiesIntoCsv_NullKvsEntries() throws ServicesAvailabilityException {
// Test writing properties into CSV with null kvsEntries
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(appName, tableId, orderedDefns, null, DocumentFile.fromFile(definitionCsv), DocumentFile.fromFile(propertiesCsv));
assertFalse(success);
}
@Test(expected = IllegalArgumentException.class)
public void testReadPropertiesFromCsv_NullTableId() throws IOException {
// Test reading properties from CSV with null tableId
PropertiesFileUtils.readPropertiesFromCsv(appName, null);
}
@Test(expected = IOException.class)
public void testReadPropertiesFromCsv_NonExistentFile() throws IOException {
// Test reading properties from non-existent CSV file
PropertiesFileUtils.readPropertiesFromCsv(appName, "NonExistentTable");
}
@Test
public void testReadPropertiesFromCsv_EmptyTableId() {
// Test reading properties from CSV with empty tableId
try {
PropertiesFileUtils.readPropertiesFromCsv(appName, "");
fail("Expected IllegalArgumentException was not thrown");
} catch (IllegalArgumentException e) {
assertTrue(true);
} catch (IOException e) {
fail("Unexpected IOException occurred");
}
}
@Test
public void testWritePropertiesIntoCsv_IOException() throws ServicesAvailabilityException {
// Test writing properties into CSV with IOException
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(appName, tableId, orderedDefns, kvsEntries, DocumentFile.fromFile(new File("definition.csv")), DocumentFile.fromFile(new File("properties.csv")));
assertFalse(success);
}
@Test
public void testReadPropertiesFromCsv_IOException() {
// Test reading properties from CSV with IOException
try {
PropertiesFileUtils.readPropertiesFromCsv(appName, "IOExceptionTable");
fail("Expected IOException was not thrown");
} catch (IOException e) {
assertTrue(true);
}
}
@Test
public void testWritePropertiesIntoCsv_PermissionDenied() throws IOException, ServicesAvailabilityException {
// Create a read-only CSV file
propertiesCsv.setReadOnly();
// Attempt to write properties into read-only CSV
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(appName, tableId, orderedDefns, kvsEntries, DocumentFile.fromFile(definitionCsv), DocumentFile.fromFile(propertiesCsv));
assertFalse(success);
}
@Test
public void testWritePropertiesIntoCsv_NullPropertiesCsv() throws ServicesAvailabilityException {
// Test writing properties into CSV with null propertiesCsv
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(appName, tableId, orderedDefns, kvsEntries, DocumentFile.fromFile(definitionCsv), null);
assertFalse(success);
}
@Test
public void testWritePropertiesIntoCsv_NullAppNameAndTableId() throws ServicesAvailabilityException {
// Test writing properties into CSV with null appName and tableId
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(null, null, orderedDefns, kvsEntries, DocumentFile.fromFile(definitionCsv), DocumentFile.fromFile(propertiesCsv));
assertFalse(success);
}
@Test
public void testReadPropertiesFromCsv_NullAppNameAndTableId() {
// Test reading properties from CSV with null appName and tableId
try {
PropertiesFileUtils.readPropertiesFromCsv(null, null);
fail("Expected IllegalArgumentException was not thrown");
} catch (IllegalArgumentException e) {
assertTrue(true);
} catch (IOException e) {
fail("Unexpected IOException occurred");
}
}
@Test
public void testWritePropertiesIntoCsv_DifferentDefinitionAndPropertiesCsv() throws ServicesAvailabilityException {
// Test writing properties into CSV with different definitionCsv and propertiesCsv files
File differentDefinitionCsv = new File("different_definition.csv");
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(appName, tableId, orderedDefns, kvsEntries, DocumentFile.fromFile(differentDefinitionCsv), DocumentFile.fromFile(propertiesCsv));
assertFalse(success);
}
@Test
public void testCreateKeyValueStoreEntries() {
List<KeyValueStoreEntry> entries = createKeyValueStoreEntries();
assertNotNull("KeyValueStoreEntries should not be null", entries);
assertEquals("Number of entries should match", 2, entries.size());
}
@Test
public void testCreateKeyValueStoreEntry() {
KeyValueStoreEntry entry = createKeyValueStoreEntry("partition1", "aspect1", "key1", "INTEGER", "123");
assertNotNull("KeyValueStoreEntry should not be null", entry);
assertEquals("Partition should match", "partition1", entry.partition);
assertEquals("Aspect should match", "aspect1", entry.aspect);
assertEquals("Key should match", "key1", entry.key);
assertEquals("Type should match", "INTEGER", entry.type);
assertEquals("Value should match", "123", entry.value);
}
@Test
public void testWritePropertiesFailure() throws ServicesAvailabilityException {
// Convert file paths to DocumentFile and check existence
boolean success = PropertiesFileUtils.writePropertiesIntoCsv(null, null, null,
null, null, null);
assertFalse("Writing properties should fail", success && fileExists("definition.csv") && fileExists("properties.csv"));
}
private List<KeyValueStoreEntry> createKeyValueStoreEntries() {
List<KeyValueStoreEntry> kvsEntries = new ArrayList<>();
kvsEntries.add(createKeyValueStoreEntry("partition1", "aspect1", "key1", "INTEGER", "123"));
kvsEntries.add(createKeyValueStoreEntry("partition2", "aspect2", "key2", "STRING", "value2"));
return kvsEntries;
}
private KeyValueStoreEntry createKeyValueStoreEntry(String partition, String aspect, String key, String type, String value) {
KeyValueStoreEntry entry = new KeyValueStoreEntry();
entry.tableId = TABLE_ID;
entry.partition = partition;
entry.aspect = aspect;
entry.key = key;
entry.type = type;
entry.value = value;
return entry;
}
private boolean fileExists(String fileName) {
// Convert fileName to DocumentFile and check existence
DocumentFile file = DocumentFile.fromFile(new File(fileName));
return file.exists();
}
}