• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 libcore.java.lang;
18 
19 import junit.framework.TestCase;
20 
21 import java.io.BufferedWriter;
22 import java.io.ByteArrayOutputStream;
23 import java.io.PrintStream;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.Formatter;
27 
28 public class SystemTest extends TestCase {
29 
testLineSeparator()30     public void testLineSeparator() throws Exception {
31         try {
32             // Before Java 7, the small number of classes that wanted the line separator would
33             // use System.getProperty. Now they should use System.lineSeparator instead, and the
34             // "line.separator" property has no effect after the VM has started.
35 
36             // Test System.lineSeparator directly.
37             assertEquals("\n", System.lineSeparator());
38             System.setProperty("line.separator", "poop");
39             assertEquals("\n", System.lineSeparator());
40             assertFalse(System.lineSeparator().equals(System.getProperty("line.separator")));
41 
42             // java.io.BufferedWriter --- uses System.lineSeparator on Android but not on RI.
43             StringWriter sw = new StringWriter();
44             BufferedWriter bw = new BufferedWriter(sw);
45             bw.newLine();
46             bw.flush();
47             assertEquals(System.lineSeparator(), sw.toString());
48             assertFalse(System.lineSeparator().equals(System.getProperty("line.separator")));
49 
50             // java.io.PrintStream --- uses System.lineSeparator on Android but not on RI.
51             ByteArrayOutputStream baos = new ByteArrayOutputStream();
52             new PrintStream(baos).println();
53             assertEquals(System.lineSeparator(), new String(baos.toByteArray(), "UTF-8"));
54             assertFalse(System.lineSeparator().equals(System.getProperty("line.separator")));
55 
56             // java.io.PrintWriter --- uses System.lineSeparator on Android but not on RI.
57             sw = new StringWriter();
58             new PrintWriter(sw).println();
59             assertEquals(System.lineSeparator(), sw.toString());
60             assertFalse(System.lineSeparator().equals(System.getProperty("line.separator")));
61 
62             // java.util.Formatter --- uses System.lineSeparator on both.
63             assertEquals(System.lineSeparator(), new Formatter().format("%n").toString());
64             assertFalse(System.lineSeparator().equals(System.getProperty("line.separator")));
65         } finally {
66             System.setProperty("line.separator", "\n");
67         }
68     }
69 
testArrayCopyTargetNotArray()70     public void testArrayCopyTargetNotArray() {
71         try {
72             System.arraycopy(new char[5], 0, "Hello", 0, 3);
73             fail();
74         } catch (ArrayStoreException e) {
75             assertEquals("destination of type java.lang.String is not an array", e.getMessage());
76         }
77     }
78 
testArrayCopySourceNotArray()79     public void testArrayCopySourceNotArray() {
80         try {
81             System.arraycopy("Hello", 0, new char[5], 0, 3);
82             fail();
83         } catch (ArrayStoreException e) {
84             assertEquals("source of type java.lang.String is not an array", e.getMessage());
85         }
86     }
87 
testArrayCopyArrayTypeMismatch()88     public void testArrayCopyArrayTypeMismatch() {
89         try {
90             System.arraycopy(new char[5], 0, new Object[5], 0, 3);
91             fail();
92         } catch (ArrayStoreException e) {
93             assertEquals("char[] and java.lang.Object[] are incompatible array types", e.getMessage());
94         }
95     }
96 
testArrayCopyElementTypeMismatch()97     public void testArrayCopyElementTypeMismatch() {
98         try {
99             System.arraycopy(new Object[] { null, 5, "hello" }, 0,
100                     new Integer[] { 1, 2, 3, null, null }, 0, 3);
101             fail();
102         } catch (ArrayStoreException e) {
103             assertEquals("source[2] of type java.lang.String cannot be stored in destination array of type java.lang.Integer[]", e.getMessage());
104         }
105     }
106 
testArrayCopyNull()107     public void testArrayCopyNull() {
108         try {
109             System.arraycopy(null, 0, new char[5], 0, 3);
110             fail();
111         } catch (NullPointerException e) {
112             assertEquals("src == null", e.getMessage());
113         }
114         try {
115             System.arraycopy(new char[5], 0, null, 0, 3);
116             fail();
117         } catch (NullPointerException e) {
118             assertEquals("dst == null", e.getMessage());
119         }
120     }
121 }
122