View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Parameter;
27  
28  /**
29   * Sets up a test to create
30   *
31   * @author Mark Donszelmann
32   */
33  public class Test implements Executable {
34  
35    /**
36     * Name of the test to create
37     */
38    @Parameter(required = true)
39    private String name = null;
40  
41    /**
42     * Type of linking to main artifact used for this test.
43     * Possible choices are: "shared" or "static".
44     * Defaults to library type if single library is built or "shared" otherwise.
45     */
46    @Parameter(defaultValue = "shared")
47    private String link = null;
48  
49    /**
50     * When true run this test. Defaults to true;
51     */
52    @Parameter(defaultValue = "true")
53    private boolean run = true;
54  
55    /**
56     * Type of the library to generate. 
57     * Possible choices are: "shared", "static" or "executable".
58     * Defaults to "executable".
59     */
60    @Parameter
61    private String type = Library.EXECUTABLE;
62    
63    /**
64     * Arguments to be used for running this test. Defaults to empty list. This
65     * option is only used if run=true.
66     */
67    @Parameter
68    private List/* <String> */args = new ArrayList();
69  
70    /**
71     * List of artifact:binding  for type of dependency to link against when there is a choice.
72     */
73    @Parameter
74    private List<String> dependencyBindings = new ArrayList<>();
75    
76  
77    @Override
78    public final List/* <String> */getArgs() {
79      return this.args;
80    }
81  
82    public String getBinding(NarArtifact dependency) {
83      for (String dependBind : dependencyBindings ) {
84        String[] pair = dependBind.trim().split( ":", 2 );  // TODO: match how much?
85        if( dependency.getArtifactId().equals(pair[0].trim()) ){
86          String result = pair[1].trim();
87          if( !result.isEmpty() )
88            return result;
89        }
90      }
91      return null;
92    }
93  
94    public final String getLink( List<Library> libraries ) {
95      if( this.link != null )
96        return this.link;
97      
98      String libraryPreferred = null;
99      if(libraries.size() == 1){
100       String type = libraries.get(0).getType();
101       if (Library.SHARED.equals(type)||Library.STATIC.equals(type) )
102         libraryPreferred = type;
103       //if(Library.JNI.equals(type)) default shared
104     }
105     return libraryPreferred == null ? Library.SHARED : libraryPreferred; 
106   }
107   
108   public final String getName() throws MojoFailureException {
109     if (this.name == null) {
110       throw new MojoFailureException("NAR: Please specify <Name> as part of <Test>");
111     }
112     return this.name;
113   }
114 
115   public String getType() {
116     return this.type;
117   }
118 
119   @Override
120   public final boolean shouldRun() {
121     return this.run;
122   }
123 }