@@ -55,30 +55,6 @@ public void testBasicSpace() throws Exception {
5555 Assert .assertArrayEquals ("%20" .getBytes (Charset .forName ("UTF-8" )), encoded );
5656 }
5757
58- @ Test
59- public void testSafeCharEncodeDecodeObject () throws Exception {
60- PercentCodec percentCodec = new PercentCodec (null , true );
61- final String input = "abc123_-.*" ;
62- Object encoded = percentCodec .encode ((Object ) input .getBytes (Charset .forName ("UTF-8" )));
63- final String encodedS = new String ((byte []) encoded , "UTF-8" );
64- Object decoded = percentCodec .decode (encoded );
65- final String decodedS = new String ((byte []) decoded , "UTF-8" );
66- assertEquals ("Basic PercentCodec safe char encoding test" , input , encodedS );
67- assertEquals ("Basic PercentCodec safe char decoding test" , input , decodedS );
68- }
69-
70- @ Test
71- public void testUnsafeCharEncodeDecode () throws Exception {
72- PercentCodec percentCodec = new PercentCodec ();
73- final String input = "\u03B1 \u03B2 \u03B3 \u03B4 \u03B5 \u03B6 % " ;
74- byte [] encoded = percentCodec .encode (input .getBytes (Charset .forName ("UTF-8" )));
75- final String encodedS = new String (encoded , "UTF-8" );
76- byte [] decoded = percentCodec .decode (encoded );
77- final String decodedS = new String (decoded , "UTF-8" );
78- assertEquals ("Basic PercentCodec unsafe char encoding test" , "%CE%B1%CE%B2%CE%B3%CE%B4%CE%B5%CE%B6%25 " , encodedS );
79- assertEquals ("Basic PercentCodec unsafe char decoding test" , input , decodedS );
80- }
81-
8258 @ Test
8359 public void testConfigurablePercentEncoder () throws Exception {
8460 final String input = "abc123_-.*\u03B1 \u03B2 " ;
@@ -90,6 +66,43 @@ public void testConfigurablePercentEncoder() throws Exception {
9066 assertEquals ("Configurable PercentCodec decoding test" , new String (decoded , "UTF-8" ), input );
9167 }
9268
69+ @ Test
70+ public void testDecodeInvalidEncodedResultDecoding () throws Exception {
71+ String inputS = "\u03B1 \u03B2 " ;
72+ PercentCodec percentCodec = new PercentCodec ();
73+ byte [] encoded = percentCodec .encode (inputS .getBytes ("UTF-8" ));
74+ try {
75+ percentCodec .decode (Arrays .copyOf (encoded , encoded .length -1 )); //exclude one byte
76+ } catch (Exception e ) {
77+ assertTrue (DecoderException .class .isInstance (e ) &&
78+ ArrayIndexOutOfBoundsException .class .isInstance (e .getCause ()));
79+ }
80+ }
81+
82+ @ Test
83+ public void testDecodeNullObject () throws Exception {
84+ PercentCodec percentCodec = new PercentCodec ();
85+ assertEquals (percentCodec .decode ((Object ) null ), null );
86+ }
87+
88+ @ Test (expected = DecoderException .class )
89+ public void testDecodeUnsupportedObject () throws Exception {
90+ PercentCodec percentCodec = new PercentCodec ();
91+ percentCodec .decode ("test" );
92+ }
93+
94+ @ Test
95+ public void testEncodeNullObject () throws Exception {
96+ PercentCodec percentCodec = new PercentCodec ();
97+ assertEquals (percentCodec .encode ((Object ) null ), null );
98+ }
99+
100+ @ Test (expected = EncoderException .class )
101+ public void testEncodeUnsupportedObject () throws Exception {
102+ PercentCodec percentCodec = new PercentCodec ();
103+ percentCodec .encode ("test" );
104+ }
105+
93106 @ Test
94107 public void testPercentEncoderDecoderWithNullOrEmptyInput () throws Exception {
95108 PercentCodec percentCodec = new PercentCodec (null , true );
@@ -111,41 +124,28 @@ public void testPercentEncoderDecoderWithPlusForSpace() throws Exception {
111124 assertEquals ("PercentCodec plus for space decoding test" , new String (decode , "UTF-8" ), input );
112125 }
113126
114- @ Test (expected = EncoderException .class )
115- public void testEncodeUnsupportedObject () throws Exception {
116- PercentCodec percentCodec = new PercentCodec ();
117- percentCodec .encode ("test" );
118- }
119-
120127 @ Test
121- public void testEncodeNullObject () throws Exception {
122- PercentCodec percentCodec = new PercentCodec ();
123- assertEquals (percentCodec .encode ((Object ) null ), null );
124- }
125-
126- @ Test (expected = DecoderException .class )
127- public void testDecodeUnsupportedObject () throws Exception {
128- PercentCodec percentCodec = new PercentCodec ();
129- percentCodec .decode ("test" );
130- }
131-
132- @ Test
133- public void testDecodeNullObject () throws Exception {
134- PercentCodec percentCodec = new PercentCodec ();
135- assertEquals (percentCodec .decode ((Object ) null ), null );
128+ public void testSafeCharEncodeDecodeObject () throws Exception {
129+ PercentCodec percentCodec = new PercentCodec (null , true );
130+ final String input = "abc123_-.*" ;
131+ Object encoded = percentCodec .encode ((Object ) input .getBytes (Charset .forName ("UTF-8" )));
132+ final String encodedS = new String ((byte []) encoded , "UTF-8" );
133+ Object decoded = percentCodec .decode (encoded );
134+ final String decodedS = new String ((byte []) decoded , "UTF-8" );
135+ assertEquals ("Basic PercentCodec safe char encoding test" , input , encodedS );
136+ assertEquals ("Basic PercentCodec safe char decoding test" , input , decodedS );
136137 }
137138
138139 @ Test
139- public void testDecodeInvalidEncodedResultDecoding () throws Exception {
140- String inputS = "\u03B1 \u03B2 " ;
140+ public void testUnsafeCharEncodeDecode () throws Exception {
141141 PercentCodec percentCodec = new PercentCodec ();
142- byte [] encoded = percentCodec . encode ( inputS . getBytes ( "UTF-8" )) ;
143- try {
144- percentCodec . decode ( Arrays . copyOf ( encoded , encoded . length - 1 )); //exclude one byte
145- } catch ( Exception e ) {
146- assertTrue ( DecoderException . class . isInstance ( e ) &&
147- ArrayIndexOutOfBoundsException . class . isInstance ( e . getCause ()) );
148- }
142+ final String input = " \u03B1 \u03B2 \u03B3 \u03B4 \u03B5 \u03B6 % " ;
143+ byte [] encoded = percentCodec . encode ( input . getBytes ( Charset . forName ( "UTF-8" )));
144+ final String encodedS = new String ( encoded , "UTF-8" );
145+ byte [] decoded = percentCodec . decode ( encoded );
146+ final String decodedS = new String ( decoded , "UTF-8" );
147+ assertEquals ( "Basic PercentCodec unsafe char encoding test" , "%CE%B1%CE%B2%CE%B3%CE%B4%CE%B5%CE%B6%25 " , encodedS );
148+ assertEquals ( "Basic PercentCodec unsafe char decoding test" , input , decodedS );
149149 }
150150
151151}
0 commit comments