-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Support Charset in Quarkus Config - Use it in Hibernate
- Loading branch information
Showing
5 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
core/runtime/src/main/java/io/quarkus/runtime/configuration/CharsetConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("Unable to create Charset from: '" + value + "'", e); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...untime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
core/runtime/src/test/java/io/quarkus/runtime/configuration/CharsetConverterTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters