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.plugins.jarsigner;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  import java.util.function.Predicate;
24  
25  import org.apache.maven.shared.jarsigner.AbstractJarSignerRequest;
26  import org.apache.maven.shared.jarsigner.JarSignerSignRequest;
27  import org.hamcrest.Description;
28  import org.hamcrest.TypeSafeMatcher;
29  
30  /**
31   * Hamcrest matcher(s) to match properties on a JarSignerRequest request instances
32   */
33  class RequestMatchers {
34  
35      /** Matcher for parameters common for JarSignerRequest instances */
36      private static class AbstractJarSignerRequestMatcher<T extends AbstractJarSignerRequest>
37              extends TypeSafeMatcher<T> {
38          private final String predicateDescription;
39          private final Object value;
40          private final Predicate<AbstractJarSignerRequest> predicate;
41  
42          private AbstractJarSignerRequestMatcher(
43                  String predicateDescription, Object value, Predicate<AbstractJarSignerRequest> predicate) {
44              this.predicateDescription = predicateDescription;
45              this.value = value;
46              this.predicate = predicate;
47          }
48  
49          @Override
50          protected boolean matchesSafely(AbstractJarSignerRequest request) {
51              return predicate.test(request);
52          }
53  
54          @Override
55          public void describeTo(Description description) {
56              description
57                      .appendText("request that ")
58                      .appendText(predicateDescription)
59                      .appendValue(value);
60          }
61      }
62  
63      /** Matcher for parameters specific to JarSignerSignRequest instances */
64      private static class JarSignerSignRequestMatcher extends TypeSafeMatcher<JarSignerSignRequest> {
65          private final String predicateDescription;
66          private final Object value;
67          private final Predicate<JarSignerSignRequest> predicate;
68  
69          private JarSignerSignRequestMatcher(
70                  String predicateDescription, Object value, Predicate<JarSignerSignRequest> predicate) {
71              this.predicateDescription = predicateDescription;
72              this.value = value;
73              this.predicate = predicate;
74          }
75  
76          @Override
77          protected boolean matchesSafely(JarSignerSignRequest request) {
78              return predicate.test(request);
79          }
80  
81          @Override
82          public void describeTo(Description description) {
83              description
84                      .appendText("request that ")
85                      .appendText(predicateDescription)
86                      .appendValue(value);
87          }
88      }
89  
90      /** Create a matcher that matches when the request is using a specific file name for the archive */
91      static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasFileName(String expectedFileName) {
92          return new AbstractJarSignerRequestMatcher<T>(
93                  "has archive file name ",
94                  expectedFileName,
95                  request -> request.getArchive().getPath().endsWith(expectedFileName));
96      }
97  
98      static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasAlias(String alias) {
99          return new AbstractJarSignerRequestMatcher<T>(
100                 "has alias ", alias, request -> request.getAlias().equals(alias));
101     }
102 
103     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasArguments(String[] arguments) {
104         return new AbstractJarSignerRequestMatcher<T>("has arguments ", arguments, request -> {
105             List<String> haystack = Arrays.asList(request.getArguments());
106             for (String argumentNeedle : arguments) {
107                 if (!haystack.contains(argumentNeedle)) {
108                     return false;
109                 }
110             }
111             return true;
112         });
113     }
114 
115     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasKeystore(String keystore) {
116         return new AbstractJarSignerRequestMatcher<T>(
117                 "has keystore ", keystore, request -> request.getKeystore().equals(keystore));
118     }
119 
120     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasMaxMemory(String maxMemory) {
121         return new AbstractJarSignerRequestMatcher<T>(
122                 "has maxMemory ", maxMemory, request -> request.getMaxMemory().equals(maxMemory));
123     }
124 
125     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasProtectedAuthenticationPath(
126             boolean protectedAuthenticationPath) {
127         return new AbstractJarSignerRequestMatcher<T>(
128                 "has protectedAuthenticationPath ",
129                 protectedAuthenticationPath,
130                 request -> request.isProtectedAuthenticationPath() == protectedAuthenticationPath);
131     }
132 
133     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasProviderArg(String providerArg) {
134         return new AbstractJarSignerRequestMatcher<T>(
135                 "has providerArg ", providerArg, request -> request.getProviderArg()
136                         .equals(providerArg));
137     }
138 
139     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasProviderClass(String providerClass) {
140         return new AbstractJarSignerRequestMatcher<T>(
141                 "has providerClass ", providerClass, request -> request.getProviderClass()
142                         .equals(providerClass));
143     }
144 
145     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasProviderName(String providerName) {
146         return new AbstractJarSignerRequestMatcher<T>(
147                 "has providerName ", providerName, request -> request.getProviderName()
148                         .equals(providerName));
149     }
150 
151     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasStorepass(String storepass) {
152         return new AbstractJarSignerRequestMatcher<T>(
153                 "has storepass ", storepass, request -> request.getStorepass().equals(storepass));
154     }
155 
156     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasStoretype(String storetype) {
157         return new AbstractJarSignerRequestMatcher<T>(
158                 "has storetype ", storetype, request -> request.getStoretype().equals(storetype));
159     }
160 
161     static <T extends AbstractJarSignerRequest> TypeSafeMatcher<T> hasVerbose(boolean verbose) {
162         return new AbstractJarSignerRequestMatcher<T>(
163                 "has verbose ", verbose, request -> request.isVerbose() == verbose);
164     }
165 
166     /* ************************************ JarSignerSignRequest specific matchers ************************************/
167 
168     static TypeSafeMatcher<JarSignerSignRequest> hasKeypass(String keypass) {
169         return new JarSignerSignRequestMatcher(
170                 "has keypass ", keypass, request -> request.getKeypass().equals(keypass));
171     }
172 
173     static TypeSafeMatcher<JarSignerSignRequest> hasSigfile(String sigfile) {
174         return new JarSignerSignRequestMatcher(
175                 "has sigfile ", sigfile, request -> request.getSigfile().equals(sigfile));
176     }
177 
178     static TypeSafeMatcher<JarSignerSignRequest> hasTsa(String tsa) {
179         return new JarSignerSignRequestMatcher(
180                 "has tsa ", tsa, request -> request.getTsaLocation().equals(tsa));
181     }
182 
183     static TypeSafeMatcher<JarSignerSignRequest> hasTsacert(String tsacert) {
184         return new JarSignerSignRequestMatcher(
185                 "has tsacert ", tsacert, request -> request.getTsaAlias().equals(tsacert));
186     }
187 
188     static TypeSafeMatcher<JarSignerSignRequest> hasTsaPolicyid(String tsapolicyid) {
189         return new JarSignerSignRequestMatcher("has tsapolicyid ", tsapolicyid, request -> request.getTsapolicyid()
190                 .equals(tsapolicyid));
191     }
192 
193     static TypeSafeMatcher<JarSignerSignRequest> hasTsaDigestalg(String tsadigestalg) {
194         return new JarSignerSignRequestMatcher("has tsadigestalg ", tsadigestalg, request -> request.getTsadigestalg()
195                 .equals(tsadigestalg));
196     }
197 
198     static TypeSafeMatcher<JarSignerSignRequest> hasCertchain(String certchain) {
199         return new JarSignerSignRequestMatcher("has certchain ", certchain, request -> request.getCertchain()
200                 .getPath()
201                 .equals(certchain));
202     }
203 }