DefaultUpdateCheckManager implements UpdateCheckManager, Service { public boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy ) { boolean checkForUpdates; if ( policy == null ) { policy = ""; } if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) ) { checkForUpdates = true; } else if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) ) { Calendar cal = Calendar.getInstance(); cal.set( Calendar.HOUR_OF_DAY, 0 ); cal.set( Calendar.MINUTE, 0 ); cal.set( Calendar.SECOND, 0 ); cal.set( Calendar.MILLISECOND, 0 ); checkForUpdates = cal.getTimeInMillis() > lastModified; } else if ( policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) ) { int minutes; try { String s = policy.substring( RepositoryPolicy.UPDATE_POLICY_INTERVAL.length() + 1 ); minutes = Integer.valueOf( s ); } catch ( RuntimeException e ) { minutes = 24 * 60; logger.warn( "Non-parseable repository update policy '" + policy + "', assuming '" + RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":1440'" ); } Calendar cal = Calendar.getInstance(); cal.add( Calendar.MINUTE, -minutes ); checkForUpdates = cal.getTimeInMillis() > lastModified; } else { checkForUpdates = false; if ( !RepositoryPolicy.UPDATE_POLICY_NEVER.equals( policy ) ) { logger.warn( "Unknown repository update policy '" + policy + "', assuming '" + RepositoryPolicy.UPDATE_POLICY_NEVER + "'" ); } } return checkForUpdates; } DefaultUpdateCheckManager();  DefaultUpdateCheckManager( Logger logger ); void initService( ServiceLocator locator ); DefaultUpdateCheckManager setLogger( Logger logger ); String getEffectiveUpdatePolicy( RepositorySystemSession session, String policy1, String policy2 ); void checkArtifact( RepositorySystemSession session, UpdateCheck<Artifact, ArtifactTransferException> check ); void checkMetadata( RepositorySystemSession session, UpdateCheck<Metadata, MetadataTransferException> check ); boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy ); void touchArtifact( RepositorySystemSession session, UpdateCheck<Artifact, ArtifactTransferException> check ); void touchMetadata( RepositorySystemSession session, UpdateCheck<Metadata, MetadataTransferException> check );  }
@Test public void testIsUpdateRequired_PolicyNever() throws Exception { String policy = RepositoryPolicy.UPDATE_POLICY_NEVER; assertEquals( false, manager.isUpdatedRequired( session, Long.MIN_VALUE, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, Long.MAX_VALUE, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, 0, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, 1, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, System.currentTimeMillis() - 604800000, policy ) ); }
@Test public void testIsUpdateRequired_PolicyAlways() throws Exception { String policy = RepositoryPolicy.UPDATE_POLICY_ALWAYS; assertEquals( true, manager.isUpdatedRequired( session, Long.MIN_VALUE, policy ) ); assertEquals( true, manager.isUpdatedRequired( session, Long.MAX_VALUE, policy ) ); assertEquals( true, manager.isUpdatedRequired( session, 0, policy ) ); assertEquals( true, manager.isUpdatedRequired( session, 1, policy ) ); assertEquals( true, manager.isUpdatedRequired( session, System.currentTimeMillis() - 1000, policy ) ); }
@Test public void testIsUpdateRequired_PolicyDaily() throws Exception { Calendar cal = Calendar.getInstance(); cal.set( Calendar.HOUR_OF_DAY, 0 ); cal.set( Calendar.MINUTE, 0 ); cal.set( Calendar.SECOND, 0 ); cal.set( Calendar.MILLISECOND, 0 ); long localMidnight = cal.getTimeInMillis(); String policy = RepositoryPolicy.UPDATE_POLICY_DAILY; assertEquals( true, manager.isUpdatedRequired( session, Long.MIN_VALUE, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, Long.MAX_VALUE, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, localMidnight + 0, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, localMidnight + 1, policy ) ); assertEquals( true, manager.isUpdatedRequired( session, localMidnight - 1, policy ) ); }
@Test public void testIsUpdateRequired_PolicyInterval() throws Exception { String policy = RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":5"; assertEquals( true, manager.isUpdatedRequired( session, Long.MIN_VALUE, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, Long.MAX_VALUE, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, System.currentTimeMillis(), policy ) ); assertEquals( false, manager.isUpdatedRequired( session, System.currentTimeMillis() - 5 - 1, policy ) ); assertEquals( false, manager.isUpdatedRequired( session, System.currentTimeMillis() - 1000 * 5 - 1, policy ) ); assertEquals( true, manager.isUpdatedRequired( session, System.currentTimeMillis() - 1000 * 60 * 5 - 1, policy ) ); policy = RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":invalid"; assertEquals( false, manager.isUpdatedRequired( session, System.currentTimeMillis(), policy ) ); }