com.gemstone.gemfire.distributed.internal.StartupMessageDataJUnitTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-junit Show documentation
Show all versions of gemfire-junit Show documentation
SnappyData store based off Pivotal GemFireXD
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire.distributed.internal;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import com.gemstone.gemfire.DataSerializer;
import com.gemstone.gemfire.internal.SocketCreator;
import com.gemstone.gemfire.internal.ByteArrayData;
import com.gemstone.gemfire.internal.admin.remote.DistributionLocatorId;
import junit.framework.TestCase;
/**
* Tests {@link StartupMessageData}.
*
* @author Kirk Lund
* @since 7.0
*/
public class StartupMessageDataJUnitTest extends TestCase {
public StartupMessageDataJUnitTest(String name) {
super(name);
}
public void testSupportedVersion() throws Exception {
try {
@SuppressWarnings("unused")
StartupMessageData data = new StartupMessageData(
null, StartupMessageData.SUPPORTED_VERSION);
fail("Supported version should have thrown NPE for null DataInput.");
} catch (NullPointerException expected) {
// passed
}
}
// compatibility with 6.6.2 is no longer needed. If this is needed
// in the future we need to change to using the new versioned data
// streams
// public void testUnsupportedVersion() throws Exception {
// try {
// @SuppressWarnings("unused")
// StartupMessageData data = new StartupMessageData(
// null, "6.6.2");
// // passed
// } catch (NullPointerException e) {
// fail("Unsupported version should simply ignore null DataInput.");
// }
// }
public void testWriteHostedLocatorsWithEmpty() throws Exception {
Collection hostedLocators = new ArrayList();
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
assertTrue(data.getOptionalFields().isEmpty());
}
public void testWriteHostedLocatorsWithNull() throws Exception {
Collection hostedLocators = null;
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
assertTrue(data.getOptionalFields().isEmpty());
}
public void testWriteHostedLocatorsWithOne() throws Exception {
String locatorString = createOneLocatorString();
List hostedLocators = new ArrayList();
hostedLocators.add(locatorString);
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
assertEquals(1, data.getOptionalFields().size());
assertEquals(locatorString, data.getOptionalFields().get(StartupMessageData.HOSTED_LOCATORS));
}
public void testWriteHostedLocatorsWithThree() throws Exception {
String[] locatorStrings = createManyLocatorStrings(3);
List hostedLocators = new ArrayList();
for (int i = 0; i < 3; i++) {
hostedLocators.add(locatorStrings[i]);
}
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
assertEquals(1, data.getOptionalFields().size());
String hostedLocatorsField =
data.getOptionalFields().getProperty(StartupMessageData.HOSTED_LOCATORS);
StringTokenizer st = new StringTokenizer(
hostedLocatorsField, StartupMessageData.COMMA_DELIMITER);
for (int i = 0; st.hasMoreTokens(); i++) {
assertEquals(locatorStrings[i], st.nextToken());
}
}
public void testReadHostedLocatorsWithThree() throws Exception {
// set up the data
String[] locatorStrings = createManyLocatorStrings(3);
List hostedLocators = new ArrayList();
for (int i = 0; i < 3; i++) {
hostedLocators.add(locatorStrings[i]);
}
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
assertEquals(1, data.getOptionalFields().size());
// test readHostedLocators
int i = 0;
Collection readLocatorStrings = data.readHostedLocators();
assertEquals(3, readLocatorStrings.size());
for (String readLocatorString : readLocatorStrings) {
assertEquals(locatorStrings[i], readLocatorString);
i++;
}
}
public void testToDataWithEmptyHostedLocators() throws Exception {
Collection hostedLocators = new ArrayList();
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
ByteArrayData testStream = new ByteArrayData();
assertTrue(testStream.isEmpty());
DataOutputStream out = testStream.getDataOutput();
data.toData(out);
assertTrue(testStream.size() > 0);
DataInput in = testStream.getDataInput();
Properties props = (Properties) DataSerializer.readObject(in);
assertNull(props);
}
public void testToDataWithNullHostedLocators() throws Exception {
Collection hostedLocators = null;
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
ByteArrayData testStream = new ByteArrayData();
assertTrue(testStream.isEmpty());
DataOutputStream out = testStream.getDataOutput();
data.toData(out);
assertTrue(testStream.size() > 0);
DataInput in = testStream.getDataInput();
Properties props = (Properties) DataSerializer.readObject(in);
assertNull(props);
}
public void testToDataWithOneHostedLocator() throws Exception {
String locatorString = createOneLocatorString();
List hostedLocators = new ArrayList();
hostedLocators.add(locatorString);
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
ByteArrayData testStream = new ByteArrayData();
assertTrue(testStream.isEmpty());
DataOutputStream out = testStream.getDataOutput();
data.toData(out);
assertTrue(testStream.size() > 0);
DataInput in = testStream.getDataInput();
Properties props = (Properties) DataSerializer.readObject(in);
assertNotNull(props);
String hostedLocatorsString = props.getProperty(StartupMessageData.HOSTED_LOCATORS);
assertNotNull(hostedLocatorsString);
assertEquals(locatorString, hostedLocatorsString);
}
public void testToDataWithThreeHostedLocators() throws Exception {
String[] locatorStrings = createManyLocatorStrings(3);
List hostedLocators = new ArrayList();
for (int i = 0; i < 3; i++) {
hostedLocators.add(locatorStrings[i]);
}
StartupMessageData data = new StartupMessageData();
data.writeHostedLocators(hostedLocators);
ByteArrayData testStream = new ByteArrayData();
assertTrue(testStream.isEmpty());
DataOutputStream out = testStream.getDataOutput();
data.toData(out);
assertTrue(testStream.size() > 0);
DataInput in = testStream.getDataInput();
Properties props = (Properties) DataSerializer.readObject(in);
assertNotNull(props);
String hostedLocatorsString = props.getProperty(
StartupMessageData.HOSTED_LOCATORS);
assertNotNull(hostedLocatorsString);
Collection actualLocatorStrings = new ArrayList(1);
StringTokenizer st = new StringTokenizer(hostedLocatorsString, StartupMessageData.COMMA_DELIMITER);
while (st.hasMoreTokens()) {
actualLocatorStrings.add(st.nextToken());
}
assertEquals(3, actualLocatorStrings.size());
int i = 0;
for (String actualLocatorString : actualLocatorStrings) {
assertEquals(locatorStrings[i], actualLocatorString);
i++;
}
}
public void testNullHostedLocator() throws Exception {
String locatorString = null;
DataInput in = getDataInputWithOneHostedLocator(locatorString);
StartupMessageData dataToRead = new StartupMessageData(
in, StartupMessageData.SUPPORTED_VERSION);
Collection readHostedLocators = dataToRead.readHostedLocators();
assertNull(readHostedLocators);
}
public void testEmptyHostedLocator() throws Exception {
String locatorString = "";
DataInput in = getDataInputWithOneHostedLocator(locatorString);
StartupMessageData dataToRead = new StartupMessageData(
in, StartupMessageData.SUPPORTED_VERSION);
Collection readHostedLocators = dataToRead.readHostedLocators();
assertNull(readHostedLocators);
}
public void testOneHostedLocator() throws Exception {
String locatorString = createOneLocatorString();
DataInput in = getDataInputWithOneHostedLocator(locatorString);
StartupMessageData dataToRead = new StartupMessageData(
in, StartupMessageData.SUPPORTED_VERSION);
Collection readHostedLocators = dataToRead.readHostedLocators();
assertNotNull(readHostedLocators);
assertEquals(1, readHostedLocators.size());
assertEquals(locatorString, readHostedLocators.iterator().next());
}
private String createOneLocatorString() throws Exception {
DistributionLocatorId locatorId = new DistributionLocatorId(
SocketCreator.getLocalHost(),
445566,
"111.222.333.444",
null);
String locatorString = locatorId.marshal();
assertEquals("" + locatorId.getHost().getHostAddress()
+ ":111.222.333.444[445566]", locatorString);
return locatorString;
}
private String[] createManyLocatorStrings(int n) throws Exception {
String[] locatorStrings = new String[3];
for (int i = 0; i < 3; i++) {
int j = i + 1;
int k = j + 1;
int l = k + 1;
DistributionLocatorId locatorId = new DistributionLocatorId(
SocketCreator.getLocalHost(),
445566,
""+i+""+i+""+i+"."+j+""+j+""+j+"."+k+""+k+""+k+"."+l+""+l+""+l,
null);
locatorStrings[i] = locatorId.marshal();
}
return locatorStrings;
}
private DataInput getDataInputWithOneHostedLocator(String locatorString) throws Exception {
List hostedLocators = new ArrayList();
if (locatorString != null) {
hostedLocators.add(locatorString);
}
StartupMessageData dataToWrite = new StartupMessageData();
dataToWrite.writeHostedLocators(hostedLocators);
ByteArrayData testStream = new ByteArrayData();
assertTrue(testStream.isEmpty());
DataOutputStream out = testStream.getDataOutput();
dataToWrite.toData(out);
assertTrue(testStream.size() > 0);
DataInput in = testStream.getDataInput();
assertNotNull(in);
return in;
}
}