2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 * I have removed the assure(...) functions from ComplexTestCase due to the fact now I can
23 * use the functions every where and don't need to be a ComplexTestCase any longer.
25 public class Assurance
27 /** Used to indicate that we should continue with test method, even if check fails */
28 public enum ContinueWithTest
{
32 /** State of the current test method **/
33 protected boolean bSuccessful
= true;
35 /** The message if the test did fail **/
36 protected String message
= null;
40 * Assure that s is true.
41 * This function generates "Assure failed." as standard message.
42 * @param s The condition that should be true.
44 protected void assure(boolean s
) {
45 assure("Assure failed.", s
, ContinueWithTest
.NO
);
49 * Assure that s is true.
50 * The given message will be only evaluated, if s is false.
51 * @param msg The message that is evaluated.
52 * @param s The condition that should be true.
54 protected void assure(String msg
, boolean s
) {
55 assure(msg
, s
, ContinueWithTest
.NO
);
59 * Assure that two int values are equal
60 * @param message the message to print when the equality test fails
61 * @param expected specifies the expected int value
62 * @param actual specifies the actual int value
64 protected void assureEquals( String message
, int expected
, int actual
) {
65 assureEquals( message
, Integer
.valueOf( expected
), Integer
.valueOf( actual
), ContinueWithTest
.NO
);
69 * Assure that two string values are equal
70 * @param message the message to print when the equality test fails
71 * @param expected specifies the expected string value
72 * @param actual specifies the actual string value
74 protected void assureEquals( String message
, String expected
, String actual
) {
75 assureEquals( message
, expected
, actual
, ContinueWithTest
.NO
);
79 * assures the two given sequences are of equal length, and have equal content
81 public <T
> void assureEquals( String i_message
, T
[] i_expected
, T
[] i_actual
, ContinueWithTest i_continue
)
83 if ( i_expected
.length
!= i_actual
.length
)
84 failed( i_message
+ ": expected element count: " + i_expected
.length
+ ", actual element count: " + i_actual
.length
);
85 for ( int i
=0; i
<i_expected
.length
; ++i
)
87 assureEquals( i_message
+ ": mismatch at element pos " + i
, i_expected
[i
], i_actual
[i
], i_continue
);
92 * Mark the currently executed method as failed.
93 * with the given message.
94 * @param msg The message of the failure.
96 protected void failed(String msg
) {
97 assure(msg
, false, ContinueWithTest
.NO
);
101 * Assure that s is true.
102 * The given message will be only evaluated, if s is false.
103 * Normally, assure() leaves the current test method, and the next one
104 * is executed. With the parameter 'cont' set to true, the current test
105 * method will continue.<br>
106 * The current method will of course marked as failed.
107 * @param msg The message that is evaluated.
108 * @param s The condition that should be true.
109 * @param cont if YES, continue with test method, even if s is false.
111 protected void assure(String msg
, boolean s
, ContinueWithTest cont
) {
114 message
+= msg
+ "\r\n";
115 if (cont
== ContinueWithTest
.NO
) {
116 throw new AssureException(msg
);
121 private void assureEquals( String message
, Object expected
, Object actual
, ContinueWithTest cont
) {
122 assure( message
+ " (expected: " + expected
.toString() + ", actual: " + actual
.toString() + ")",
123 expected
.equals( actual
), cont
);
127 * Mark the currently executed method as failed.
128 * with the given message.
129 * The given message will be only evaluated, if s is false.
130 * With the parameter 'cont' set to true, the current test
131 * method will continue.<br>
132 * The current method will of course marked as failed.
133 * @param msg The message that is evaluated.
134 * @param cont if YES, continue with test method, even if s is false.
136 protected void failed(String msg
, ContinueWithTest cont
) {
137 assure(msg
, false, cont
);
140 public static class AssureException
extends RuntimeException
{
142 private AssureException(String msg
) {