Skip to content

Commit

Permalink
More unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed Jul 3, 2014
1 parent 613f539 commit b4769fb
Show file tree
Hide file tree
Showing 7 changed files with 571 additions and 85 deletions.
234 changes: 221 additions & 13 deletions src/main/java/com/sangupta/jerry/util/EqualUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,236 @@
public class EqualUtils {

/**
* Tests if two byte arrays are equal in content or not.
* Tests if two <code>byte</code> arrays are equal in content or not.
*
* @param bytes1 the first byte array
* @param array1
* the first array
*
* @param bytes2 the second byte array
* @param array2
* the second array
*
* @return <code>true</code> if arrays are equal in object or content,
* <code>false</code> otherwise
* <code>false</code> otherwise
*/
public static boolean equals(byte[] bytes1, byte[] bytes2) {
if(bytes1 == null || bytes2 == null) {
public static boolean equals(byte[] array1, byte[] array2) {
if(array1 == null || array2 == null) {
return false;
}

if(bytes1 == bytes2) {
if(array1 == array2) {
return true;
}

if(bytes1.length != bytes2.length) {
if(array1.length != array2.length) {
return false;
}

for(int index = 0; index < bytes1.length; index++) {
if(bytes1[index] != bytes2[index]) {
for(int index = 0; index < array1.length; index++) {
if(array1[index] != array2[index]) {
return false;
}
}

return true;
}

/**
* Tests if two <code>char</code> arrays are equal in content or not.
*
* @param array1
* the first array
*
* @param array2
* the second array
*
* @return <code>true</code> if arrays are equal in object or content,
* <code>false</code> otherwise
*/
public static boolean equals(char[] array1, char[] array2) {
if(array1 == null || array2 == null) {
return false;
}

if(array1 == array2) {
return true;
}

if(array1.length != array2.length) {
return false;
}

for(int index = 0; index < array1.length; index++) {
if(array1[index] != array2[index]) {
return false;
}
}

return true;
}

/**
* Tests if two <code>short</code> arrays are equal in content or not.
*
* @param array1
* the first array
*
* @param array2
* the second array
*
* @return <code>true</code> if arrays are equal in object or content,
* <code>false</code> otherwise
*/
public static boolean equals(short[] array1, short[] array2) {
if(array1 == null || array2 == null) {
return false;
}

if(array1 == array2) {
return true;
}

if(array1.length != array2.length) {
return false;
}

for(int index = 0; index < array1.length; index++) {
if(array1[index] != array2[index]) {
return false;
}
}

return true;
}

/**
* Tests if two <code>int</code> arrays are equal in content or not.
*
* @param array1
* the first array
*
* @param array2
* the second array
*
* @return <code>true</code> if arrays are equal in object or content,
* <code>false</code> otherwise
*/
public static boolean equals(int[] array1, int[] array2) {
if(array1 == null || array2 == null) {
return false;
}

if(array1 == array2) {
return true;
}

if(array1.length != array2.length) {
return false;
}

for(int index = 0; index < array1.length; index++) {
if(array1[index] != array2[index]) {
return false;
}
}

return true;
}

/**
* Tests if two <code>long</code> arrays are equal in content or not.
*
* @param array1
* the first array
*
* @param array2
* the second array
*
* @return <code>true</code> if arrays are equal in object or content,
* <code>false</code> otherwise
*/
public static boolean equals(long[] array1, long[] array2) {
if(array1 == null || array2 == null) {
return false;
}

if(array1 == array2) {
return true;
}

if(array1.length != array2.length) {
return false;
}

for(int index = 0; index < array1.length; index++) {
if(array1[index] != array2[index]) {
return false;
}
}

return true;
}

/**
* Tests if two <code>float</code> arrays are equal in content or not.
*
* @param array1
* the first array
*
* @param array2
* the second array
*
* @return <code>true</code> if arrays are equal in object or content,
* <code>false</code> otherwise
*/
public static boolean equals(float[] array1, float[] array2) {
if(array1 == null || array2 == null) {
return false;
}

if(array1 == array2) {
return true;
}

if(array1.length != array2.length) {
return false;
}

for(int index = 0; index < array1.length; index++) {
if(array1[index] != array2[index]) {
return false;
}
}

return true;
}

/**
* Tests if two <code>double</code> arrays are equal in content or not.
*
* @param array1
* the first array
*
* @param array2
* the second array
*
* @return <code>true</code> if arrays are equal in object or content,
* <code>false</code> otherwise
*/
public static boolean equals(double[] array1, double[] array2) {
if(array1 == null || array2 == null) {
return false;
}

if(array1 == array2) {
return true;
}

if(array1.length != array2.length) {
return false;
}

for(int index = 0; index < array1.length; index++) {
if(array1[index] != array2[index]) {
return false;
}
}
Expand All @@ -62,12 +268,14 @@ public static boolean equals(byte[] bytes1, byte[] bytes2) {
/**
* Test if two byte arrays are not equal in all respects.
*
* @param bytes1 the first byte array
* @param bytes1
* the first byte array
*
* @param bytes2 the second byte array
* @param bytes2
* the second byte array
*
* @return <code>true</code> if arrays are not equal, <code>false</code>
* otherwise
* otherwise
*/
public static boolean notEquals(byte[] bytes1, byte[] bytes2) {
return !equals(bytes1, bytes2);
Expand Down
67 changes: 13 additions & 54 deletions src/main/java/com/sangupta/jerry/util/UriUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
Expand Down Expand Up @@ -192,7 +190,7 @@ public static String decodeURIComponent(String encodedURI) {
*
* @return extracted filename from the URL
*/
public static String extractName(String url) {
public static String extractFileName(String url) {
int index1 = url.indexOf('?');
int index2 = url.indexOf('#');

Expand All @@ -219,41 +217,6 @@ public static String extractName(String url) {
return url;
}

/**
* Extract the filename from the URL.
*
* @param url
* the url from which the filename needs to be extracted
*
* @return the extracted filename
*
* @throws NullPointerException
* if the URL presented is <code>null</code>
*/
public static String extractFileName(String url) {
int index = url.lastIndexOf('/');

if(index == -1) {
return null;
}

url = url.substring(index + 1);

// query param
int end = url.indexOf('?');
if(end != -1) {
url = url.substring(0, end);
}

// anchor name
end = url.indexOf('#');
if(end != -1) {
url = url.substring(0, end);
}

return url;
}

/**
* Extract the extension from the given URL.
*
Expand Down Expand Up @@ -599,20 +562,12 @@ public static String extractProtocol(String url) {
return null;
}

try {
URI uri = new URI(url);
return uri.getScheme();
} catch (URISyntaxException e) {
// eat up
}

// we will try and fall back to approach of sub-strings
int index = url.indexOf("://");
if(index == -1) {
return null;
}

return url.substring(0, index);
return url.substring(0, index).toLowerCase();
}

/**
Expand Down Expand Up @@ -748,15 +703,19 @@ public static String addWebPaths(String... components) {
* <code>false</code> otherwise
*/
public static boolean appearsValidUrl(String url) {
if(AssertUtils.isEmpty(url)) {
return false;
}

if(!url.contains(".")) {
try {
UrlManipulator manipulator = new UrlManipulator(url, true);
String host = manipulator.getHost();
if(host != null) {
if(host.indexOf('.') == -1) {
return false;
}
}

return true;
} catch(IllegalArgumentException e) {
return false;
}

return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void testIntegerCounter() {
// test removal
Assert.assertEquals(0, counter.remove("test"));
Assert.assertEquals(0, counter.get("test"));
Assert.assertEquals(0, counter.remove("test"));

// test for new counters with one of the methods
Assert.assertEquals(1, counter.increment("test1"));
Expand Down
Loading

0 comments on commit b4769fb

Please sign in to comment.