1   /* Contributed in the public domain.
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.compiler.plugin;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  
22  import javax.tools.JavaCompiler;
23  import javax.tools.ToolProvider;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.nio.file.FileVisitResult;
27  import java.nio.file.FileVisitor;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.nio.file.attribute.BasicFileAttributes;
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.List;
35  
36  /**
37   * Unit test for {@link DefaultDataContextPlugin}.
38   *
39   * @author Evan Ward
40   */
41  public class DefaultDataContextPluginTest {
42  
43      /**
44       * Check compiling an example program generates the expected number of warnings.
45       *
46       * @throws IOException on error.
47       */
48      @Test
49      public void testWarnings() throws IOException {
50          // setup
51          JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
52          ByteArrayOutputStream err = new ByteArrayOutputStream();
53          Path output = Paths.get("target/example/classes");
54          rmTree(output);
55          Files.createDirectories(output);
56          List<String> arguments = new ArrayList<>(Arrays.asList(
57                  "-cp", System.getProperty("java.class.path"),
58                  "-source", "1.8", "-target", "1.8",
59                  "-d", output.toAbsolutePath().toString(),
60                  "-Xmaxwarns", "9999",
61                  "-Xplugin:dataContextPlugin"));
62          Files.list(Paths.get("src/test/resources/compiler-plugin"))
63                  .forEach(a -> arguments.add(a.toAbsolutePath().toString()));
64  
65          // action
66          int retVal = javac.run(null, null, err, arguments.toArray(new String[0]));
67  
68          // verify
69          String actual = err.toString();
70          // count warnings ignoring duplicates
71          long count = Arrays.stream(actual.split("\n"))
72                  .filter(s -> s.contains(DefaultDataContextPlugin.MESSAGE))
73                  .count();
74          Assertions.assertEquals(count, 30, actual);
75          Assertions.assertFalse(actual.contains(" error:"),actual);
76          Assertions.assertEquals(0, retVal, actual);
77      }
78  
79      /**
80       * {@code rm -r path}.
81       *
82       * @param path to remove.
83       * @throws IOException on error.
84       */
85      private void rmTree(Path path) throws IOException {
86          if (!Files.exists(path)) {
87              return;
88          }
89          Files.walkFileTree(
90                  path,
91                  new FileVisitor<Path>() {
92                      @Override
93                      public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
94                          return FileVisitResult.CONTINUE;
95                      }
96  
97                      @Override
98                      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
99                          Files.delete(file);
100                         return FileVisitResult.CONTINUE;
101                     }
102 
103                     @Override
104                     public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
105                         throw exc;
106                     }
107 
108                     @Override
109                     public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
110                         Files.delete(dir);
111                         return FileVisitResult.CONTINUE;
112                     }
113                 }
114         );
115     }
116 
117 }