Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #268 Make sure license file name has only one extension #269

Merged
merged 1 commit into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<name>New BSD License</name>
<url>http://www.opensource.org/licenses/bsd-license.php</url>
<distribution>repo</distribution>
<file>new bsd license - bsd-license.php.html</file>
<file>new bsd license - bsd-license.html</file>
</license>
</licenses>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<name>New BSD License</name>
<url>http://www.opensource.org/licenses/bsd-license.php</url>
<distribution>repo</distribution>
<file>new bsd license - bsd-license.php.html</file>
<file>new bsd license - bsd-license.html</file>
</license>
</licenses>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public class LicenseDownloader implements AutoCloseable
*/
public static final int DEFAULT_CONNECTION_TIMEOUT = 5000;

private static final Pattern EXTENSION_PATTERN = Pattern.compile( "\\.[a-z]{1,4}$", Pattern.CASE_INSENSITIVE );

private final CloseableHttpClient client;

public LicenseDownloader( Proxy proxy )
Expand Down Expand Up @@ -232,23 +234,20 @@ public LicenseDownloadResult downloadLicense( String licenseUrlString, FileNameE
}
}

private static File updateFileExtension( File outputFile, String mimeType )
static File updateFileExtension( File outputFile, String mimeType )
{
final String realExtension = FileUtil.toExtension( mimeType, false );

if ( realExtension != null )
String realExtension = FileUtil.toExtension( mimeType, false );
if ( realExtension == null )
{
if ( !outputFile.getName().endsWith( realExtension ) )
{
return new File( outputFile.getAbsolutePath() + realExtension );
}
/* default extension is .txt */
realExtension = ".txt";
}
/* default extension is .txt */
final String name = outputFile.getName();
final int periodPos = name.lastIndexOf( '.' );
if ( periodPos < 0 || name.length() - periodPos > 5 )

final String oldFileName = outputFile.getName();
if ( !oldFileName.endsWith( realExtension ) )
{
return new File( outputFile.getParent(), name.substring( 0, periodPos ) + ".txt" );
final String newFileName = EXTENSION_PATTERN.matcher( oldFileName ).replaceAll( "" ) + realExtension;
return new File( outputFile.getParentFile(), newFileName );
}
return outputFile;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.codehaus.mojo.license.download;

/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2019 Codehaus
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/

import java.io.File;

import org.junit.Assert;
import org.junit.Test;

public class LicenseDownloaderTest
{

@Test
public void updateFileExtension() {
assertExtension( "path/to/file.html", "path/to/file.php", "text/html" );
assertExtension( "path/to/file.txt", "path/to/file", null );
}

private static void assertExtension( String expected, String input, String mimeType )
{
final File in = new File( input );
final File result = LicenseDownloader.updateFileExtension( in, mimeType );
Assert.assertEquals( expected, result.getPath().replace( '\\', '/' ) );
}

}