Skip to content

Commit b6c1c62

Browse files
committed
Add remove and removeAll functionality
Fixes odiszapc#2
1 parent 4cbe184 commit b6c1c62

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

src/main/java/com/github/odiszapc/nginxparser/NgxBlock.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* Describes block section. Example:
2323
* http {
24-
* ...
24+
* ...
2525
* }
2626
*/
2727
public class NgxBlock extends NgxAbstractEntry implements Iterable<NgxEntry> {
@@ -46,6 +46,38 @@ public Iterator<NgxEntry> iterator() {
4646
return getEntries().iterator();
4747
}
4848

49+
public void remove(NgxEntry itemToRemove) {
50+
if (null == itemToRemove)
51+
throw new NullPointerException("Item can not be null");
52+
53+
Iterator<NgxEntry> it = entries.iterator();
54+
while(it.hasNext()) {
55+
NgxEntry entry = it.next();
56+
switch (NgxEntryType.fromClass(entry.getClass())) {
57+
case PARAM:
58+
if (entry.equals(itemToRemove))
59+
it.remove();
60+
break;
61+
case BLOCK:
62+
if (entry.equals(itemToRemove))
63+
it.remove();
64+
else {
65+
NgxBlock block = (NgxBlock) entry;
66+
block.remove(itemToRemove);
67+
}
68+
break;
69+
}
70+
}
71+
}
72+
73+
public void removeAll(Iterable<NgxEntry> itemsToRemove) {
74+
if (null == itemsToRemove)
75+
throw new NullPointerException("Items can not be null");
76+
for (NgxEntry itemToRemove : itemsToRemove) {
77+
remove(itemToRemove);
78+
}
79+
}
80+
4981
public <T extends NgxEntry> T find(Class<T> clazz, String... params) {
5082
List<NgxEntry> all = findAll(clazz, new ArrayList<NgxEntry>(), params);
5183
if (all.isEmpty())
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.github.odiszapc.nginxparser;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.junit.rules.ExpectedException;
6+
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class AlterConfigTest extends ParseTestBase {
12+
@Test
13+
public void removeOneLocation() throws Exception {
14+
NgxConfig conf = parse("nested/c2.conf");
15+
NgxEntry ngxEntry = conf.find(NgxConfig.BLOCK, "http", "server", "location");
16+
conf.remove(ngxEntry);
17+
18+
final String expected = "" +
19+
"http {\n" +
20+
" server {\n" +
21+
" }\n" +
22+
"}\n";
23+
24+
assertEquals(new NgxDumper(conf).dump(), expected);
25+
}
26+
27+
@Test
28+
public void removeManyLocations() throws Exception {
29+
NgxConfig conf = parse("nested/c4.conf");
30+
List<NgxEntry> locations = conf.findAll(NgxConfig.BLOCK, "http", "server", "location");
31+
assertEquals(2, locations.size());
32+
conf.removeAll(locations);
33+
assertEquals(0, conf.findAll(NgxConfig.BLOCK, "http", "server", "location").size());
34+
35+
final String expected = "" +
36+
"http {\n" +
37+
" server {\n" +
38+
" }\n" +
39+
"}\n";
40+
41+
assertEquals(new NgxDumper(conf).dump(), expected);
42+
}
43+
44+
@Rule
45+
public ExpectedException expectedEx = ExpectedException.none();
46+
47+
@Test
48+
public void removeNullElement() throws Exception {
49+
expectedEx.expect(NullPointerException.class);
50+
expectedEx.expectMessage("Item can not be null");
51+
52+
NgxConfig conf = parse("nested/c4.conf");
53+
conf.remove(null);
54+
}
55+
56+
@Test
57+
public void removeNullElements() throws Exception {
58+
expectedEx.expect(NullPointerException.class);
59+
expectedEx.expectMessage("Items can not be null");
60+
61+
NgxConfig conf = parse("nested/c4.conf");
62+
conf.removeAll(null);
63+
}
64+
}

src/test/resources/nested/c4.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
http {
2+
server {
3+
location /foo {
4+
set $foo "bar";
5+
}
6+
location /foo {
7+
set $bar "baz";
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)