Home | History | Annotate | Download | only in text
      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 
     18 package libcore.java.text;
     19 
     20 import java.text.AttributedCharacterIterator;
     21 import java.text.FieldPosition;
     22 import java.text.Format;
     23 import java.text.ParseException;
     24 import java.text.ParsePosition;
     25 import junit.framework.TestCase;
     26 
     27 
     28 public class OldFormatTest extends TestCase {
     29     private class MockFormat extends Format {
     30 
     31         public StringBuffer format(Object obj, StringBuffer toAppendTo,
     32                 FieldPosition pos) {
     33             // it is a fake
     34             if (obj == null)
     35                 throw new NullPointerException("obj is null");
     36             return new StringBuffer("");
     37         }
     38 
     39         public Object parseObject(String source, ParsePosition pos) {
     40             // it is a fake
     41             return null;
     42         }
     43     }
     44 
     45     public void test_Constructor() {
     46         new MockFormat();
     47     }
     48 
     49     /**
     50      * java.text.Format#clone() Test of method java.text.Format#clone().
     51      *        Compare of internal variables of cloned objects.
     52      */
     53     public void test_clone() {
     54         // Compare of internal variables of cloned objects
     55         Format fm = new MockFormat();
     56         Format fmc = (Format) fm.clone();
     57         assertEquals(fm.getClass(), fmc.getClass());
     58     }
     59 
     60     public void test_formatLjava_lang_Object() {
     61         MockFormat mf = new MockFormat();
     62         assertEquals("", mf.format(""));
     63         assertTrue("It calls an abstract metod format", true);
     64     }
     65 
     66     public void test_formatToCharacterIteratorLjava_lang_Object() {
     67         MockFormat mf = new MockFormat();
     68         AttributedCharacterIterator aci = mf.formatToCharacterIterator("Test 123 Test");
     69 
     70         assertEquals(0, aci.getBeginIndex());
     71 
     72         try {
     73             mf.formatToCharacterIterator(null);
     74             fail("NullPointerException was not thrown.");
     75         } catch(NullPointerException npe) {
     76             //expected
     77         }
     78 
     79         try {
     80             mf.formatToCharacterIterator("");
     81         } catch(IllegalArgumentException  iae) {
     82             //expected
     83         }
     84     }
     85 
     86     public void test_parseObjectLjava_lang_String() {
     87         MockFormat mf = new MockFormat();
     88         try {
     89             assertNull(mf.parseObject(""));
     90             fail("ParseException was not thrown.");
     91         } catch (ParseException e) {
     92             //expected
     93         }
     94     }
     95 }
     96