Adding shared utilities to scan for stale resources, and provide a common utility...
authorJohn Dennis Casey <jdcasey@apache.org>
Sun, 18 Dec 2005 04:53:37 +0000 (04:53 +0000)
committerJohn Dennis Casey <jdcasey@apache.org>
Sun, 18 Dec 2005 04:53:37 +0000 (04:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@357432 13f79535-47bb-0310-9956-ffa450edef68

pom.xml [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/Locator.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/scan/AbstractResourceInclusionScanner.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/scan/InclusionScanException.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/scan/ResourceInclusionScanner.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/scan/SimpleResourceInclusionScanner.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/scan/StaleResourceScanner.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/scan/mapping/SingleTargetMapping.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/scan/mapping/SourceMapping.java [new file with mode: 0644]
src/main/java/org/apache/maven/shared/io/scan/mapping/SuffixMapping.java [new file with mode: 0644]
src/test/java/org/apache/maven/shared/io/scan/mapping/SuffixMappingTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
new file mode 100644 (file)
index 0000000..62a81b4
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,38 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+  xsi:noNamespaceSchemaLocation="http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+  
+  <parent>
+    <groupId>org.apache.maven.shared</groupId>
+    <artifactId>shared-components-parent</artifactId>
+    <version>1</version>
+  </parent>
+
+  <artifactId>maven-shared-io</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  
+  <name>Maven Shared I/O API</name>
+  <description>Basic API for lightweight logging</description>
+  
+  <contributors>
+    <contributor>
+      <name>Joakim Erdfelt</name>
+      <email>joakim@erdfelt.com</email>
+    </contributor>
+  </contributors>
+  
+  <dependencies>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>1.0.4</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-shared-monitor</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/src/main/java/org/apache/maven/shared/io/Locator.java b/src/main/java/org/apache/maven/shared/io/Locator.java
new file mode 100644 (file)
index 0000000..a226fd3
--- /dev/null
@@ -0,0 +1,140 @@
+package org.apache.maven.shared.io;
+
+/*
+ * Copyright 2004-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.maven.shared.monitor.BasicMonitor;
+import org.apache.maven.shared.monitor.Monitor;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.StringUtils;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URL;
+
+/**
+ * Performs Locator services for the <code>*Location</code> parameters in the 
+ * Reports.
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ */
+public class Locator
+{
+    private Monitor monitor;
+
+    /**
+     * Create a Locator object.
+     * 
+     * @param logger the logger object to log with.
+     */
+    public Locator( Monitor logger )
+    {
+        this.monitor = logger;
+    }
+
+    /**
+     * Obtain a Log object.
+     * 
+     * @return the Log object.
+     */
+    private Monitor getLog()
+    {
+        if ( this.monitor == null )
+        {
+            this.monitor = new BasicMonitor( System.out );
+        }
+        return this.monitor;
+    }
+
+    /**
+     * <p>
+     * Attempts to resolve a location parameter into a real file.
+     * </p>
+     * 
+     * <p>
+     * Checks a location string to for a resource, URL, or File that matches.
+     * If a resource or URL is found, then a local file is created with that
+     * locations contents.
+     * </p>
+     * 
+     * @param location the location string to match against.
+     * @param localfile the local file to use in case of resource or URL.
+     * @return the File of the resolved location.
+     * @throws IOException if file is unable to be found or copied into <code>localfile</code> destination.
+     */
+    public File resolveLocation( String location, String localfile )
+        throws IOException
+    {
+        getLog().debug( "resolveLocation(" + location + ", " + localfile + ")" );
+        if ( StringUtils.isEmpty( location ) )
+        {
+            return null;
+        }
+
+        File retFile = new File( localfile );
+
+        // Attempt a URL
+        if ( location.indexOf( "://" ) > 1 )
+        {
+            // Found a URL
+            URL url = new URL( location );
+            getLog().debug( "Potential URL: " + url.toExternalForm() );
+            FileUtils.copyURLToFile( url, retFile );
+        }
+        else
+        {
+            getLog().debug( "Location is not a URL." );
+            // Attempt a File.
+            File fileLocation = new File( location );
+            if ( fileLocation.exists() )
+            {
+                // Found a File.
+                getLog().debug( "Potential File: " + fileLocation.getAbsolutePath() );
+                FileUtils.copyFile( fileLocation, retFile );
+            }
+            else
+            {
+                getLog().debug( "Location is not a File." );
+                // Attempt a Resource.
+                URL url = this.getClass().getClassLoader().getResource( location );
+                if ( url != null )
+                {
+                    // Found a Resource.
+                    getLog().debug( "Potential Resource: " + url.toExternalForm() );
+                    FileUtils.copyURLToFile( url, retFile );
+                }
+                else
+                {
+                    getLog().debug( "Location is not a Resource." );
+                    throw new IOException( "Unable to find location '" + location + "' as URL, File or Resource." );
+                }
+            }
+        }
+
+        if ( !retFile.exists() )
+        {
+            throw new FileNotFoundException( "Destination file does not exist." );
+        }
+
+        if ( retFile.length() <= 0 )
+        {
+            throw new IOException( "Destination file has no content." );
+        }
+
+        return retFile;
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/io/scan/AbstractResourceInclusionScanner.java b/src/main/java/org/apache/maven/shared/io/scan/AbstractResourceInclusionScanner.java
new file mode 100644 (file)
index 0000000..307f19c
--- /dev/null
@@ -0,0 +1,82 @@
+package org.apache.maven.shared.io.scan;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.maven.shared.io.scan.mapping.SourceMapping;
+import org.codehaus.plexus.util.DirectoryScanner;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author jdcasey
+ * @version $Id$
+ */
+public abstract class AbstractResourceInclusionScanner
+    implements ResourceInclusionScanner
+{
+    private final List sourceMappings = new ArrayList();
+
+    public final void addSourceMapping( SourceMapping sourceMapping )
+    {
+        sourceMappings.add( sourceMapping );
+    }
+
+    protected final List getSourceMappings()
+    {
+        return Collections.unmodifiableList( sourceMappings );
+    }
+
+    protected String[] scanForSources( File sourceDir, Set sourceIncludes, Set sourceExcludes )
+    {
+        DirectoryScanner ds = new DirectoryScanner();
+        ds.setFollowSymlinks( true );
+        ds.setBasedir( sourceDir );
+
+        String[] includes;
+        if ( sourceIncludes.isEmpty() )
+        {
+            includes = new String[0];
+        }
+        else
+        {
+            includes = (String[]) sourceIncludes.toArray( new String[sourceIncludes.size()] );
+        }
+
+        ds.setIncludes( includes );
+
+        String[] excludes;
+        if ( sourceExcludes.isEmpty() )
+        {
+            excludes = new String[0];
+        }
+        else
+        {
+            excludes = (String[]) sourceExcludes.toArray( new String[sourceExcludes.size()] );
+        }
+
+        ds.setExcludes( excludes );
+        ds.addDefaultExcludes();
+
+        ds.scan();
+
+        return ds.getIncludedFiles();
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/io/scan/InclusionScanException.java b/src/main/java/org/apache/maven/shared/io/scan/InclusionScanException.java
new file mode 100644 (file)
index 0000000..481ad40
--- /dev/null
@@ -0,0 +1,35 @@
+package org.apache.maven.shared.io.scan;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author jdcasey
+ * @version $Id$
+ */
+public class InclusionScanException
+    extends Exception
+{
+    public InclusionScanException( String message )
+    {
+        super( message );
+    }
+
+    public InclusionScanException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/io/scan/ResourceInclusionScanner.java b/src/main/java/org/apache/maven/shared/io/scan/ResourceInclusionScanner.java
new file mode 100644 (file)
index 0000000..188a373
--- /dev/null
@@ -0,0 +1,32 @@
+package org.apache.maven.shared.io.scan;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+import org.apache.maven.shared.io.scan.mapping.SourceMapping;
+
+import java.io.File;
+import java.util.Set;
+
+/**
+ * @author jdcasey
+ * @version $Id$
+ */
+public interface ResourceInclusionScanner
+{
+    void addSourceMapping( SourceMapping sourceMapping );
+
+    Set getIncludedSources( File sourceDir, File targetDir )
+        throws InclusionScanException;
+}
diff --git a/src/main/java/org/apache/maven/shared/io/scan/SimpleResourceInclusionScanner.java b/src/main/java/org/apache/maven/shared/io/scan/SimpleResourceInclusionScanner.java
new file mode 100644 (file)
index 0000000..a0c7963
--- /dev/null
@@ -0,0 +1,52 @@
+package org.apache.maven.shared.io.scan;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
+ * @version $Id$
+ */
+public class SimpleResourceInclusionScanner
+    extends AbstractResourceInclusionScanner
+{
+    private Set sourceIncludes;
+
+    private Set sourceExcludes;
+
+    public SimpleResourceInclusionScanner( Set sourceIncludes, Set sourceExcludes )
+    {
+        this.sourceIncludes = sourceIncludes;
+
+        this.sourceExcludes = sourceExcludes;
+    }
+
+    public Set getIncludedSources( File sourceDir, File targetDir )
+        throws InclusionScanException
+    {
+        List srcMappings = getSourceMappings();
+
+        if ( srcMappings.isEmpty() )
+        {
+            return Collections.EMPTY_SET;
+        }
+
+        return Collections.singleton( scanForSources( sourceDir, sourceIncludes, sourceExcludes ) );
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/io/scan/StaleResourceScanner.java b/src/main/java/org/apache/maven/shared/io/scan/StaleResourceScanner.java
new file mode 100644 (file)
index 0000000..a5fbebd
--- /dev/null
@@ -0,0 +1,113 @@
+package org.apache.maven.shared.io.scan;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.maven.shared.io.scan.mapping.SourceMapping;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author jdcasey
+ * @version $Id$
+ */
+public class StaleResourceScanner
+    extends AbstractResourceInclusionScanner
+{
+    private final long lastUpdatedWithinMsecs;
+
+    private final Set sourceIncludes;
+
+    private final Set sourceExcludes;
+
+    // ----------------------------------------------------------------------
+    //
+    // ----------------------------------------------------------------------
+
+    public StaleResourceScanner()
+    {
+        this( 0, Collections.singleton( "**/*" ), Collections.EMPTY_SET );
+    }
+
+    public StaleResourceScanner( long lastUpdatedWithinMsecs )
+    {
+        this( lastUpdatedWithinMsecs, Collections.singleton( "**/*" ), Collections.EMPTY_SET );
+    }
+
+    public StaleResourceScanner( long lastUpdatedWithinMsecs, Set sourceIncludes, Set sourceExcludes )
+    {
+        this.lastUpdatedWithinMsecs = lastUpdatedWithinMsecs;
+
+        this.sourceIncludes = sourceIncludes;
+
+        this.sourceExcludes = sourceExcludes;
+    }
+
+    // ----------------------------------------------------------------------
+    // SourceInclusionScanner Implementation
+    // ----------------------------------------------------------------------
+
+    public Set getIncludedSources( File sourceDir, File targetDir )
+        throws InclusionScanException
+    {
+        List srcMappings = getSourceMappings();
+
+        if ( srcMappings.isEmpty() )
+        {
+            return Collections.EMPTY_SET;
+        }
+
+        String[] potentialIncludes = scanForSources( sourceDir, sourceIncludes, sourceExcludes );
+
+        Set matchingSources = new HashSet();
+
+        for ( int i = 0; i < potentialIncludes.length; i++ )
+        {
+            String path = potentialIncludes[i];
+
+            File sourceFile = new File( sourceDir, path );
+
+            staleSourceFileTesting: for ( Iterator patternIt = srcMappings.iterator(); patternIt.hasNext(); )
+            {
+                SourceMapping mapping = (SourceMapping) patternIt.next();
+
+                Set targetFiles = mapping.getTargetFiles( targetDir, path );
+
+                // never include files that don't have corresponding target mappings.
+                // the targets don't have to exist on the filesystem, but the
+                // mappers must tell us to look for them.
+                for ( Iterator targetIt = targetFiles.iterator(); targetIt.hasNext(); )
+                {
+                    File targetFile = (File) targetIt.next();
+
+                    if ( !targetFile.exists()
+                        || ( targetFile.lastModified() + lastUpdatedWithinMsecs < sourceFile.lastModified() ) )
+                    {
+                        matchingSources.add( sourceFile );
+                        break staleSourceFileTesting;
+                    }
+                }
+            }
+        }
+
+        return matchingSources;
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/io/scan/mapping/SingleTargetMapping.java b/src/main/java/org/apache/maven/shared/io/scan/mapping/SingleTargetMapping.java
new file mode 100644 (file)
index 0000000..e3f4a31
--- /dev/null
@@ -0,0 +1,53 @@
+package org.apache.maven.shared.io.scan.mapping;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+import org.apache.maven.shared.io.scan.InclusionScanException;
+
+import java.util.Set;
+import java.util.Collections;
+import java.io.File;
+
+/**
+ * Maps a set of input files to a single output file.
+ *
+ * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
+ * @version $Id$
+ */
+public class SingleTargetMapping
+    implements SourceMapping
+{
+    private String sourceSuffix;
+
+    private String outputFile;
+
+    public SingleTargetMapping( String sourceSuffix, String outputFile )
+    {
+        this.sourceSuffix = sourceSuffix;
+
+        this.outputFile = outputFile;
+    }
+
+    public Set getTargetFiles( File targetDir, String source )
+        throws InclusionScanException
+    {
+        if ( !source.endsWith( sourceSuffix ) )
+        {
+            return Collections.EMPTY_SET;
+        }
+
+        return Collections.singleton( new File( targetDir, outputFile ) );
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/io/scan/mapping/SourceMapping.java b/src/main/java/org/apache/maven/shared/io/scan/mapping/SourceMapping.java
new file mode 100644 (file)
index 0000000..b3529af
--- /dev/null
@@ -0,0 +1,32 @@
+package org.apache.maven.shared.io.scan.mapping;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.maven.shared.io.scan.InclusionScanException;
+
+import java.io.File;
+import java.util.Set;
+
+/**
+ * @author jdcasey
+ * @version $Id$
+ */
+public interface SourceMapping
+{
+    Set getTargetFiles( File targetDir, String source )
+        throws InclusionScanException;
+}
diff --git a/src/main/java/org/apache/maven/shared/io/scan/mapping/SuffixMapping.java b/src/main/java/org/apache/maven/shared/io/scan/mapping/SuffixMapping.java
new file mode 100644 (file)
index 0000000..523f7e1
--- /dev/null
@@ -0,0 +1,68 @@
+package org.apache.maven.shared.io.scan.mapping;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.File;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * @author jdcasey
+ * @version $Id$
+ */
+public final class SuffixMapping
+    implements SourceMapping
+{
+    private final String sourceSuffix;
+
+    private final Set targetSuffixes;
+
+    public SuffixMapping( String sourceSuffix, String targetSuffix )
+    {
+        this.sourceSuffix = sourceSuffix;
+
+        this.targetSuffixes = Collections.singleton( targetSuffix );
+    }
+
+    public SuffixMapping( String sourceSuffix, Set targetSuffixes )
+    {
+        this.sourceSuffix = sourceSuffix;
+
+        this.targetSuffixes = Collections.unmodifiableSet( targetSuffixes );
+    }
+
+    public Set getTargetFiles( File targetDir, String source )
+    {
+        Set targetFiles = new HashSet();
+
+        if ( source.endsWith( sourceSuffix ) )
+        {
+            String base = source.substring( 0, source.length() - sourceSuffix.length() );
+
+            for ( Iterator it = targetSuffixes.iterator(); it.hasNext(); )
+            {
+                String suffix = (String) it.next();
+
+                targetFiles.add( new File( targetDir, base + suffix ) );
+            }
+        }
+
+        return targetFiles;
+    }
+}
diff --git a/src/test/java/org/apache/maven/shared/io/scan/mapping/SuffixMappingTest.java b/src/test/java/org/apache/maven/shared/io/scan/mapping/SuffixMappingTest.java
new file mode 100644 (file)
index 0000000..d0df135
--- /dev/null
@@ -0,0 +1,119 @@
+package org.apache.maven.shared.io.scan.mapping;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import junit.framework.TestCase;
+import org.apache.maven.shared.io.scan.InclusionScanException;
+
+import java.io.File;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author jdcasey
+ */
+public class SuffixMappingTest
+    extends TestCase
+{
+    public void testShouldReturnSingleClassFileForSingleJavaFile()
+        throws InclusionScanException
+    {
+        String base = "path/to/file";
+
+        File basedir = new File( "." );
+
+        SuffixMapping mapping = new SuffixMapping( ".java", ".class" );
+
+        Set results = mapping.getTargetFiles( basedir, base + ".java" );
+
+        assertEquals( "Returned wrong number of target files.", 1, results.size() );
+
+        assertEquals( "Target file is wrong.", new File( basedir, base + ".class" ), results.iterator().next() );
+    }
+
+    public void testShouldNotReturnClassFileWhenSourceFileHasWrongSuffix()
+        throws InclusionScanException
+    {
+        String base = "path/to/file";
+
+        File basedir = new File( "." );
+
+        SuffixMapping mapping = new SuffixMapping( ".java", ".class" );
+
+        Set results = mapping.getTargetFiles( basedir, base + ".xml" );
+
+        assertTrue( "Returned wrong number of target files.", results.isEmpty() );
+    }
+
+    public void testShouldReturnOneClassFileAndOneXmlFileForSingleJavaFile()
+        throws InclusionScanException
+    {
+        String base = "path/to/file";
+
+        File basedir = new File( "." );
+
+        Set targets = new HashSet();
+        targets.add( ".class" );
+        targets.add( ".xml" );
+
+        SuffixMapping mapping = new SuffixMapping( ".java", targets );
+
+        Set results = mapping.getTargetFiles( basedir, base + ".java" );
+
+        assertEquals( "Returned wrong number of target files.", 2, results.size() );
+
+        assertTrue( "Targets do not contain class target.", results.contains( new File( basedir, base + ".class" ) ) );
+
+        assertTrue( "Targets do not contain class target.", results.contains( new File( basedir, base + ".xml" ) ) );
+    }
+
+    public void testShouldReturnNoTargetFilesWhenSourceFileHasWrongSuffix()
+        throws InclusionScanException
+    {
+        String base = "path/to/file";
+
+        File basedir = new File( "." );
+
+        Set targets = new HashSet();
+        targets.add( ".class" );
+        targets.add( ".xml" );
+
+        SuffixMapping mapping = new SuffixMapping( ".java", targets );
+
+        Set results = mapping.getTargetFiles( basedir, base + ".apt" );
+
+        assertTrue( "Returned wrong number of target files.", results.isEmpty() );
+    }
+
+    public void testSingleTargetMapper()
+        throws InclusionScanException
+    {
+        String base = "path/to/file";
+
+        File basedir = new File( "target/" );
+
+        SingleTargetMapping mapping = new SingleTargetMapping( ".cs", "/foo" );
+
+        Set results = mapping.getTargetFiles( basedir, base + ".apt" );
+
+        assertTrue( results.isEmpty() );
+
+        results = mapping.getTargetFiles( basedir, base + ".cs" );
+
+        assertEquals( 1, results.size() );
+    }
+}