1616import static org .assertj .core .api .Assertions .assertThat ;
1717
1818import com .fasterxml .jackson .databind .ObjectMapper ;
19+ import java .io .IOException ;
20+ import java .nio .file .Files ;
1921import java .nio .file .Path ;
22+ import java .util .List ;
2023import java .util .concurrent .Executors ;
2124import java .util .concurrent .ForkJoinPool ;
2225import java .util .concurrent .ScheduledExecutorService ;
2326import java .util .concurrent .TimeUnit ;
2427import org .apache .couchdb .nouveau .api .IndexDefinition ;
2528import org .apache .couchdb .nouveau .api .SearchRequest ;
2629import org .apache .couchdb .nouveau .lucene9 .ParallelSearcherFactory ;
27- import org .junit .jupiter .api .AfterAll ;
28- import org .junit .jupiter .api .BeforeAll ;
30+ import org .junit .jupiter .api .AfterEach ;
31+ import org .junit .jupiter .api .BeforeEach ;
2932import org .junit .jupiter .api .Test ;
3033import org .junit .jupiter .api .io .TempDir ;
3134
3235public class IndexManagerTest {
3336
34- private static IndexManager manager ;
35- private static ScheduledExecutorService executorService ;
37+ private Path rootDir ;
38+ private IndexManager manager ;
39+ private ScheduledExecutorService executorService ;
3640
37- @ BeforeAll
38- public static void setupManager (@ TempDir Path path ) throws Exception {
41+ @ BeforeEach
42+ public void setupManager (@ TempDir Path path ) throws Exception {
3943 executorService = Executors .newScheduledThreadPool (2 );
44+ rootDir = path ;
4045
4146 manager = new IndexManager ();
4247 manager .setRootDir (path );
@@ -47,8 +52,8 @@ public static void setupManager(@TempDir Path path) throws Exception {
4752 manager .start ();
4853 }
4954
50- @ AfterAll
51- public static void cleanup () throws Exception {
55+ @ AfterEach
56+ public void cleanup () throws Exception {
5257 executorService .shutdownNow ();
5358 executorService .awaitTermination (5 , TimeUnit .SECONDS );
5459 manager .stop ();
@@ -82,4 +87,60 @@ public void managerReopensAClosedIndex() throws Exception {
8287 });
8388 assertThat (isOpen );
8489 }
90+
91+ @ Test
92+ public void deleteAllRemovesIndexByName () throws Exception {
93+ final IndexDefinition indexDefinition = new IndexDefinition ();
94+ indexDefinition .setDefaultAnalyzer ("standard" );
95+
96+ assertThat (countIndexes ()).isEqualTo (0 );
97+ manager .create ("bar" , indexDefinition );
98+ assertThat (countIndexes ()).isEqualTo (1 );
99+ manager .deleteAll ("bar" , null );
100+ assertThat (countIndexes ()).isEqualTo (0 );
101+ }
102+
103+ @ Test
104+ public void deleteAllRemovesIndexByPath () throws Exception {
105+ final IndexDefinition indexDefinition = new IndexDefinition ();
106+ indexDefinition .setDefaultAnalyzer ("standard" );
107+
108+ assertThat (countIndexes ()).isEqualTo (0 );
109+ manager .create ("foo/bar" , indexDefinition );
110+ assertThat (countIndexes ()).isEqualTo (1 );
111+ manager .deleteAll ("foo/bar" , null );
112+ assertThat (countIndexes ()).isEqualTo (0 );
113+ }
114+
115+ @ Test
116+ public void deleteAllRemovesIndexByGlob () throws Exception {
117+ final IndexDefinition indexDefinition = new IndexDefinition ();
118+ indexDefinition .setDefaultAnalyzer ("standard" );
119+
120+ assertThat (countIndexes ()).isEqualTo (0 );
121+ manager .create ("foo/bar" , indexDefinition );
122+ assertThat (countIndexes ()).isEqualTo (1 );
123+ manager .deleteAll ("foo/*" , null );
124+ assertThat (countIndexes ()).isEqualTo (0 );
125+ }
126+
127+ @ Test
128+ public void deleteAllRemovesIndexByGlobExceptExclusions () throws Exception {
129+ final IndexDefinition indexDefinition = new IndexDefinition ();
130+ indexDefinition .setDefaultAnalyzer ("standard" );
131+
132+ assertThat (countIndexes ()).isEqualTo (0 );
133+ manager .create ("foo/bar" , indexDefinition );
134+ manager .create ("foo/baz" , indexDefinition );
135+ assertThat (countIndexes ()).isEqualTo (2 );
136+ manager .deleteAll ("foo/*" , List .of ("bar" ));
137+ assertThat (countIndexes ()).isEqualTo (1 );
138+ }
139+
140+ private long countIndexes () throws IOException {
141+ try (var stream =
142+ Files .find (rootDir , 10 , (p , attr ) -> p .getFileName ().toString ().equals ("index_definition.json" ))) {
143+ return stream .count ();
144+ }
145+ }
85146}
0 commit comments