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
17package libcore.java.util;
18
19import java.util.Random;
20
21public class RandomTest extends junit.framework.TestCase {
22    public void test_subclassing() throws Exception {
23        // http://b/2502231
24        // Ensure that Random's constructors call setSeed by emulating the active ingredient
25        // from the bug: the subclass' setSeed had a side-effect necessary for the correct
26        // functioning of next.
27        class MyRandom extends Random {
28            public String state;
29            public MyRandom() { super(); }
30            public MyRandom(long l) { super(l); }
31            @Override protected synchronized int next(int bits) { return state.length(); }
32            @Override public synchronized void setSeed(long seed) { state = Long.toString(seed); }
33        }
34        // Test the 0-argument constructor...
35        MyRandom r1 = new MyRandom();
36        r1.nextInt();
37        assertNotNull(r1.state);
38        // Test the 1-argument constructor...
39        MyRandom r2 = new MyRandom(123L);
40        r2.nextInt();
41        assertNotNull(r2.state);
42    }
43}
44