KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbapi > DbMetaDataListenerImplTest


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.dbapi;
21
22 import java.net.URL JavaDoc;
23 import org.netbeans.api.db.explorer.DatabaseConnection;
24 import org.netbeans.api.db.explorer.JDBCDriver;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.modules.db.api.explorer.MetaDataListener;
27 import org.netbeans.modules.db.explorer.DbMetaDataListener;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileSystem;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.Repository;
32
33 /**
34  *
35  * @author Andrei Badea
36  */

37 public class DbMetaDataListenerImplTest extends NbTestCase {
38
39     public DbMetaDataListenerImplTest(String JavaDoc testName) {
40         super(testName);
41     }
42
43     /**
44      * Tests the registered listeners are invoked when the tableChanged and tablesChanged
45      * methods of DbMetaDataListenerImpl are invoked.
46      */

47     public void testListenerFired() throws Exception JavaDoc {
48         JDBCDriver driver = JDBCDriver.create("foo", "Foo", "org.example.Foo", new URL JavaDoc[0]);
49         DatabaseConnection dbconn = DatabaseConnection.create(driver, "url", "user", "schema", "pwd", false);
50
51         class TestListener implements MetaDataListener {
52
53             DatabaseConnection dbconn;
54             String JavaDoc tableName;
55
56             public void tablesChanged(DatabaseConnection dbconn) {
57                 this.dbconn = dbconn;
58             }
59
60             public void tableChanged(DatabaseConnection dbconn, String JavaDoc tableName) {
61                 this.dbconn = dbconn;
62                 this.tableName = tableName;
63             }
64         }
65
66         FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
67         FileObject listenersFO = FileUtil.createFolder(sfs.getRoot(), DbMetaDataListenerImpl.REFRESH_LISTENERS_PATH);
68         FileObject listenerFO = listenersFO.createData("TestListener", "instance");
69         TestListener listener = new TestListener();
70         listenerFO.setAttribute("instanceCreate", listener);
71
72         DbMetaDataListener dbListener = new DbMetaDataListenerImpl();
73
74         assertNull(listener.dbconn);
75         dbListener.tablesChanged(dbconn);
76         assertSame(dbconn, listener.dbconn);
77
78         listener.dbconn = null;
79         assertNull(listener.dbconn);
80         assertNull(listener.tableName);
81         dbListener.tableChanged(dbconn, "TABLE");
82         assertSame(dbconn, listener.dbconn);
83         assertEquals("TABLE", listener.tableName);
84     }
85 }
86
Popular Tags