Modified: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/LocatorTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/LocatorTest.java?rev=412788&r1=412787&r2=412788&view=diff ============================================================================== --- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/LocatorTest.java (original) +++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/LocatorTest.java Thu Jun 8 09:18:03 2006 @@ -1,17 +1,136 @@ package org.apache.maven.shared.io.location; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.maven.shared.io.MockManager; +import org.apache.maven.shared.io.logging.DefaultMessageHolder; +import org.apache.maven.shared.io.logging.MessageHolder; +import org.easymock.MockControl; + import junit.framework.TestCase; public class LocatorTest extends TestCase { + + public void testShouldConstructWithNoParams() + { + new Locator(); + } + + public void testShouldConstructWithStrategyStackAndMessageHolder() + { + new Locator( Collections.EMPTY_LIST, new DefaultMessageHolder() ); + } + + public void testShouldAllowModificationOfStrategiesAfterConstructionWithUnmodifiableStack() + { + Locator locator = new Locator( Collections.unmodifiableList( Collections.EMPTY_LIST ), + new DefaultMessageHolder() ); + + locator.addStrategy( new FileLocatorStrategy() ); + + assertEquals( 1, locator.getStrategies().size() ); + } + + public void testShouldRetrieveNonNullMessageHolderWhenConstructedWithoutParams() + { + assertNotNull( new Locator().getMessageHolder() ); + } + + public void testSetStrategiesShouldClearAnyPreExistingStrategiesOut() + { + MockManager mgr = new MockManager(); + + MockControl originalStrategyControl = MockControl.createControl( LocatorStrategy.class ); + + mgr.add( originalStrategyControl ); + + LocatorStrategy originalStrategy = (LocatorStrategy) originalStrategyControl.getMock(); + + MockControl replacementStrategyControl = MockControl.createControl( LocatorStrategy.class ); + + mgr.add( replacementStrategyControl ); + + LocatorStrategy replacementStrategy = (LocatorStrategy) replacementStrategyControl.getMock(); + + mgr.replayAll(); + + Locator locator = new Locator(); + locator.addStrategy( originalStrategy ); + + locator.setStrategies( Collections.singletonList( replacementStrategy ) ); + + List strategies = locator.getStrategies(); + + assertFalse( strategies.contains( originalStrategy ) ); + assertTrue( strategies.contains( replacementStrategy ) ); + + mgr.verifyAll(); + } + + public void testShouldRemovePreviouslyAddedStrategy() + { + MockManager mgr = new MockManager(); + + MockControl originalStrategyControl = MockControl.createControl( LocatorStrategy.class ); + + mgr.add( originalStrategyControl ); + + LocatorStrategy originalStrategy = (LocatorStrategy) originalStrategyControl.getMock(); + + mgr.replayAll(); + + Locator locator = new Locator(); + locator.addStrategy( originalStrategy ); + + List strategies = locator.getStrategies(); + + assertTrue( strategies.contains( originalStrategy ) ); + + locator.removeStrategy( originalStrategy ); + + strategies = locator.getStrategies(); + + assertFalse( strategies.contains( originalStrategy ) ); + + mgr.verifyAll(); + } - public void testClasspathResource() + public void testResolutionFallsThroughStrategyStackAndReturnsNullIfNotResolved() { - String url = getClass().getName().replace( '.', '/' ) + ".class"; + List strategies = new ArrayList(); + + strategies.add( new LoggingLocatorStrategy() ); + strategies.add( new LoggingLocatorStrategy() ); + strategies.add( new LoggingLocatorStrategy() ); + + MessageHolder mh = new DefaultMessageHolder(); + + Locator locator = new Locator( strategies, mh ); + + Location location = locator.resolve( "some-specification" ); + + assertNull( location ); + + assertEquals( 3, mh.size() ); + } + + public static final class LoggingLocatorStrategy implements LocatorStrategy + { + + static int instanceCounter = 0; + + int counter = instanceCounter++; + + public Location resolve( String locationSpecification, MessageHolder messageHolder ) + { + messageHolder.addMessage( "resolve hit on strategy-" + (counter) ); + return null; + } - Locator locator = new Locator(); - locator.resolve( url ); } }
Modified: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java?rev=412788&r1=412787&r2=412788&view=diff ============================================================================== --- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java (original) +++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java Thu Jun 8 09:18:03 2006 @@ -4,13 +4,18 @@ import java.io.IOException; import java.net.URL; +import org.apache.maven.shared.io.TestUtils; + +import junit.framework.TestCase; + public class URLLocationTest - extends AbstractLocationTest + extends TestCase { public void testShouldConstructFromUrlAndTempFileSpecifications() throws IOException { File f = File.createTempFile( "url-location.", ".test" ); + f.deleteOnExit(); URL url = f.toURL(); @@ -20,6 +25,7 @@ public void testShouldTransferFromTempFile() throws IOException { File f = File.createTempFile( "url-location.", ".test" ); + f.deleteOnExit(); URL url = f.toURL(); @@ -32,10 +38,11 @@ public void testShouldTransferFromTempFileThenRead() throws IOException { File f = File.createTempFile( "url-location.", ".test" ); + f.deleteOnExit(); String testStr = "This is a test"; - writeToFile( f, testStr ); + TestUtils.writeToFile( f, testStr ); URL url = f.toURL(); Added: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java?rev=412788&view=auto ============================================================================== --- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java (added) +++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java Thu Jun 8 09:18:03 2006 @@ -0,0 +1,60 @@ +package org.apache.maven.shared.io.location; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +import org.apache.maven.shared.io.TestUtils; +import org.apache.maven.shared.io.logging.DefaultMessageHolder; +import org.apache.maven.shared.io.logging.MessageHolder; + +public class URLLocatorStrategyTest + extends TestCase +{ + + public void testShouldConstructWithNoParams() + { + new URLLocatorStrategy(); + } + + public void testShouldConstructWithTempFileOptions() + { + new URLLocatorStrategy( "prefix.", ".suffix", true ); + } + + public void testShouldFailToResolveWithMalformedUrl() + { + MessageHolder mh = new DefaultMessageHolder(); + + Location location = new URLLocatorStrategy().resolve( "://www.google.com", mh ); + + assertNull( location ); + assertEquals( 1, mh.size() ); + } + + public void testShouldResolveUrlForTempFile() throws IOException + { + File tempFile = File.createTempFile( "prefix.", ".suffix" ); + tempFile.deleteOnExit(); + + String testStr = "This is a test."; + + TestUtils.writeToFile( tempFile, testStr ); + + MessageHolder mh = new DefaultMessageHolder(); + + Location location = new URLLocatorStrategy().resolve( tempFile.toURL().toExternalForm(), mh ); + + assertNotNull( location ); + assertEquals( 0, mh.size() ); + + location.open(); + + byte[] buffer = new byte[testStr.length()]; + location.read( buffer ); + + assertEquals( testStr, new String( buffer ) ); + } + +} Propchange: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision"
