Skip to content

Commit b69969a

Browse files
committed
build: unit test for output and exception packages
1 parent 58dc252 commit b69969a

22 files changed

Lines changed: 2163 additions & 32 deletions

jooby/src/main/java/io/jooby/exception/MissingValueException.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ public String getName() {
3636
return name;
3737
}
3838

39-
/**
40-
* Check if the given value is null and throw a {@link MissingValueException} exception.
41-
*
42-
* @param name Attribute's name.
43-
* @param value Value to check.
44-
* @param <T> Value type.
45-
* @return Input value
46-
*/
4739
public static <T> T requireNonNull(String name, @Nullable T value) {
4840
if (value == null) {
4941
throw new MissingValueException(name);

jooby/src/main/java/io/jooby/internal/output/CompositeOutput.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public BufferedOutput write(byte[] source, int offset, int length) {
5050
public BufferedOutput clear() {
5151
chunks.forEach(ByteBuffer::clear);
5252
chunks.clear();
53+
size = 0;
5354
return this;
5455
}
5556

jooby/src/main/java/io/jooby/output/OutputOptions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public class OutputOptions {
1818

1919
/** Creates a default options. */
2020
public OutputOptions() {
21-
long maxMemory = Runtime.getRuntime().maxMemory();
21+
this(Runtime.getRuntime().maxMemory());
22+
}
23+
24+
protected OutputOptions(long maxMemory) {
2225
// smaller than 64mb of ram we use 512b buffers
2326
if (maxMemory < 64 * 1024 * 1024) {
2427
// use 512b buffers

jooby/src/test/java/io/jooby/SessionStoreTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
*/
66
package io.jooby;
77

8-
import static org.junit.jupiter.api.Assertions.assertEquals;
9-
import static org.junit.jupiter.api.Assertions.assertFalse;
10-
import static org.junit.jupiter.api.Assertions.assertNotNull;
11-
import static org.junit.jupiter.api.Assertions.assertNull;
12-
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
import static org.junit.jupiter.api.Assertions.*;
139
import static org.mockito.Mockito.mock;
1410
import static org.mockito.Mockito.verify;
1511
import static org.mockito.Mockito.when;
@@ -241,4 +237,18 @@ void testSignedFactories() {
241237
// the lambdas instantiated by the `SessionStore.signed()` factory.
242238
}
243239
}
240+
241+
@Test
242+
@DisplayName("Verify unsupported session store")
243+
void testUnsupportedStore() {
244+
assertThrows(Usage.class, () -> SessionStore.UNSUPPORTED.newSession(ctx));
245+
assertThrows(Usage.class, () -> SessionStore.UNSUPPORTED.findSession(ctx));
246+
assertThrows(
247+
Usage.class, () -> SessionStore.UNSUPPORTED.deleteSession(ctx, mock(Session.class)));
248+
assertThrows(
249+
Usage.class, () -> SessionStore.UNSUPPORTED.touchSession(ctx, mock(Session.class)));
250+
assertThrows(Usage.class, () -> SessionStore.UNSUPPORTED.saveSession(ctx, mock(Session.class)));
251+
assertThrows(
252+
Usage.class, () -> SessionStore.UNSUPPORTED.renewSessionId(ctx, mock(Session.class)));
253+
}
244254
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.exception;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Test;
12+
13+
class MissingValueExceptionTest {
14+
15+
@Test
16+
@DisplayName("Verify constructor and getName accurately store and return the parameter name")
17+
void testConstructorAndGetter() {
18+
String paramName = "userId";
19+
MissingValueException exception = new MissingValueException(paramName);
20+
21+
assertEquals(paramName, exception.getName());
22+
assertEquals("Missing value: 'userId'", exception.getMessage());
23+
}
24+
25+
@Test
26+
@DisplayName("requireNonNull should return the value if it is not null")
27+
void testRequireNonNullSuccess() {
28+
String name = "username";
29+
String value = "edgar";
30+
31+
String result = MissingValueException.requireNonNull(name, value);
32+
33+
assertEquals(value, result);
34+
}
35+
36+
@Test
37+
@DisplayName("requireNonNull should throw MissingValueException if the value is null")
38+
void testRequireNonNullFailure() {
39+
String name = "apiKey";
40+
41+
MissingValueException ex =
42+
assertThrows(
43+
MissingValueException.class, () -> MissingValueException.requireNonNull(name, null));
44+
45+
assertEquals(name, ex.getName());
46+
assertEquals("Missing value: 'apiKey'", ex.getMessage());
47+
}
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.exception;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNull;
10+
11+
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Test;
13+
14+
import io.jooby.StatusCode;
15+
16+
class RegistryExceptionTest {
17+
18+
@Test
19+
@DisplayName("Verify constructor with message sets status to 500 and stores message")
20+
void testConstructorWithMessage() {
21+
String message = "Service not found: MyService";
22+
RegistryException exception = new RegistryException(message);
23+
24+
assertEquals(StatusCode.SERVER_ERROR, exception.getStatusCode());
25+
assertEquals(message, exception.getMessage());
26+
assertNull(exception.getCause());
27+
}
28+
29+
@Test
30+
@DisplayName("Verify constructor with message and cause sets status to 500 and stores both")
31+
void testConstructorWithMessageAndCause() {
32+
String message = "Dependency injection failed";
33+
Throwable cause = new RuntimeException("Root cause error");
34+
RegistryException exception = new RegistryException(message, cause);
35+
36+
assertEquals(StatusCode.SERVER_ERROR, exception.getStatusCode());
37+
assertEquals(message, exception.getMessage());
38+
assertEquals(cause, exception.getCause());
39+
}
40+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.exception;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNull;
10+
11+
import java.lang.reflect.Type;
12+
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Test;
15+
16+
import io.jooby.StatusCode;
17+
import io.jooby.problem.HttpProblem;
18+
19+
class TypeMismatchExceptionTest {
20+
21+
@Test
22+
@DisplayName("Verify constructor with cause stores name, message, and cause correctly")
23+
void testConstructorWithCause() {
24+
String name = "age";
25+
Type type = int.class;
26+
Throwable cause = new NumberFormatException("For input string: \"abc\"");
27+
28+
TypeMismatchException exception = new TypeMismatchException(name, type, cause);
29+
30+
assertEquals(name, exception.getName());
31+
assertEquals("Cannot convert value: 'age', to: 'int'", exception.getMessage());
32+
assertEquals(cause, exception.getCause());
33+
}
34+
35+
@Test
36+
@DisplayName("Verify constructor without cause sets cause to null")
37+
void testConstructorWithoutCause() {
38+
String name = "active";
39+
Type type = boolean.class;
40+
41+
TypeMismatchException exception = new TypeMismatchException(name, type);
42+
43+
assertEquals(name, exception.getName());
44+
assertEquals("Cannot convert value: 'active', to: 'boolean'", exception.getMessage());
45+
assertNull(exception.getCause());
46+
}
47+
48+
@Test
49+
@DisplayName("Verify toHttpProblem returns a correctly populated HttpProblem instance")
50+
void testToHttpProblem() {
51+
String name = "price";
52+
Type type = double.class;
53+
TypeMismatchException exception = new TypeMismatchException(name, type);
54+
55+
HttpProblem problem = exception.toHttpProblem();
56+
57+
// BadRequestException usually defaults to 400
58+
assertEquals(StatusCode.BAD_REQUEST.value(), problem.getStatus());
59+
assertEquals("Type Mismatch", problem.getTitle());
60+
assertEquals("Cannot convert value: 'price', to: 'double'", problem.getDetail());
61+
}
62+
}

0 commit comments

Comments
 (0)