1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH München (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo;
24  
25  import java.util.ResourceBundle;
26  
27  import junit.framework.TestCase;
28  
29  /***
30   * Base class for all domingo test cases.
31   *
32   * @author MarcusT
33   */
34  public abstract class BaseProxyTest extends TestCase {
35  
36      private static ResourceBundle myResources = ResourceBundle.getBundle("de.bea.domingo.test");
37  
38      private static final String SERVER_URL = myResources.getString("test.server");
39      private static final String USERNAME = myResources.getString("test.username");
40      private static final String PASSWORD = myResources.getString("test.password");
41  
42      private DNotesFactory factory;
43      private DSession session;
44      private DDatabase logDatabase;
45      private String serverName = "";
46  
47      /***
48       * @param name the name of the test
49       */
50      public BaseProxyTest(String name) {
51          super(name);
52      }
53  
54      /***
55       * Sets up the test class.
56       */
57      protected final void baseSetUp() {
58          try {
59              factory = DNotesFactory.getInstance();
60              if (isAssignableFrom(factory, "de.bea.domingo.http.NotesHttpFactory")) {
61                  session = factory.getSession(SERVER_URL, USERNAME, PASSWORD);
62                  logDatabase = session.getDatabase("", "log.nsf");
63              } else {
64                  session = factory.getSession();
65                  logDatabase = session.getDatabase(serverName, "log.nsf");
66              }
67          } catch (Exception e) {
68              e.printStackTrace();
69              fail(e.getMessage());
70          }
71      }
72  
73      /***
74       * Dynamic checks if an object is an instance of a class given by its name.
75       *
76       * @param object the object to check
77       * @param className the class name to check against
78       * @return <code>true</code> if the object is an instance of the specified class, else <code>false</code>
79       */
80      private boolean isAssignableFrom(final Object object, final String className) {
81          if (object == null) {
82              return false;
83          }
84          try {
85              return object.getClass().isAssignableFrom(Class.forName(className));
86          } catch (ClassNotFoundException e) {
87              return false;
88          }
89      }
90  
91      /***
92       * Returns the current session.
93       *
94       * @return session
95       */
96      protected final DSession getSession() {
97          return session;
98      }
99  
100     /***
101      * Returns a log database of the specified server to test with.
102      *
103      * @return the log database
104      */
105     protected final DDatabase getDatabase() {
106         return logDatabase;
107     }
108 
109     /***
110      * Returns the name of the server to test with.
111      *
112      * @return server name
113      */
114     protected final String getServerName() {
115         return serverName;
116     }
117 }