/nexus-maven-plugins/nexus-maven-plugin/src/test/java/org/sonatype/nexus/plugin/discovery/DefaultNexusDiscovery_DiscoverTest.java

https://github.com/galak/nexus · Java · 495 lines · 354 code · 123 blank · 18 comment · 0 complexity · 1ca0e73efc1659d9fa9126fc38b72d97 MD5 · raw file

  1. /**
  2. * Copyright (c) 2008-2011 Sonatype, Inc.
  3. * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions
  4. *
  5. * This program is free software: you can redistribute it and/or modify it only under the terms of the GNU Affero General
  6. * Public License Version 3 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  9. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License Version 3
  10. * for more details.
  11. *
  12. * You should have received a copy of the GNU Affero General Public License Version 3 along with this program. If not, see
  13. * http://www.gnu.org/licenses.
  14. *
  15. * Sonatype Nexus (TM) Open Source Version is available from Sonatype, Inc. Sonatype and Sonatype Nexus are trademarks of
  16. * Sonatype, Inc. Apache Maven is a trademark of the Apache Foundation. M2Eclipse is a trademark of the Eclipse Foundation.
  17. * All other trademarks are the property of their respective owners.
  18. */
  19. package org.sonatype.nexus.plugin.discovery;
  20. import static junit.framework.Assert.assertEquals;
  21. import static junit.framework.Assert.assertNotNull;
  22. import org.apache.maven.model.DeploymentRepository;
  23. import org.apache.maven.model.DistributionManagement;
  24. import org.apache.maven.model.Model;
  25. import org.apache.maven.model.Repository;
  26. import org.apache.maven.project.MavenProject;
  27. import org.apache.maven.settings.Mirror;
  28. import org.apache.maven.settings.Server;
  29. import org.apache.maven.settings.Settings;
  30. import org.junit.Test;
  31. public class DefaultNexusDiscovery_DiscoverTest
  32. extends AbstractNexusDiscoveryTest
  33. {
  34. @Test
  35. public void autoDiscoverWithOneCompleteLocationFromMirrors()
  36. throws NexusDiscoveryException
  37. {
  38. Settings settings = new Settings();
  39. String url = "http://nexus.somewhere.com/";
  40. testClientManager.testUrl = url;
  41. testClientManager.testUser = "user";
  42. testClientManager.testPassword = "password";
  43. Mirror mirror = new Mirror();
  44. mirror.setId( "some-mirror" );
  45. mirror.setName( "A Mirror" );
  46. mirror.setUrl( url );
  47. settings.addMirror( mirror );
  48. Server server = new Server();
  49. server.setId( "some-mirror" );
  50. server.setUsername( "user" );
  51. server.setPassword( "password" );
  52. settings.addServer( server );
  53. Model model = new Model();
  54. model.setModelVersion( "4.0.0" );
  55. model.setGroupId( "group.id" );
  56. model.setArtifactId( "artifact-id" );
  57. model.setVersion( "1" );
  58. MavenProject project = new MavenProject( model );
  59. discovery.discover( settings, project, "blah", true );
  60. }
  61. @Test
  62. public void autoDiscoverWithOneCompleteLocationFromMirrorWithEncryptedPassword()
  63. throws NexusDiscoveryException
  64. {
  65. Settings settings = new Settings();
  66. String url = "http://nexus.somewhere.com/";
  67. testClientManager.testUrl = url;
  68. testClientManager.testUser = "user";
  69. testClientManager.testPassword = "password";
  70. Mirror mirror = new Mirror();
  71. mirror.setId( "some-mirror" );
  72. mirror.setName( "A Mirror" );
  73. mirror.setUrl( url );
  74. settings.addMirror( mirror );
  75. Server server = new Server();
  76. server.setId( "some-mirror" );
  77. server.setUsername( "user" );
  78. server.setPassword( encryptedPassword );
  79. settings.addServer( server );
  80. Model model = new Model();
  81. model.setModelVersion( "4.0.0" );
  82. model.setGroupId( "group.id" );
  83. model.setArtifactId( "artifact-id" );
  84. model.setVersion( "1" );
  85. MavenProject project = new MavenProject( model );
  86. discovery.discover( settings, project, "blah", true );
  87. }
  88. @Test
  89. public void autoDiscoverWithOneCompleteLocationFromMirrorWithEncryptedPasswordContainingComment()
  90. throws NexusDiscoveryException
  91. {
  92. Settings settings = new Settings();
  93. String url = "http://nexus.somewhere.com/";
  94. testClientManager.testUrl = url;
  95. testClientManager.testUser = "user";
  96. testClientManager.testPassword = "password";
  97. Mirror mirror = new Mirror();
  98. mirror.setId( "some-mirror" );
  99. mirror.setName( "A Mirror" );
  100. mirror.setUrl( url );
  101. settings.addMirror( mirror );
  102. Server server = new Server();
  103. server.setId( "some-mirror" );
  104. server.setUsername( "user" );
  105. server.setPassword( "this is a comment " + encryptedPassword );
  106. settings.addServer( server );
  107. Model model = new Model();
  108. model.setModelVersion( "4.0.0" );
  109. model.setGroupId( "group.id" );
  110. model.setArtifactId( "artifact-id" );
  111. model.setVersion( "1" );
  112. MavenProject project = new MavenProject( model );
  113. discovery.discover( settings, project, "blah", true );
  114. }
  115. @Test
  116. public void autoDiscoverWithOneCompleteLocationFromSettingsProfileRepo()
  117. throws NexusDiscoveryException
  118. {
  119. Settings settings = new Settings();
  120. String url = "http://nexus.somewhere.com/";
  121. String id = "some-mirror";
  122. String user = "user";
  123. String password = "password";
  124. testClientManager.testUrl = url;
  125. testClientManager.testUser = user;
  126. testClientManager.testPassword = password;
  127. Server server = new Server();
  128. server.setId( id );
  129. server.setUsername( user );
  130. server.setPassword( password );
  131. settings.addServer( server );
  132. org.apache.maven.settings.Repository repo = new org.apache.maven.settings.Repository();
  133. repo.setId( id );
  134. repo.setUrl( url );
  135. org.apache.maven.settings.Profile profile = new org.apache.maven.settings.Profile();
  136. profile.addRepository( repo );
  137. settings.addProfile( profile );
  138. Model model = new Model();
  139. model.setModelVersion( "4.0.0" );
  140. model.setGroupId( "group.id" );
  141. model.setArtifactId( "artifact-id" );
  142. model.setVersion( "1" );
  143. MavenProject project = new MavenProject( model );
  144. discovery.discover( settings, project, "blah", true );
  145. }
  146. @Test
  147. public void autoDiscoverWithOneCompleteLocationFromSettingsProfileRepoWithConfirmation()
  148. throws NexusDiscoveryException
  149. {
  150. Settings settings = new Settings();
  151. String url = "http://nexus.somewhere.com/";
  152. String id = "some-mirror";
  153. String user = "user";
  154. String password = "password";
  155. testClientManager.testUrl = url;
  156. testClientManager.testUser = user;
  157. testClientManager.testPassword = password;
  158. Server server = new Server();
  159. server.setId( id );
  160. server.setUsername( user );
  161. server.setPassword( password );
  162. settings.addServer( server );
  163. org.apache.maven.settings.Repository repo = new org.apache.maven.settings.Repository();
  164. repo.setId( id );
  165. repo.setUrl( url );
  166. repo.setName( "Profile Repository" );
  167. org.apache.maven.settings.Profile profile = new org.apache.maven.settings.Profile();
  168. profile.addRepository( repo );
  169. settings.addProfile( profile );
  170. Model model = new Model();
  171. model.setModelVersion( "4.0.0" );
  172. model.setGroupId( "group.id" );
  173. model.setArtifactId( "artifact-id" );
  174. model.setVersion( "1" );
  175. MavenProject project = new MavenProject( model );
  176. prompter.addExpectation( "Use this connection?", "y" );
  177. discovery.discover( settings, project, "blah", false );
  178. }
  179. @Test
  180. public void autoDiscoverWithContentUrlInSettingsProfileRepo()
  181. throws NexusDiscoveryException
  182. {
  183. Settings settings = new Settings();
  184. String url = "http://nexus.somewhere.com";
  185. String id = "some-mirror";
  186. String user = "user";
  187. String password = "password";
  188. testClientManager.testUrl = url;
  189. testClientManager.testUser = user;
  190. testClientManager.testPassword = password;
  191. Server server = new Server();
  192. server.setId( id );
  193. server.setUsername( user );
  194. server.setPassword( password );
  195. settings.addServer( server );
  196. org.apache.maven.settings.Repository repo = new org.apache.maven.settings.Repository();
  197. repo.setId( id );
  198. repo.setUrl( url + "/content/groups/public/" );
  199. repo.setName( "Profile Repository" );
  200. org.apache.maven.settings.Profile profile = new org.apache.maven.settings.Profile();
  201. profile.addRepository( repo );
  202. settings.addProfile( profile );
  203. Model model = new Model();
  204. model.setModelVersion( "4.0.0" );
  205. model.setGroupId( "group.id" );
  206. model.setArtifactId( "artifact-id" );
  207. model.setVersion( "1" );
  208. MavenProject project = new MavenProject( model );
  209. prompter.addExpectation( "Use this connection?", "y" );
  210. discovery.discover( settings, project, "blah", false );
  211. }
  212. @Test
  213. public void autoDiscoverWithOneCompleteLocationFromPOMRepo()
  214. throws NexusDiscoveryException
  215. {
  216. Settings settings = new Settings();
  217. String url = "http://nexus.somewhere.com/";
  218. String id = "some-mirror";
  219. String user = "user";
  220. String password = "password";
  221. testClientManager.testUrl = url;
  222. testClientManager.testUser = user;
  223. testClientManager.testPassword = password;
  224. Server server = new Server();
  225. server.setId( id );
  226. server.setUsername( user );
  227. server.setPassword( password );
  228. settings.addServer( server );
  229. Model model = new Model();
  230. model.setModelVersion( "4.0.0" );
  231. model.setGroupId( "group.id" );
  232. model.setArtifactId( "artifact-id" );
  233. model.setVersion( "1" );
  234. Repository repo = new Repository();
  235. repo.setId( id );
  236. repo.setUrl( url );
  237. model.addRepository( repo );
  238. MavenProject project = new MavenProject( model );
  239. discovery.discover( settings, project, "blah", true );
  240. }
  241. @Test
  242. public void autoDiscoverWithOneCompleteLocationFromPOMDistMgmt()
  243. throws NexusDiscoveryException
  244. {
  245. Settings settings = new Settings();
  246. String url = "http://nexus.somewhere.com/";
  247. String id = "some-mirror";
  248. String user = "user";
  249. String password = "password";
  250. testClientManager.testUrl = url;
  251. testClientManager.testUser = user;
  252. testClientManager.testPassword = password;
  253. Server server = new Server();
  254. server.setId( id );
  255. server.setUsername( user );
  256. server.setPassword( password );
  257. settings.addServer( server );
  258. Model model = new Model();
  259. model.setModelVersion( "4.0.0" );
  260. model.setGroupId( "group.id" );
  261. model.setArtifactId( "artifact-id" );
  262. model.setVersion( "1" );
  263. DistributionManagement dm = new DistributionManagement();
  264. DeploymentRepository repo = new DeploymentRepository();
  265. repo.setId( id );
  266. repo.setUrl( url );
  267. dm.setRepository( repo );
  268. model.setDistributionManagement( dm );
  269. MavenProject project = new MavenProject( model );
  270. project.setArtifact( factory.create( project ) );
  271. discovery.discover( settings, project, "blah", true );
  272. }
  273. @Test
  274. public void autoDiscoverWithOneCompleteLocationFromSnapshotPOMDistMgmt()
  275. throws NexusDiscoveryException
  276. {
  277. Settings settings = new Settings();
  278. String url = "http://nexus.somewhere.com/";
  279. String id = "some-mirror";
  280. String user = "user";
  281. String password = "password";
  282. testClientManager.testUrl = url;
  283. testClientManager.testUser = user;
  284. testClientManager.testPassword = password;
  285. Server server = new Server();
  286. server.setId( id );
  287. server.setUsername( user );
  288. server.setPassword( password );
  289. settings.addServer( server );
  290. Model model = new Model();
  291. model.setModelVersion( "4.0.0" );
  292. model.setGroupId( "group.id" );
  293. model.setArtifactId( "artifact-id" );
  294. model.setVersion( "1-SNAPSHOT" );
  295. DistributionManagement dm = new DistributionManagement();
  296. DeploymentRepository repo = new DeploymentRepository();
  297. repo.setId( id );
  298. repo.setUrl( url );
  299. dm.setSnapshotRepository( repo );
  300. model.setDistributionManagement( dm );
  301. MavenProject project = new MavenProject( model );
  302. project.setArtifact( factory.create( project ) );
  303. discovery.discover( settings, project, "blah", true );
  304. }
  305. @Test
  306. public void promptWithOnePotentialLocationFromMirrors()
  307. throws NexusDiscoveryException
  308. {
  309. Settings settings = new Settings();
  310. String url = "http://nexus.somewhere.com/";
  311. String user = "user";
  312. String password = "password";
  313. testClientManager.testUrl = url;
  314. testClientManager.testUser = user;
  315. testClientManager.testPassword = password;
  316. Mirror mirror = new Mirror();
  317. mirror.setId( "some-mirror" );
  318. mirror.setName( "A Mirror" );
  319. mirror.setUrl( url );
  320. settings.addMirror( mirror );
  321. Model model = new Model();
  322. model.setModelVersion( "4.0.0" );
  323. model.setGroupId( "group.id" );
  324. model.setArtifactId( "artifact-id" );
  325. model.setVersion( "1" );
  326. MavenProject project = new MavenProject( model );
  327. prompter.addExpectation( "1", "http://nexus.somewhere.com/", "Selection:" );
  328. prompter.addExpectation( "Enter Username", user );
  329. prompter.addExpectation( "Enter Password", password );
  330. NexusConnectionInfo info = discovery.discover( settings, project, "blah", false );
  331. assertNotNull( info );
  332. assertEquals( url, info.getNexusUrl() );
  333. assertEquals( user, info.getUser() );
  334. assertEquals( password, info.getPassword() );
  335. }
  336. @Test
  337. public void promptWithTwoPotentialLocationsFromMirrors()
  338. throws NexusDiscoveryException
  339. {
  340. Settings settings = new Settings();
  341. String url = "http://nexus.somewhere.com/";
  342. String user = "user";
  343. String password = "password";
  344. testClientManager.testUrl = url;
  345. testClientManager.testUser = user;
  346. testClientManager.testPassword = password;
  347. Mirror mirror = new Mirror();
  348. mirror.setId( "some-mirror" );
  349. mirror.setName( "A Mirror" );
  350. mirror.setUrl( url );
  351. settings.addMirror( mirror );
  352. Mirror mirror2 = new Mirror();
  353. mirror2.setId( "some-other-mirror" );
  354. mirror2.setName( "Another Mirror" );
  355. mirror2.setUrl( "http://nexus.somewhere-else.com/" );
  356. settings.addMirror( mirror2 );
  357. Model model = new Model();
  358. model.setModelVersion( "4.0.0" );
  359. model.setGroupId( "group.id" );
  360. model.setArtifactId( "artifact-id" );
  361. model.setVersion( "1" );
  362. MavenProject project = new MavenProject( model );
  363. prompter.addExpectation( "1", "http://nexus.somewhere.com/", "Selection:" );
  364. prompter.addExpectation( "Enter Username", user );
  365. prompter.addExpectation( "Enter Password", password );
  366. NexusConnectionInfo info = discovery.discover( settings, project, "blah", false );
  367. assertNotNull( info );
  368. assertEquals( url, info.getNexusUrl() );
  369. assertEquals( user, info.getUser() );
  370. assertEquals( password, info.getPassword() );
  371. }
  372. }