Skip to content

Commit

Permalink
Add support for Charset in Quarkus Config system
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed May 25, 2020
1 parent b6f3625 commit d75cf1e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.runtime.configuration;

import static io.quarkus.runtime.configuration.ConverterSupport.DEFAULT_QUARKUS_CONVERTER_PRIORITY;

import java.io.Serializable;
import java.nio.charset.Charset;

import javax.annotation.Priority;

import org.eclipse.microprofile.config.spi.Converter;

/**
* A converter which converts a Charset string into an instance of {@link java.nio.charset.Charset}.
*/
@Priority(DEFAULT_QUARKUS_CONVERTER_PRIORITY)
public class CharsetConverter implements Converter<Charset>, Serializable {

private static final long serialVersionUID = 2320905063828247874L;

@Override
public Charset convert(String value) {
try {
return Charset.forName(value);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
io.quarkus.runtime.configuration.InetSocketAddressConverter
io.quarkus.runtime.configuration.CharsetConverter
io.quarkus.runtime.configuration.CidrAddressConverter
io.quarkus.runtime.configuration.InetAddressConverter
io.quarkus.runtime.configuration.RegexConverter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkus.runtime.configuration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CharsetConverterTestCase {

private CharsetConverter charsetConverter;

@BeforeEach
public void setup() {
charsetConverter = new CharsetConverter();
}

@Test
public void testUTF8Uppercase() {
assertEquals(charsetConverter.convert("UTF-8"), StandardCharsets.UTF_8);
}

@Test
public void testUTF8Lowercase() {
assertEquals(charsetConverter.convert("utf-8"), StandardCharsets.UTF_8);
}

@Test
public void testOther() {
assertEquals(charsetConverter.convert("US-ASCII"), StandardCharsets.US_ASCII);
}

@Test
public void testInvalidCharset() {
assertThrows(IllegalArgumentException.class, () -> charsetConverter.convert("whatever"));
}
}

0 comments on commit d75cf1e

Please sign in to comment.