throw EOFException when using empty files with ValueServer in replay and digest modes

JIRA: MATH-185

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@617850 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-02-02 18:01:29 +00:00
parent 812b7db174
commit 0152e67e9e
5 changed files with 47 additions and 3 deletions

View File

@ -17,6 +17,7 @@
package org.apache.commons.math.random;
import java.io.EOFException;
import java.io.Serializable;
import java.io.BufferedReader;
import java.io.FileReader;
@ -131,6 +132,9 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
} catch (Exception e) {
throw new IOException(e.getMessage());
}
if (sampleStats.getN() == 0) {
throw new EOFException("URL " + url + " contains no data");
}
in = new BufferedReader(new InputStreamReader(url.openStream()));
fillBinStats(in);
loaded = true;

View File

@ -17,6 +17,7 @@
package org.apache.commons.math.random;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
@ -308,7 +309,9 @@ public class ValueServer {
* replay file open.</p>
* <p>
* The <code>valuesFileURL</code> will be closed and reopened to wrap around
* from EOF to BOF if EOF is encountered.</p>
* from EOF to BOF if EOF is encountered. EOFException (which is a kind of
* IOException) may still be thrown if the <code>valuesFileURL</code> is
* empty.</p>
*
* @return next value from the replay file
* @throws IOException if there is a problem reading from the file
@ -321,9 +324,12 @@ public class ValueServer {
resetReplayFile();
}
if ((str = filePointer.readLine()) == null) {
// we have probably reached end of file, wrap around from EOF to BOF
closeReplayFile();
resetReplayFile();
str = filePointer.readLine();
if ((str = filePointer.readLine()) == null) {
throw new EOFException("URL " + valuesFileURL + " contains no data");
}
}
return Double.valueOf(str).doubleValue();
}

View File

@ -18,6 +18,8 @@ package org.apache.commons.math.random;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.EOFException;
import java.net.URL;
import org.apache.commons.math.RetryTestCase;
@ -98,7 +100,35 @@ public final class ValueServerTest extends RetryTestCase {
fail("Expecting IllegalStateException");
} catch (IllegalStateException ex) {;}
}
public void testEmptyReplayFile() {
try {
URL url = getClass().getResource("emptyFile.txt");
vs.setMode(ValueServer.REPLAY_MODE);
vs.setValuesFileURL(url);
vs.getNext();
fail("an exception should have been thrown");
} catch (EOFException eof) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
}
}
public void testEmptyDigestFile() {
try {
URL url = getClass().getResource("emptyFile.txt");
vs.setMode(ValueServer.DIGEST_MODE);
vs.setValuesFileURL(url);
vs.computeDistribution();
fail("an exception should have been thrown");
} catch (EOFException eof) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
}
}
/**
* Test ValueServer REPLAY_MODE using values in testData file.<br>
* Check that the values 1,2,1001,1002 match data file values 1 and 2.

View File

@ -139,6 +139,10 @@ Commons Math Release Notes</title>
<action dev="luc" type="fix" issue="MATH-182">
Add integer overflow checks in Fraction constructor using double parameter.
</action>
<action dev="luc" type="fix" issue="MATH-185">
Throw EOFException when using empty files with ValueServer in replay and
digest modes.
</action>
</release>
<release version="1.1" date="2005-12-17"
description="This is a maintenance release containing bug fixes and enhancements.