1 /*
2 * Copyright 2007 united internet (unitedinternet.com) Robert Zimmermann
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17 package com.unitedinternet.portal.selenium.utils.logging;
18
19 import java.io.BufferedWriter;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26
27 /**
28 * Utility Methods to support easy usage from an (junit4 driven) selenium test.
29 *
30 * @author Robert Zimmermann
31 *
32 * $Id: LoggingUtils.java 52 2008-02-05 19:58:00Z bobbyde $
33 */
34 public final class LoggingUtils {
35
36 private LoggingUtils() {
37 // static class only
38 }
39
40 /**
41 * Create an BufferedWriter handling encodings right. The writer is only created. Nothing will be written yet. Checks that the
42 * file to be written is writeable. Replacing existing files is configurable. Closing the writer has to taken care of on the
43 * caller side.
44 *
45 * @param resultFileNameAndPath location and filename where to write later
46 * @param resultEncoding any encoding supported by the used jdk. UTF-8 is recommended.
47 * @param replaceExistingFile if true try to replace an already existing file.
48 * @return BufferedWriter ready to be written to
49 * @throws RuntimeException if file already exists or file cannot be written or if an IOException occurs during writer
50 * creation
51 */
52 public static BufferedWriter createWriter(final String resultFileNameAndPath, final String resultEncoding,
53 boolean replaceExistingFile) throws RuntimeException {
54 BufferedWriter loggingWriter = null;
55 try {
56 // check early if we can create and write to this file
57 File resultFile = new File(resultFileNameAndPath);
58 if (replaceExistingFile && resultFile.exists()) {
59 resultFile.delete();
60 }
61 boolean newFileCreated = resultFile.createNewFile();
62 if (!newFileCreated) {
63 throw new RuntimeException("Failed to create new file: '"
64 + resultFileNameAndPath
65 + "'. Does this file already exist?");
66 }
67 // TODO: do we need this check? if the new file could be created previously we should be able to write to it
68 if (!resultFile.canWrite()) {
69 throw new RuntimeException("Cannot write to file: '" + resultFileNameAndPath + "'");
70 }
71 // http://www.jorendorff.com/articles/unicode/java.html
72 loggingWriter = new BufferedWriter(
73 new OutputStreamWriter(new FileOutputStream(resultFileNameAndPath), resultEncoding));
74 } catch (IOException ioExc) {
75 // try to close loggingWriter not needed as it could not be created here
76 throw new RuntimeException("ERROR while creating file: '" + resultFileNameAndPath + "'", ioExc);
77 }
78 return loggingWriter;
79 }
80
81 /**
82 * Create an BufferedWriter handling encodings right. The writer is only created. Nothing will be written yet. Checks that no
83 * such file already exists and the file to be written is writeable. Closing the writer has to taken care of on the caller
84 * side.
85 *
86 * @param resultFileNameAndPath location and filename where to write later
87 * @param resultEncoding any encoding supported by the used jdk. UTF-8 is recommended.
88 * @return BufferedWriter ready to be written to
89 * @throws RuntimeException if file already exists or file cannot be written or if an IOException occurs during writer
90 * creation
91 * @deprecated use the 3 parameters version instead
92 */
93 @Deprecated
94 public static BufferedWriter createWriter(final String resultFileNameAndPath, final String resultEncoding)
95 throws RuntimeException {
96 return createWriter(resultFileNameAndPath, resultEncoding, false);
97 }
98
99 /**
100 * Current date-time as string with format "yyyy-MM-dd_HH-mm". Example result: 2007-12-28_12-28
101 *
102 * @return fixed format "yyyy-MM-dd_HH-mm" date-time as string
103 */
104 public static String timeStampForFileName() {
105 return timeStampForFileName("yyyy-MM-dd_HH-mm");
106 }
107
108 /**
109 * Current date-time string with provided format.
110 *
111 * @see SimpleDateFormat
112 * @param simpleDateFormat string format to be used
113 * @return date-time as string in given format
114 */
115 public static String timeStampForFileName(final String simpleDateFormat) {
116 Date currentDateTime = new Date(System.currentTimeMillis());
117 SimpleDateFormat humanReadableFormat = new SimpleDateFormat(simpleDateFormat);
118 return humanReadableFormat.format(currentDateTime);
119 }
120
121 /**
122 * Presets the args array with default values if the are absent. creates if necessary: - new array - default values for args
123 * array as many as presetNumArgs TODO: there should be an utility method in eg. common.utils that does similar things
124 *
125 * @param loggingBean args array to be taken from here
126 * @param presetNumArgs number of array elements to be preset
127 * @param defaultValue default value to be used for missing array elements
128 * @return array containing previous values and filled with default ones - will be created if null previously
129 */
130 static String[] getCorrectedArgsArray(LoggingBean loggingBean, int presetNumArgs, String defaultValue) {
131 String[] currentArgs;
132 if (null == loggingBean || null == loggingBean.getArgs()) {
133 currentArgs = new String[] {};
134 } else {
135 currentArgs = loggingBean.getArgs();
136 }
137 String[] newArgs = new String[presetNumArgs];
138 for (int i = 0; i < currentArgs.length; i++) {
139 newArgs[i] = currentArgs[i];
140 }
141 for (int i = currentArgs.length; i < presetNumArgs; i++) {
142 newArgs[i] = defaultValue;
143 }
144 return newArgs;
145 }
146 }