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 libcore.java.util.logging;
19
20import java.util.logging.Level;
21import java.util.logging.LogRecord;
22import junit.framework.TestCase;
23
24public class OldLogRecordTest extends TestCase {
25
26    static final String MSG = "test msg, pls. ignore itb";
27
28    private LogRecord lr = new LogRecord(Level.CONFIG, MSG);
29
30    public void testGetSetTimeCheck() {
31        long before = lr.getMillis();
32        try {
33            Thread.sleep(2);
34        } catch (InterruptedException e) {
35            e.printStackTrace();
36        }
37        LogRecord lr2 = new LogRecord(Level.CONFIG, "MSG2");
38        long after = lr2.getMillis();
39        assertTrue(after-before>0);
40    }
41
42    public void testGetSetLevelNormal() {
43        assertSame(lr.getLevel(), Level.CONFIG);
44        lr.setLevel(Level.ALL);
45        assertSame(lr.getLevel(), Level.ALL);
46        lr.setLevel(Level.FINEST);
47        assertSame(lr.getLevel(), Level.FINEST);
48    }
49
50    public void testGetSetThreadID_DifferentThread() {
51        lr.getThreadID();
52        // Create and start the thread
53        MockThread thread = new MockThread();
54        thread.start();
55        try {
56            thread.join();
57        } catch (InterruptedException e) {
58            e.printStackTrace();
59        }
60        // Create and start the thread2
61        MockThread thread2 = new MockThread();
62        thread2.start();
63        try {
64            thread2.join();
65        } catch (InterruptedException e) {
66            e.printStackTrace();
67        }
68
69        //All threadID must be different, based on the ThreadLocal.java ID
70        assertTrue(lr.getThreadID() != thread.lr.getThreadID());
71        assertTrue(lr.getThreadID() != thread2.lr.getThreadID());
72        assertTrue(thread.lr.getThreadID() != thread2.lr.getThreadID());
73    }
74
75    public class MockThread extends Thread {
76        public LogRecord lr = null; //will be update by the thread
77
78        public void run() {
79            update();
80        }
81
82        public synchronized void update(){
83            lr = new LogRecord(Level.CONFIG, "msg thread");
84        }
85    }
86}
87