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.apache.maven.model.building;
20  
21  import java.io.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Paths;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertThrows;
31  
32  public class GraphTest {
33  
34      @Test
35      void testCycle() throws Graph.CycleDetectedException {
36          Graph graph = new Graph();
37          graph.addEdge("a1", "a2");
38          assertThrows(Graph.CycleDetectedException.class, () -> graph.addEdge("a2", "a1"));
39      }
40  
41      @Test
42      public void testPerf() throws IOException {
43          List<String[]> data = new ArrayList<>();
44          String k = null;
45          for (String line : Files.readAllLines(Paths.get("src/test/resources/dag.txt"))) {
46              if (line.startsWith("\t")) {
47                  data.add(new String[] {k, line.trim()});
48              } else {
49                  k = line;
50              }
51          }
52          Collections.shuffle(data);
53  
54          long t0 = System.nanoTime();
55          Graph g = new Graph();
56          data.parallelStream().forEach(s -> {
57              try {
58                  g.addEdge(s[0], s[1]);
59              } catch (Exception e) {
60                  throw new RuntimeException(e);
61              }
62          });
63          long t1 = System.nanoTime();
64      }
65  }