Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0f3783e39a3778e091a0374efdb8efbdebf279a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.eclipse.jpt.jpadiagrameditor.swtbot.tests.internal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Semaphore;


/**
 * Utility for testing purposes
 * @author i057508
 *
 */
@SuppressWarnings({"restriction", "PMD"})
public class Utils {

	/**
	 * User space system variable key.
	 */
	private static Semaphore writeToConsolePermission = new Semaphore(1);
	static SimpleDateFormat sdf = new SimpleDateFormat(">> [HH:mm:ss.S] ");
	
	public final static String NEW_LINE = (System.getProperty("line.separator") != null  ? System.getProperty("line.separator") : "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$




	/** A constant for java validation error markers*/
	
	/**
	 * the plug-in ID of this plug-in
	 */
	public static final String COMMON_TEST_PLUGIN_ID = "com.sap.core.tools.eclipse.server.test.common";

	/**
	 * Issues a message into the system console into a friendly format. The format includes current time.
	 * The order is preserved, so if another request is issued in meantime, it will be processed consequently.
	 * At the end of the message no new line will be added.
	 * @param message the message to be issued
	 */
	public static void printFormatted(String message)  {
		try {
			writeToConsolePermission.acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.print(sdf.format(new Date()) + message);
		writeToConsolePermission.release();
	}

	/**
	 * Issues a message into the system console. The format includes current time.
	 * The order is preserved, so if another request is issued in meantime, it will be processed consequently.
	 * At the end of the message no new line will be added.
	 * @param message the message to be issued
	 */
	public static void print(String message)  {
		try {
			writeToConsolePermission.acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.print(message);
		writeToConsolePermission.release();
	}

	/**
	 * Issues a message into the system console into a friendly format. The format includes current time.
	 * The order is preserved, so if another request is issued in meantime, it will be processed consequently.
	 * At the end of the message a new line will be added.
	 * @param message the message to be issued
	 */
	public static void printlnFormatted(String message) {
		try {
			writeToConsolePermission.acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println(sdf.format(new Date()) + message);
		writeToConsolePermission.release();
	}

	/**
	 * Utility for printing into the system console
	 * @param message a message to be printed
	 * @param t an exception to be printed
	 */
	public static void printlnFormatted(String message, Throwable t) {
		StringBuilder buf = new StringBuilder();
		buf.append(message);

		if (t != null  && t.getStackTrace() != null) {
			buf.append(NEW_LINE);
			buf.append("Exception:").append(t.getMessage());
			buf.append(NEW_LINE);
			buf.append("Stacktrace:");
			buf.append(NEW_LINE);
			buf.append(traceToString(t.getStackTrace()));
		}

		printlnFormatted(buf.toString());
	}

	/**
	 * Issues a message into the system console. The format includes current time.
	 * The order is preserved, so if another request is issued in meantime, it will be processed consequently.
	 * At the end of the message a new line will be added.
	 * @param message the message to be issued
	 */
	public static void println(String message)  {
		try {
			writeToConsolePermission.acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(message);
		writeToConsolePermission.release();
	}

	/**
	 * Indicates a start of a test into the system console into a use friendly format.
	 * @param testName the name of the test.
	 */
	public static void sayTestStarted(String testName) {

		Utils.println("\n----------------------------------------------------------------------------");
		Utils.printlnFormatted("Test [" +  testName + "] started.");
		Utils.println("----------------------------------------------------------------------------");
	}

	/**
	 * Indicates an end of a test into the system console into a use friendly format.
	 * @param testName the name of the test.
	 */
	public static void sayTestFinished(String testName) {

		Utils.println("\n----------------------------------------------------------------------------");
		Utils.printlnFormatted("Test [" +  testName + "] finished.");
		Utils.println("----------------------------------------------------------------------------");
	}

	/**
	 * Converts stacktrace into a string
	 * @param trace the stacktrace
	 * @return the converted stacktrace
	 */
	public static String traceToString(StackTraceElement[] trace) {
		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < trace.length; i++) {
			builder.append(trace[i]).append("\n");
		}
		return builder.toString();
	}
}

Back to the top