@@ -93,35 +93,29 @@ public final void testEmptyString() {
9393 @ Test
9494 public final void testOverwrite () {
9595 try {
96- byte [] utf8Bytes = unicodeStr .getBytes ("UTF-8" );
97- PlainText pt = new PlainText (utf8Bytes );
96+ byte [] origBytes = unicodeStr .getBytes ("UTF-8" );
97+ PlainText pt = new PlainText (origBytes );
9898 assertTrue ( pt .toString ().equals (unicodeStr ) );
99- assertTrue ( Arrays .equals (utf8Bytes , pt .asBytes ()) );
99+ assertTrue ( Arrays .equals (origBytes , pt .asBytes ()) );
100100 assertTrue ( pt .hashCode () == unicodeStr .hashCode () );
101101
102- int origLen = utf8Bytes .length ;
102+ int origLen = origBytes .length ;
103103
104104 pt .overwrite ();
105- assertTrue ( utf8Bytes != null );
106- assertTrue ( pt . asBytes () != null );
107- assertTrue ( utf8Bytes == pt . asBytes ( ) );
105+ byte [] overwrittenBytes = pt . asBytes ( );
106+ assertTrue ( overwrittenBytes != null );
107+ assertFalse ( Arrays . equals ( origBytes , overwrittenBytes ) );
108108
109- // DISCUSS:
110- // See discussion note regarding method PlainText.overwrite(). Uncomment this
111- // try / catch block if we set 'rawBytes' in this method to null.
112- //
113- // try {
114- // @SuppressWarnings("unused")
115- // int len = pt.length();
116- // } catch( NullPointerException npe ) {
117- // assertTrue( "Caught expected NullPointerException", true );
118- // }
119- int afterLen = utf8Bytes .length ;
109+ // Ensure that ALL the bytes overwritten with '*'.
110+ int afterLen = overwrittenBytes .length ;
120111 assertTrue ( origLen == afterLen );
121- System .out .println ("Length after overwritting: " + afterLen );
122- for (int i = 0 ; i < afterLen ; i ++ ) {
123- System .out .println ("utf8Bytes[" + i + "] = " + utf8Bytes [i ]);
112+ int sum = 0 ;
113+ for ( int i = 0 ; i < afterLen ; i ++ ) {
114+ if ( overwrittenBytes [i ] == '*' ) {
115+ sum ++;
116+ }
124117 }
118+ assertTrue ( afterLen == sum );
125119 } catch (UnsupportedEncodingException e ) {
126120 fail ("No UTF-8 byte encoding: " + e );
127121 e .printStackTrace (System .err );
0 commit comments