jakarta-bsf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nil...@opensource.lk
Subject [PATHC]TestCase for org.apache.bsf.util.StringUtils.java
Date Wed, 28 Apr 2004 06:54:32 GMT
Dear All,

Please accept this patch which is a testcase for
org.apache.bsf.util.StringUtils.java

Regards !!!

Nilupa Bandara

**************************************************************************
   following is the diff for the Jakarta-BSF\org\apache\bsf\test\BSFTest.java
**************************************************************************


--- BSFTest.java	2004-04-27 09:56:34.000000000 +0600
+++ BSFTestModified.java	2004-04-28 12:16:11.000000000 +0600
@@ -8,6 +8,7 @@
 import org.apache.bsf.BSFManager;

 import org.apache.bsf.test.engineTests.*;
+import org.apache.bsf.test.utilTests.*;

 /**
  * Primary test class and testing front end for BSF.
@@ -41,7 +42,7 @@
          * Please add testcases here as needed.
          */
         TestSuite suite = new TestSuite();
-        testNames = new String [4];
+        testNames = new String [5];

         suite.addTestSuite(BSFTest.class);
         testNames[0] = "BSFManager Base Tests";
@@ -51,7 +52,10 @@
         testNames[2] = "Rhino Engine Tests";
         suite.addTestSuite(jythonTest.class);
         testNames[3] = "Jython Engine Tests";
-
+	suite.addTestSuite(StringUtilsTest.class);
+        testNames[4] = "StringUtils Test";
+
+
         return suite;
     }



@@ -63,7 +67,7 @@
     }

     public void testRegisterEngine() {
-        assertTrue(bsfManager.isLanguageRegistered("fakeEngine"));
+        assertTrue(BSFManager.isLanguageRegistered("fakeEngine"));
     }

     public void testGetLangFromFileName() {

*****************************************************************
    new testcase for the org.apache.bsf.util.StringUtils.java
*****************************************************************


package org.apache.bsf.test.utilTests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.MalformedURLException;
import java.util.Vector;

import org.apache.bsf.util.StringUtils;

import junit.framework.TestCase;

/**
 * This is a testcase for org.apache.bsf.util.StringUtils.java
 *
 * @author Sanka Samaranayake <sanka_hdins@yahoo.com>
 * @author Nilupa Bandara     <nilupa@opensource.lk>
 *
 */
public class StringUtilsTest extends TestCase {

	static private final String lineSeparator =
			System.getProperty("line.separator", "\n");

	/**
	 * Constructor for StringUtilsTest.
	 * @param arg0
	 */
	public StringUtilsTest(String arg0) {
		super(arg0);
	}

	public void testClassNameToVarName() {

		assertTrue((StringUtils.classNameToVarName("int")).equals(
				new String("int")));
		assertTrue((StringUtils.classNameToVarName("int[][][]")).equals(
				new String("int_3D")));
		assertNull((StringUtils.classNameToVarName("")));
	}

	public void testCleanString() {

		String result;

		result = StringUtils.cleanString("\"");
		assertTrue(result.equals("\\\""));

		result = StringUtils.cleanString("\\");
		assertTrue(result.equals("\\\\"));

		result = StringUtils.cleanString("\n");
		assertTrue(result.equals("\\n"));

		result = StringUtils.cleanString("\r");
		assertTrue(result.equals("\\r"));

	}

	public void testGetChars() {

		String result;

		result = StringUtils.getChars(1, 'a');
		assertTrue(result.equals(new String("a")));

		result = StringUtils.getChars(1, ' ');
		assertTrue(result.equals(new String(" ")));

		result = StringUtils.getChars(10, ' ');
		assertTrue(result.equals(new String("          ")));

		result = StringUtils.getChars(-1, 'a');
		assertTrue(result.equals(new String("")));

	}

	public void testGetClassName() {
		String result;

		result = StringUtils.getClassName((new Byte("0")).getClass());
		assertTrue(result.equals(new String("java.lang.Byte")));

		result = StringUtils.getClassName((new Byte[0][0][0]).getClass());
		assertTrue(result.equals(new String("java.lang.Byte[][][]")));

		result = StringUtils.getClassName((new String("")).getClass());
		assertTrue(result.equals(new String("java.lang.String")));

		result = StringUtils.getClassName((new String[0][0][0]).getClass());
		assertTrue(result.equals(new String("java.lang.String[][][]")));

	}

	public void testGetCommaListFromVector() {

		String result;

		Vector vector = new Vector();
		vector.add(new Character('a'));
		vector.add(new Character('b'));

		result = StringUtils.getCommaListFromVector(vector);
		assertTrue(result.equals(new String("a, b")));

		result = StringUtils.getCommaListFromVector(new Vector());
		assertTrue(result.equals(new String("")));

	}

	public void testGetContentAsReader() throws MalformedURLException,
IOException{

		Reader reader;

		File myFile = File.createTempFile("Test", "txt");

		FileWriter fw = new FileWriter(myFile);
		PrintWriter pw = new PrintWriter(fw);
		pw.println("file name : Test.txt");
		pw.flush();

		reader = StringUtils.getContentAsReader(myFile.toURL());
		BufferedReader bf = new BufferedReader(reader);
		assertTrue(bf.readLine().equals(
				new String("file name : Test.txt")));

	}

	public void testGetContentAsString() throws IOException{

		String result;

		File myFile = File.createTempFile("Test", "txt");

		FileWriter fw = new FileWriter(myFile);
		PrintWriter pw = new PrintWriter(fw);
		pw.println("file name : Test.txt");
		pw.flush();

		result = StringUtils.getContentAsString(myFile.toURL());
		assertTrue(result.equals(new String("file name : Test.txt" +
lineSeparator)));

	}

	public void testGetSafeString() {
		String result;

		result = StringUtils.getSafeString("test-string");
		assertTrue(result.equals(new String("\"test-string\"" +
				lineSeparator)));
		//checks for an empty string ..
		result = StringUtils.getSafeString("");
		assertTrue(result.equals(new String("\"\"" +
				lineSeparator)));

		result = StringUtils.getSafeString("\n");
		assertTrue(result.equals(new String("\"\"" +
				lineSeparator)));

		result = StringUtils.getSafeString("\r");
		assertTrue(result.equals(new String("\"\"" +
				lineSeparator)));

		result = StringUtils.getSafeString("\\n");
		assertTrue(result.equals(new String("\"\\\\n\"" +
				lineSeparator)));

		result = StringUtils.getSafeString("\\r");
		assertTrue(result.equals(new String("\"\\\\r\"" +
				lineSeparator)));

	}

	public void testGetValidIdentifierName(){

		assertTrue((StringUtils.getValidIdentifierName("identifier")).equals(
				new String("identifier")));

		assertTrue((StringUtils.getValidIdentifierName("0identifier")).equals(
				new String("_identifier")));

		assertTrue((StringUtils.getValidIdentifierName("i0dentifier")).equals(
				new String("i0dentifier")));

		assertTrue((StringUtils.getValidIdentifierName("$identifier")).equals(
				new String("$identifier")));

		assertTrue((StringUtils.getValidIdentifierName("identi$fier")).equals(
				new String("identi$fier")));

		assertTrue((StringUtils.getValidIdentifierName(" identifier")).equals(
				new String("_identifier")));

		assertTrue((StringUtils.getValidIdentifierName("identi fier")).equals(
				new String("identi_fier")));

		// checks for a empty string which should return null
		assertNull(StringUtils.getValidIdentifierName(""));

		// checks for a null value which should return null
		assertNull(StringUtils.getValidIdentifierName(null));
	}

	public void testIsValidIdentifierName() {

		assertTrue(StringUtils.isValidIdentifierName("identifier"));

		assertFalse(StringUtils.isValidIdentifierName("0identifier"));

		assertTrue(StringUtils.isValidIdentifierName("i0dentifier"));

		assertTrue(StringUtils.isValidIdentifierName("$identifier"));

		assertTrue(StringUtils.isValidIdentifierName("identi$fier"));

		assertFalse(StringUtils.isValidIdentifierName(" identifier"));

		assertFalse(StringUtils.isValidIdentifierName("identi fier"));

		// checks for a null value .. should return null
		assertNull(StringUtils.getValidIdentifierName(null));

		// checks for an empty string .. should return null
		assertNull(StringUtils.getValidIdentifierName(""));

	}

	public void testIsValidPackageName() {

		assertTrue(StringUtils.isValidPackageName("org"));

		assertTrue(StringUtils.isValidPackageName("org.apache.bsf"));

		// checks whether the package name ends with a '.'
		assertFalse(StringUtils.isValidPackageName("org.apache.bsf."));

		// checks whether the package name includes '..'
		assertFalse(StringUtils.isValidPackageName("org.apache.bsf.."));

		// checks for an empty string which is ok ..
		assertTrue(StringUtils.isValidPackageName(""));

		// checks for a null value
		assertFalse(StringUtils.isValidPackageName(null));
	}

}


---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Mime
View raw message