Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/main/java/org/codejive/properties/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ public Set<Entry<String, String>> entrySet() {
@Override
public Iterator<Entry<String, String>> iterator() {
return new Iterator<Entry<String, String>>() {
final Iterator<Entry<String, String>> iter = values.entrySet().iterator();
private final Iterator<Entry<String, String>> iter =
values.entrySet().iterator();
private Entry<String, String> currentEntry;

@Override
public boolean hasNext() {
Expand All @@ -219,12 +221,14 @@ public boolean hasNext() {

@Override
public Entry<String, String> next() {
return iter.next();
return (currentEntry = iter.next());
}

@Override
public void remove() {
// TODO handle remove
if (currentEntry != null) {
removeItem(currentEntry.getKey());
}
iter.remove();
}
};
Expand Down Expand Up @@ -382,8 +386,29 @@ private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String

@Override
public String remove(Object key) {
// TODO handle remove
return values.remove(key);
String skey = key.toString();
removeItem(skey);
return values.remove(skey);
}

private void removeItem(String skey) {
setComment(skey, Collections.emptyList());
Cursor pos = indexOf(skey);
assert pos.isType(PropertiesParser.Type.KEY);
pos.remove();
assert pos.isType(PropertiesParser.Type.SEPARATOR);
pos.remove();
assert pos.isType(PropertiesParser.Type.VALUE);
pos.remove();
if (pos.isEol()) {
pos.remove();
}
}

@Override
public void clear() {
tokens.clear();
values.clear();
}

/**
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/org/codejive/properties/TestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Iterator;
import org.junit.jupiter.api.Test;

public class TestProperties {
Expand Down Expand Up @@ -314,6 +315,72 @@ void testPutFirstWithHeader() throws IOException, URISyntaxException {
}
}

@Test
void testRemoveFirst() throws IOException, URISyntaxException {
Properties p = Properties.loadProperties(getResource("/test.properties"));
p.remove("one");
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removefirst.properties"))));
}

@Test
void testRemoveMiddle() throws IOException, URISyntaxException {
Properties p = Properties.loadProperties(getResource("/test.properties"));
p.remove("three");
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removemiddle.properties"))));
}

@Test
void testRemoveLast() throws IOException, URISyntaxException {
Properties p = Properties.loadProperties(getResource("/test.properties"));
p.remove("key.4");
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removelast.properties"))));
}

@Test
void testRemoveAll() throws IOException, URISyntaxException {
Properties p = Properties.loadProperties(getResource("/test.properties"));
p.remove("one");
p.remove("two");
p.remove("three");
p.remove(" with spaces");
p.remove("altsep");
p.remove("multiline");
p.remove("key.4");
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removeall.properties"))));
}

@Test
void testRemoveMiddleIterator() throws IOException, URISyntaxException {
Properties p = Properties.loadProperties(getResource("/test.properties"));
Iterator iter = p.keySet().iterator();
while (iter.hasNext()) {
if (iter.next().equals("three")) {
iter.remove();
break;
}
}
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removemiddle.properties"))));
}

@Test
void testClear() throws IOException, URISyntaxException {
Properties p = Properties.loadProperties(getResource("/test.properties"));
p.clear();
StringWriter sw = new StringWriter();
p.store(sw);
assertThat(sw.toString(), equalTo(readAll(getResource("/test-clear.properties"))));
}

@Test
void testRemoveComment() throws IOException, URISyntaxException {
Properties p = Properties.loadProperties(getResource("/test.properties"));
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions src/test/resources/test-removeall.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#comment1
# comment2

# final comment
15 changes: 15 additions & 0 deletions src/test/resources/test-removefirst.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#comment1
# comment2

two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234
# final comment
16 changes: 16 additions & 0 deletions src/test/resources/test-removelast.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
# another comment
! and a comment
! block
three=and escapes\n\t\r\f
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
# final comment
13 changes: 13 additions & 0 deletions src/test/resources/test-removemiddle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#comment1
# comment2

! comment3
one=simple
two=value containing spaces
\ with\ spaces = everywhere
altsep:value
multiline = one \
two \
three
key.4 = \u1234
# final comment