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