You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractProxyTestCase.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. package org.apache.archiva.proxy;
  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 net.sf.ehcache.CacheManager;
  21. import org.apache.archiva.admin.model.beans.ManagedRepository;
  22. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  23. import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
  24. import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
  25. import org.apache.archiva.configuration.ArchivaConfiguration;
  26. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  27. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  28. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  29. import org.apache.archiva.policies.CachedFailuresPolicy;
  30. import org.apache.archiva.policies.ChecksumPolicy;
  31. import org.apache.archiva.policies.PropagateErrorsDownloadPolicy;
  32. import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
  33. import org.apache.archiva.policies.ReleasesPolicy;
  34. import org.apache.archiva.policies.SnapshotsPolicy;
  35. import org.apache.archiva.repository.ManagedRepositoryContent;
  36. import org.apache.commons.io.FileUtils;
  37. import org.apache.commons.lang.ArrayUtils;
  38. import org.apache.maven.index.NexusIndexer;
  39. import org.apache.maven.index.context.IndexingContext;
  40. import org.apache.maven.wagon.Wagon;
  41. import org.easymock.ArgumentsMatcher;
  42. import org.easymock.MockControl;
  43. import org.junit.After;
  44. import org.junit.Before;
  45. import org.junit.runner.RunWith;
  46. import org.slf4j.Logger;
  47. import org.slf4j.LoggerFactory;
  48. import org.springframework.context.ApplicationContext;
  49. import org.springframework.test.context.ContextConfiguration;
  50. import javax.inject.Inject;
  51. import java.io.BufferedReader;
  52. import java.io.File;
  53. import java.io.FileReader;
  54. import java.io.IOException;
  55. import java.text.ParseException;
  56. import java.text.SimpleDateFormat;
  57. import java.util.ArrayList;
  58. import java.util.Calendar;
  59. import java.util.Collection;
  60. import java.util.Date;
  61. import java.util.Locale;
  62. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  63. import static org.junit.Assert.*;
  64. /**
  65. * AbstractProxyTestCase
  66. *
  67. *
  68. */
  69. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  70. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
  71. public abstract class AbstractProxyTestCase
  72. {
  73. @Inject
  74. protected ApplicationContext applicationContext;
  75. protected static final String ID_LEGACY_PROXIED = "legacy-proxied";
  76. protected static final String ID_PROXIED1 = "proxied1";
  77. protected static final String ID_PROXIED1_TARGET = "proxied1-target";
  78. protected static final String ID_PROXIED2 = "proxied2";
  79. protected static final String ID_PROXIED2_TARGET = "proxied2-target";
  80. protected static final String ID_DEFAULT_MANAGED = "default-managed-repository";
  81. protected static final String ID_LEGACY_MANAGED = "legacy-managed-repository";
  82. protected static final String REPOPATH_PROXIED_LEGACY = "src/test/repositories/legacy-proxied";
  83. protected static final String REPOPATH_PROXIED1 = "src/test/repositories/proxied1";
  84. protected static final String REPOPATH_PROXIED1_TARGET = "target/test-repository/proxied1";
  85. protected static final String REPOPATH_PROXIED2 = "src/test/repositories/proxied2";
  86. protected static final String REPOPATH_PROXIED2_TARGET = "target/test-repository/proxied2";
  87. protected static final String REPOPATH_DEFAULT_MANAGED = "src/test/repositories/managed";
  88. // protected static final String REPOPATH_DEFAULT_MANAGED_TARGET = "target/test-repository/managed";
  89. protected static final String REPOPATH_LEGACY_MANAGED = "src/test/repositories/legacy-managed";
  90. protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed";
  91. protected MockControl wagonMockControl;
  92. protected Wagon wagonMock;
  93. protected RepositoryProxyConnectors proxyHandler;
  94. protected ManagedRepositoryContent managedDefaultRepository;
  95. protected File managedDefaultDir;
  96. protected ManagedRepositoryContent managedLegacyRepository;
  97. protected File managedLegacyDir;
  98. protected MockConfiguration config;
  99. protected Logger log = LoggerFactory.getLogger( getClass() );
  100. WagonDelegate delegate;
  101. @Inject
  102. protected ManagedRepositoryAdmin managedRepositoryAdmin;
  103. @Inject
  104. protected PlexusSisuBridge plexusSisuBridge;
  105. @Before
  106. public void setUp()
  107. throws Exception
  108. {
  109. config =
  110. (MockConfiguration) applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
  111. config.getConfiguration().setManagedRepositories( new ArrayList<ManagedRepositoryConfiguration>() );
  112. config.getConfiguration().setRemoteRepositories( new ArrayList<RemoteRepositoryConfiguration>() );
  113. config.getConfiguration().setProxyConnectors( new ArrayList<ProxyConnectorConfiguration>() );
  114. // Setup source repository (using default layout)
  115. String name = getClass().getSimpleName();
  116. String repoPath = "target/test-repository/managed/" + name;
  117. File repoLocation = new File( repoPath );
  118. managedDefaultRepository =
  119. createRepository( ID_DEFAULT_MANAGED, "Default Managed Repository", repoPath, "default" );
  120. managedDefaultDir = new File( managedDefaultRepository.getRepoRoot() );
  121. ManagedRepository repoConfig = managedDefaultRepository.getRepository();
  122. ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
  123. ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
  124. applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository( repoConfig, false, null );
  125. // to prevent windauze file leaking
  126. removeMavenIndexes();
  127. // Setup source repository (using legacy layout)
  128. repoLocation = new File( REPOPATH_LEGACY_MANAGED_TARGET );
  129. if ( repoLocation.exists() )
  130. {
  131. FileUtils.deleteDirectory( repoLocation );
  132. }
  133. copyDirectoryStructure( new File( REPOPATH_LEGACY_MANAGED ), repoLocation );
  134. managedLegacyRepository =
  135. createRepository( ID_LEGACY_MANAGED, "Legacy Managed Repository", REPOPATH_LEGACY_MANAGED_TARGET,
  136. "legacy" );
  137. managedLegacyDir = new File( managedLegacyRepository.getRepoRoot() );
  138. repoConfig = managedLegacyRepository.getRepository();
  139. //config.getConfiguration().addManagedRepository( repoConfig );
  140. applicationContext.getBean( ManagedRepositoryAdmin.class ).addManagedRepository( repoConfig, false, null );
  141. // Setup target (proxied to) repository.
  142. saveRemoteRepositoryConfig( ID_PROXIED1, "Proxied Repository 1",
  143. new File( REPOPATH_PROXIED1 ).toURL().toExternalForm(), "default" );
  144. // Setup target (proxied to) repository.
  145. saveRemoteRepositoryConfig( ID_PROXIED2, "Proxied Repository 2",
  146. new File( REPOPATH_PROXIED2 ).toURL().toExternalForm(), "default" );
  147. // Setup target (proxied to) repository using legacy layout.
  148. saveRemoteRepositoryConfig( ID_LEGACY_PROXIED, "Proxied Legacy Repository",
  149. new File( REPOPATH_PROXIED_LEGACY ).toURL().toExternalForm(), "legacy" );
  150. // Setup the proxy handler.
  151. //proxyHandler = applicationContext.getBean (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
  152. proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
  153. // Setup the wagon mock.
  154. wagonMockControl = MockControl.createNiceControl( Wagon.class );
  155. wagonMock = (Wagon) wagonMockControl.getMock();
  156. delegate = (WagonDelegate) applicationContext.getBean( "wagon#test", Wagon.class );
  157. delegate.setDelegate( wagonMock );
  158. CacheManager.getInstance().clearAll();
  159. log.info( "\n.\\ " + name + "() \\._________________________________________\n" );
  160. }
  161. @After
  162. public void shutdown()
  163. throws Exception
  164. {
  165. removeMavenIndexes();
  166. }
  167. protected void removeMavenIndexes()
  168. throws Exception
  169. {
  170. NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
  171. for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
  172. {
  173. nexusIndexer.removeIndexingContext( indexingContext, false );
  174. }
  175. }
  176. protected static final ArgumentsMatcher customWagonGetIfNewerMatcher = new ArgumentsMatcher()
  177. {
  178. public boolean matches( Object[] expected, Object[] actual )
  179. {
  180. if ( expected.length < 1 || actual.length < 1 )
  181. {
  182. return false;
  183. }
  184. return MockControl.ARRAY_MATCHER.matches( ArrayUtils.remove( expected, 1 ),
  185. ArrayUtils.remove( actual, 1 ) );
  186. }
  187. public String toString( Object[] arguments )
  188. {
  189. return ArrayUtils.toString( arguments );
  190. }
  191. };
  192. protected static final ArgumentsMatcher customWagonGetMatcher = new ArgumentsMatcher()
  193. {
  194. public boolean matches( Object[] expected, Object[] actual )
  195. {
  196. if ( expected.length == 2 && actual.length == 2 )
  197. {
  198. if ( expected[0] == null && actual[0] == null )
  199. {
  200. return true;
  201. }
  202. if ( expected[0] == null )
  203. {
  204. return actual[0] == null;
  205. }
  206. if ( actual[0] == null )
  207. {
  208. return expected[0] == null;
  209. }
  210. return expected[0].equals( actual[0] );
  211. }
  212. return false;
  213. }
  214. public String toString( Object[] arguments )
  215. {
  216. return ArrayUtils.toString( arguments );
  217. }
  218. };
  219. protected void assertChecksums( File expectedFile, String expectedSha1Contents, String expectedMd5Contents )
  220. throws Exception
  221. {
  222. File sha1File = new File( expectedFile.getAbsolutePath() + ".sha1" );
  223. File md5File = new File( expectedFile.getAbsolutePath() + ".md5" );
  224. if ( expectedSha1Contents == null )
  225. {
  226. assertFalse( "SHA1 File should NOT exist: " + sha1File.getPath(), sha1File.exists() );
  227. }
  228. else
  229. {
  230. assertTrue( "SHA1 File should exist: " + sha1File.getPath(), sha1File.exists() );
  231. String actualSha1Contents = readChecksumFile( sha1File );
  232. assertEquals( "SHA1 File contents: " + sha1File.getPath(), expectedSha1Contents, actualSha1Contents );
  233. }
  234. if ( expectedMd5Contents == null )
  235. {
  236. assertFalse( "MD5 File should NOT exist: " + md5File.getPath(), md5File.exists() );
  237. }
  238. else
  239. {
  240. assertTrue( "MD5 File should exist: " + md5File.getPath(), md5File.exists() );
  241. String actualMd5Contents = readChecksumFile( md5File );
  242. assertEquals( "MD5 File contents: " + md5File.getPath(), expectedMd5Contents, actualMd5Contents );
  243. }
  244. }
  245. protected void assertFileEquals( File expectedFile, File actualFile, File sourceFile )
  246. throws Exception
  247. {
  248. assertNotNull( "Expected File should not be null.", expectedFile );
  249. assertNotNull( "Actual File should not be null.", actualFile );
  250. assertTrue( "Check actual file exists.", actualFile.exists() );
  251. assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
  252. actualFile.getCanonicalPath() );
  253. assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), actualFile.getAbsolutePath() );
  254. String expectedContents = org.apache.commons.io.FileUtils.readFileToString( sourceFile, null );
  255. String actualContents = org.apache.commons.io.FileUtils.readFileToString( actualFile, null );
  256. assertEquals( "Check file contents.", expectedContents, actualContents );
  257. }
  258. protected void assertNotDownloaded( File downloadedFile )
  259. {
  260. assertNull( "Found file: " + downloadedFile + "; but was expecting a failure", downloadedFile );
  261. }
  262. @SuppressWarnings( "unchecked" )
  263. protected void assertNoTempFiles( File expectedFile )
  264. {
  265. File workingDir = expectedFile.getParentFile();
  266. if ( ( workingDir == null ) || !workingDir.isDirectory() )
  267. {
  268. return;
  269. }
  270. Collection<File> tmpFiles =
  271. org.apache.commons.io.FileUtils.listFiles( workingDir, new String[]{ "tmp" }, false );
  272. if ( !tmpFiles.isEmpty() )
  273. {
  274. StringBuffer emsg = new StringBuffer();
  275. emsg.append( "Found Temp Files in dir: " ).append( workingDir.getPath() );
  276. for ( File tfile : tmpFiles )
  277. {
  278. emsg.append( "\n " ).append( tfile.getName() );
  279. }
  280. fail( emsg.toString() );
  281. }
  282. }
  283. /**
  284. * A faster recursive copy that omits .svn directories.
  285. *
  286. * @param sourceDirectory the source directory to copy
  287. * @param destDirectory the target location
  288. * @throws java.io.IOException if there is a copying problem
  289. * @todo get back into plexus-utils, share with converter module
  290. */
  291. protected void copyDirectoryStructure( File sourceDirectory, File destDirectory )
  292. throws IOException
  293. {
  294. if ( !sourceDirectory.exists() )
  295. {
  296. throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
  297. }
  298. File[] files = sourceDirectory.listFiles();
  299. String sourcePath = sourceDirectory.getAbsolutePath();
  300. for ( int i = 0; i < files.length; i++ )
  301. {
  302. File file = files[i];
  303. String dest = file.getAbsolutePath();
  304. dest = dest.substring( sourcePath.length() + 1 );
  305. File destination = new File( destDirectory, dest );
  306. if ( file.isFile() )
  307. {
  308. destination = destination.getParentFile();
  309. org.apache.commons.io.FileUtils.copyFile( file, new File( destination, file.getName() ), false );
  310. // TODO: Change when there is a FileUtils.copyFileToDirectory(file, destination, boolean) option
  311. //FileUtils.copyFileToDirectory( file, destination );
  312. }
  313. else if ( file.isDirectory() )
  314. {
  315. if ( !".svn".equals( file.getName() ) )
  316. {
  317. if ( !destination.exists() && !destination.mkdirs() )
  318. {
  319. throw new IOException(
  320. "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
  321. }
  322. copyDirectoryStructure( file, destination );
  323. }
  324. }
  325. else
  326. {
  327. throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
  328. }
  329. }
  330. }
  331. protected ManagedRepositoryContent createManagedLegacyRepository()
  332. throws Exception
  333. {
  334. return createRepository( "testManagedLegacyRepo", "Test Managed (Legacy) Repository",
  335. "src/test/repositories/legacy-managed", "legacy" );
  336. }
  337. protected ManagedRepositoryContent createProxiedLegacyRepository()
  338. throws Exception
  339. {
  340. return createRepository( "testProxiedLegacyRepo", "Test Proxied (Legacy) Repository",
  341. "src/test/repositories/legacy-proxied", "legacy" );
  342. }
  343. protected ManagedRepositoryContent createRepository( String id, String name, String path, String layout )
  344. throws Exception
  345. {
  346. ManagedRepository repo = new ManagedRepository();
  347. repo.setId( id );
  348. repo.setName( name );
  349. repo.setLocation( path );
  350. repo.setLayout( layout );
  351. ManagedRepositoryContent repoContent =
  352. applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class );
  353. repoContent.setRepository( repo );
  354. return repoContent;
  355. }
  356. /**
  357. * Read the first line from the checksum file, and return it (trimmed).
  358. */
  359. protected String readChecksumFile( File checksumFile )
  360. throws Exception
  361. {
  362. FileReader freader = null;
  363. BufferedReader buf = null;
  364. try
  365. {
  366. freader = new FileReader( checksumFile );
  367. buf = new BufferedReader( freader );
  368. return buf.readLine();
  369. }
  370. finally
  371. {
  372. if ( buf != null )
  373. {
  374. buf.close();
  375. }
  376. if ( freader != null )
  377. {
  378. freader.close();
  379. }
  380. }
  381. }
  382. protected void saveConnector( String sourceRepoId, String targetRepoId, boolean disabled )
  383. {
  384. saveConnector( sourceRepoId, targetRepoId, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS,
  385. CachedFailuresPolicy.NO, disabled );
  386. }
  387. protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
  388. String snapshotPolicy, String cacheFailuresPolicy, boolean disabled )
  389. {
  390. saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
  391. PropagateErrorsDownloadPolicy.QUEUE, disabled );
  392. }
  393. protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
  394. String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
  395. boolean disabled )
  396. {
  397. saveConnector( sourceRepoId, targetRepoId, checksumPolicy, releasePolicy, snapshotPolicy, cacheFailuresPolicy,
  398. errorPolicy, PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT, disabled );
  399. }
  400. protected void saveConnector( String sourceRepoId, String targetRepoId, String checksumPolicy, String releasePolicy,
  401. String snapshotPolicy, String cacheFailuresPolicy, String errorPolicy,
  402. String errorOnUpdatePolicy, boolean disabled )
  403. {
  404. ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
  405. connectorConfig.setSourceRepoId( sourceRepoId );
  406. connectorConfig.setTargetRepoId( targetRepoId );
  407. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
  408. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
  409. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
  410. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
  411. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS, errorPolicy );
  412. connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE, errorOnUpdatePolicy );
  413. connectorConfig.setDisabled( disabled );
  414. int count = config.getConfiguration().getProxyConnectors().size();
  415. config.getConfiguration().addProxyConnector( connectorConfig );
  416. // Proper Triggering ...
  417. String prefix = "proxyConnectors.proxyConnector(" + count + ")";
  418. config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
  419. config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
  420. config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
  421. config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
  422. config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
  423. config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
  424. config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
  425. config.triggerChange( prefix + ".policies.propagate-errors",
  426. connectorConfig.getPolicy( "propagate-errors", "" ) );
  427. config.triggerChange( prefix + ".policies.propagate-errors-on-update",
  428. connectorConfig.getPolicy( "propagate-errors-on-update", "" ) );
  429. }
  430. protected void saveManagedRepositoryConfig( String id, String name, String path, String layout )
  431. {
  432. ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
  433. repoConfig.setId( id );
  434. repoConfig.setName( name );
  435. repoConfig.setLayout( layout );
  436. repoConfig.setLocation( path );
  437. int count = config.getConfiguration().getManagedRepositories().size();
  438. config.getConfiguration().addManagedRepository( repoConfig );
  439. String prefix = "managedRepositories.managedRepository(" + count + ")";
  440. config.triggerChange( prefix + ".id", repoConfig.getId() );
  441. config.triggerChange( prefix + ".name", repoConfig.getName() );
  442. config.triggerChange( prefix + ".location", repoConfig.getLocation() );
  443. config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
  444. }
  445. protected void saveRemoteRepositoryConfig( String id, String name, String url, String layout )
  446. {
  447. RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
  448. repoConfig.setId( id );
  449. repoConfig.setName( name );
  450. repoConfig.setLayout( layout );
  451. repoConfig.setUrl( url );
  452. int count = config.getConfiguration().getRemoteRepositories().size();
  453. config.getConfiguration().addRemoteRepository( repoConfig );
  454. String prefix = "remoteRepositories.remoteRepository(" + count + ")";
  455. config.triggerChange( prefix + ".id", repoConfig.getId() );
  456. config.triggerChange( prefix + ".name", repoConfig.getName() );
  457. config.triggerChange( prefix + ".url", repoConfig.getUrl() );
  458. config.triggerChange( prefix + ".layout", repoConfig.getLayout() );
  459. }
  460. protected File saveTargetedRepositoryConfig( String id, String originalPath, String targetPath, String layout )
  461. throws IOException
  462. {
  463. File repoLocation = new File( targetPath );
  464. FileUtils.deleteDirectory( repoLocation );
  465. copyDirectoryStructure( new File( originalPath ), repoLocation );
  466. saveRemoteRepositoryConfig( id, "Target Repo-" + id, targetPath, layout );
  467. return repoLocation;
  468. }
  469. /**
  470. * Copy the specified resource directory from the src/test/repository/managed/ to
  471. * the testable directory under target/test-repository/managed/${testName}/
  472. *
  473. * @param resourcePath
  474. * @throws IOException
  475. */
  476. protected void setupTestableManagedRepository( String resourcePath )
  477. throws IOException
  478. {
  479. String resourceDir = resourcePath;
  480. if ( !resourcePath.endsWith( "/" ) )
  481. {
  482. int idx = resourcePath.lastIndexOf( '/' );
  483. resourceDir = resourcePath.substring( 0, idx );
  484. }
  485. File sourceRepoDir = new File( REPOPATH_DEFAULT_MANAGED );
  486. File sourceDir = new File( sourceRepoDir, resourceDir );
  487. File destRepoDir = managedDefaultDir;
  488. File destDir = new File( destRepoDir, resourceDir );
  489. // Cleanout destination dirs.
  490. if ( destDir.exists() )
  491. {
  492. FileUtils.deleteDirectory( destDir );
  493. }
  494. // Make the destination dir.
  495. destDir.mkdirs();
  496. // Test the source dir.
  497. if ( !sourceDir.exists() )
  498. {
  499. // This is just a warning.
  500. System.err.println(
  501. "[WARN] Skipping setup of testable managed repository, source dir does not exist: " + sourceDir );
  502. }
  503. else
  504. {
  505. // Test that the source is a dir.
  506. if ( !sourceDir.isDirectory() )
  507. {
  508. fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
  509. }
  510. // Copy directory structure.
  511. copyDirectoryStructure( sourceDir, destDir );
  512. }
  513. }
  514. protected void setManagedNewerThanRemote( File managedFile, File remoteFile )
  515. {
  516. setManagedNewerThanRemote( managedFile, remoteFile, 55000 );
  517. }
  518. protected void setManagedNewerThanRemote( File managedFile, File remoteFile, long time )
  519. {
  520. assertTrue( "Managed File should exist: ", managedFile.exists() );
  521. assertTrue( "Remote File should exist: ", remoteFile.exists() );
  522. managedFile.setLastModified( remoteFile.lastModified() + time );
  523. assertTrue( managedFile.lastModified() > remoteFile.lastModified() );
  524. }
  525. protected void setManagedOlderThanRemote( File managedFile, File remoteFile )
  526. {
  527. setManagedOlderThanRemote( managedFile, remoteFile, 55000 );
  528. }
  529. protected void setManagedOlderThanRemote( File managedFile, File remoteFile, long time )
  530. {
  531. assertTrue( "Managed File should exist: ", managedFile.exists() );
  532. assertTrue( "Remote File should exist: ", remoteFile.exists() );
  533. managedFile.setLastModified( remoteFile.lastModified() - time );
  534. assertTrue( managedFile.lastModified() < remoteFile.lastModified() );
  535. }
  536. protected void assertNotModified( File file, long expectedModificationTime )
  537. {
  538. assertEquals( "File <" + file.getAbsolutePath() + "> not have been modified.", expectedModificationTime,
  539. file.lastModified() );
  540. }
  541. protected void assertNotExistsInManagedLegacyRepo( File file )
  542. throws Exception
  543. {
  544. String managedLegacyPath = managedLegacyDir.getCanonicalPath();
  545. String testFile = file.getCanonicalPath();
  546. assertTrue(
  547. "Unit Test Failure: File <" + testFile + "> should be have been defined within the legacy managed path of <"
  548. + managedLegacyPath + ">", testFile.startsWith( managedLegacyPath ) );
  549. assertFalse( "File < " + testFile + "> should not exist in managed legacy repository.", file.exists() );
  550. }
  551. protected void assertNotExistsInManagedDefaultRepo( File file )
  552. throws Exception
  553. {
  554. String managedDefaultPath = managedDefaultDir.getCanonicalPath();
  555. String testFile = file.getCanonicalPath();
  556. assertTrue( "Unit Test Failure: File <" + testFile
  557. + "> should be have been defined within the managed default path of <" + managedDefaultPath
  558. + ">", testFile.startsWith( managedDefaultPath ) );
  559. assertFalse( "File < " + testFile + "> should not exist in managed default repository.", file.exists() );
  560. }
  561. protected static Date getFutureDate()
  562. throws ParseException
  563. {
  564. Calendar cal = Calendar.getInstance();
  565. cal.add( Calendar.YEAR, 1 );
  566. return cal.getTime();
  567. }
  568. protected static Date getPastDate()
  569. throws ParseException
  570. {
  571. return new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" );
  572. }
  573. }