174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes/*
2994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more
3994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * contributor license agreements.  See the NOTICE file distributed with
4994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * this work for additional information regarding copyright ownership.
5994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0
6994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * (the "License"); you may not use this file except in compliance with
7994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * the License.  You may obtain a copy of the License at
874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes *
9994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
1074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes *
11994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * Unless required by applicable law or agreed to in writing, software
12994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
13994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * See the License for the specific language governing permissions and
15994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * limitations under the License.
16994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes */
17994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
18994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughespackage org.apache.harmony.tests.java.text;
19994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
20994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.text.AttributedCharacterIterator;
21994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.text.FieldPosition;
22994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.text.Format;
23994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.text.AttributedCharacterIterator.Attribute;
2474473971cc9d960376295fbcc430320c9ed62991Elliott Hughesimport java.util.Collections;
25994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.util.Iterator;
26994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.util.Vector;
27994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport junit.framework.TestCase;
28994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
29994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughespublic class Support_Format extends TestCase {
30994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
3174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  protected String text;
3274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
3374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  public Support_Format(String p1) {
3474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    super(p1);
3574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  }
3674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
3774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  protected void t_FormatWithField(int count, Format format, Object object,
3874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes                                   String text, Format.Field field, int begin, int end) {
3974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    StringBuffer buffer = new StringBuffer();
4074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    FieldPosition pos = new FieldPosition(field);
4174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    format.format(object, buffer, pos);
4274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
4374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    // System.out.println(buffer);
4474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    // System.out.println(pos);
4574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
4674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    if (text == null) {
4774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      assertEquals("Test " + count + ": incorrect formatted text", this.text, buffer.toString());
4874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    } else {
4974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      assertEquals(text, buffer.toString());
5074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
5174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
5274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    if (begin != pos.getBeginIndex() || end != pos.getEndIndex()) {
5374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      assertEquals(field + " " + begin + ".." + end,
5474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes                   pos.getFieldAttribute() + " " + pos.getBeginIndex() + ".." + pos.getEndIndex());
5574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
5674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  }
5774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
5874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  protected void t_Format(int count, Object object, Format format, Vector<FieldContainer> expectedResults) {
5974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    Vector<FieldContainer> results = findFields(format.formatToCharacterIterator(object));
6074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    assertEquals("size mismatch\n" +
6174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes                 format.format(object) + "\n" +
6274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes                 "expectedResults=" + expectedResults + "\n" +
6374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes                 "        results=" + results, expectedResults.size(), results.size());
6474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    for (int i = 0; i < results.size(); ++i) {
6574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      if (!results.contains(expectedResults.get(i))) {
6674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        fail("didn't find expected result " + expectedResults.get(i) + "\n" +
6774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes            "expectedResults=" + expectedResults + "\n" +
6874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes            "        results=" + results);
6974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      }
7074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
7174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  }
7274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
7374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  /**
7474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes   * finds attributes with regards to char index in this
7574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes   * AttributedCharacterIterator, and puts them in a vector
7674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes   *
7774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes   * @param iterator
7874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes   * @return a vector, each entry in this vector are of type FieldContainer,
7974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes   *       which stores start and end indexes and an attribute this range has
8074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes   */
8174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  protected static Vector<FieldContainer> findFields(AttributedCharacterIterator iterator) {
8274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    Vector<FieldContainer> result = new Vector<FieldContainer>();
8374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    while (iterator.getIndex() != iterator.getEndIndex()) {
8474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      int start = iterator.getRunStart();
8574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      int end = iterator.getRunLimit();
8674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
8774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      Iterator<Attribute> it = iterator.getAttributes().keySet().iterator();
8874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      while (it.hasNext()) {
8974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        AttributedCharacterIterator.Attribute attribute = it.next();
9074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        Object value = iterator.getAttribute(attribute);
9174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        result.add(new FieldContainer(start, end, attribute, value));
9274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        // System.out.println(start + " " + end + ": " + attribute + ",
9374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        // " + value );
9474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        // System.out.println("v.add(new FieldContainer(" + start +"," +
9574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        // end +"," + attribute+ "," + value+ "));");
9674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      }
9774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      iterator.setIndex(end);
9874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
9974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    return result;
10074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  }
10174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
10274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  static class FieldContainer {
10374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    final int start;
10474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    final int end;
10574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    final Attribute attribute;
10674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    final Object value;
10774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
10874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    // called from Support_DecimalFormat and Support_SimpleDateFormat tests
10974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    public FieldContainer(int start, int end, Attribute attribute) {
11074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      this(start, end, attribute, attribute);
11174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
11274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
11374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    // called from Support_MessageFormat tests
11474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    public FieldContainer(int start, int end, Attribute attribute, int value) {
11574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      this(start, end, attribute, new Integer(value));
11674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
11774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
11874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    // called from Support_MessageFormat tests
11974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    public FieldContainer(int start, int end, Attribute attribute, Object value) {
12074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      this.start = start;
12174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      this.end = end;
12274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      this.attribute = attribute;
12374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      this.value = value;
12474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
12574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
12674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    @Override public boolean equals(Object obj) {
12774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      if (!(obj instanceof FieldContainer)) {
12874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes        return false;
12974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      }
13074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
13174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      FieldContainer fc = (FieldContainer) obj;
13274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      return (start == fc.start && end == fc.end && attribute == fc.attribute && value.equals(fc.value));
13374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
13474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes
13574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    @Override public String toString() {
13674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes      return "FC[" + start + ".." + end + ",attribute=" + attribute + ",value=" + value + "]";
13774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    }
13874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  }
139994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes}
140