Code example for StreamTokenizer

Methods: nextToken, quoteChar

0
	 * @tests java.io.StreamTokenizer#quoteChar(int) 
	 */ 
	public void test_quoteCharI() throws IOException { 
		// SM 
		setTest("<Hello World<    HelloWorldH"); 
		st.quoteChar('<');
		assertEquals("QuoteChar failed.", '<', st.nextToken());
		assertEquals("QuoteChar failed.", "Hello World", st.sval);
		st.quoteChar('H');
		st.nextToken();
		assertEquals("QuoteChar failed for word.", "elloWorld", st.sval
			     ); 
	} 
 
	/** 
	 * @tests java.io.StreamTokenizer#resetSyntax() 
	 */ 
	public void test_resetSyntax() throws IOException { 
		// SM 
		setTest("H 9\' ello World"); 
		st.resetSyntax();
		assertTrue("resetSyntax failed1." + (char) st.ttype,
			   st.nextToken() == 'H');
		assertTrue("resetSyntax failed1." + (char) st.ttype,
			   st.nextToken() == ' ');
		assertTrue("resetSyntax failed2." + (char) st.ttype,
			   st.nextToken() == '9');
		assertTrue("resetSyntax failed3." + (char) st.ttype,
			   st.nextToken() == '\'');
	} 
 
	/** 
	 * @tests java.io.StreamTokenizer#slashSlashComments(boolean) 
	 */ 
	public void test_slashSlashCommentsZ() throws IOException { 
		// SM. 
		setTest("// foo \r\n /fiji \r\n -456"); 
		st.ordinaryChar('/');
		st.slashSlashComments(true);
		assertEquals("Test failed.", '/', st.nextToken());
		assertTrue("Test failed.",
			   st.nextToken() == StreamTokenizer.TT_WORD);
	} 
     
    /** 
     * @tests java.io.StreamTokenizer#slashSlashComments(boolean) 
     */ 
    public void test_slashSlashComments_withSSOpen() throws IOException { 
        Reader reader = new CharArrayReader( "t // t t t".toCharArray());
 
        StreamTokenizer st = new StreamTokenizer(reader);
        st.slashSlashComments(true);
 
        assertEquals(StreamTokenizer.TT_WORD,st.nextToken());
        assertEquals(StreamTokenizer.TT_EOF,st.nextToken());
    } 
 
    /** 
     * @tests java.io.StreamTokenizer#slashSlashComments(boolean) 
     */ 
    public void test_slashSlashComments_withSSOpen_NoComment() throws IOException { 
        Reader reader = new CharArrayReader( "// t".toCharArray());
 
        StreamTokenizer st = new StreamTokenizer(reader);
        st.slashSlashComments(true);
        st.ordinaryChar('/');
 
        assertEquals(StreamTokenizer.TT_EOF,st.nextToken());
    } 
     
    /** 
     * @tests java.io.StreamTokenizer#slashSlashComments(boolean) 
     */ 
    public void test_slashSlashComments_withSSClosed() throws IOException { 
        Reader reader = new CharArrayReader( "// t".toCharArray());
 
        StreamTokenizer st = new StreamTokenizer(reader);
        st.slashSlashComments(false);
        st.ordinaryChar('/');
 
        assertEquals('/',st.nextToken());
        assertEquals('/',st.nextToken());
        assertEquals(StreamTokenizer.TT_WORD,st.nextToken());
    } 
     
	/** 
	 * @tests java.io.StreamTokenizer#slashStarComments(boolean) 
	 */ 
	public void test_slashStarCommentsZ() throws IOException { 
		setTest("/* foo \r\n /fiji \r\n*/ -456"); 
		st.ordinaryChar('/');
		st.slashStarComments(true);
		assertTrue("Test failed.",
			   st.nextToken() == StreamTokenizer.TT_NUMBER);
	} 
 
    /** 
     * @tests java.io.StreamTokenizer#slashStarComments(boolean) 
     */ 
    public void test_slashStarComments_withSTOpen() throws IOException { 
        Reader reader = new CharArrayReader( "t /* t */ t".toCharArray());
 
        StreamTokenizer st = new StreamTokenizer(reader);
        st.slashStarComments(true);
 
        assertEquals(StreamTokenizer.TT_WORD,st.nextToken());
        assertEquals(StreamTokenizer.TT_WORD,st.nextToken());
        assertEquals(StreamTokenizer.TT_EOF,st.nextToken());
    } 
 
    /** 
     * @tests java.io.StreamTokenizer#slashStarComments(boolean) 
     */ 
    public void test_slashStarComments_withSTClosed() throws IOException { 
        Reader reader = new CharArrayReader( "t /* t */ t".toCharArray());
 
        StreamTokenizer st = new StreamTokenizer(reader);
        st.slashStarComments(false);
 
        assertEquals(StreamTokenizer.TT_WORD,st.nextToken());
        assertEquals(StreamTokenizer.TT_EOF,st.nextToken());
    } 
     
	/** 
	 * @tests java.io.StreamTokenizer#toString() 
	 */ 
	public void test_toString() throws IOException { 
		setTest("ABC Hello World"); 
		st.nextToken();
		assertTrue("toString failed." + st.toString(),
			   st.toString().equals(
						"Token[ABC], line 1")); 
 
		// Regression test for HARMONY-4070 
        byte[] data = new byte[] { (byte) '-' };
        StreamTokenizer tokenizer = new StreamTokenizer(
                new ByteArrayInputStream(data));
        tokenizer.nextToken();
        String result = tokenizer.toString();
        assertEquals("Token['-'], line 1", result);
	} 
 
	/** 
	 * @tests java.io.StreamTokenizer#whitespaceChars(int, int) 
	 */ 
	public void test_whitespaceCharsII() throws IOException { 
		setTest("azbc iof z 893"); 
		st.whitespaceChars('a', 'z');
		assertTrue("OrdinaryChar failed.",
			   st.nextToken() == StreamTokenizer.TT_NUMBER);
	} 
 
	/** 
	 * @tests java.io.StreamTokenizer#wordChars(int, int) 
	 */ 
	public void test_wordCharsII() throws IOException { 
		setTest("A893 -9B87"); 
		st.wordChars('0', '9');
		assertTrue("WordChar failed1.",
			   st.nextToken() == StreamTokenizer.TT_WORD);
		assertEquals("WordChar failed2.", "A893", st.sval);
		assertTrue("WordChar failed3.",
			   st.nextToken() == StreamTokenizer.TT_NUMBER);
		st.nextToken();
		assertEquals("WordChar failed4.", "B87", st.sval);
		 
		setTest("    Hello World"); 
		st.wordChars(' ', ' ');
		st.nextToken();
		assertEquals("WordChars failed for whitespace.", "Hello World", st.sval
			     ); 
		 
		setTest("    Hello World\r\n  \'Hello World\' Hello\' World"); 
		st.wordChars(' ', ' ');
		st.wordChars('\'', '\'');
		st.nextToken();
		assertTrue("WordChars failed for whitespace: " + st.sval, st.sval
			   .equals("Hello World"));
		st.nextToken();
		assertTrue("WordChars failed for quote1: " + st.sval, st.sval
			   .equals("\'Hello World\' Hello\' World"));
	} 
 
	private void setTest(String s) {
		testString = s;
		r = new Support_StringReader(testString);
		st = new StreamTokenizer(r);
	} 
 
	protected void setUp() { 
	}