001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov
019: * @version $Revision$
020: */package org.apache.harmony.awt.wtk;
021:
022: import junit.framework.TestCase;
023:
024: public class SynchronizerTest extends TestCase {
025:
026: class ConcreteSynchronizer extends Synchronizer {
027: protected boolean isDispatchThread() {
028: return false;
029: }
030: }
031:
032: Synchronizer s;
033:
034: public static void main(String[] args) {
035: junit.textui.TestRunner.run(SynchronizerTest.class);
036: }
037:
038: @Override
039: protected void setUp() throws Exception {
040: super .setUp();
041:
042: s = new ConcreteSynchronizer();
043: }
044:
045: public void testLockUnlock() {
046: s.lock();
047: s.lock();
048: s.unlock();
049: s.unlock();
050:
051: boolean unlocked = false;
052: try {
053: s.unlock();
054: } catch (Throwable t) {
055: unlocked = true;
056: }
057: assertTrue(unlocked);
058: }
059:
060: public void testStoreStateAndUnlockAllLockAndRestoreState() {
061: boolean free = false;
062: try {
063: s.storeStateAndFree();
064: } catch (Throwable t) {
065: free = true;
066: }
067: assertTrue(free);
068:
069: s.lock();
070: s.lock();
071: s.storeStateAndFree();
072:
073: boolean secondTime = false;
074: try {
075: s.storeStateAndFree();
076: } catch (Throwable t) {
077: secondTime = true;
078: }
079: assertTrue(secondTime);
080:
081: boolean locked = false;
082: try {
083: s.lock();
084: s.lockAndRestoreState();
085: } catch (Throwable t) {
086: locked = true;
087: s.unlock();
088: }
089: assertTrue(locked);
090:
091: s.lockAndRestoreState();
092:
093: boolean notStored = false;
094: try {
095: s.lockAndRestoreState();
096: } catch (Throwable t) {
097: notStored = true;
098: }
099: assertTrue(notStored);
100:
101: s.unlock();
102: s.unlock();
103: }
104:
105: // Regression test for HARMONY-3601
106: public void testHarmony_3601() throws Exception {
107: final Thread[] threads = new Thread[10];
108: final boolean[] errFlag = { false };
109:
110: for (int i = 0; i < threads.length; i++) {
111: threads[i] = new Thread() {
112: public void run() {
113: try {
114: java.awt.Toolkit.getDefaultToolkit()
115: .createImage(
116: new java.net.URL(
117: "file://any/thing"));
118: } catch (Throwable t) {
119: errFlag[0] = true;
120: fail(t.getMessage());
121: }
122: }
123: };
124: }
125:
126: for (int i = 0; i < threads.length; i++) {
127: threads[i].start();
128: }
129:
130: for (int i = 0; i < threads.length; i++) {
131: threads[i].join();
132: }
133:
134: assertFalse("Exception in child thread", errFlag[0]);
135: }
136: }
|