1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.text.tests.java.text; 19 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import junit.framework.TestCase; 26 27import java.text.AttributedCharacterIterator; 28import java.text.FieldPosition; 29import java.text.Format; 30import java.text.ParseException; 31import java.text.ParsePosition; 32 33 34@TestTargetClass(Format.class) 35public class FormatTest extends TestCase { 36 private class MockFormat extends Format { 37 38 public StringBuffer format(Object obj, StringBuffer toAppendTo, 39 FieldPosition pos) { 40 // it is a fake 41 if (obj == null) 42 throw new NullPointerException("obj is null"); 43 return new StringBuffer(""); 44 } 45 46 public Object parseObject(String source, ParsePosition pos) { 47 // it is a fake 48 return null; 49 } 50 } 51 52 /** 53 * @tests java.text.Format#format(Object) Test of method 54 * java.text.Format#format(Object). 55 */ 56 @TestTargetNew( 57 level = TestLevel.COMPLETE, 58 notes = "", 59 method = "Format", 60 args = {} 61 ) 62 public void test_Constructor() { 63 try { 64 new MockFormat(); 65 } catch (Exception e) { 66 fail("Unexpected exception " + e.toString()); 67 } 68 } 69 70 /** 71 * @tests java.text.Format#clone() Test of method java.text.Format#clone(). 72 * Compare of internal variables of cloned objects. 73 */ 74 @TestTargetNew( 75 level = TestLevel.COMPLETE, 76 notes = "", 77 method = "clone", 78 args = {} 79 ) 80 public void test_clone() { 81 try { 82 // Compare of internal variables of cloned objects 83 Format fm = new MockFormat(); 84 Format fmc = (Format) fm.clone(); 85 assertEquals(fm.getClass(), fmc.getClass()); 86 } catch (Exception e) { 87 fail("Unexpected exception " + e.toString()); 88 } 89 } 90 91 /** 92 * @tests java.text.Format#format(java.lang.Object) Test of method 93 * java.text.Format#format(java.lang.Object). 94 */ 95 @TestTargetNew( 96 level = TestLevel.COMPLETE, 97 notes = "Verifies that format(Object) calls format(Object, StringBuffer, FieldPosition) method.", 98 method = "format", 99 args = {java.lang.Object.class} 100 ) 101 public void test_formatLjava_lang_Object() { 102 103 MockFormat mf = new MockFormat(); 104 assertEquals("", mf.format("")); 105 assertTrue("It calls an abstract metod format", true); 106 } 107 108 /** 109 * @tests java.text.Format#formatToCharacterIterator(java.lang.Object) Test 110 * of method 111 * java.text.Format#formatToCharacterIterator(java.lang.Object). 112 */ 113 @TestTargetNew( 114 level = TestLevel.COMPLETE, 115 notes = "", 116 method = "formatToCharacterIterator", 117 args = {java.lang.Object.class} 118 ) 119 public void test_formatToCharacterIteratorLjava_lang_Object() { 120 121 MockFormat mf = new MockFormat(); 122 AttributedCharacterIterator aci = 123 mf.formatToCharacterIterator("Test 123 Test"); 124 125 assertEquals(0, aci.getBeginIndex()); 126 127 try { 128 mf.formatToCharacterIterator(null); 129 fail("NullPointerException was not thrown."); 130 } catch(NullPointerException npe) { 131 //expected 132 } 133 134 try { 135 mf.formatToCharacterIterator(""); 136 } catch(IllegalArgumentException iae) { 137 //expected 138 } 139 } 140 141 /** 142 * @tests java.text.Format#parseObject(java.lang.String source) Test of 143 * method java.text.Format#parseObject(java.lang.String source). 144 */ 145 @TestTargetNew( 146 level = TestLevel.COMPLETE, 147 notes = "Verifies that parseObject(String) method calls parseObject(String source, ParsePosition pos) method.", 148 method = "parseObject", 149 args = {java.lang.String.class} 150 ) 151 public void test_parseObjectLjava_lang_String() { 152 MockFormat mf = new MockFormat(); 153 try { 154 assertNull(mf.parseObject("")); 155 fail("ParseException was not thrown."); 156 } catch (ParseException e) { 157 //expected 158 } 159 } 160} 161