PageRenderTime 74ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/maven-shared-components-15/maven-common-artifact-filters/src/test/java/org/apache/maven/shared/artifact/filter/AbstractStrictPatternArtifactFilterTest.java

#
Java | 420 lines | 266 code | 76 blank | 78 comment | 0 complexity | b8277bc0d1ac48841457f46943dc3e7d MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.apache.maven.shared.artifact.filter;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. 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,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.util.Collections;
  21. import java.util.List;
  22. import org.apache.maven.artifact.Artifact;
  23. import org.apache.maven.artifact.DefaultArtifact;
  24. import org.apache.maven.artifact.handler.ArtifactHandler;
  25. import org.apache.maven.artifact.handler.DefaultArtifactHandler;
  26. import org.apache.maven.artifact.versioning.VersionRange;
  27. import junit.framework.AssertionFailedError;
  28. import junit.framework.TestCase;
  29. /**
  30. * Tests subclasses of <code>AbstractStrictPatternArtifactFilter</code>.
  31. *
  32. * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
  33. * @version $Id: AbstractStrictPatternArtifactFilterTest.java 803321 2009-08-11 23:09:24Z aheritier $
  34. * @see AbstractStrictPatternArtifactFilter
  35. */
  36. public abstract class AbstractStrictPatternArtifactFilterTest extends TestCase
  37. {
  38. // fields -----------------------------------------------------------------
  39. protected Artifact artifact;
  40. // TestCase methods -------------------------------------------------------
  41. /*
  42. * @see junit.framework.TestCase#setUp()
  43. */
  44. protected void setUp() throws Exception
  45. {
  46. artifact = createArtifact( "groupId", "artifactId", "type", "version" );
  47. }
  48. // tests ------------------------------------------------------------------
  49. public void testExactIncluded()
  50. {
  51. assertIncluded( "groupId:artifactId" );
  52. }
  53. public void testExactExcluded()
  54. {
  55. assertExcluded( "differentGroupId:differentArtifactId" );
  56. }
  57. public void testGroupIdIncluded()
  58. {
  59. assertIncluded( "groupId" );
  60. }
  61. public void testGroupIdExcluded()
  62. {
  63. assertExcluded( "differentGroupId" );
  64. }
  65. public void testGroupIdWildcardIncluded()
  66. {
  67. assertIncluded( "*" );
  68. }
  69. public void testGroupIdImplicitWildcardIncluded()
  70. {
  71. assertIncluded( "" );
  72. }
  73. public void testGroupIdStartsWithWildcardIncluded()
  74. {
  75. assertIncluded( "groupId*" );
  76. }
  77. public void testGroupIdStartsWithPartialWildcardIncluded()
  78. {
  79. assertIncluded( "group*" );
  80. }
  81. public void testGroupIdStartsWithWildcardExcluded()
  82. {
  83. assertExcluded( "different*" );
  84. }
  85. public void testGroupIdEndsWithWildcardIncluded()
  86. {
  87. assertIncluded( "*groupId" );
  88. }
  89. public void testGroupIdEndsWithPartialWildcardIncluded()
  90. {
  91. assertIncluded( "*Id" );
  92. }
  93. public void testGroupIdEndsWithWildcardExcluded()
  94. {
  95. assertExcluded( "*different" );
  96. }
  97. public void testGroupIdContainsWildcardIncluded()
  98. {
  99. assertIncluded( "*oup*" );
  100. }
  101. public void testGroupIdContainsWildcardExcluded()
  102. {
  103. assertExcluded( "*different*" );
  104. }
  105. public void testArtifactIdIncluded()
  106. {
  107. assertIncluded( ":artifactId" );
  108. }
  109. public void testArtifactIdExcluded()
  110. {
  111. assertExcluded( ":differentArtifactId" );
  112. }
  113. public void testArtifactIdWildcardIncluded()
  114. {
  115. assertIncluded( ":*" );
  116. }
  117. public void testArtifactIdImplicitWildcardIncluded()
  118. {
  119. assertIncluded( ":" );
  120. }
  121. public void testArtifactIdStartsWithWildcardIncluded()
  122. {
  123. assertIncluded( ":artifactId*" );
  124. }
  125. public void testArtifactIdStartsWithPartialWildcardIncluded()
  126. {
  127. assertIncluded( ":artifact*" );
  128. }
  129. public void testArtifactIdStartsWithWildcardExcluded()
  130. {
  131. assertExcluded( ":different*" );
  132. }
  133. public void testArtifactIdEndsWithWildcardIncluded()
  134. {
  135. assertIncluded( ":*artifactId" );
  136. }
  137. public void testArtifactIdEndsWithPartialWildcardIncluded()
  138. {
  139. assertIncluded( ":*Id" );
  140. }
  141. public void testArtifactIdEndsWithWildcardExcluded()
  142. {
  143. assertExcluded( ":*different" );
  144. }
  145. public void testArtifactIdContainsWildcardIncluded()
  146. {
  147. assertIncluded( ":*fact*" );
  148. }
  149. public void testArtifactIdContainsWildcardExcluded()
  150. {
  151. assertExcluded( ":*different*" );
  152. }
  153. public void testTypeIncluded()
  154. {
  155. assertIncluded( "::type" );
  156. }
  157. public void testTypeExcluded()
  158. {
  159. assertExcluded( "::differentType" );
  160. }
  161. public void testTypeWildcardIncluded()
  162. {
  163. assertIncluded( "::*" );
  164. }
  165. public void testTypeImplicitWildcardIncluded()
  166. {
  167. assertIncluded( "::" );
  168. }
  169. public void testTypeStartsWithWildcardIncluded()
  170. {
  171. assertIncluded( "::type*" );
  172. }
  173. public void testTypeStartsWithPartialWildcardIncluded()
  174. {
  175. assertIncluded( "::t*" );
  176. }
  177. public void testTypeStartsWithWildcardExcluded()
  178. {
  179. assertExcluded( "::different*" );
  180. }
  181. public void testTypeEndsWithWildcardIncluded()
  182. {
  183. assertIncluded( "::*type" );
  184. }
  185. public void testTypeEndsWithPartialWildcardIncluded()
  186. {
  187. assertIncluded( "::*e" );
  188. }
  189. public void testTypeEndsWithWildcardExcluded()
  190. {
  191. assertExcluded( "::*different" );
  192. }
  193. public void testTypeContainsWildcardIncluded()
  194. {
  195. assertIncluded( "::*yp*" );
  196. }
  197. public void testTypeContainsWildcardExcluded()
  198. {
  199. assertExcluded( "::*different*" );
  200. }
  201. public void testVersionIncluded()
  202. {
  203. assertIncluded( ":::version" );
  204. }
  205. public void testVersionExcluded()
  206. {
  207. assertExcluded( ":::differentVersion" );
  208. }
  209. public void testVersionWildcardIncluded()
  210. {
  211. assertIncluded( ":::*" );
  212. }
  213. public void testVersionImplicitWildcardIncluded()
  214. {
  215. assertIncluded( ":::" );
  216. }
  217. public void testVersionStartsWithWildcardIncluded()
  218. {
  219. assertIncluded( ":::version*" );
  220. }
  221. public void testVersionStartsWithPartialWildcardIncluded()
  222. {
  223. assertIncluded( ":::ver*" );
  224. }
  225. public void testVersionStartsWithWildcardExcluded()
  226. {
  227. assertExcluded( ":::different*" );
  228. }
  229. public void testVersionEndsWithWildcardIncluded()
  230. {
  231. assertIncluded( ":::*version" );
  232. }
  233. public void testVersionEndsWithPartialWildcardIncluded()
  234. {
  235. assertIncluded( ":::*ion" );
  236. }
  237. public void testVersionEndsWithWildcardExcluded()
  238. {
  239. assertExcluded( ":::*different" );
  240. }
  241. public void testVersionContainsWildcardIncluded()
  242. {
  243. assertIncluded( ":::*si*" );
  244. }
  245. public void testVersionContainsWildcardExcluded()
  246. {
  247. assertExcluded( ":::*different*" );
  248. }
  249. public void testComplex()
  250. {
  251. assertIncluded( "group*:*Id:*:version" );
  252. }
  253. public void testSnapshotVersion()
  254. {
  255. artifact = createArtifact( "groupId", "artifactId", "type", "version-12345678.123456-1" );
  256. assertIncluded( ":::*-SNAPSHOT" );
  257. }
  258. public void testRangeVersion()
  259. {
  260. artifact = createArtifact( "groupId", "artifactId", "type", "1.0.1" );
  261. assertIncluded( "groupId:artifactId:type:[1.0.1]");
  262. assertIncluded( "groupId:artifactId:type:[1.0,1.1)");
  263. assertExcluded( "groupId:artifactId:type:[1.5,)");
  264. assertExcluded( "groupId:artifactId:type:(,1.0],[1.2,)");
  265. assertExcluded( "groupId:artifactId:type:(,1.0],[1.2,)");
  266. }
  267. public void testWildcardsWithRangeVersion()
  268. {
  269. artifact = createArtifact( "groupId", "artifactId", "type", "1.0.1" );
  270. assertIncluded( ":::[1.0.1]");
  271. assertIncluded( ":artifact*:*:[1.0,1.1)");
  272. assertExcluded( "*group*:*:t*e:[1.5,)");
  273. artifact = createArtifact( "test", "uf", "jar", "0.2.0" );
  274. assertIncluded( "test:*:*:[0.0.2,)" );
  275. }
  276. // protected methods ------------------------------------------------------
  277. /**
  278. * Creates an artifact with the specified attributes.
  279. *
  280. * @param groupId
  281. * the group id for the new artifact
  282. * @param artifactId
  283. * the artifact id for the new artifact
  284. * @param type
  285. * the type for the new artifact
  286. * @param version
  287. * the version for the new artifact
  288. * @return the artifact
  289. */
  290. protected Artifact createArtifact( String groupId, String artifactId, String type, String version )
  291. {
  292. VersionRange versionRange = VersionRange.createFromVersion( version );
  293. ArtifactHandler handler = new DefaultArtifactHandler();
  294. return new DefaultArtifact( groupId, artifactId, versionRange, null, type, null, handler );
  295. }
  296. /**
  297. * Asserts that the specified pattern is included by the filter being tested.
  298. *
  299. * @param pattern
  300. * the pattern to test for inclusion
  301. * @throws AssertionFailedError
  302. * if the assertion fails
  303. */
  304. protected void assertIncluded( String pattern )
  305. {
  306. assertFilter( true, pattern );
  307. }
  308. /**
  309. * Asserts that the specified pattern is excluded by the filter being tested.
  310. *
  311. * @param pattern
  312. * the pattern to test for exclusion
  313. * @throws AssertionFailedError
  314. * if the assertion fails
  315. */
  316. protected void assertExcluded( String pattern )
  317. {
  318. assertFilter( false, pattern );
  319. }
  320. /**
  321. * Asserts that the filter being tested returns the specified result for the specified pattern.
  322. *
  323. * @param expected
  324. * the result expected from the filter
  325. * @param pattern
  326. * the pattern to test
  327. * @throws AssertionFailedError
  328. * if the assertion fails
  329. */
  330. protected void assertFilter( boolean expected, String pattern )
  331. {
  332. List patterns = Collections.singletonList( pattern );
  333. AbstractStrictPatternArtifactFilter filter = createFilter( patterns );
  334. assertEquals( expected, filter.include( artifact ) );
  335. }
  336. /**
  337. * Creates the strict pattern artifact filter to test for the specified patterns.
  338. *
  339. * @param patterns
  340. * the list of artifact patterns that the filter should match
  341. * @return the filter to test
  342. */
  343. protected abstract AbstractStrictPatternArtifactFilter createFilter( List patterns );
  344. }