KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > sync > SyncDeadlockTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi.sync;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import junit.framework.*;
26 import org.netbeans.modules.xml.axi.AXIComponent;
27 import org.netbeans.modules.xml.axi.AXIDocument;
28 import org.netbeans.modules.xml.schema.model.Schema;
29 import org.netbeans.modules.xml.schema.model.SchemaModel;
30
31 /**
32  * The unit test covers various use cases of sync on Element
33  * and ElementRef.
34  *
35  * @author Samaresh (Samaresh.Panda@Sun.Com)
36  */

37 public class SyncDeadlockTest extends AbstractSyncTestCase {
38     
39     public static final String JavaDoc TEST_XSD = "resources/po.xsd";
40     public static final String JavaDoc GLOBAL_ELEMENT = "purchaseOrder";
41     private boolean done = false;
42     
43     /**
44      * SyncElementTest
45      */

46     public SyncDeadlockTest(String JavaDoc testName) {
47     super(testName, TEST_XSD, GLOBAL_ELEMENT);
48     }
49     
50     public static Test suite() {
51     TestSuite suite = new TestSuite(SyncDeadlockTest.class);
52     return suite;
53     }
54     
55     public void testDeadlock() throws Exception JavaDoc {
56         final AXIDocument doc = getAXIModel().getRoot();
57         doc.addPropertyChangeListener(new AXIDocumentListener(doc));
58     SchemaModel sm = getAXIModel().getSchemaModel();
59     // This thread will run a sync on axiom
60
// this will be run when the schema model is known to be locked
61
// from the listener.
62
// The listener will wait until this thread is blocked
63
// then continue. will force the locking
64
Thread JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc() {
65        public void run() {
66            try {
67             doc.getModel().sync();
68            } catch (IOException JavaDoc ioe) {
69            fail();
70            }
71        }
72     });
73         sm.addPropertyChangeListener(new SchemaModelListener(t));
74         sm.startTransaction();
75         Schema schema = sm.getSchema();
76         schema.setTargetNamespace("http://xml.netbeans.org/schema/po");
77         sm.endTransaction();
78     t.join();
79         assertTrue("axi model event not fired", done);
80     }
81         
82     private class SchemaModelListener implements PropertyChangeListener JavaDoc {
83     private Thread JavaDoc t;
84     
85     public SchemaModelListener(Thread JavaDoc t) {
86         this.t = t;
87     }
88     
89     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
90             if(evt.getSource() instanceof Schema &&
91            evt.getPropertyName().equals(Schema.TARGET_NAMESPACE_PROPERTY)) {
92                 // we are now holding a lock on the schema model
93
t.start();
94         while (!t.getState().equals(Thread.State.BLOCKED)) {
95             try {
96             Thread.currentThread().sleep(50);
97             } catch (InterruptedException JavaDoc ex) {
98             
99             }
100         }
101         getAXIModel().isIntransaction();
102             }
103     }
104     }
105     
106     private class AXIDocumentListener implements PropertyChangeListener JavaDoc {
107         private AXIComponent source;
108         
109         public AXIDocumentListener(AXIComponent source) {
110             this.source = source;
111         }
112         
113     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
114             if(evt.getSource() == source && evt.getPropertyName().equals(AXIDocument.PROP_TARGET_NAMESPACE)) {
115                 String JavaDoc name = (String JavaDoc)evt.getNewValue();
116                 assertEquals("http://xml.netbeans.org/schema/po",name);
117                 done = true;
118             }
119     }
120     }
121 }
122
Popular Tags