-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
229 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
openedge-plugin/src/main/java/org/sonar/plugins/openedge/foundation/FilenameUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
package org.sonar.plugins.openedge.foundation; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Imported from commons-io | ||
*/ | ||
public class FilenameUtils { | ||
|
||
private static final int NOT_FOUND = -1; | ||
|
||
private static final char EXTENSION_SEPARATOR = '.'; | ||
/** | ||
* The Unix separator character. | ||
*/ | ||
private static final char UNIX_SEPARATOR = '/'; | ||
|
||
/** | ||
* The Windows separator character. | ||
*/ | ||
private static final char WINDOWS_SEPARATOR = '\\'; | ||
|
||
private FilenameUtils() { | ||
// Only static methods here | ||
} | ||
|
||
public static String getExtension(String filename) { | ||
requireNonNull(filename); | ||
final int index = indexOfExtension(filename); | ||
if (index == NOT_FOUND) { | ||
return ""; | ||
} else { | ||
return filename.substring(index + 1); | ||
} | ||
} | ||
|
||
public static String removeExtension(final String fileName) { | ||
requireNonNull(fileName); | ||
|
||
final int index = indexOfExtension(fileName); | ||
if (index == NOT_FOUND) { | ||
return fileName; | ||
} | ||
return fileName.substring(0, index); | ||
} | ||
|
||
public static String getBaseName(final String fileName) { | ||
return removeExtension(getName(fileName)); | ||
} | ||
|
||
public static String getName(final String fileName) { | ||
if (fileName == null) { | ||
return null; | ||
} | ||
return fileName.substring(indexOfLastSeparator(fileName) + 1); | ||
} | ||
|
||
private static int indexOfExtension(String filename) { | ||
final int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); | ||
final int lastSeparator = indexOfLastSeparator(filename); | ||
return lastSeparator > extensionPos ? NOT_FOUND : extensionPos; | ||
} | ||
|
||
private static int indexOfLastSeparator(String filename) { | ||
final int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR); | ||
final int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR); | ||
return Math.max(lastUnixPos, lastWindowsPos); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
openedge-plugin/src/test/java/org/sonar/plugins/openedge/foundation/FilenameUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.sonar.plugins.openedge.foundation; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.expectThrows; | ||
import static org.sonar.plugins.openedge.foundation.FilenameUtils.getExtension; | ||
import static org.sonar.plugins.openedge.foundation.FilenameUtils.removeExtension; | ||
import static org.sonar.plugins.openedge.foundation.FilenameUtils.getBaseName; | ||
import static org.sonar.plugins.openedge.foundation.FilenameUtils.getName; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
public class FilenameUtilsTest { | ||
|
||
@Test | ||
public void testGetExtension() { | ||
assertEquals(getExtension("C:\\foo\\bar\\filename.txt"), "txt"); | ||
assertEquals(getExtension("filename.txt"), "txt"); | ||
assertEquals(getExtension("filename.t"), "t"); | ||
assertEquals(getExtension("filename"), ""); | ||
assertEquals(getExtension("foo.bar\\filename"), ""); | ||
expectThrows(NullPointerException.class, () -> getExtension(null)); | ||
} | ||
|
||
@Test | ||
public void testRemoveExtension() { | ||
assertEquals(removeExtension("C:\\foo\\bar\\filename.txt"), "C:\\foo\\bar\\filename"); | ||
assertEquals(removeExtension("filename.txt"), "filename"); | ||
assertEquals(removeExtension("filename.t"), "filename"); | ||
assertEquals(removeExtension("filename"), "filename"); | ||
expectThrows(NullPointerException.class, () -> removeExtension(null)); | ||
} | ||
|
||
@Test | ||
public void testGetBaseName() { | ||
assertEquals(getBaseName("C:\\foo\\bar\\filename.txt"), "filename"); | ||
assertEquals(getBaseName("filename.txt"), "filename"); | ||
assertEquals(getBaseName("filename.t"), "filename"); | ||
assertEquals(getBaseName("filename"), "filename"); | ||
expectThrows(NullPointerException.class, () -> getBaseName(null)); | ||
} | ||
|
||
@Test | ||
public void testGetName() { | ||
assertEquals(getName("C:\\foo\\bar\\filename.txt"), "filename.txt"); | ||
assertEquals(getName("filename.txt"), "filename.txt"); | ||
assertEquals(getName("filename.t"), "filename.t"); | ||
assertEquals(getName("filename"), "filename"); | ||
assertEquals(getName(null), null); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.