|
| 1 | +packagecom.github.scribejava.core.base64; |
| 2 | + |
| 3 | +importjava.io.UnsupportedEncodingException; |
| 4 | +importorg.junit.Before; |
| 5 | +importorg.junit.Test; |
| 6 | +importstaticorg.junit.Assert.assertTrue; |
| 7 | +importstaticorg.junit.Assert.assertEquals; |
| 8 | +importstaticorg.junit.Assert.assertArrayEquals; |
| 9 | + |
| 10 | +publicclassBase64Test { |
| 11 | + |
| 12 | +privateBase64java8Base64; |
| 13 | +privatebyte[]helloWorldBytes; |
| 14 | +privatebyte[]helloWorldTwoLinesBytes; |
| 15 | +privatebyte[]helloWorldTwoLinesAndNewLineBytes; |
| 16 | +privatebyte[]helloWorldDifferentCharsBytes; |
| 17 | + |
| 18 | +@Before |
| 19 | +publicvoidsetUp()throwsUnsupportedEncodingException { |
| 20 | +helloWorldBytes ="Hello World".getBytes("UTF-8"); |
| 21 | +helloWorldTwoLinesBytes ="Hello World\r\nNew Line2".getBytes("UTF-8"); |
| 22 | +helloWorldTwoLinesAndNewLineBytes ="Hello World\r\nSecond Line\r\n".getBytes("UTF-8"); |
| 23 | +helloWorldDifferentCharsBytes = ("`1234567890-=~!@#$%^&*()_+ёЁ\"№;:?qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP" |
| 24 | + +"{}|ASDFGHJKL:ZXCVBNM<>?йфяцычувскамепинртгоьшлбщдюзж.хэъ\\ЙФЯЦЫЧУВСКАМЕПИНРТГОЬШЛБЩДЮЗЖ,ХЭЪ/\r\t\f\'" |
| 25 | + +"\b\n").getBytes("UTF-8"); |
| 26 | +java8Base64 =newJava8Base64(); |
| 27 | + } |
| 28 | + |
| 29 | +@Test |
| 30 | +publicvoidallImplementationsAreAvailable() { |
| 31 | +assertTrue(Java8Base64.isAvailable()); |
| 32 | + } |
| 33 | + |
| 34 | +@Test |
| 35 | +publicvoidtestEncode() { |
| 36 | +finalStringhelloWorldEncoded ="SGVsbG8gV29ybGQ="; |
| 37 | +finalStringhelloWorldTwoLinesEncoded ="SGVsbG8gV29ybGQNCk5ldyBMaW5lMg=="; |
| 38 | +finalStringhelloWorldTwoLinesAndNewLineEncoded ="SGVsbG8gV29ybGQNClNlY29uZCBMaW5lDQo="; |
| 39 | +finalStringhelloWorldDifferentCharsEncoded ="YDEyMzQ1Njc4OTAtPX4hQCMkJV4mKigpXyvRkdCBIuKEljs6P3F3ZXJ0eXVpb3B" |
| 40 | + +"bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw+P9C50YTRj9GG0YvRh9GD0LLRgdC" |
| 41 | + +"60LDQvNC10L/QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J/" |
| 42 | + +"QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg=="; |
| 43 | + |
| 44 | +assertEquals(helloWorldEncoded,java8Base64.internalEncode(helloWorldBytes)); |
| 45 | +assertEquals(helloWorldTwoLinesEncoded,java8Base64.internalEncode(helloWorldTwoLinesBytes)); |
| 46 | +assertEquals(helloWorldTwoLinesAndNewLineEncoded, |
| 47 | +java8Base64.internalEncode(helloWorldTwoLinesAndNewLineBytes)); |
| 48 | +assertEquals(helloWorldDifferentCharsEncoded,java8Base64.internalEncode(helloWorldDifferentCharsBytes)); |
| 49 | + } |
| 50 | + |
| 51 | +@Test |
| 52 | +publicvoidtestEncodeUrlWithoutPadding() { |
| 53 | +finalStringhelloWorldEncoded ="SGVsbG8gV29ybGQ"; |
| 54 | +finalStringhelloWorldTwoLinesEncoded ="SGVsbG8gV29ybGQNCk5ldyBMaW5lMg"; |
| 55 | +finalStringhelloWorldTwoLinesAndNewLineEncoded ="SGVsbG8gV29ybGQNClNlY29uZCBMaW5lDQo"; |
| 56 | +finalStringhelloWorldDifferentCharsEncoded ="YDEyMzQ1Njc4OTAtPX4hQCMkJV4mKigpXyvRkdCBIuKEljs6P3F3ZXJ0eXVpb3B" |
| 57 | + +"bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw-P9C50YTRj9GG0YvRh9GD0LLRgdC" |
| 58 | + +"60LDQvNC10L_QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J_" |
| 59 | + +"QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg"; |
| 60 | + |
| 61 | +assertEquals(helloWorldEncoded,java8Base64.internalEncodeUrlWithoutPadding(helloWorldBytes)); |
| 62 | +assertEquals(helloWorldTwoLinesEncoded,java8Base64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesBytes)); |
| 63 | +assertEquals(helloWorldTwoLinesAndNewLineEncoded, |
| 64 | +java8Base64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesAndNewLineBytes)); |
| 65 | +assertEquals(helloWorldDifferentCharsEncoded, |
| 66 | +java8Base64.internalEncodeUrlWithoutPadding(helloWorldDifferentCharsBytes)); |
| 67 | + } |
| 68 | + |
| 69 | +@Test |
| 70 | +publicvoidtestDecodeMime() { |
| 71 | +finalStringhelloWorldEncoded ="SGVsbG8gV29ybGQ="; |
| 72 | +finalStringhelloWorldTwoLinesEncoded ="SGVsbG8gV29ybGQNCk5ldyBMaW5lMg=="; |
| 73 | +finalStringhelloWorldTwoLinesAndNewLineEncoded ="SGVsbG8gV29ybGQNClNlY29uZCBMaW5lDQo="; |
| 74 | +finalStringhelloWorldDifferentCharsEncoded ="YDEyMzQ1Njc4OTAtPX4hQCMkJV4mKigpXyvRkdCBIuKEljs6P3F3ZXJ0eXVpb3B" |
| 75 | + +"bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw+P9C50YTRj9GG0YvRh9GD0LLRgdC" |
| 76 | + +"60LDQvNC10L/QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J/" |
| 77 | + +"QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg=="; |
| 78 | + |
| 79 | +assertArrayEquals(helloWorldBytes,java8Base64.internalDecodeMime(helloWorldEncoded)); |
| 80 | +assertArrayEquals(helloWorldTwoLinesBytes,java8Base64.internalDecodeMime(helloWorldTwoLinesEncoded)); |
| 81 | +assertArrayEquals(helloWorldTwoLinesAndNewLineBytes, |
| 82 | +java8Base64.internalDecodeMime(helloWorldTwoLinesAndNewLineEncoded)); |
| 83 | +assertArrayEquals(helloWorldDifferentCharsBytes, |
| 84 | +java8Base64.internalDecodeMime(helloWorldDifferentCharsEncoded)); |
| 85 | + } |
| 86 | + |
| 87 | +} |