Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractProxyTestCase.java 28KB

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