View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.graph.transformer;
20  
21  import org.eclipse.aether.util.graph.transformer.ConflictIdSorter.ConflictId;
22  import org.eclipse.aether.util.graph.transformer.ConflictIdSorter.RootQueue;
23  import org.junit.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.*;
26  
27  public class RootQueueTest {
28  
29      @Test
30      void testIsEmpty() {
31          ConflictId id = new ConflictId("a", 0);
32          RootQueue queue = new RootQueue(10);
33          assertTrue(queue.isEmpty());
34          queue.add(id);
35          assertFalse(queue.isEmpty());
36          assertSame(id, queue.remove());
37          assertTrue(queue.isEmpty());
38      }
39  
40      @Test
41      void testAddSortsByDepth() {
42          ConflictId id1 = new ConflictId("a", 0);
43          ConflictId id2 = new ConflictId("b", 1);
44          ConflictId id3 = new ConflictId("c", 2);
45          ConflictId id4 = new ConflictId("d", 3);
46  
47          RootQueue queue = new RootQueue(10);
48          queue.add(id1);
49          queue.add(id2);
50          queue.add(id3);
51          queue.add(id4);
52          assertSame(id1, queue.remove());
53          assertSame(id2, queue.remove());
54          assertSame(id3, queue.remove());
55          assertSame(id4, queue.remove());
56  
57          queue = new RootQueue(10);
58          queue.add(id4);
59          queue.add(id3);
60          queue.add(id2);
61          queue.add(id1);
62          assertSame(id1, queue.remove());
63          assertSame(id2, queue.remove());
64          assertSame(id3, queue.remove());
65          assertSame(id4, queue.remove());
66      }
67  
68      @Test
69      void testAddWithArrayCompact() {
70          ConflictId id = new ConflictId("a", 0);
71  
72          RootQueue queue = new RootQueue(10);
73          assertTrue(queue.isEmpty());
74          queue.add(id);
75          assertFalse(queue.isEmpty());
76          assertSame(id, queue.remove());
77          assertTrue(queue.isEmpty());
78          queue.add(id);
79          assertFalse(queue.isEmpty());
80          assertSame(id, queue.remove());
81          assertTrue(queue.isEmpty());
82      }
83  
84      @Test
85      void testAddMinimumAfterSomeRemoves() {
86          ConflictId id1 = new ConflictId("a", 0);
87          ConflictId id2 = new ConflictId("b", 1);
88          ConflictId id3 = new ConflictId("c", 2);
89  
90          RootQueue queue = new RootQueue(10);
91          queue.add(id2);
92          queue.add(id3);
93          assertSame(id2, queue.remove());
94          queue.add(id1);
95          assertSame(id1, queue.remove());
96          assertSame(id3, queue.remove());
97          assertTrue(queue.isEmpty());
98      }
99  }