/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 tests.support.Support_Locale;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.Permission;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.DuplicateFormatFlagsException;
import java.util.FormatFlagsConversionMismatchException;
import java.util.Formattable;
import java.util.FormattableFlags;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.IllegalFormatCodePointException;
import java.util.IllegalFormatConversionException;
import java.util.IllegalFormatException;
import java.util.IllegalFormatFlagsException;
import java.util.IllegalFormatPrecisionException;
import java.util.IllegalFormatWidthException;
import java.util.Locale;
import java.util.MissingFormatArgumentException;
import java.util.MissingFormatWidthException;
import java.util.TimeZone;
import java.util.UnknownFormatConversionException;
import junit.framework.TestCase;
import dalvik.annotation.AndroidOnly;
import dalvik.annotation.BrokenTest;
import dalvik.annotation.KnownFailure;
import dalvik.annotation.TestLevel;
import dalvik.annotation.TestTargetClass;
import dalvik.annotation.TestTargetNew;
@TestTargetClass(Formatter.class)
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 checkPermission(Permission p) {
if (p.getActions().equals("write") && p instanceof FilePermission) {
throw new SecurityException("Always throw security exception");
}
}
public void checkPermission(Permission p, Object ctx) {
checkPermission(p);
}
}
class MockFormattable implements Formattable {
public void formatTo(Formatter formatter, int flags, int width,
int precision) throws IllegalFormatException {
if ((flags & FormattableFlags.UPPERCASE) != 0) {
formatter.format("CUSTOMIZED FORMAT FUNCTION" + " WIDTH: "
+ width + " PRECISION: " + precision);
} else {
formatter.format("customized format function" + " width: "
+ width + " precision: " + precision);
}
}
public String toString() {
return "formattable object";
}
public int hashCode() {
return 0xf;
}
}
class MockDestination implements Appendable, Flushable {
private StringBuilder data = new StringBuilder();
private boolean enabled = false;
public Appendable append(char c) throws IOException {
if (enabled) {
data.append(c);
enabled = true; // enable it after the first append
} else {
throw new IOException();
}
return this;
}
public Appendable append(CharSequence csq) throws IOException {
if (enabled) {
data.append(csq);
enabled = true; // enable it after the first append
} else {
throw new IOException();
}
return this;
}
public Appendable append(CharSequence csq, int start, int end)
throws IOException {
if (enabled) {
data.append(csq, start, end);
enabled = true; // enable it after the first append
} else {
throw new IOException();
}
return this;
}
public void flush() throws IOException {
throw new IOException("Always throw IOException");
}
public String toString() {
return data.toString();
}
}
private File notExist;
private File fileWithContent;
private File readOnly;
private File secret;
private TimeZone defaultTimeZone;
/**
* @tests java.util.Formatter#Formatter()
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "Formatter",
args = {}
)
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)
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "Formatter",
args = {java.lang.Appendable.class}
)
@AndroidOnly("the RI trows an exception that makes no sense. See comment.")
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)
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "Formatter",
args = {java.util.Locale.class}
)
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)
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "Formatter",
args = {java.lang.Appendable.class, java.util.Locale.class}
)
public