001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.harmony.luni.tests.java.io;
017:
018: import java.io.IOException;
019: import java.io.Writer;
020:
021: import junit.framework.TestCase;
022:
023: public class WriterTest extends TestCase {
024:
025: /**
026: * @tests java.io.Writer#append(char)
027: */
028: public void test_appendChar() throws IOException {
029: char testChar = ' ';
030: MockWriter writer = new MockWriter(20);
031: writer.append(testChar);
032: assertEquals(String.valueOf(testChar), String.valueOf(writer
033: .getContents()));
034: writer.close();
035: }
036:
037: /**
038: * @tests java.io.Writer#append(CharSequence)
039: */
040: public void test_appendCharSequence() throws IOException {
041: String testString = "My Test String";
042: MockWriter writer = new MockWriter(20);
043: writer.append(testString);
044: assertEquals(testString, String.valueOf(writer.getContents()));
045: writer.close();
046:
047: }
048:
049: /**
050: * @tests java.io.Writer#append(CharSequence, int, int)
051: */
052: public void test_appendCharSequenceIntInt() throws IOException {
053: String testString = "My Test String";
054: MockWriter writer = new MockWriter(20);
055: writer.append(testString, 1, 3);
056: assertEquals(testString.substring(1, 3), String.valueOf(writer
057: .getContents()));
058: writer.close();
059:
060: }
061:
062: /**
063: * @tests java.io.Writer#write(String)
064: */
065: public void test_writeLjava_lang_String() throws IOException {
066: // Regression for HARMONY-51
067: Object lock = new Object();
068: Writer wr = new MockLockWriter(lock);
069: // FIXME This test should be added to the exclusion list until
070: // Thread.holdsLock works on IBM VME
071: // wr.write("Some string");
072: wr.close();
073: }
074:
075: class MockLockWriter extends Writer {
076: final Object myLock;
077:
078: MockLockWriter(Object lock) {
079: super (lock);
080: myLock = lock;
081: }
082:
083: @Override
084: public synchronized void close() throws IOException {
085: // do nothing
086: }
087:
088: @Override
089: public synchronized void flush() throws IOException {
090: // do nothing
091: }
092:
093: @Override
094: public void write(char[] arg0, int arg1, int arg2)
095: throws IOException {
096: assertTrue(Thread.holdsLock(myLock));
097: }
098: }
099:
100: class MockWriter extends Writer {
101: private char[] contents;
102:
103: private int length;
104:
105: private int offset;
106:
107: MockWriter(int capacity) {
108: contents = new char[capacity];
109: length = capacity;
110: offset = 0;
111: }
112:
113: public synchronized void close() throws IOException {
114: flush();
115: contents = null;
116: }
117:
118: public synchronized void flush() throws IOException {
119: // do nothing
120: }
121:
122: public void write(char[] buffer, int offset, int count)
123: throws IOException {
124: if (null == contents) {
125: throw new IOException();
126: }
127: if (offset < 0 || count < 0 || offset >= buffer.length) {
128: throw new IndexOutOfBoundsException();
129: }
130: count = Math.min(count, buffer.length - offset);
131: count = Math.min(count, this .length - this .offset);
132: for (int i = 0; i < count; i++) {
133: contents[this .offset + i] = buffer[offset + i];
134: }
135: this .offset += count;
136:
137: }
138:
139: public char[] getContents() {
140: char[] result = new char[offset];
141: for (int i = 0; i < offset; i++) {
142: result[i] = contents[i];
143: }
144: return result;
145: }
146: }
147:
148: }
|