Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#651 - ENH: Add type support for java.nio.file.Path ... such that it …
…stores the underlying URI as VARCHAR/string
- Loading branch information
Showing
7 changed files
with
135 additions
and
11 deletions.
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypePath.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,40 @@ | ||
package com.avaje.ebeaninternal.server.type; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* ScalarType for java.nio.file.Path which converts to and from a VARCHAR database column. | ||
*/ | ||
public class ScalarTypePath extends ScalarTypeBaseVarchar<Path> { | ||
|
||
public ScalarTypePath() { | ||
super(Path.class); | ||
} | ||
|
||
@Override | ||
public Path convertFromDbString(String dbValue) { | ||
try { | ||
return Paths.get(new URI(dbValue)); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException("Error with Path URI [" + dbValue + "] " + e); | ||
} | ||
} | ||
|
||
@Override | ||
public String convertToDbString(Path beanValue) { | ||
return beanValue.toUri().toString(); | ||
} | ||
|
||
@Override | ||
public String formatValue(Path path) { | ||
return convertToDbString(path); | ||
} | ||
|
||
@Override | ||
public Path parse(String value) { | ||
return convertFromDbString(value); | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/test/java/com/avaje/ebeaninternal/server/type/ScalarTypePathTest.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,36 @@ | ||
package com.avaje.ebeaninternal.server.type; | ||
|
||
import org.junit.Test; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ScalarTypePathTest { | ||
|
||
private ScalarTypePath type = new ScalarTypePath(); | ||
|
||
@Test | ||
public void convertFromDbString() throws Exception { | ||
|
||
Path path = Paths.get("."); | ||
|
||
String asString = type.convertToDbString(path); | ||
Path converted = type.convertFromDbString(asString); | ||
|
||
assertEquals(path, converted); | ||
} | ||
|
||
@Test | ||
public void formatAndParse() throws Exception { | ||
|
||
Path path = Paths.get("."); | ||
|
||
String asString = type.formatValue(path); | ||
Path converted = type.parse(asString); | ||
|
||
assertEquals(path, converted); | ||
} | ||
|
||
} |
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