removed MapUtils
This commit is contained in:
1 parent
ce531cdc38
commit
b380b3af3d
6 files changed
+46
-131
No files matched your search
@@ -35,8 +35,7 @@ public class BaseStringExtractorImpl implements BaseStringExtractor
|
|||||||
params.addAll(request.getQueryStringParams());
|
params.addAll(request.getQueryStringParams());
|
||||||
params.addAll(request.getBodyParams());
|
params.addAll(request.getBodyParams());
|
||||||
params.addAll(new ParameterList(request.getOauthParameters()));
|
params.addAll(new ParameterList(request.getOauthParameters()));
|
||||||
params.sort();
|
return params.sort().asOauthBaseString();
|
||||||
return params.asOauthBaseString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkPreconditions(OAuthRequest request)
|
private void checkPreconditions(OAuthRequest request)
|
||||||
|
|||||||
@@ -26,7 +26,12 @@ public class ParameterList
|
|||||||
{
|
{
|
||||||
params = new ArrayList<Parameter>();
|
params = new ArrayList<Parameter>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ParameterList(List<Parameter> params)
|
||||||
|
{
|
||||||
|
this.params = new ArrayList<Parameter>(params);
|
||||||
|
}
|
||||||
|
|
||||||
public ParameterList(Map<String, String> map)
|
public ParameterList(Map<String, String> map)
|
||||||
{
|
{
|
||||||
this();
|
this();
|
||||||
@@ -110,8 +115,10 @@ public class ParameterList
|
|||||||
return params.size();
|
return params.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sort()
|
public ParameterList sort()
|
||||||
{
|
{
|
||||||
Collections.sort(params);
|
ParameterList sorted = new ParameterList(params);
|
||||||
|
Collections.sort(sorted.params);
|
||||||
|
return sorted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
package org.scribe.utils;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utils for {@link Map} manipulation
|
|
||||||
*
|
|
||||||
* @author Pablo Fernandez
|
|
||||||
*/
|
|
||||||
public class MapUtils
|
|
||||||
{
|
|
||||||
private static final String EMPTY_STRING = "";
|
|
||||||
private static final String PAIR_SEPARATOR = "=";
|
|
||||||
private static final String PARAM_SEPARATOR = "&";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sorts a Map
|
|
||||||
*
|
|
||||||
* @param map unsorted map
|
|
||||||
* @return sorted map
|
|
||||||
*/
|
|
||||||
public static final Map<String, String> sort(Map<String, String> map)
|
|
||||||
{
|
|
||||||
Preconditions.checkNotNull(map, "Cannot sort a null object.");
|
|
||||||
|
|
||||||
Map<String, String> sorted = new LinkedHashMap<String, String>();
|
|
||||||
for (String key : getSortedKeys(map))
|
|
||||||
{
|
|
||||||
sorted.put(key, map.get(key));
|
|
||||||
}
|
|
||||||
return sorted;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<String> getSortedKeys(Map<String, String> map)
|
|
||||||
{
|
|
||||||
List<String> keys = new ArrayList<String>(map.keySet());
|
|
||||||
Collections.sort(keys);
|
|
||||||
return keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Form-urlDecodes and appends all keys from the source {@link Map} to the
|
|
||||||
* target {@link Map}
|
|
||||||
*
|
|
||||||
* @param source Map from where the keys get copied and decoded
|
|
||||||
* @param target Map where the decoded keys are copied to
|
|
||||||
*/
|
|
||||||
public static void decodeAndAppendEntries(Map<String, String> source, Map<String, String> target)
|
|
||||||
{
|
|
||||||
for (String key: source.keySet())
|
|
||||||
{
|
|
||||||
target.put(URLUtils.percentEncode(key), URLUtils.percentEncode(source.get(key)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Concats a key-value map into a querystring-like String
|
|
||||||
*
|
|
||||||
* @param params key-value map
|
|
||||||
* @return querystring-like String
|
|
||||||
*/
|
|
||||||
public static String concatSortedPercentEncodedParams(Map<String, String> params)
|
|
||||||
{
|
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
for (String key : params.keySet())
|
|
||||||
{
|
|
||||||
result.append(key).append(PAIR_SEPARATOR);
|
|
||||||
result.append(params.get(key)).append(PARAM_SEPARATOR);
|
|
||||||
}
|
|
||||||
return result.toString().substring(0, result.length() - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -41,7 +41,7 @@ public class BaseStringExtractorTest
|
|||||||
OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
|
OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
|
||||||
extractor.extract(request);
|
extractor.extract(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldProperlyEncodeSpaces()
|
public void shouldProperlyEncodeSpaces()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
package org.scribe.model;
|
package org.scribe.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.scribe.utils.URLUtils;
|
import org.scribe.utils.URLUtils;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotSame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author: Pablo Fernandez
|
* @author: Pablo Fernandez
|
||||||
*/
|
*/
|
||||||
@@ -59,4 +64,33 @@ public class ParameterListTest
|
|||||||
url = params.appendTo(url);
|
url = params.appendTo(url);
|
||||||
Assert.assertEquals(url, expectedUrl);
|
Assert.assertEquals(url, expectedUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldProperlySortParameters()
|
||||||
|
{
|
||||||
|
params.add("param1", "v1");
|
||||||
|
params.add("param6", "v2");
|
||||||
|
params.add("a_param", "v3");
|
||||||
|
params.add("param2", "v4");
|
||||||
|
Assert.assertEquals("a_param=v3¶m1=v1¶m2=v4¶m6=v2", params.sort().asFormUrlEncodedString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldProperlySortParametersWithTheSameName()
|
||||||
|
{
|
||||||
|
params.add("param1", "v1");
|
||||||
|
params.add("param6", "v2");
|
||||||
|
params.add("a_param", "v3");
|
||||||
|
params.add("param2", "v4");
|
||||||
|
Assert.assertEquals("a_param=v3¶m1=v1¶m2=v4¶m6=v2", params.sort().asFormUrlEncodedString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotModifyTheOriginalParameterList()
|
||||||
|
{
|
||||||
|
params.add("param1", "v1");
|
||||||
|
params.add("param6", "v2");
|
||||||
|
|
||||||
|
assertNotSame(params, params.sort());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package org.scribe.utils;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.junit.*;
|
|
||||||
|
|
||||||
public class MapUtilsTest
|
|
||||||
{
|
|
||||||
|
|
||||||
private Map<String, String> unsorted;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setup()
|
|
||||||
{
|
|
||||||
unsorted = new LinkedHashMap<String, String>();
|
|
||||||
unsorted.put("d", "fourth");
|
|
||||||
unsorted.put("a", "first");
|
|
||||||
unsorted.put("c", "third");
|
|
||||||
unsorted.put("b", "second");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldSortMap()
|
|
||||||
{
|
|
||||||
Map<String, String> sorted = MapUtils.sort(unsorted);
|
|
||||||
List<String> values = new ArrayList<String>(sorted.values());
|
|
||||||
assertEquals("first", values.get(0));
|
|
||||||
assertEquals("second", values.get(1));
|
|
||||||
assertEquals("third", values.get(2));
|
|
||||||
assertEquals("fourth", values.get(3));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldNotModifyTheOriginalMap()
|
|
||||||
{
|
|
||||||
Map<String, String> sorted = MapUtils.sort(unsorted);
|
|
||||||
assertNotSame(sorted, unsorted);
|
|
||||||
|
|
||||||
List<String> values = new ArrayList<String>(unsorted.values());
|
|
||||||
assertEquals("fourth", values.get(0));
|
|
||||||
assertEquals("first", values.get(1));
|
|
||||||
assertEquals("third", values.get(2));
|
|
||||||
assertEquals("second", values.get(3));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
|
||||||
public void shouldThrowExceptionForNullMap()
|
|
||||||
{
|
|
||||||
MapUtils.sort(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user