Skip to content

Commit

Permalink
Сделано модульное тестирование
Browse files Browse the repository at this point in the history
  • Loading branch information
kalenchukov committed Mar 27, 2022
1 parent 7a40f8a commit 39b59a9
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 74 deletions.
37 changes: 19 additions & 18 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@
# Использование
### Сборщик параметров
```java
Map<String, List<String>> uriQueryMap = new HashMap<>(
Map.ofEntries(
Map.entry("param1", List.of("value1.1")),
Map.entry("param2", List.of("значение2.1")),
Map.entry("param3", List.of("value3.1", "value3.2")),
Map.entry("param4", List.of("value4.1"))
)
);
Map<String, List<String>> queryMap = new LinkedHashMap<>();
queryMap.put("param1", List.of("value1.1"));
queryMap.put("param2", List.of("значение2.1"));
queryMap.put("param3", List.of("value3.1", "value3.2"));
queryMap.put("param4", List.of("value4.1"));

UriQuery.compose(uriQueryMap);
UriQuery.compose(queryMap);
/*
Результат выполнения:
param4=value4.1&param3[]=value3.1&param3[]=value3.2&param2=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B52.1&param1=value1.1
param1=value1.1&param2=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B52.1&param3[]=value3.1&param3[]=value3.2&param4=value4.1
*/
```

### Разборщик параметров
```java
String uriQueryLine =
"param1=value1.1" +
"&param2=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B52.1" +
"&param3[]=value3.1" +
"&param3[]=value3.2" +
"&param4=value4.1";
StringBuilder queryString = new StringBuilder();
queryString.append("param1=value1.1");
queryString.append("&");
queryString.append("param2=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B52.1");
queryString.append("&");
queryString.append("param3[]=value3.1");
queryString.append("&");
queryString.append("param3[]=value3.2");
queryString.append("&");
queryString.append("param4=value4.1");

UriQuery.parse(uriQueryLine);
UriQuery.parse(queryString);
/*
Результат выполнения:
{
Expand All @@ -39,4 +40,4 @@ UriQuery.parse(uriQueryLine);
param4=[value4.1]
}
*/
```
```
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

<groupId>dev.kalenchukov</groupId>
<artifactId>uri-query</artifactId>
<version>1.3.0</version>

<name>URIQuery</name>
<description>Сборщик / разборщик параметров URI</description>
<version>1.2.0</version>
<url>https://github.com/kalenchukov/URIQuery</url>

<licenses>
Expand Down Expand Up @@ -46,5 +46,11 @@
<artifactId>annotations</artifactId>
<version>23.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>

</project>
2 changes: 1 addition & 1 deletion src/main/java/dev/kalenchukov/uriquery/UriQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private UriQuery() {}
@NotNull
public static Map<@NotNull String, @NotNull List<@NotNull String>> parse(@Nullable final String uriQueryEncode)
{
Map<String, List<String>> queryParams = new HashMap<>();
Map<String, List<String>> queryParams = new LinkedHashMap<>();

if (uriQueryEncode == null || uriQueryEncode.length() < 3) {
return queryParams;
Expand Down
54 changes: 0 additions & 54 deletions src/main/java/dev/kalenchukov/uriquery/tests/Test.java

This file was deleted.

81 changes: 81 additions & 0 deletions src/test/java/dev/kalenchukov/uriquery/UriQueryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright © 2022 Алексей Каленчуков
* GitHub: https://github.com/kalenchukov
* E-mail: mailto:[email protected]
*/

package dev.kalenchukov.uriquery;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

public class UriQueryTest
{
private static final StringBuilder QUERY_STRING = new StringBuilder();

private static final Map<String, List<String>> QUERY_MAP = new LinkedHashMap<>();

@Before
public void setUp()
{
QUERY_STRING.append("param1=value1.1");
QUERY_STRING.append("&");
QUERY_STRING.append("param2=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B52.1");
QUERY_STRING.append("&");
QUERY_STRING.append("param3[]=value3.1");
QUERY_STRING.append("&");
QUERY_STRING.append("param3[]=value3.2");
QUERY_STRING.append("&");
QUERY_STRING.append("param4=value4.1");

QUERY_MAP.put("param1", List.of("value1.1"));
QUERY_MAP.put("param2", List.of("значение2.1"));
QUERY_MAP.put("param3", List.of("value3.1", "value3.2"));
QUERY_MAP.put("param4", List.of("value4.1"));
}

@After
public void tearDown()
{
QUERY_STRING.setLength(0);
QUERY_MAP.clear();
}

@Test
public void testParse()
{
Map<String, List<String>> queryMap = UriQuery.parse(QUERY_STRING.toString());

assertEquals(
QUERY_MAP.size(),
queryMap.size()
);

for (Map.Entry<String, List<String>> entryQueryMap : queryMap.entrySet())
{
assertTrue(QUERY_MAP.containsKey(entryQueryMap.getKey()));

assertEquals(
QUERY_MAP.get(entryQueryMap.getKey()).size(),
queryMap.get(entryQueryMap.getKey()).size()
);

assertTrue(QUERY_MAP.get(entryQueryMap.getKey()).containsAll(queryMap.get(entryQueryMap.getKey())));
}
}

@Test
public void testCompose()
{
String queryString = UriQuery.compose(QUERY_MAP);

assertEquals(QUERY_STRING.toString(), queryString);
}
}

0 comments on commit 39b59a9

Please sign in to comment.