1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.util.cts; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.content.Context; 22 import android.support.test.InstrumentationRegistry; 23 import android.support.test.filters.SmallTest; 24 import android.support.test.runner.AndroidJUnit4; 25 import android.util.PrintWriterPrinter; 26 27 import org.junit.After; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.io.BufferedReader; 33 import java.io.File; 34 import java.io.FileInputStream; 35 import java.io.FileNotFoundException; 36 import java.io.IOException; 37 import java.io.InputStream; 38 import java.io.InputStreamReader; 39 import java.io.PrintWriter; 40 41 @SmallTest 42 @RunWith(AndroidJUnit4.class) 43 public class PrintWriterPrinterTest { 44 private File mFile; 45 46 @Before 47 public void setup() throws IOException { 48 File dbDir = InstrumentationRegistry.getTargetContext().getDir("tests", 49 Context.MODE_PRIVATE); 50 mFile = new File(dbDir,"print.log"); 51 if (!mFile.exists()) { 52 mFile.createNewFile(); 53 } 54 } 55 56 @After 57 public void teardown() throws Exception { 58 if (mFile.exists()) { 59 mFile.delete(); 60 } 61 } 62 63 @Test 64 public void testConstructor() throws FileNotFoundException { 65 PrintWriter pw = new PrintWriter(mFile); 66 new PrintWriterPrinter(pw); 67 } 68 69 @Test 70 public void testPrintln() throws FileNotFoundException { 71 String mMessage = "testMessage"; 72 PrintWriter pw = new PrintWriter(mFile); 73 PrintWriterPrinter printWriterPrinter = new PrintWriterPrinter(pw); 74 printWriterPrinter.println(mMessage); 75 pw.flush(); 76 pw.close(); 77 String mLine = ""; 78 try { 79 InputStream is = new FileInputStream(mFile); 80 BufferedReader reader = new BufferedReader( 81 new InputStreamReader(is)); 82 mLine = reader.readLine(); 83 } catch (Exception e) { 84 } 85 assertEquals(mMessage, mLine); 86 } 87 } 88 89