eachwithreaderandinputstreamtest.groovy

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 77 行

GROOVY
77
字号
package org.codehaus.groovy.runtimeimport java.io.StringReaderimport java.io.StringBufferInputStream/**  * Test .each with Reader and InputStream *  * @author <a href="mailto:joachim.baumann@xinaris.de">Joachim Baumann</a> * @version $Revision: $ */class EachWithReaderAndInputStreamTest extends GroovyTestCase {	/**	 * The following instances are used in testing the file operations	 */	String multiLineVal = """This textas can be seenhas multiple linesand not one punctuation mark"""	// Our file instance	def File file;		void setUpFile() {		// Setup guarantees us that we use a non-existent file		file = File.createTempFile("unitTest", ".txt") 		assert file.exists() == true		//println file.canonicalPath		assert file.length() == 0L		file << multiLineVal	}	void tearDownFile() {		// we remove our temporary file		def deleted = false		while(deleted == false)			deleted = file.delete()		assert file.exists() == false	}	void testEachForStringBufferInputStream(){		def ist = new StringBufferInputStream(multiLineVal)		def readVal = ""		ist.each { readVal += (char)it }		assert readVal == multiLineVal	}	 			void testEachForStringReader(){		def ir = new StringReader(multiLineVal)		def readVal = ""		ir.each { readVal += it + "\n" }		assert readVal == multiLineVal	}	void testEachForFileWithInputStream() {		setUpFile()		def readVal = ""		file.withInputStream{ is ->			is.each { readVal += (char)it }		}		tearDownFile()		assert readVal == multiLineVal	}	void testEachForFileWithReader() {		setUpFile()		def readVal = ""		file.withReader{ reader ->			reader.each { readVal += it + "\n" }		}		tearDownFile()		assert readVal == multiLineVal	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?