2323
2424import org .junit .Test ;
2525
26+ import com .baidu .hugegraph .backend .BackendException ;
2627import com .baidu .hugegraph .backend .page .PageState ;
2728import com .baidu .hugegraph .testutil .Assert ;
2829
2930public class PageStateTest {
3031
32+ private String pageWith3Base64Chars = "AAAAADsyABwAEAqI546LS6WW57unBgA" +
33+ "EAAAAAPB////+8H////4alhxAZS8va6" +
34+ "opcAKpklipAAQAAAAAAAAAAQ==" ;
35+
36+ private String pageWithSpace = "AAAAADsyABwAEAqI546LS6WW57unBgAEAAAAAP" +
37+ "B//// 8H////4alhxAZS8va6opcAKpklipAAQA" +
38+ "AAAAAAAAAQ==" ;
39+
40+ @ Test
41+ public void testOriginalStringPageToBytes () {
42+ byte [] validPage = PageState .toBytes (pageWith3Base64Chars );
43+ Assert .assertNotNull (validPage );
44+
45+ Assert .assertThrows (BackendException .class , () -> {
46+ PageState .toBytes (pageWithSpace );
47+ }, e -> {
48+ Assert .assertContains ("Invalid page:" , e .toString ());
49+ });
50+ }
51+
3152 @ Test
3253 public void testDecodePageWithSpecialBase64Chars () {
3354 // Assert decode '+' and '/' and '=' and space successfully
34- final String pageWith3Base64Chars = "AAAAADsyABwAEAqI546LS6WW57unBgA" +
35- "EAAAAAPB////+8H////4alhxAZS8va6" +
36- "opcAKpklipAAQAAAAAAAAAAQ==" ;
37-
38- final String pageWithSpace = "AAAAADsyABwAEAqI546LS6WW57unBgAEAAAAAP" +
39- "B//// 8H////4alhxAZS8va6opcAKpklipAAQA" +
40- "AAAAAAAAAQ==" ;
4155 Assert .assertNotNull (PageState .fromString (pageWith3Base64Chars ));
4256
4357 byte [] decodePlus = PageState .fromString (pageWith3Base64Chars )
@@ -46,4 +60,31 @@ public void testDecodePageWithSpecialBase64Chars() {
4660
4761 Assert .assertTrue (Arrays .equals (decodePlus , decodeSpace ));
4862 }
63+
64+ @ Test
65+ public void testDecodePageWithInvalidStringPage () {
66+ final String invalidPageWithBase64Chars = "dGVzdCBiYXNlNjQ=" ;
67+
68+ Assert .assertThrows (BackendException .class , () -> {
69+ PageState .fromString (invalidPageWithBase64Chars );
70+ }, e -> {
71+ Assert .assertContains ("Invalid page: '0x" , e .toString ());
72+ });
73+
74+ final String invalidBase64Chars = "!abc~" ;
75+ Assert .assertThrows (BackendException .class , () -> {
76+ PageState .fromString (invalidBase64Chars );
77+ }, e -> {
78+ Assert .assertContains ("Invalid page:" , e .toString ());
79+ });
80+ }
81+
82+ @ Test
83+ public void testEmptyPageState () {
84+ Assert .assertEquals (0 , PageState .EMPTY .offset ());
85+ Assert .assertNull (PageState .EMPTY .toString ());
86+
87+ Assert .assertEquals (PageState .EMPTY ,
88+ PageState .fromBytes (PageState .EMPTY_BYTES ));
89+ }
4990}
0 commit comments