Commit e915ab16 authored by matthias's avatar matthias

work on new tests load/save config

parent 9de04139
......@@ -38,15 +38,15 @@ public class ClientConfig extends ConfigParameters {
/* setter */
private void setResolutionWidth(int value) throws IllegalArgumentException {
if (value < 640) {
throw new IllegalArgumentException("width cant be < 640");
if (value < 360) {
throw new IllegalArgumentException("width cant be < 360");
}
this.resolutionWidth = value;
}
private void setResolutionHeight(int value) {
if (value < 400) {
throw new IllegalArgumentException("height cant be < 400");
if (value < 300) {
throw new IllegalArgumentException("height cant be < 300");
}
this.resolutionHeight = value;
}
......@@ -78,16 +78,10 @@ public class ClientConfig extends ConfigParameters {
}
private void setRcStandard(RcStandard value) throws IllegalArgumentException {
if (value != RcStandard.ICWS88 && value != RcStandard.ICWS94) {
throw new IllegalArgumentException("rcStandard must be icws88 or icws94");
}
this.rcStandard = value;
}
private void setLanguage(Language value) {
if (!value.equals(Language.de) && !value.equals(Language.en)) {
throw new IllegalArgumentException("not a correct language input variable!");
}
this.language = value;
}
......
......@@ -29,16 +29,13 @@ public class LoadConfigFile {
/* getter */
public ConfigParameters getConfigParameters() throws IllegalAccessException {
if (configParameters == null) {
throw new IllegalAccessException("configParameters must be set");
}
return configParameters;
}
/* class methods */
/**
* * loads a config pref file, transfer it into a intern ClientConfig object
* loads a config pref file, transfer it into a intern ClientConfig object
* @param defaultFilePath location of the default config file
* @param userFilePath location of the user config file
* @throws IOException if file not found
......
......@@ -83,19 +83,21 @@ public class SaveConfigFile {
private void saveServerConfig(ServerConfig params, String userFilePath, String nodeName)
throws IOException {
if (userFilePath.isEmpty()) {
throw new IllegalArgumentException("user file path should not be empty!");
}
File input = new File(userFilePath);
if (!input.exists()) {
input.getParentFile().mkdirs();
}
if (userFilePath.isEmpty()) {
throw new IllegalArgumentException("user file path should not be empty!");
}
Preferences userPrefs = Preferences.userRoot().node(nodeName);
userPrefs.putInt("MAX_START_INSTRUCTIONS_COUNT", params.getMaxStartInstructions());
userPrefs.putInt("MAX_PLAYER_COUNT", params.getMaxPlayersCount());
userPrefs.putInt("MAX_CYCLES", params.getCoreSize());
userPrefs.putInt("MAX_CYCLES", params.getMaxCycles());
userPrefs.putInt("CORE_SIZE", params.getCoreSize());
userPrefs.putInt("PORT", params.getPort());
userPrefs.putInt("WAIT_TIME_MS", params.getWaitTimeMs());
......
......@@ -4,19 +4,18 @@ import static org.junit.Assert.assertEquals;
import gone.lib.common.Language;
import gone.lib.common.RcStandard;
import gone.lib.config.ClientConfig;
import gone.lib.config.ConfigTypes;
import gone.lib.config.LoadConfigFile;
import gone.lib.config.SaveConfigFile;
import gone.lib.config.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
......@@ -25,7 +24,7 @@ import java.util.prefs.InvalidPreferencesFormatException;
*
* @author matthias
*/
public class ConfigIoTest {
public class ClientConfigTest {
// TODO: help function for less code redundance
......@@ -45,6 +44,9 @@ public class ConfigIoTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Before
public void saveFilesToHome() throws IOException {
random = new SecureRandom();
......@@ -130,4 +132,46 @@ public class ConfigIoTest {
assertEquals("ICWS88", loadCfgParams.getRcStandard().toString());
assertEquals("en", loadCfgParams.getLanguage().toString());
}
@Test
public void testSetWidth_Error() throws UnknownHostException {
thrown.expect(IllegalArgumentException.class);
new ClientConfig(-5, 400, "bernd", System.getProperty("user.home"),
InetAddress.getByName("localhost"), 4000, RcStandard.ICWS88, Language.de);
}
@Test
public void testSetHeight_Error() throws UnknownHostException {
thrown.expect(IllegalArgumentException.class);
new ClientConfig(640, 0, "bernd", System.getProperty("user.home"),
InetAddress.getByName("localhost"), 4000, RcStandard.ICWS88, Language.de);
}
@Test
public void testSetName_Error() throws UnknownHostException {
thrown.expect(IllegalArgumentException.class);
new ClientConfig(640, 400, "", System.getProperty("user.home"),
InetAddress.getByName("localhost"), 4000, RcStandard.ICWS88, Language.de);
}
@Test
public void testSetPath_Error() throws UnknownHostException {
thrown.expect(IllegalArgumentException.class);
new ClientConfig(640, 400, "bernd", "", InetAddress.getByName("localhost"),
4000, RcStandard.ICWS88, Language.de);
}
@Test
public void testSetPort_Error1() throws UnknownHostException {
thrown.expect(IllegalArgumentException.class);
new ClientConfig(640, 400, "bernd", System.getProperty("user.home"),
InetAddress.getByName("localhost"), -1, RcStandard.ICWS88, Language.de);
}
@Test
public void testSetPort_Error2() throws UnknownHostException {
thrown.expect(IllegalArgumentException.class);
new ClientConfig(640, 400, "bernd", "", InetAddress.getByName("localhost"), 999999,
RcStandard.ICWS88, Language.de);
}
}
\ No newline at end of file
package gone.lib.network.config;
import gone.lib.config.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import static org.junit.Assert.assertEquals;
/**
* @author matthias
*/
public class ServerConfigTest {
private SecureRandom random;
private ServerConfig defaultCfg;
private String defaultCfgS;
// help function to generate random strings (file name)
private String nextRandomString() {
return new BigInteger(130, random).toString(32);
}
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Before
public void saveFilesToHome() throws IOException {
random = new SecureRandom();
File defaultConfig = folder.newFile(nextRandomString() + ".xml");
defaultCfgS = defaultConfig.getAbsolutePath();
// create config params objects
defaultCfg = new ServerConfig(3000, 1000, 1337, 2, 8000, 100);
// save config files
new SaveConfigFile(defaultCfg, defaultCfgS, ConfigTypes.SERVER_CONFIG);
}
@Test
public void testCfgFileLoader_defaultClientConfig() throws IOException,
InvalidPreferencesFormatException,
IllegalAccessException, BackingStoreException {
LoadConfigFile loadCfg = new LoadConfigFile();
loadCfg.loadServerPrefs(defaultCfgS, ConfigTypes.SERVER_CONFIG);
ServerConfig loadCfgParams = (ServerConfig) loadCfg.getConfigParameters();
assertEquals(3000, loadCfgParams.getMaxCycles());
assertEquals(1000, loadCfgParams.getMaxStartInstructions());
assertEquals(1337, loadCfgParams.getPort());
assertEquals(2, loadCfgParams.getMaxPlayersCount());
assertEquals(8000, loadCfgParams.getCoreSize());
assertEquals(100, loadCfgParams.getWaitTimeMs());
}
@Test
public void testSetMaxCycles_Error() {
thrown.expect(IllegalArgumentException.class);
new ServerConfig(-5, 1000, 1337, 2, 8000, 100);
}
@Test
public void testSetMaxStartInstr_Error() {
thrown.expect(IllegalArgumentException.class);
new ServerConfig(3000, 0, 1337, 2, 8000, 100);
}
@Test
public void testSetPort_Error() {
thrown.expect(IllegalArgumentException.class);
new ServerConfig(3000, 1000, -1, 2, 8000, 100);
}
@Test
public void testSetMaxPlayers_Error() {
thrown.expect(IllegalArgumentException.class);
new ServerConfig(3000, 1000, 1337, 0, 8000, 100);
}
@Test
public void testSetCoreSize_Error() {
thrown.expect(IllegalArgumentException.class);
new ServerConfig(3000, 1000, 1337, 2, 0, 100);
}
@Test
public void testSetWaitTime_Error() {
thrown.expect(IllegalArgumentException.class);
new ServerConfig(3000, 1000, 1337, 2, 8000, 0);
}
@Test
public void saveServerConfig() throws IOException {
thrown.expect(IllegalArgumentException.class);
ServerConfig params = new ServerConfig(3000, 1000, 1337, 2, 8000, 100);
new SaveConfigFile(params, "", ConfigTypes.SERVER_CONFIG);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment