Skip to content

Commit 55a082f

Browse files
committed
fix test broken during LocationInfo refactoring
1 parent d2f001d commit 55a082f

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/main/java/org/apache/log4j/spi/LocationInfo.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class LocationInfo implements java.io.Serializable {
4848
* All available caller information, in the format
4949
* <code>fully.qualified.classname.of.caller.methodName(Filename.java:line)</code>
5050
*/
51+
// ATTENTION: fullInfo is used to reconstruct the other fields post deserialization
5152
public String fullInfo;
5253

5354
/**
@@ -178,12 +179,33 @@ public LocationInfo(final String file, final String classname, final String meth
178179
this.fullInfo = buf.toString();
179180
}
180181

182+
181183
/**
182184
* Return the fully qualified class name of the caller making the logging
183185
* request.
184186
*/
185187
public String getClassName() {
186-
return className;
188+
if (fullInfo == null)
189+
return NA;
190+
if (className != null) {
191+
return className;
192+
} else {
193+
// Starting the search from '(' is safer because there is
194+
// potentially a dot between the parentheses.
195+
int iend = fullInfo.lastIndexOf('(');
196+
if (iend == -1)
197+
className = NA;
198+
else {
199+
iend = fullInfo.lastIndexOf('.', iend);
200+
int ibegin = 0;
201+
202+
if (iend == -1)
203+
className = NA;
204+
else
205+
className = this.fullInfo.substring(ibegin, iend);
206+
}
207+
return className;
208+
}
187209
}
188210

189211
/**
@@ -193,6 +215,18 @@ public String getClassName() {
193215
* This information is not always available.
194216
*/
195217
public String getFileName() {
218+
if (fullInfo == null)
219+
return NA;
220+
if (fileName == null) {
221+
int iend = fullInfo.lastIndexOf(':');
222+
if (iend == -1)
223+
fileName = NA;
224+
else {
225+
int ibegin = fullInfo.lastIndexOf('(', iend - 1);
226+
fileName = this.fullInfo.substring(ibegin + 1, iend);
227+
}
228+
}
229+
196230
return fileName;
197231
}
198232

@@ -203,13 +237,33 @@ public String getFileName() {
203237
* This information is not always available.
204238
*/
205239
public String getLineNumber() {
240+
if (fullInfo == null)
241+
return NA;
242+
if (lineNumber == null) {
243+
int iend = fullInfo.lastIndexOf(')');
244+
int ibegin = fullInfo.lastIndexOf(':', iend - 1);
245+
if (ibegin == -1)
246+
lineNumber = NA;
247+
else
248+
lineNumber = this.fullInfo.substring(ibegin + 1, iend);
249+
}
206250
return lineNumber;
207251
}
208252

209253
/**
210254
* Returns the method name of the caller.
211255
*/
212256
public String getMethodName() {
257+
if (fullInfo == null)
258+
return NA;
259+
if (methodName == null) {
260+
int iend = fullInfo.lastIndexOf('(');
261+
int ibegin = fullInfo.lastIndexOf('.', iend);
262+
if (ibegin == -1)
263+
methodName = NA;
264+
else
265+
methodName = this.fullInfo.substring(ibegin + 1, iend);
266+
}
213267
return methodName;
214268
}
215269
}

src/test/java/org/apache/log4j/net/ShortSocketServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
/**
3131
* This SocketServer exits after certain number of connections from a client.
32-
* This number is determined the totalsTest parameter, that is the first
32+
* This number is determined the totalTests parameter, that is the first
3333
* argument on the commmand line. The second argument, prefix, determines the
3434
* prefix of the configuration file to use. Each run of the server will use a
3535
* different properties file. For the i-th run, the path to the file is

src/test/java/org/apache/log4j/net/SocketServerTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,15 @@ public static Test suite() {
381381
String fileSeparator = System.getProperty("file.separator");
382382
String pathToJavaExecutable = javaHome + fileSeparator + "bin" + fileSeparator + "java";
383383
System.out.println("java executable assumed to be located at [" + pathToJavaExecutable + "]");
384+
// run ShortSocketServer
384385
ProcessBuilder processBuilder = new ProcessBuilder(pathToJavaExecutable, "-cp", claspath,
385386
shortSocketServerClassName, "8", TEST_INPUT_PREFIX + "socketServer");
386387
processBuilder.redirectErrorStream(true);
387388
processBuilder.inheritIO();
388389
System.out.println(shortSocketServerClassName);
389390
try {
390391
Process process = processBuilder.start();
391-
Thread.sleep(2000);
392+
Thread.sleep(1000);
392393
// List<String> results = readLines(process.getInputStream());
393394
// System.out.println(results);
394395

0 commit comments

Comments
 (0)