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 org.apache.harmony.tests.java.text;
     19 
     20 import java.text.AttributedCharacterIterator;
     21 import java.text.FieldPosition;
     22 import java.text.Format;
     23 import java.text.AttributedCharacterIterator.Attribute;
     24 import java.util.Collections;
     25 import java.util.Iterator;
     26 import java.util.Vector;
     27 import junit.framework.TestCase;
     28 
     29 public class Support_Format extends TestCase {
     30 
     31   protected String text;
     32 
     33   public Support_Format(String p1) {
     34     super(p1);
     35   }
     36 
     37   protected void t_FormatWithField(int count, Format format, Object object,
     38                                    String text, Format.Field field, int begin, int end) {
     39     StringBuffer buffer = new StringBuffer();
     40     FieldPosition pos = new FieldPosition(field);
     41     format.format(object, buffer, pos);
     42 
     43     // System.out.println(buffer);
     44     // System.out.println(pos);
     45 
     46     if (text == null) {
     47       assertEquals("Test " + count + ": incorrect formatted text", this.text, buffer.toString());
     48     } else {
     49       assertEquals(text, buffer.toString());
     50     }
     51 
     52     if (begin != pos.getBeginIndex() || end != pos.getEndIndex()) {
     53       assertEquals(field + " " + begin + ".." + end,
     54                    pos.getFieldAttribute() + " " + pos.getBeginIndex() + ".." + pos.getEndIndex());
     55     }
     56   }
     57 
     58   protected void t_Format(int count, Object object, Format format, Vector<FieldContainer> expectedResults) {
     59     Vector<FieldContainer> results = findFields(format.formatToCharacterIterator(object));
     60     assertEquals("size mismatch\n" +
     61                  format.format(object) + "\n" +
     62                  "expectedResults=" + expectedResults + "\n" +
     63                  "        results=" + results, expectedResults.size(), results.size());
     64     for (int i = 0; i < results.size(); ++i) {
     65       if (!results.contains(expectedResults.get(i))) {
     66         fail("didn't find expected result " + expectedResults.get(i) + "\n" +
     67             "expectedResults=" + expectedResults + "\n" +
     68             "        results=" + results);
     69       }
     70     }
     71   }
     72 
     73   /**
     74    * finds attributes with regards to char index in this
     75    * AttributedCharacterIterator, and puts them in a vector
     76    *
     77    * @param iterator
     78    * @return a vector, each entry in this vector are of type FieldContainer,
     79    *       which stores start and end indexes and an attribute this range has
     80    */
     81   protected static Vector<FieldContainer> findFields(AttributedCharacterIterator iterator) {
     82     Vector<FieldContainer> result = new Vector<FieldContainer>();
     83     while (iterator.getIndex() != iterator.getEndIndex()) {
     84       int start = iterator.getRunStart();
     85       int end = iterator.getRunLimit();
     86 
     87       Iterator<Attribute> it = iterator.getAttributes().keySet().iterator();
     88       while (it.hasNext()) {
     89         AttributedCharacterIterator.Attribute attribute = it.next();
     90         Object value = iterator.getAttribute(attribute);
     91         result.add(new FieldContainer(start, end, attribute, value));
     92         // System.out.println(start + " " + end + ": " + attribute + ",
     93         // " + value );
     94         // System.out.println("v.add(new FieldContainer(" + start +"," +
     95         // end +"," + attribute+ "," + value+ "));");
     96       }
     97       iterator.setIndex(end);
     98     }
     99     return result;
    100   }
    101 
    102   static class FieldContainer {
    103     final int start;
    104     final int end;
    105     final Attribute attribute;
    106     final Object value;
    107 
    108     // called from Support_DecimalFormat and Support_SimpleDateFormat tests
    109     public FieldContainer(int start, int end, Attribute attribute) {
    110       this(start, end, attribute, attribute);
    111     }
    112 
    113     // called from Support_MessageFormat tests
    114     public FieldContainer(int start, int end, Attribute attribute, int value) {
    115       this(start, end, attribute, new Integer(value));
    116     }
    117 
    118     // called from Support_MessageFormat tests
    119     public FieldContainer(int start, int end, Attribute attribute, Object value) {
    120       this.start = start;
    121       this.end = end;
    122       this.attribute = attribute;
    123       this.value = value;
    124     }
    125 
    126     @Override public boolean equals(Object obj) {
    127       if (!(obj instanceof FieldContainer)) {
    128         return false;
    129       }
    130 
    131       FieldContainer fc = (FieldContainer) obj;
    132       return (start == fc.start && end == fc.end && attribute == fc.attribute && value.equals(fc.value));
    133     }
    134 
    135     @Override public String toString() {
    136       return "FC[" + start + ".." + end + ",attribute=" + attribute + ",value=" + value + "]";
    137     }
    138   }
    139 }
    140