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.index.cli;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.codehaus.plexus.util.cli.CommandLineException;
25  import org.codehaus.plexus.util.cli.CommandLineUtils;
26  import org.codehaus.plexus.util.cli.Commandline;
27  import org.codehaus.plexus.util.cli.StreamConsumer;
28  
29  public class NexusIndexerCliIT extends AbstractNexusIndexerCliTest {
30  
31      private StreamConsumer sout;
32  
33      @Override
34      public void setUp() throws Exception {
35          super.setUp();
36  
37          sout = line -> {
38              try {
39                  out.write(line.getBytes());
40                  out.write("\n".getBytes());
41              } catch (IOException e) {
42                  throw new RuntimeException(e.getMessage(), e);
43              }
44          };
45      }
46  
47      private Commandline createCommandLine() {
48          try {
49              Commandline cmd = new Commandline();
50              cmd.setExecutable("java");
51              cmd.setWorkingDirectory(new File(".").getCanonicalFile());
52              cmd.createArg().setValue("-jar");
53              cmd.createArg().setValue(new File(System.getProperty("indexerJar")).getCanonicalPath());
54              return cmd;
55          } catch (IOException e) {
56              throw new RuntimeException(e);
57          }
58      }
59  
60      @Override
61      protected int execute(String... args) {
62          Commandline cmd = createCommandLine();
63          for (String arg : args) {
64              cmd.createArg().setValue(arg);
65          }
66          try {
67              return CommandLineUtils.executeCommandLine(cmd, sout, sout);
68          } catch (CommandLineException e) {
69              return -1;
70          }
71      }
72  }