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.util.logging; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.PrintStream; 22 import java.util.logging.ErrorManager; 23 import junit.framework.TestCase; 24 25 public class OldErrorManagerTest extends TestCase { 26 27 private final PrintStream err = System.err; 28 private final PrintStream out = System.out; 29 tearDown()30 public void tearDown() throws Exception{ 31 System.setErr(err); 32 System.setOut(out); 33 super.tearDown(); 34 } 35 test_errorCheck()36 public void test_errorCheck() { 37 ErrorManager em = new ErrorManager(); 38 MockStream aos = new MockStream(); 39 PrintStream st = new PrintStream(aos); 40 System.setErr(st); 41 System.setOut(st); 42 em.error("supertest", null, ErrorManager.GENERIC_FAILURE); 43 st.flush(); 44 assertTrue("message appears (supertest)", aos.getWrittenData().indexOf("supertest") != -1); 45 } 46 47 public class MockStream extends ByteArrayOutputStream { 48 49 private StringBuffer linesWritten = new StringBuffer(); 50 flush()51 public void flush() {} close()52 public void close() {} 53 54 @Override write(byte[] buffer)55 public void write(byte[] buffer) { 56 linesWritten.append(new String(buffer)); 57 } 58 59 @Override write(byte[] buffer, int offset, int len)60 public synchronized void write(byte[] buffer, int offset, int len) { 61 linesWritten.append(new String(buffer, offset, len)); 62 } 63 getWrittenData()64 public String getWrittenData() {return linesWritten.toString();} 65 } 66 } 67