-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add type handlers for SimpleUri and BlockUri (#5061)
- Loading branch information
1 parent
f3f9d92
commit 03023ad
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...rg/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandlerTest.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,50 @@ | ||
// Copyright 2022 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.engine.persistence.typeHandling.extensionTypes; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.terasology.engine.world.block.BlockUri; | ||
import org.terasology.engine.world.block.BlockUriParseException; | ||
import org.terasology.gestalt.assets.ResourceUrn; | ||
import org.terasology.gestalt.naming.Name; | ||
import org.terasology.persistence.typeHandling.PersistedData; | ||
import org.terasology.persistence.typeHandling.inMemory.PersistedString; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class BlockUriTypeHandlerTest { | ||
|
||
private final BlockUriTypeHandler handler = new BlockUriTypeHandler(); | ||
|
||
@Test | ||
public void testDeserializeValidShortUri() { | ||
PersistedData data = new PersistedString("foo:bar"); | ||
Optional<BlockUri> uri = handler.deserialize(data); | ||
assertTrue(uri.isPresent()); | ||
assertEquals(new BlockUri(new ResourceUrn("foo", "bar")), uri.get()); | ||
} | ||
|
||
@Test | ||
public void testDeserializeValidFullUri() { | ||
PersistedData data = new PersistedString("package:family:shapePackage:shapeName.identifier"); | ||
Optional<BlockUri> uri = handler.deserialize(data); | ||
assertTrue(uri.isPresent()); | ||
assertEquals(new BlockUri( | ||
new ResourceUrn("package", "family"), new ResourceUrn("shapePackage", "shapeName"), new Name("identifier")), uri.get()); | ||
} | ||
|
||
@Test | ||
public void testDeserializeInvalidUri() { | ||
PersistedData data = new PersistedString("baz"); | ||
BlockUriParseException thrown = Assertions.assertThrows(BlockUriParseException.class, () -> { | ||
handler.deserialize(data); | ||
}); | ||
|
||
Assertions.assertEquals("Could not parse block uri: 'baz'", thrown.getMessage()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...g/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandlerTest.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,34 @@ | ||
// Copyright 2022 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.engine.persistence.typeHandling.extensionTypes; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.terasology.engine.core.SimpleUri; | ||
import org.terasology.persistence.typeHandling.PersistedData; | ||
import org.terasology.persistence.typeHandling.inMemory.PersistedString; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class SimpleUriTypeHandlerTest { | ||
|
||
private final SimpleUriTypeHandler handler = new SimpleUriTypeHandler(); | ||
|
||
@Test | ||
public void testDeserializeValidUri() { | ||
PersistedData data = new PersistedString("foo:bar"); | ||
Optional<SimpleUri> uri = handler.deserialize(data); | ||
assertTrue(uri.isPresent()); | ||
assertEquals(new SimpleUri("foo", "bar"), uri.get()); | ||
} | ||
|
||
@Test | ||
public void testDeserializeInvalidUri() { | ||
PersistedData data = new PersistedString("baz"); | ||
Optional<SimpleUri> uri = handler.deserialize(data); | ||
assertTrue(uri.isEmpty()); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...va/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandler.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,34 @@ | ||
// Copyright 2022 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.engine.persistence.typeHandling.extensionTypes; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.terasology.engine.world.block.BlockUri; | ||
import org.terasology.persistence.typeHandling.StringRepresentationTypeHandler; | ||
|
||
public class BlockUriTypeHandler extends StringRepresentationTypeHandler<BlockUri> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(BlockUriTypeHandler.class); | ||
|
||
@Override | ||
public String getAsString(BlockUri uri) { | ||
if (uri == null) { | ||
return ""; | ||
} | ||
return uri.toString(); | ||
} | ||
|
||
@Override | ||
public BlockUri getFromString(String representation) { | ||
BlockUri uri = new BlockUri(representation); | ||
if (!uri.isValid()) { | ||
logger.error("Failed to create valid BlockUri from string '{}'", representation); | ||
// StringRepresentationTypeHandler will turn this 'null' value into an empty Optional | ||
return null; | ||
} | ||
|
||
return uri; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...a/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandler.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,34 @@ | ||
// Copyright 2022 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.engine.persistence.typeHandling.extensionTypes; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.terasology.engine.core.SimpleUri; | ||
import org.terasology.persistence.typeHandling.StringRepresentationTypeHandler; | ||
|
||
public class SimpleUriTypeHandler extends StringRepresentationTypeHandler<SimpleUri> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(SimpleUriTypeHandler.class); | ||
|
||
@Override | ||
public String getAsString(SimpleUri uri) { | ||
if (uri == null) { | ||
return ""; | ||
} | ||
return uri.toString(); | ||
} | ||
|
||
@Override | ||
public SimpleUri getFromString(String representation) { | ||
SimpleUri uri = new SimpleUri(representation); | ||
if (!uri.isValid()) { | ||
logger.error("Failed to create valid SimpleURI from string '{}'", representation); | ||
// StringRepresentationTypeHandler will turn this 'null' value into an empty Optional | ||
return null; | ||
} | ||
|
||
return uri; | ||
} | ||
} |