harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r399263 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Formattable.java main/java/java/util/Formatter.java test/java/tests/api/java/util/AllTests.java test/java/tests/api/java/util/FormatterTest.java
Date Wed, 03 May 2006 11:26:34 GMT
Author: tellison
Date: Wed May  3 04:26:32 2006
New Revision: 399263

URL: http://svn.apache.org/viewcvs?rev=399263&view=rev
Log:
Apply patch for HARMONY-395 (Java 5 Enhancement:new classes java.util.Formattable and java.util.Formatter)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formattable.java
  (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
  (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java
  (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formattable.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formattable.java?rev=399263&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formattable.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formattable.java
Wed May  3 04:26:32 2006
@@ -0,0 +1,56 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+/**
+ * Any class that need to perform customer formatting by transferring converter
+ * specifier 's' to Formatter should implement the Formattable interface. Basic
+ * format is allowed by the interface to format arbitrary objects.
+ */
+
+public interface Formattable {
+
+	/**
+	 * Formats the object using the specified formatter.
+	 * 
+	 * @param formatter
+	 *            The formatter to use in the formatTo.
+	 * @param flags
+	 *            The flags applied to the output format, which is a bitmask
+	 *            that is any combination of FormattableFlags.LEFT_JUSTIFY,
+	 *            FormattableFlags.UPPERCASE, and FormattableFlags.ALTERNATE. If
+	 *            no such flag is set, the output is formatted by the default
+	 *            formatting of the implementation of the interface.
+	 * @param width
+	 *            The minimum number of characters that should be written to the
+	 *            output. Additional space ' ' is added to the output if the
+	 *            length of the converted value is less than the width until the
+	 *            length equals the width. These spaces are added at the
+	 *            beginning by default unless the flag
+	 *            FormattableFlags.LEFT_JUSTIFY is set, which denotes that
+	 *            padding should be added at the end. If width is -1, then no
+	 *            minimum requirement.
+	 * @param precision
+	 *            The maximum number of characters that can be written to the
+	 *            output. The procedure to trunk the output according to the
+	 *            precision is invoked before that of padding to width. If the
+	 *            precision is -1, then no maximum requirement.
+	 * @throws IllegalFormatException
+	 *             If any of the parameters is not supported.
+	 */
+	void formatTo(Formatter formatter, int flags, int width, int precision)
+			throws IllegalFormatException;
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formattable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java?rev=399263&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
Wed May  3 04:26:32 2006
@@ -0,0 +1,527 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package java.util;
+
+import java.io.BufferedWriter;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+
+import org.apache.harmony.luni.util.NotYetImplementedException;
+
+/**
+ * Formatter provides the method to give out formatted string just like the
+ * printf-style. Layout,alignment and other format flags are provided to format
+ * numeric,string and datetime as well as locale-specified formats applied.
+ * Besides primitive types, formatter also support some java object types such
+ * as BigInteger,BigDecimal and Calendar. Customized formatting is provided
+ * through the Formattable interface.
+ * 
+ * The class is not multi-threaded safe. The responsibility to maintain thread
+ * safety is the user's job.
+ * 
+ */
+public final class Formatter implements Closeable, Flushable {
+
+	private Appendable out;
+
+	private Locale locale;
+
+	private boolean closed = false;
+
+	private IOException lastIOException;
+
+	/**
+	 * Constructs a formatter.
+	 * 
+	 * The output is a StringBuilder which can be achieved by invoking the out
+	 * method and whose contents can be attained by calling the toString method.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 */
+	public Formatter() {
+		this(new StringBuilder(), Locale.getDefault());
+	}
+
+	/**
+	 * Constructs a formatter of which the output is denoted.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 * @param a
+	 *            The output of the formatter. If a is null, then a
+	 *            StringBuilder will be used.
+	 */
+	public Formatter(Appendable a) {
+		this(a, Locale.getDefault());
+	}
+
+	/**
+	 * Constructs a formatter of which the locale is denoted.
+	 * 
+	 * The output destination is a StringBuilder which can be achieved by
+	 * invoking the out method and whose contents can be attained by calling the
+	 * toString method.
+	 * 
+	 * @param l
+	 *            The locale of the formatter. If l is null, then no
+	 *            localization will be used.
+	 */
+	public Formatter(Locale l) {
+		this(new StringBuilder(), l);
+	}
+
+	/**
+	 * Constructs a formatter of which the output and locale is denoted.
+	 * 
+	 * @param a
+	 *            The output of the formatter. If a is null, then a
+	 *            StringBuilder will be used.
+	 * @param l
+	 *            The locale of the formatter. If l is null, then no
+	 *            localization will be used.
+	 */
+	public Formatter(Appendable a, Locale l) {
+		if (null == a) {
+			out = new StringBuilder();
+		} else {
+			out = a;
+		}
+		locale = l;
+	}
+
+	/**
+	 * Constructs a formatter of which the filename is denoted.
+	 * 
+	 * The charset of the formatter is the default charset of JVM.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 * @param fileName
+	 *            The filename of the file that is used as the output
+	 *            destination for the formatter. The file will be truncated to
+	 *            zero size if the file exists, or else a new file will be
+	 *            created. The output of the formatter is buffered.
+	 * 
+	 * @throws FileNotFoundException
+	 *             If the filename does not denote a normal and writable file,
+	 *             or a new file cannot be created or any error rises when
+	 *             opening or creating the file.
+	 * @throws SecurityException
+	 *             If there is a security manager and it denies writing to the
+	 *             file in checkWrite(file.getPath()).
+	 */
+	public Formatter(String fileName) throws FileNotFoundException {
+		this(new File(fileName));
+
+	}
+
+	/**
+	 * Constructs a formatter of which the filename and charset is denoted.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 * @param fileName
+	 *            The filename of the file that is used as the output
+	 *            destination for the formatter. The file will be truncated to
+	 *            zero size if the file exists, or else a new file will be
+	 *            created. The output of the formatter is buffered.
+	 * @param csn
+	 *            The name of the charset for the formatter.
+	 * 
+	 * @throws FileNotFoundException
+	 *             If the filename does not denote a normal and writable file,
+	 *             or a new file cannot be created or any error rises when
+	 *             opening or creating the file.
+	 * @throws SecurityException
+	 *             If there is a security manager and it denies writing to the
+	 *             file in checkWrite(file.getPath()).
+	 * @throws UnsupportedEncodingException
+	 *             If the charset with the specified name is not supported.
+	 */
+	public Formatter(String fileName, String csn) throws FileNotFoundException,
+			UnsupportedEncodingException {
+		this(new File(fileName), csn);
+	}
+
+	/**
+	 * Constructs a formatter of which the filename, charset and locale is
+	 * denoted.
+	 * 
+	 * @param fileName
+	 *            The filename of the file that is used as the output
+	 *            destination for the formatter. The file will be truncated to
+	 *            zero size if the file exists, or else a new file will be
+	 *            created. The output of the formatter is buffered.
+	 * @param csn
+	 *            The name of the charset for the formatter.
+	 * @param l
+	 *            The locale of the formatter. If l is null, then no
+	 *            localization will be used.
+	 * 
+	 * @throws FileNotFoundException
+	 *             If the filename does not denote a normal and writable file,
+	 *             or a new file cannot be created or any error rises when
+	 *             opening or creating the file.
+	 * @throws SecurityException
+	 *             If there is a security manager and it denies writing to the
+	 *             file in checkWrite(file.getPath()).
+	 * @throws UnsupportedEncodingException
+	 *             If the charset with the specified name is not supported.
+	 * 
+	 */
+	public Formatter(String fileName, String csn, Locale l)
+			throws FileNotFoundException, UnsupportedEncodingException {
+
+		this(new File(fileName), csn, l);
+	}
+
+	/**
+	 * Constructs a formatter of which the file is denoted.
+	 * 
+	 * The charset of the formatter is the default charset of JVM.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 * @param file
+	 *            The file that is used as the output destination for the
+	 *            formatter. The file will be truncated to zero size if the file
+	 *            exists, or else a new file will be created. The output of the
+	 *            formatter is buffered.
+	 * 
+	 * @throws FileNotFoundException
+	 *             If the file does not denote a normal and writable file, or a
+	 *             new file cannot be created or any error rises when opening or
+	 *             creating the file.
+	 * @throws SecurityException
+	 *             If there is a security manager and it denies writing to the
+	 *             file in checkWrite(file.getPath()).
+	 */
+	public Formatter(File file) throws FileNotFoundException {
+		this(new FileOutputStream(file));
+	}
+
+	/**
+	 * Constructs a formatter of which the file and charset is denoted.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 * @param file
+	 *            The file of the file that is used as the output destination
+	 *            for the formatter. The file will be truncated to zero size if
+	 *            the file exists, or else a new file will be created. The
+	 *            output of the formatter is buffered.
+	 * @param csn
+	 *            The name of the charset for the formatter.
+	 * @throws FileNotFoundException
+	 *             If the file does not denote a normal and writable file, or a
+	 *             new file cannot be created or any error rises when opening or
+	 *             creating the file.
+	 * @throws SecurityException
+	 *             If there is a security manager and it denies writing to the
+	 *             file in checkWrite(file.getPath()).
+	 * @throws UnsupportedEncodingException
+	 *             If the charset with the specified name is not supported.
+	 */
+	public Formatter(File file, String csn) throws FileNotFoundException,
+			UnsupportedEncodingException {
+		this(file, csn, Locale.getDefault());
+	}
+
+	/**
+	 * Constructs a formatter of which the file, charset and locale is denoted.
+	 * 
+	 * @param The
+	 *            file that is used as the output destination for the formatter.
+	 *            The file will be truncated to zero size if the file exists, or
+	 *            else a new file will be created. The output of the formatter
+	 *            is buffered.
+	 * @param csn
+	 *            The name of the charset for the formatter.
+	 * @param l
+	 *            The locale of the formatter. If l is null, then no
+	 *            localization will be used.
+	 * @throws FileNotFoundException
+	 *             If the file does not denote a normal and writable file, or a
+	 *             new file cannot be created or any error rises when opening or
+	 *             creating the file.
+	 * @throws SecurityException
+	 *             If there is a security manager and it denies writing to the
+	 *             file in checkWrite(file.getPath()).
+	 * @throws UnsupportedEncodingException
+	 *             If the charset with the specified name is not supported.
+	 */
+	public Formatter(File file, String csn, Locale l)
+			throws FileNotFoundException, UnsupportedEncodingException {
+		FileOutputStream fout = null;
+		try {
+			fout = new FileOutputStream(file);
+			OutputStreamWriter writer = new OutputStreamWriter(fout, csn);
+			out = new BufferedWriter(writer);
+		} catch (RuntimeException e) {
+			closeOutputStream(fout);
+			throw e;
+		} catch (UnsupportedEncodingException e) {
+			closeOutputStream(fout);
+			throw e;
+		}
+
+		locale = l;
+	}
+
+	/**
+	 * Constructs a formatter of which the output destination is specified.
+	 * 
+	 * The charset of the formatter is the default charset of JVM.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 * @param os
+	 *            The stream used as the destination of the formatter.
+	 */
+	public Formatter(OutputStream os) {
+		OutputStreamWriter writer = new OutputStreamWriter(os, Charset
+				.defaultCharset());
+		out = new BufferedWriter(writer);
+		locale = Locale.getDefault();
+	}
+
+	/**
+	 * Constructs a formatter of which the output destination and the charset is
+	 * specified.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 * @param os
+	 *            The stream used as the destination of the formatter.
+	 * @param csn
+	 *            The name of the charset for the formatter.
+	 * @throws UnsupportedEncodingException
+	 *             If the charset with the specified name is not supported.
+	 */
+	public Formatter(OutputStream os, String csn)
+			throws UnsupportedEncodingException {
+
+		this(os, csn, Locale.getDefault());
+	}
+
+	/**
+	 * Constructs a formatter of which the output destination, the charset and
+	 * the locale is specified.
+	 * 
+	 * @param os
+	 *            The stream used as the destination of the formatter.
+	 * @param csn
+	 *            The name of the charset for the formatter.
+	 * @param l
+	 *            The locale of the formatter. If l is null, then no
+	 *            localization will be used.
+	 * @throws UnsupportedEncodingException
+	 *             If the charset with the specified name is not supported.
+	 */
+	public Formatter(OutputStream os, String csn, Locale l)
+			throws UnsupportedEncodingException {
+
+		OutputStreamWriter writer = new OutputStreamWriter(os, csn);
+		out = new BufferedWriter(writer);
+
+		locale = l;
+	}
+
+	/**
+	 * Constructs a formatter of which the output destination is specified.
+	 * 
+	 * The charset of the formatter is the default charset of JVM.
+	 * 
+	 * The locale for the formatter is the default locale of the JVM.
+	 * 
+	 * @param ps
+	 *            The print stream used as destination of the formatter. If ps
+	 *            is null, then NullPointerExcepiton will be thrown out.
+	 */
+	public Formatter(PrintStream ps) {
+		if (null == ps) {
+			throw new NullPointerException();
+		}
+		out = ps;
+		locale = Locale.getDefault();
+	}
+
+	private void checkClosed() {
+		if (closed) {
+			throw new FormatterClosedException();
+		}
+	}
+
+	/**
+	 * Returns the locale of the formatter.
+	 * 
+	 * @return The locale for the formatter and null for no locale.
+	 * @throws FormatterClosedException
+	 *             If the formatter has been closed.
+	 */
+	public Locale locale() {
+		checkClosed();
+		return locale;
+	}
+
+	/**
+	 * Returns the output destination of the formatter.
+	 * 
+	 * @return The output destination of the formatter.
+	 * @throws FormatterClosedException
+	 *             If the formatter has been closed.
+	 */
+	public Appendable out() {
+		checkClosed();
+		return out;
+	}
+
+	/**
+	 * Returns the content by calling the toString() method of the output
+	 * destination.
+	 * 
+	 * @return The content by calling the toString() method of the output
+	 *         destination.
+	 * @throws FormatterClosedException
+	 *             If the formatter has been closed.
+	 */
+	public String toString() {
+		checkClosed();
+		return out.toString();
+	}
+
+	/**
+	 * Flushes the formatter. If the output destination is flushable, then the
+	 * method flush() will be called on that destinaiton.
+	 * 
+	 * @throws FormatterClosedException
+	 *             If the formatter has been closed.
+	 */
+	public void flush() throws IOException {
+		checkClosed();
+		if (out instanceof Flushable) {
+			try {
+				((Flushable) out).flush();
+			} catch (IOException e) {
+				lastIOException = e;
+			}
+			;
+		}
+
+	}
+
+	/**
+	 * Closes the formatter. If the output destination is Closeable, then the
+	 * method close() will be called on that destination.
+	 * 
+	 * If the formatter has been closed, then calling the close will have no
+	 * effect.
+	 * 
+	 * Any method but the ioException() that is called after the formatter has
+	 * been closed will raise a FormatterClosedException.
+	 */
+	public void close() {
+		closed = true;
+		try {
+			if (out instanceof Closeable) {
+				((Closeable) out).close();
+			}
+		} catch (IOException e) {
+
+			lastIOException = e;
+		}
+	}
+
+	/**
+	 * Returns the last IOException thrown out by the formatter's output
+	 * destination. If the append() method of the destination will not throw
+	 * IOException, the ioException() method will always return null.
+	 * 
+	 * @return The last IOException thrown out by the formatter's output
+	 *         destination.
+	 */
+	public IOException ioException() {
+		return lastIOException;
+	}
+
+	/**
+	 * Writes a formatted string to the output destination of the formatter.
+	 * 
+	 * @param format
+	 *            A format string.
+	 * @param args
+	 *            The arguments list used in the format() method. If there are
+	 *            more arguments than those specified by the format string, then
+	 *            the additional arguments are ignored.
+	 * @return This formatter.
+	 * @throws IllegalFormatException
+	 *             If the format string is illegal or incompatible with the
+	 *             arguments or the arguments are less than those required by
+	 *             the format string or any other illegal situcation.
+	 * @throws FormatterClosedException
+	 *             If the formatter has been closed.
+	 */
+	public Formatter format(String format, Object... args) {
+		return format(locale, format, args);
+	}
+
+	/**
+	 * Writes a formatted string to the output destination of the formatter.
+	 * 
+	 * @param l
+	 *            The locale used in the method. If locale is null, then no
+	 *            localization will be applied. This param does not influence
+	 *            the locale specified during construction.
+	 * @param format
+	 *            A format string.
+	 * @param args
+	 *            The arguments list used in the format() method. If there are
+	 *            more arguments than those specified by the format string, then
+	 *            the additional arguments are ignored.
+	 * @return This formatter.
+	 * @throws IllegalFormatException
+	 *             If the format string is illegal or incompatible with the
+	 *             arguments or the arguments are less than those required by
+	 *             the format string or any other illegal situcation.
+	 * @throws FormatterClosedException
+	 *             If the formatter has been closed.
+	 */
+	public Formatter format(Locale l, String format, Object... args) {
+		throw new NotYetImplementedException();
+	}
+
+	private static void closeOutputStream(OutputStream os) {
+		if (null == os) {
+			return;
+		}
+		try {
+			os.close();
+
+		} catch (IOException e) {
+			// silently
+		}
+	}
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java?rev=399263&r1=399262&r2=399263&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java
Wed May  3 04:26:32 2006
@@ -49,6 +49,7 @@
 		suite.addTestSuite(FormatFlagsConversionMismatchExceptionTest.class);
         suite.addTestSuite(FormattableFlagsTest.class);
         suite.addTestSuite(FormatterClosedExceptionTest.class);
+		suite.addTestSuite(FormatterTest.class);
 		suite.addTestSuite(GregorianCalendarTest.class);
 		suite.addTestSuite(HashMapTest.class);
 		suite.addTestSuite(HashSetTest.class);

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java?rev=399263&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java
Wed May  3 04:26:32 2006
@@ -0,0 +1,708 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.util;
+
+import java.io.BufferedOutputStream;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.PipedOutputStream;
+import java.io.PrintStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.security.Permission;
+import java.util.Formatter;
+import java.util.FormatterClosedException;
+import java.util.Locale;
+import java.nio.charset.Charset;
+import junit.framework.TestCase;
+
+public class FormatterTest extends TestCase {
+
+	class MockAppendable implements Appendable {
+		public Appendable append(CharSequence arg0) throws IOException {
+			return null;
+		}
+
+		public Appendable append(char arg0) throws IOException {
+			return null;
+		}
+
+		public Appendable append(CharSequence arg0, int arg1, int arg2)
+				throws IOException {
+			return null;
+		}
+	}
+
+	class MockSecurityManager extends SecurityManager {
+		public void checkWrite(String filename) {
+			throw new SecurityException();
+		}
+
+		public void checkPermission(Permission p) {
+			if (p.getActions().equals("setSecurityManager")) {
+				return;
+			}
+		}
+	}
+
+	private File notExist;
+
+	private File fileWithContent;
+
+	private File readOnly;
+
+	private File secret;
+
+	/**
+	 * @tests java.util.Formatter#Formatter()
+	 */
+	public void test_Constructor() {
+		Formatter f = new Formatter();
+		assertNotNull(f);
+		assertTrue(f.out() instanceof StringBuilder);
+		assertEquals(f.locale(), Locale.getDefault());
+		assertNotNull(f.toString());
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(Appendable)
+	 */
+	public void test_ConstructorLjava_lang_Appendable() {
+		MockAppendable ma = new MockAppendable();
+		Formatter f1 = new Formatter(ma);
+		assertEquals(ma, f1.out());
+		assertEquals(f1.locale(), Locale.getDefault());
+		assertNotNull(f1.toString());
+
+		Formatter f2 = new Formatter((Appendable) null);
+		/*
+		 * If a(the input param) is null then a StringBuilder will be created
+		 * and the output can be attained by invoking the out() method. But RI
+		 * raises an error of FormatterClosedException when invoking out() or
+		 * toString().
+		 */
+		Appendable sb = f2.out();
+		assertTrue(sb instanceof StringBuilder);
+		assertNotNull(f2.toString());
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(Locale)
+	 */
+	public void test_ConstructorLjava_util_Locale() {
+		Formatter f1 = new Formatter(Locale.FRANCE);
+		assertTrue(f1.out() instanceof StringBuilder);
+		assertEquals(f1.locale(), Locale.FRANCE);
+		assertNotNull(f1.toString());
+
+		Formatter f2 = new Formatter((Locale) null);
+		assertNull(f2.locale());
+		assertTrue(f2.out() instanceof StringBuilder);
+		assertNotNull(f2.toString());
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(Appendable, Locale)
+	 */
+	public void test_ConstructorLjava_lang_AppendableLjava_util_Locale() {
+		MockAppendable ma = new MockAppendable();
+		Formatter f1 = new Formatter(ma, Locale.CANADA);
+		assertEquals(ma, f1.out());
+		assertEquals(f1.locale(), Locale.CANADA);
+
+		Formatter f2 = new Formatter(ma, null);
+		assertNull(f2.locale());
+		assertEquals(ma, f1.out());
+
+		Formatter f3 = new Formatter(null, Locale.GERMAN);
+		assertEquals(f3.locale(), Locale.GERMAN);
+		assertTrue(f3.out() instanceof StringBuilder);
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(String)
+	 */
+	public void test_ConstructorLjava_lang_String() throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((String) null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		try {
+			f = new Formatter("notexist");
+			assertEquals(f.locale(), Locale.getDefault());
+			f.close();
+		} catch (FileNotFoundException e) {
+			fail("File can not be created");
+		}
+
+		f = new Formatter(fileWithContent.getPath());
+		assertEquals(0, fileWithContent.length());
+
+		f.close();
+
+		try {
+			f = new Formatter(readOnly.getPath());
+			fail("FileNotFoundException is expected");
+		} catch (FileNotFoundException e) {
+			// expected
+		}
+
+		SecurityManager oldsm = System.getSecurityManager();
+		try {
+			System.setSecurityManager(new MockSecurityManager());
+			f = new Formatter(secret.getPath());
+		} catch (SecurityException se) {
+			// expected
+		} finally {
+			System.setSecurityManager(oldsm);
+		}
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(String, String)
+	 */
+	public void test_ConstructorLjava_lang_StringLjava_lang_String()
+			throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((String) null, Charset.defaultCharset().name());
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		try {
+			f = new Formatter("notexist", null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e2) {
+			// expected
+		}
+
+		f = new Formatter("notexist", Charset.defaultCharset().name());
+		assertEquals(f.locale(), Locale.getDefault());
+		f.close();
+
+		try {
+			f = new Formatter("notexist", "ISO 1111-1");
+			fail("should throw UnsupportedEncodingException");
+		} catch (UnsupportedEncodingException e1) {
+			// expected
+		}
+
+		f = new Formatter(fileWithContent.getPath(), "UTF-16BE");
+		assertEquals(0, fileWithContent.length());
+		f.close();
+
+		try {
+			f = new Formatter(readOnly.getPath(), "UTF-16BE");
+			fail("should throw FileNotFoundException");
+		} catch (FileNotFoundException e) {
+			// expected
+		}
+
+		SecurityManager oldsm = System.getSecurityManager();
+		try {
+			System.setSecurityManager(new MockSecurityManager());
+			f = new Formatter(secret.getPath(), "UTF-16BE");
+			fail("should throw UnsupportedEncodingException");
+		} catch (SecurityException se) {
+			// expected
+		} finally {
+			System.setSecurityManager(oldsm);
+		}
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(String, String, Locale)
+	 */
+	public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_util_Locale()
+			throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((String) null, Charset.defaultCharset().name(),
+					Locale.KOREA);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		try {
+			f = new Formatter("notexist", null, Locale.KOREA);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e2) {
+			// expected
+		}
+
+		f = new Formatter("notexist", Charset.defaultCharset().name(), null);
+		assertNotNull(f);
+		f.close();
+
+		f = new Formatter("notexist", Charset.defaultCharset().name(),
+				Locale.KOREA);
+		assertEquals(f.locale(), Locale.KOREA);
+		f.close();
+
+		try {
+			f = new Formatter("notexist", "ISO 1111-1", Locale.CHINA);
+			fail("should throw UnsupportedEncodingException");
+		} catch (UnsupportedEncodingException e1) {
+			// expected
+		}
+
+		f = new Formatter(fileWithContent.getPath(), "UTF-16BE",
+				Locale.CANADA_FRENCH);
+		assertEquals(0, fileWithContent.length());
+
+		f.close();
+
+		try {
+			f = new Formatter(readOnly.getPath(), Charset.defaultCharset()
+					.name(), Locale.ITALY);
+			fail("should throw FileNotFoundException");
+		} catch (FileNotFoundException e) {
+			// expected
+		}
+
+		SecurityManager oldsm = System.getSecurityManager();
+		try {
+			System.setSecurityManager(new MockSecurityManager());
+			f = new Formatter(secret.getPath(),
+					Charset.defaultCharset().name(), Locale.SIMPLIFIED_CHINESE);
+			fail("should throw SecurityException");
+		} catch (SecurityException se) {
+			// expected
+		} finally {
+			System.setSecurityManager(oldsm);
+		}
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(File)
+	 */
+	public void test_ConstructorLjava_io_File() throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((File) null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		f = new Formatter(notExist);
+		assertEquals(f.locale(), Locale.getDefault());
+		f.close();
+
+		f = new Formatter(fileWithContent);
+		assertEquals(0, fileWithContent.length());
+		f.close();
+
+		try {
+			f = new Formatter(readOnly);
+			fail("should throw FileNotFoundException");
+		} catch (FileNotFoundException e) {
+			// expected
+		}
+
+		SecurityManager oldsm = System.getSecurityManager();
+		try {
+			System.setSecurityManager(new MockSecurityManager());
+			f = new Formatter(secret);
+			fail("should throw SecurityException");
+		} catch (SecurityException se) {
+			// expected
+		} finally {
+			System.setSecurityManager(oldsm);
+		}
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(File, String)
+	 */
+	public void test_ConstructorLjava_io_FileLjava_lang_String()
+			throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((File) null, Charset.defaultCharset().name());
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		f = new Formatter(notExist, Charset.defaultCharset().name());
+		assertEquals(f.locale(), Locale.getDefault());
+		f.close();
+
+		f = new Formatter(fileWithContent, "UTF-16BE");
+		assertEquals(0, fileWithContent.length());
+		f.close();
+
+		try {
+			f = new Formatter(readOnly, Charset.defaultCharset().name());
+			fail("should throw FileNotFoundException");
+		} catch (FileNotFoundException e) {
+			// expected
+		}
+
+		SecurityManager oldsm = System.getSecurityManager();
+		try {
+			System.setSecurityManager(new MockSecurityManager());
+			f = new Formatter(secret, Charset.defaultCharset().name());
+			fail("should throw SecurityException");
+		} catch (SecurityException se) {
+			// expected
+		} finally {
+			System.setSecurityManager(oldsm);
+		}
+
+		try {
+			f = new Formatter(notExist, null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e2) {
+			// expected
+		} finally {
+			if (notExist.exists()) {
+				assertTrue(notExist.delete());
+				// Fail on RI on Windows, because output stream is created and
+				// not closed when exception thrown
+			}
+		}
+
+		try {
+			f = new Formatter(notExist, "ISO 1111-1");
+			fail("should throw UnsupportedEncodingException");
+		} catch (UnsupportedEncodingException e1) {
+			// expected
+		} finally {
+			if (notExist.exists()) {
+				assertTrue(notExist.delete());
+				// Fail on RI on Windows, because output stream is created and
+				// not closed when exception thrown
+			}
+		}
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(File, String, Locale)
+	 */
+	public void test_ConstructorLjava_io_FileLjava_lang_StringLjava_util_Locale()
+			throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((File) null, Charset.defaultCharset().name(),
+					Locale.KOREA);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		try {
+			f = new Formatter(notExist, null, Locale.KOREA);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e2) {
+			// expected
+		}
+		f = new Formatter(notExist, Charset.defaultCharset().name(), null);
+		assertNotNull(f);
+		f.close();
+
+		f = new Formatter(notExist, Charset.defaultCharset().name(),
+				Locale.KOREA);
+		assertEquals(f.locale(), Locale.KOREA);
+		f.close();
+
+		try {
+			f = new Formatter(notExist, "ISO 1111-1", Locale.CHINA);
+			fail("should throw UnsupportedEncodingException");
+		} catch (UnsupportedEncodingException e1) {
+			// expected
+		}
+		f = new Formatter(fileWithContent.getPath(), "UTF-16BE",
+				Locale.CANADA_FRENCH);
+		assertEquals(0, fileWithContent.length());
+
+		f.close();
+
+		try {
+			f = new Formatter(readOnly.getPath(), Charset.defaultCharset()
+					.name(), Locale.ITALY);
+			fail("should throw FileNotFoundException");
+		} catch (FileNotFoundException e) {
+			// expected
+		}
+
+		SecurityManager oldsm = System.getSecurityManager();
+		try {
+			System.setSecurityManager(new MockSecurityManager());
+			f = new Formatter(secret.getPath(),
+					Charset.defaultCharset().name(), Locale.SIMPLIFIED_CHINESE);
+			fail("should throw SecurityException");
+		} catch (SecurityException se) {
+			// expected
+		} finally {
+			System.setSecurityManager(oldsm);
+		}
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(PrintStream)
+	 */
+	public void test_ConstructorLjava_io_PrintStream() throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((PrintStream) null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		PrintStream ps = new PrintStream(notExist, "UTF-16BE");
+		f = new Formatter(ps);
+		assertEquals(Locale.getDefault(), f.locale());
+		f.close();
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(OutputStream)
+	 */
+	public void test_ConstructorLjava_io_OutputStream() throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((OutputStream) null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		OutputStream os = new FileOutputStream(notExist);
+		f = new Formatter(os);
+		assertEquals(Locale.getDefault(), f.locale());
+		f.close();
+	}
+
+	/**
+	 * @tests java.util.Formatter#Formatter(OutputStream, String)
+	 */
+	public void test_ConstructorLjava_io_OutputStreamLjava_lang_String()
+			throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((OutputStream) null, Charset.defaultCharset()
+					.name());
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		OutputStream os = null;
+		try {
+			os = new FileOutputStream(notExist);
+			f = new Formatter(os, null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e2) {
+			// expected
+		} finally {
+			os.close();
+		}
+
+		try {
+			os = new PipedOutputStream();
+			f = new Formatter(os, "TMP-1111");
+			fail("should throw UnsupportedEncodingException");
+		} catch (UnsupportedEncodingException e1) {
+			// expected
+		} finally {
+			os.close();
+		}
+
+		os = new FileOutputStream(fileWithContent);
+		f = new Formatter(os, "UTF-16BE");
+		assertEquals(Locale.getDefault(), f.locale());
+		f.close();
+	}
+
+	/**
+	 * Test method for 'java.util.Formatter.Formatter(OutputStream, String,
+	 * Locale)
+	 */
+	public void test_ConstructorLjava_io_OutputStreamLjava_lang_StringLjava_util_Locale()
+			throws IOException {
+		Formatter f = null;
+		try {
+			f = new Formatter((OutputStream) null, Charset.defaultCharset()
+					.name(), Locale.getDefault());
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e1) {
+			// expected
+		}
+
+		OutputStream os = null;
+		try {
+			os = new FileOutputStream(notExist);
+			f = new Formatter(os, null, Locale.getDefault());
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e2) {
+			// expected
+		} finally {
+			os.close();
+		}
+
+		os = new FileOutputStream(notExist);
+		f = new Formatter(os, Charset.defaultCharset().name(), null);
+		f.close();
+
+		try {
+			os = new PipedOutputStream();
+			f = new Formatter(os, "TMP-1111", Locale.getDefault());
+			fail("should throw UnsupportedEncodingException");
+		} catch (UnsupportedEncodingException e1) {
+			// expected
+		}
+
+		os = new FileOutputStream(fileWithContent);
+		f = new Formatter(os, "UTF-16BE", Locale.ENGLISH);
+		assertEquals(Locale.ENGLISH, f.locale());
+		f.close();
+	}
+
+	/**
+	 * @tests java.util.Formatter#locale()
+	 */
+	public void test_locale() {
+		Formatter f = null;
+		f = new Formatter((Locale) null);
+		assertNull(f.locale());
+
+		f.close();
+		try {
+			f.locale();
+			fail("should throw FormatterClosedException");
+		} catch (FormatterClosedException e) {
+			// expected
+		}
+	}
+
+	/**
+	 * @tests java.util.Formatter#out()
+	 */
+	public void test_out() {
+		Formatter f = null;
+		f = new Formatter();
+		assertNotNull(f.out());
+		assertTrue(f.out() instanceof StringBuilder);
+		f.close();
+		try {
+			f.out();
+			fail("should throw FormatterClosedException");
+		} catch (FormatterClosedException e) {
+			// expected
+		}
+
+	}
+
+	/**
+	 * @tests java.util.Formatter#flush()
+	 */
+	public void test_flush() throws IOException {
+		Formatter f = null;
+		f = new Formatter(notExist);
+		assertTrue(f instanceof Flushable);
+		f.close();
+		try {
+			f.flush();
+			fail("should throw FormatterClosedException");
+		} catch (FormatterClosedException e) {
+			// expected
+		}
+
+		// For destination that does not implement Flushable
+		f = new Formatter();
+		f.flush();
+		// No exception should be throw
+	}
+
+	/**
+	 * @tests java.util.Formatter#close()
+	 */
+	public void test_close() throws IOException {
+		Formatter f = new Formatter(notExist);
+		assertTrue(f instanceof Closeable);
+		f.close();
+		// close next time will not throw exception
+		f.close();
+		f.ioException();
+	}
+
+	/**
+	 * @tests java.util.Formatter#ioException()
+	 */
+	public void test_ioException() throws IOException {
+		Formatter f = null;
+		FileOutputStream fos;
+		fos = new FileOutputStream(notExist);
+		f = new Formatter(fos);
+		assertNull(f.ioException());
+		fos.close();
+		f.flush();
+		f.close();
+	}
+
+	/**
+	 * Setup resource files for testing
+	 */
+	protected void setUp() throws IOException {
+		notExist = new File("notexist");
+		if (notExist.exists()) {
+			notExist.delete();
+		}
+
+		fileWithContent = File.createTempFile("filewithcontent", null);
+		BufferedOutputStream bw = new BufferedOutputStream(
+				new FileOutputStream(fileWithContent));
+		bw.write(1);// write something into the file
+		bw.close();
+
+		readOnly = File.createTempFile("readonly", null);
+		readOnly.setReadOnly();
+
+		secret = File.createTempFile("secret", null);
+	}
+
+	/**
+	 * Delete the resource files if they exist
+	 */
+	protected void tearDown() {
+		if (notExist.exists()) {
+			notExist.delete();
+		}
+
+		if (fileWithContent.exists()) {
+			fileWithContent.delete();
+		}
+		if (readOnly.exists()) {
+			readOnly.delete();
+		}
+		if (secret.exists()) {
+			secret.delete();
+		}
+	}
+}
\ No newline at end of file

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message