001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_Diagnosticable
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derby.iapi.services.context.ContextService;
025: import org.apache.derby.iapi.services.diag.Diagnosticable;
026: import org.apache.derby.iapi.services.diag.DiagnosticUtil;
027: import org.apache.derby.iapi.services.monitor.Monitor;
028: import org.apache.derby.iapi.services.sanity.SanityManager;
029: import org.apache.derby.iapi.error.StandardException;
030: import org.apache.derby.iapi.store.access.AccessFactory;
031: import org.apache.derby.iapi.store.access.ConglomerateController;
032: import org.apache.derby.iapi.store.access.TransactionController;
033: import org.apache.derby.iapi.store.raw.ContainerHandle;
034: import org.apache.derby.iapi.store.raw.ContainerKey;
035: import org.apache.derby.iapi.store.raw.Page;
036: import org.apache.derby.iapi.store.raw.Transaction;
037: import org.apache.derby.iapi.store.raw.RawStoreFactory;
038:
039: import org.apache.derbyTesting.unitTests.harness.T_MultiIterations;
040: import org.apache.derbyTesting.unitTests.harness.T_Fail;
041: import org.apache.derby.iapi.reference.Property;
042:
043: import java.util.Properties;
044:
045: // DEBUGGING:
046:
047: /**
048:
049: This T_Diagnosticable class provides a sample of how to use the "Diagnostic"
050: facility. The classes methods are built to be called by a "values" or
051: a "call" statement from "ij". Eventually there will be some sort of
052: diagnostic monitor which will be used to call the various "D_*" routines.
053:
054: **/
055:
056: public class T_Diagnosticable extends T_MultiIterations {
057: private static final String testService = "DiagnosticableTest";
058:
059: /* Constructors for This class: */
060:
061: /**
062: * No arg Constructor.
063: **/
064: public T_Diagnosticable() {
065: }
066:
067: /* Private/Protected methods of This class: */
068:
069: /**
070: * Simple test of DiagnosticUtil interfaces.
071: * <p>
072: * Simple test of DiagnosticUtil.toDiagString() and
073: * DiagnosticUtil.findDiagnostic() interfaces.
074: *
075: * @exception T_Fail If test fails for some reason.
076: **/
077: private void t_001() throws T_Fail {
078: // Create object with also has a diagnostic interface:
079: Object diag_obj = new T_DiagTestClass1(
080: "object with diag interface");
081:
082: // Create an object in a sub-class that doesn't have a D_ class, but
083: // its super-class does.
084: Object diagSubObj = new T_DiagTestClass1Sub("sub-class");
085:
086: // Create object with neither Diagnosticable:
087: Object obj = new Long(5);
088:
089: // Test just getting a single string back, from each type of object.
090: String str = null;
091: String expected_str = null;
092: Diagnosticable helper_class = null;
093:
094: // Here the string should come from the Diagnostic object's diag().
095: str = DiagnosticUtil.toDiagString(diag_obj);
096: expected_str = "D_T_DiagTestClass1: object with diag interface";
097:
098: if (str.compareTo(expected_str) != 0) {
099: throw T_Fail
100: .testFailMsg("DiagnosticUtil.toDiagString() failed, got: ("
101: + str
102: + "), expected: ("
103: + expected_str
104: + ").");
105: }
106:
107: // make sure right class was found.
108:
109: helper_class = DiagnosticUtil.findDiagnostic(diag_obj);
110:
111: if (!(helper_class instanceof D_T_DiagTestClass1))
112: throw T_Fail.testFailMsg("Bad helper class lookup.");
113:
114: // make sure helper class gives right string.
115:
116: try {
117: str = helper_class.diag();
118: } catch (Throwable t) {
119: throw T_Fail
120: .testFailMsg("Unexpected exception from helper_class.diag() call");
121: }
122:
123: if (!str.equals(expected_str)) {
124: throw T_Fail
125: .testFailMsg("DiagnosticUtil.toDiagString() failed, got: ("
126: + str
127: + "), expected: ("
128: + expected_str
129: + ").");
130: }
131:
132: // make sure the Diagnostic class picks up a super-version of the D_ class
133: str = DiagnosticUtil.toDiagString(diagSubObj);
134: expected_str = "D_T_DiagTestClass1: sub-class";
135: if (!str.equals(expected_str)) {
136: throw T_Fail
137: .testFailMsg("DiagnosticUtil.toDiagString() failed, got: ("
138: + str
139: + "), expected: ("
140: + expected_str
141: + ").");
142: }
143:
144: // Here the string should just be the system's default toString.
145: str = DiagnosticUtil.toDiagString(obj);
146: expected_str = "5";
147:
148: if (str.compareTo(expected_str) != 0) {
149: throw T_Fail
150: .testFailMsg("DiagnosticUtil.toDiagString() failed, got: ("
151: + str
152: + "), expected: ("
153: + expected_str
154: + ").");
155: }
156:
157: // check that lookup for this class return correctly returns null,
158: // since help class does not exist.
159: helper_class = DiagnosticUtil.findDiagnostic(obj);
160:
161: if (helper_class != null)
162: throw T_Fail
163: .testFailMsg("Bad helper class - should be null.");
164: }
165:
166: /* Public Methods of T_MultiIterations class: */
167:
168: /**
169: * Routine one once per invocation of the test by the driver.
170: * <p>
171: * Do work that should only be done once, no matter how many times
172: * runTests() may be executed.
173: *
174: * @exception T_Fail Thrown on any error.
175: **/
176: protected void setupTest() throws T_Fail {
177: // don't automatic boot this service if it gets left around
178: if (startParams == null) {
179: startParams = new Properties();
180: }
181: startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
182: // remove the service directory to ensure a clean run
183: startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE
184: .toString());
185: }
186:
187: /*
188: ** Methods required by T_Generic
189: */
190:
191: public String getModuleToTestProtocolName() {
192: return ("org.apache.derby.iapi.services.diag.DiagnosticUtil");
193: }
194:
195: /**
196: * Driver routine for the btree secondary index tests.
197: * <p>
198: *
199: * @exception T_Fail Throws T_Fail on any test failure.
200: **/
201: protected void runTestSet() throws T_Fail {
202: out.println("Executing " + testService + " test.");
203:
204: t_001();
205:
206: out.println("Finished Executing " + testService + " test.");
207: }
208: }
|