diff options
Diffstat (limited to 'org.eclipse.osee.ote.core.test/src/org/eclipse/osee')
30 files changed, 3789 insertions, 0 deletions
diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/OTEApiTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/OTEApiTest.java new file mode 100644 index 000000000..7662b3e70 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/OTEApiTest.java @@ -0,0 +1,223 @@ +package org.eclipse.osee.ote; + +import java.io.IOException; +import java.net.URL; +import java.util.concurrent.Future; +import org.eclipse.osee.framework.jdk.core.util.ChecksumUtil; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; + +@SuppressWarnings("restriction") +public class OTEApiTest { + + private OTEApi oteApi; + + @Before + public void setup() { + BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); + ServiceReference<OTEApi> ref = context.getServiceReference(OTEApi.class); + Assert.assertNotNull(ref); + oteApi = context.getService(ref); + Assert.assertNotNull(oteApi); + } + + @Test + public void testConfigurationLoading() throws IOException, Exception { + clearJarCache(); + URL bundle1 = findEntry("data/loading.test1.jar"); + URL bundle2 = findEntry("data/loading.test2.jar"); + URL bundle3 = findEntry("data/loading.test3.jar"); + ConfigurationItem config1 = + new ConfigurationItem(bundle1.toString(), "1.0.0", "loading.test1", ChecksumUtil.createChecksumAsString( + bundle1.openStream(), "MD5"), true); + ConfigurationItem config2 = + new ConfigurationItem(bundle2.toString(), "1.0.0", "loading.test2", ChecksumUtil.createChecksumAsString( + bundle2.openStream(), "MD5"), true); + ConfigurationItem config3 = + new ConfigurationItem(bundle3.toString(), "1.0.0", "loading.test3", ChecksumUtil.createChecksumAsString( + bundle3.openStream(), "MD5"), true); + + Configuration validConfiguration = new Configuration(); + validConfiguration.addItem(config1); + validConfiguration.addItem(config2); + validConfiguration.addItem(config3); + + OTEStatusCallback<ConfigurationStatus> callable = new OTEStatusCallbackForTests<>(); + + Future<ConfigurationStatus> statusFuture = oteApi.loadConfiguration(validConfiguration, callable); + ConfigurationStatus status = statusFuture.get(); + Assert.assertTrue(status.isSuccess()); + Assert.assertEquals(validConfiguration, status.getConfiguration()); + Assert.assertEquals(validConfiguration, oteApi.getConfiguration().get().getConfiguration()); + + Bundle bundle = findBundle("loading.test1"); + Assert.assertNotNull(bundle); + Assert.assertEquals(Bundle.ACTIVE, bundle.getState()); + + bundle = findBundle("loading.test2"); + Assert.assertNotNull(bundle); + Assert.assertEquals(Bundle.ACTIVE, bundle.getState()); + + bundle = findBundle("loading.test3"); + Assert.assertNotNull(bundle); + Assert.assertEquals(Bundle.ACTIVE, bundle.getState()); + + Future<ConfigurationStatus> resetFeature = oteApi.resetConfiguration(callable); + ConfigurationStatus resetStatus = resetFeature.get(); + Assert.assertTrue(resetStatus.isSuccess()); + Assert.assertNull(findBundle("loading.test1")); + Assert.assertNull(findBundle("loading.test2")); + Assert.assertNull(findBundle("loading.test3")); + + Configuration invalidConfiguration = new Configuration(); + invalidConfiguration.addItem(config1); + invalidConfiguration.addItem(config3); + Future<ConfigurationStatus> failStatusFuture = oteApi.loadConfiguration(invalidConfiguration, callable); + ConfigurationStatus failStatus = failStatusFuture.get(); + Assert.assertFalse(failStatus.isSuccess()); + System.out.println(failStatus.getMessage()); + Assert.assertNull(findBundle("loading.test1")); + Assert.assertNull(findBundle("loading.test2")); + Assert.assertNull(findBundle("loading.test3")); + + //test the doing load case + Future<ConfigurationStatus> good = oteApi.loadConfiguration(validConfiguration, callable); + Future<ConfigurationStatus> bad = oteApi.loadConfiguration(validConfiguration, callable); + + ConfigurationStatus goodStatus = good.get(); + ConfigurationStatus badStatus = bad.get(); + Assert.assertTrue(goodStatus.isSuccess()); + Assert.assertFalse(badStatus.isSuccess()); + System.out.println(badStatus.getMessage()); + + //test already configured + bad = oteApi.loadConfiguration(validConfiguration, callable); + badStatus = bad.get(); + Assert.assertFalse(badStatus.isSuccess()); + System.out.println(badStatus.getMessage()); + } + + @Test + public void testJarAcquire() throws IOException, Exception { + clearJarCache(); + URL bundle1 = findEntry("data/loading.test1.jar"); + URL bundle2 = findEntry("data/loading.test2.jar"); + URL bundle3 = findEntry("data/loading.test3.jar"); + ConfigurationItem config1 = + new ConfigurationItem(bundle1.toString(), "1.0.0", "loading.test1", ChecksumUtil.createChecksumAsString( + bundle1.openStream(), "MD5"), true); + ConfigurationItem config2 = + new ConfigurationItem(bundle2.toString(), "1.0.0", "loading.test2", ChecksumUtil.createChecksumAsString( + bundle2.openStream(), "MD5"), true); + ConfigurationItem config3 = + new ConfigurationItem(bundle3.toString(), "1.0.0", "loading.test3", ChecksumUtil.createChecksumAsString( + bundle3.openStream(), "MD5"), true); + + Configuration validConfiguration = new Configuration(); + validConfiguration.addItem(config1); + validConfiguration.addItem(config2); + validConfiguration.addItem(config3); + + OTEStatusCallback<ConfigurationStatus> callable = new OTEStatusCallbackForTests<>(); + + Future<ConfigurationStatus> statusFuture = oteApi.downloadConfigurationJars(validConfiguration, callable); + ConfigurationStatus status = statusFuture.get(); + Assert.assertTrue(status.isSuccess()); + OTEServerRuntimeCache cache = oteApi.getRuntimeCache(); + Assert.assertNotNull(cache.get(config1.getSymbolicName(), config1.getMd5Digest())); + Assert.assertNotNull(cache.get(config2.getSymbolicName(), config2.getMd5Digest())); + Assert.assertNotNull(cache.get(config3.getSymbolicName(), config3.getMd5Digest())); + + Assert.assertNull(cache.get(config1.getSymbolicName(), config2.getMd5Digest())); + Assert.assertNull(cache.get(config2.getSymbolicName(), config3.getMd5Digest())); + Assert.assertNull(cache.get(config3.getSymbolicName(), config1.getMd5Digest())); + + //test failure to download + clearJarCache(); + ConfigurationItem config4 = + new ConfigurationItem("file://this/file/does/not/exist/2351.jar", "1.0.0", "loading.test11", + ChecksumUtil.createChecksumAsString(bundle1.openStream(), "MD5"), true); + Configuration invalidConfiguration = new Configuration(); + invalidConfiguration.addItem(config4); + statusFuture = oteApi.downloadConfigurationJars(invalidConfiguration, callable); + status = statusFuture.get(); + Assert.assertFalse(status.isSuccess()); + + // Assert.assertEquals(validConfiguration, status.getConfiguration()); + // Assert.assertEquals(validConfiguration, oteApi.getConfiguration().get().getConfiguration()); + + // Bundle bundle = findBundle("loading.test1"); + // Assert.assertNotNull(bundle); + // Assert.assertEquals(Bundle.ACTIVE, bundle.getState()); + // + // bundle = findBundle("loading.test2"); + // Assert.assertNotNull(bundle); + // Assert.assertEquals(Bundle.ACTIVE, bundle.getState()); + // + // bundle = findBundle("loading.test3"); + // Assert.assertNotNull(bundle); + // Assert.assertEquals(Bundle.ACTIVE, bundle.getState()); + + // Future<ConfigurationStatus> resetFeature = oteApi.resetConfiguration(callable); + // ConfigurationStatus resetStatus = resetFeature.get(); + // Assert.assertTrue(resetStatus.isSuccess()); + // Assert.assertNull(findBundle("loading.test1")); + // Assert.assertNull(findBundle("loading.test2")); + // Assert.assertNull(findBundle("loading.test3")); + + // Configuration invalidConfiguration = new Configuration(); + // invalidConfiguration.addItem(config1); + // invalidConfiguration.addItem(config3); + // Future<ConfigurationStatus> failStatusFuture = oteApi.loadConfiguration(invalidConfiguration, callable); + // ConfigurationStatus failStatus = failStatusFuture.get(); + // Assert.assertFalse(failStatus.isSuccess()); + // System.out.println(failStatus.getMessage()); + // Assert.assertNull(findBundle("loading.test1")); + // Assert.assertNull(findBundle("loading.test2")); + // Assert.assertNull(findBundle("loading.test3")); + // + // //test the doing load case + // Future<ConfigurationStatus> good = oteApi.loadConfiguration(validConfiguration, callable); + // Future<ConfigurationStatus> bad = oteApi.loadConfiguration(validConfiguration, callable); + // + // ConfigurationStatus goodStatus = good.get(); + // ConfigurationStatus badStatus = bad.get(); + // Assert.assertTrue(goodStatus.isSuccess()); + // Assert.assertFalse(badStatus.isSuccess()); + // System.out.println(badStatus.getMessage()); + // + // //test already configured + // bad = oteApi.loadConfiguration(validConfiguration, callable); + // badStatus = bad.get(); + // Assert.assertFalse(badStatus.isSuccess()); + // System.out.println(badStatus.getMessage()); + } + + private void clearJarCache() { + oteApi.getRuntimeCache().clearJarCache(); + } + + private Bundle findBundle(String symbolicName) { + Bundle[] bundles = FrameworkUtil.getBundle(OTEApiTest.class).getBundleContext().getBundles(); + for (Bundle bundle : bundles) { + if (bundle.getSymbolicName().equals(symbolicName)) { + return bundle; + } + } + return null; + } + + private URL findEntry(String path) { + URL url = null; + Bundle bundle = FrameworkUtil.getBundle(OTEApiTest.class); + url = bundle.getEntry(path); + Assert.assertNotNull(url); + return url; + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/OTEStatusCallbackForTests.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/OTEStatusCallbackForTests.java new file mode 100644 index 000000000..73d78edae --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/OTEStatusCallbackForTests.java @@ -0,0 +1,37 @@ +package org.eclipse.osee.ote; + + +public class OTEStatusCallbackForTests<T> implements OTEStatusCallback<ConfigurationStatus> { + + @Override + public void complete(ConfigurationStatus done) { + System.out.println("done"); + } + + @Override + public void setTotalUnitsOfWork(int totalUnitsOfWork) { + System.out.println("units " + totalUnitsOfWork); + } + + @Override + public void incrememtUnitsWorked(int count) { + System.out.println("units " + count); + } + + @Override + public void log(String string) { + System.out.println(string); + } + + @Override + public void error(String message, Throwable th) { + System.out.println(message); + th.printStackTrace(); + } + + @Override + public void error(String message) { + System.out.println(message); + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/XmlSupportTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/XmlSupportTest.java new file mode 100644 index 000000000..026bd918f --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/XmlSupportTest.java @@ -0,0 +1,36 @@ +/* + * Created on Jun 10, 2013 + * + * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE + */ +package org.eclipse.osee.ote.core; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Michael P. Masterson + */ +public class XmlSupportTest { + + /** + * Test method for {@link org.eclipse.osee.ote.core.XmlSupport#convertNonPrintableCharacers(java.lang.String)}. + */ + @Test + public void testConvertXmlCharacers() { + String testString = "abc\n\r\0\t 123\f&<>"; + String expected = "abc\n\r[ASCII=0]\t 123[ASCII=12][ampersand][less-than][greater-than]"; + String actual = XmlSupport.convertXmlCharacters(testString); + Assert.assertEquals(expected, actual); + } + + @Test + public void testNonPrintableCharacter() { + String testString = "abc\n\r\0\t 123\f&<>"; + String expected = "abc\n\r[ASCII=0]\t 123[ASCII=12]&<>"; + + String actual = XmlSupport.convertNonPrintableCharacers(testString); + Assert.assertEquals(expected, actual); + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/environment/status/msg/SerializedMessageTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/environment/status/msg/SerializedMessageTest.java new file mode 100644 index 000000000..ef03ddbf5 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/environment/status/msg/SerializedMessageTest.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.core.environment.status.msg; + +import java.io.IOException; + +import org.junit.Assert; + +import org.eclipse.osee.ote.core.environment.status.EnvironmentError; +import org.junit.Test; + +public class SerializedMessageTest { + + @Test + public void testErrorMessage() throws IOException, ClassNotFoundException { + String exMessage = "MY EX TEST"; + EnvironmentError error = new EnvironmentError(new Exception(exMessage)); + EnvErrorMessage msg = new EnvErrorMessage(); + msg.setObject(error); + EnvironmentError errorBack = msg.getObject(); + Assert.assertEquals(errorBack.getErr().getMessage(), exMessage); + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/log/record/PropertyStoreRecordTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/log/record/PropertyStoreRecordTest.java new file mode 100644 index 000000000..730a4bbc0 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/core/log/record/PropertyStoreRecordTest.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.core.log.record; + +import java.io.StringWriter; +import java.io.Writer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import junit.framework.TestCase; +import org.eclipse.osee.framework.jdk.core.type.PropertyStore; + +/** + * @author Donald G. Dunne + */ +public class PropertyStoreRecordTest extends TestCase { + + public void testSimpleXmlWriting() throws XMLStreamException { + PropertyStore store = new PropertyStore(); + store.put("test1", "data1"); + store.put("test2", "data2"); + store.put("test3", "data3"); + store.put("test4", "data4"); + PropertyStoreRecord record = new PropertyStoreRecord(store); + XMLOutputFactory factory = XMLOutputFactory.newInstance(); + Writer stringStream = new StringWriter(); + XMLStreamWriter writer = factory.createXMLStreamWriter(stringStream); + record.toXml(writer); + + Pattern pattern = Pattern.compile("key=\"test1\""); + Matcher matcher = pattern.matcher(stringStream.toString()); + assertTrue(matcher.find()); + + pattern = Pattern.compile("key=\"test2\""); + matcher = pattern.matcher(stringStream.toString()); + assertTrue(matcher.find()); + + pattern = Pattern.compile("data2"); + matcher = pattern.matcher(stringStream.toString()); + assertTrue(matcher.find()); + + System.out.println(stringStream.toString()); + + pattern = Pattern.compile("data4"); + matcher = pattern.matcher(stringStream.toString()); + assertTrue(matcher.find()); + + pattern = Pattern.compile("key=\"test5\""); + matcher = pattern.matcher(stringStream.toString()); + assertTrue(!matcher.find()); + } + + public void testNullPropertyStore() throws XMLStreamException { + PropertyStoreRecord record = new PropertyStoreRecord(null); + XMLOutputFactory factory = XMLOutputFactory.newInstance(); + Writer stringStream = new StringWriter(); + XMLStreamWriter writer = factory.createXMLStreamWriter(stringStream); + record.toXml(writer); + } + + public void testEmptyPropertyStore() throws XMLStreamException { + PropertyStoreRecord record = new PropertyStoreRecord(new PropertyStore()); + XMLOutputFactory factory = XMLOutputFactory.newInstance(); + Writer stringStream = new StringWriter(); + XMLStreamWriter writer = factory.createXMLStreamWriter(stringStream); + record.toXml(writer); + + assertTrue(stringStream.toString().length() == 31); + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/endpoint/OteUdpEndpointTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/endpoint/OteUdpEndpointTest.java new file mode 100644 index 000000000..6caf95b48 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/endpoint/OteUdpEndpointTest.java @@ -0,0 +1,43 @@ +package org.eclipse.osee.ote.endpoint; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; + +import org.eclipse.osee.framework.jdk.core.util.network.PortUtil; +import org.eclipse.osee.ote.message.event.FileAvailableRequest; +import org.junit.Assert; +import org.junit.Test; + +public class OteUdpEndpointTest { + + @Test + public void testCreation() throws IOException { + int port = PortUtil.getInstance().getValidPort(); + OteUdpEndpointSender sender = new OteUdpEndpointSender(new InetSocketAddress(port)); + Assert.assertNotNull(sender); + port = PortUtil.getInstance().getValidPort(); + OteUdpEndpointReceiverImpl receiver = new OteUdpEndpointReceiverImpl(new InetSocketAddress(port)); + Assert.assertNotNull(receiver); + } + + @Test + public void testSendReceive() throws IOException, InterruptedException { + int port = PortUtil.getInstance().getValidPort(); + OteUdpEndpointReceiverImpl receiver = new OteUdpEndpointReceiverImpl(new InetSocketAddress(InetAddress.getLocalHost(), port)); + receiver.setDebugOutput(true); + receiver.start(); + Thread.sleep(20); + InetSocketAddress address = receiver.getEndpoint(); + OteUdpEndpointSender sender = new OteUdpEndpointSender(address); + sender.start(); + FileAvailableRequest request = new FileAvailableRequest(); + sender.send(request); + + Thread.sleep(20); + + sender.stop(); + receiver.stop(); + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/data/MemoryResourceTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/data/MemoryResourceTest.java new file mode 100644 index 000000000..89ecaeeac --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/data/MemoryResourceTest.java @@ -0,0 +1,43 @@ +package org.eclipse.osee.ote.message.data; + +import java.nio.ByteBuffer; + +import org.junit.Assert; +import org.junit.Test; + +public class MemoryResourceTest { + + @Test + public void testcopyDataByteBuffer() { + byte[] data = new byte[32]; + ByteBuffer buffer = ByteBuffer.allocate(32); + for(int i = 0; i < 16; i++){ + buffer.put((byte)0xDD); + } + for(int i = 0; i < 16; i++){ + buffer.put((byte)0xFF); + } + buffer.position(16); + MemoryResource mem = new MemoryResource(data, 0, 64); + mem.copyData(0, buffer, buffer.remaining()); + + byte[] answer = new byte[32]; + for(int i = 0; i < 16; i++){ + answer[i] = (byte)0xFF; + } + + Assert.assertArrayEquals(answer, mem.getData()); + + buffer.position(0); + mem.copyData(0, buffer, buffer.remaining()); + for(int i = 0; i < 16; i++){ + answer[i] = (byte)0xDD; + } + for(int i = 16; i < 32; i++){ + answer[i] = (byte)0xFF; + } + + Assert.assertArrayEquals(answer, mem.getData()); + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/BiSci16ElementTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/BiSci16ElementTest.java new file mode 100644 index 000000000..c8a94284f --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/BiSci16ElementTest.java @@ -0,0 +1,167 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.elements; + +import org.eclipse.osee.ote.message.data.HeaderData; +import org.eclipse.osee.ote.message.data.MemoryResource; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Michael P. Masterson + */ +public class BiSci16ElementTest { + + private BiSci16Element uut; + + @Before + public void setup() { + final HeaderData hd = new HeaderData("test_data", new MemoryResource(new byte[64], 2, 64)); + uut = new BiSci16Element(null, "derp", hd, 0, 0, 15); + } + + @Test + public void testLongToBiSci() { + + long logicalValue ; + long biSciValue; + + logicalValue = 1; + biSciValue = 0x10; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = 0; + biSciValue = 0x0; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = -1; + biSciValue = 0xFFF0; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = 946176; + biSciValue = 0xE73; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = 25624576; + biSciValue = 0x1874; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = -25624576; + biSciValue = 0xE794; + assertLongToBiSci(logicalValue, biSciValue); + + long sixteenCubed = (long) Math.pow(16, 3); + logicalValue = (long) Math.pow(16, 5); + biSciValue = 0x1003; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = (long) (Math.pow(16, 5) + 1); + biSciValue = 0x1003; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = (long) (Math.pow(16, 5) - 1); + biSciValue = 0x1003; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = (long) (Math.pow(16, 5) + (sixteenCubed/2) - 1); + biSciValue = 0x1003; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = (long) (Math.pow(16, 5) + (sixteenCubed/2)); + biSciValue = 0x1013; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = (long) (Math.pow(16, 5) - sixteenCubed/2 - 1); + biSciValue = 0x0FF3; + assertLongToBiSci(logicalValue, biSciValue); + + logicalValue = (long) (Math.pow(16, 5) - sixteenCubed/2); + biSciValue = 0x1003; + assertLongToBiSci(logicalValue, biSciValue); + + } + + private void assertLongToBiSci(long input, long expected) { + long actual = uut.convertLogicalValueToBiSci(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void testBiSciToLong() { + long biSciValue; + long logicalValue; + + logicalValue = 1; + biSciValue = 0x10; + assertBiSciToLong(biSciValue, logicalValue); + + logicalValue = 0; + biSciValue = 0x0; + assertBiSciToLong(biSciValue, logicalValue); + + logicalValue = -1; + biSciValue = 0xFFF0; + assertBiSciToLong(biSciValue, logicalValue); + + logicalValue = 946176; + biSciValue = 0xE73; + assertBiSciToLong(biSciValue, logicalValue); + + logicalValue = 25624576; + biSciValue = 0x1874; + assertBiSciToLong(biSciValue, logicalValue); + + logicalValue = -25624576; + biSciValue = ((short)0xE794); + assertBiSciToLong(biSciValue, logicalValue); + + } + + /** + * @param bisciValue + * @param logicalValue + */ + private void assertBiSciToLong(long input, long expected) { + long actual = uut.convertBiSciToLogicalValue(input); + Assert.assertEquals(expected, actual); + + } + + @Test + public void testSetValue() { + checkSet(25624576); + checkSet(-25624576); + checkSet(1); + checkSet(0); + checkSet(-1); + + checkTiming(0x7FF << (4 * 15)); + } + + private void checkTiming(int input) { + long timeBefore, timeAfter; + for( int i = 0 ; i < 10000 ; i++) { + timeBefore = System.nanoTime(); + checkSet(input); + timeAfter = System.nanoTime(); + long runningTime = timeAfter - timeBefore; + Assert.assertTrue(runningTime < 500000); + + } + + } + + private void checkSet(long input) { + uut.setValue(input); + Assert.assertEquals(input, (long)uut.getValue()); + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/CharElementTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/CharElementTest.java new file mode 100644 index 000000000..78267fd0f --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/CharElementTest.java @@ -0,0 +1,80 @@ +/* + * Created on Oct 29, 2013 + * + * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE + */ +package org.eclipse.osee.ote.message.elements; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import java.util.Arrays; +import org.eclipse.osee.ote.message.Message; +import org.eclipse.osee.ote.message.data.MemoryResource; +import org.eclipse.osee.ote.message.data.MessageData; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class CharElementTest { + + public class TestMessage extends Message { + public TestMessage(String name, int defaultByteSize, int defaultOffset, boolean isScheduled, int phase, double rate) { + super(name, defaultByteSize, defaultOffset, isScheduled, phase, rate); + } + } + + @Mock + private TestMessage message; + @Mock + private MessageData msgData; + private final byte[] bytes = new byte[5]; + private final MemoryResource memoryResource = new MemoryResource(bytes, 0, bytes.length); + + @Before + public void before() { + MockitoAnnotations.initMocks(this); + when(message.getData()).thenReturn(bytes); + when(message.getName()).thenReturn("MSG"); + when(msgData.getMem()).thenReturn(memoryResource); + } + + @Test + public void parseAndSetWithByteAlignedElementTest() throws Exception { + CharElement sut = new CharElement(message, "TEST", msgData, 0, 0, 7); + assertEquals("Start with all zeros in the byte array", "[0, 0, 0, 0, 0]", + Arrays.toString(memoryResource.getData())); + sut.parseAndSet(null, " "); + assertEquals("getValue should return space character", ' ', sut.getValue().charValue()); + sut.parseAndSet(null, "A"); + assertEquals("getValue should return 'A'", 'A', sut.getValue().charValue()); + sut.parseAndSet(null, ""); + assertEquals("getValue should return null character", '\0', sut.getValue().charValue()); + sut.parseAndSet(null, "FOO"); + assertEquals("Decimal ASCII values for FOO", "[70, 79, 79, 0, 0]", Arrays.toString(memoryResource.getData())); + assertEquals("FOO should be returned by the getString method", "FOO", sut.getString(null, 5)); + } + + @Test + public void parseAndSetWithNonByteAlignedElementTest() throws Exception { + CharElement sut = new CharElement(message, "TEST", msgData, 0, 1, 7); + assertEquals("Start with all zeros in the byte array", "[0, 0, 0, 0, 0]", + Arrays.toString(memoryResource.getData())); + Arrays.fill(bytes, (byte) 0xFF); + sut.parseAndSet(null, " "); + assertEquals("getValue should return space character", ' ', sut.getValue().charValue()); + sut.parseAndSet(null, "A"); + assertEquals("getValue should return 'A'", 'A', sut.getValue().charValue()); + sut.parseAndSet(null, ""); + assertEquals("getValue should return null character", '\0', sut.getValue().charValue()); + sut.parseAndSet(null, "FOO"); + assertEquals("Unfortunately we tromple the spare bit data", "[70, 79, 79, -1, -1]", + Arrays.toString(memoryResource.getData())); + } + + @Test(expected = IllegalArgumentException.class) + public void parseAndSetOverflowExceptionTest() throws Exception { + CharElement sut = new CharElement(message, "TEST", msgData, 0, 0, 7); + sut.parseAndSet(null, "TOOLONG"); + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/Float32ElementTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/Float32ElementTest.java new file mode 100644 index 000000000..887e06e7a --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/Float32ElementTest.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.elements; + +import java.util.Random; +import org.eclipse.osee.ote.message.data.HeaderData; +import org.eclipse.osee.ote.message.data.MemoryResource; +import org.eclipse.osee.ote.message.elements.Float32Element; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class Float32ElementTest { + + @Test + public void testFloat32() { + final HeaderData hd = new HeaderData("test_data", new MemoryResource(new byte[64], 0, 64)); + Float32Element[] e = new Float32Element[8]; + for (int i = 0; i < e.length; i++) { + e[i] = new Float32Element(null, "Float32@" + i, hd, i * 4, 0, 31); + } + Random r = new Random(System.currentTimeMillis()); + float[] vals = new float[e.length]; + for (int i = 0; i < 1000; i++) { + generateAscending(r, e, vals, e.length); + check(e, vals, e.length); + + generateDescending(r, e, vals, e.length); + check(e, vals, e.length); + + // zeroize test + for (int z = 0; z < e.length; z += 2) { + e[z].zeroize(); + vals[z] = 0; + } + + check(e, vals, e.length); + + } + Double x = new Double(r.nextFloat()); + e[0].setValue(x); + Assert.assertTrue(e[0].getValue().equals(x)); + } + + private void generateAscending(Random r, Float32Element[] e, float[] expectedVals, int length) { + for (int i = 0; i < length; i++) { + float val = r.nextFloat(); + Float32Element el = e[i]; + el.setValue(val); + expectedVals[i] = val; + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + Double.valueOf(expectedVals[i]), el.getValue()); + } + } + + private void generateDescending(Random r, Float32Element[] e, float[] expectedVals, int length) { + for (int i = length - 1; i >= 0; i--) { + float val = r.nextFloat(); + Float32Element el = e[i]; + el.setValue(val); + expectedVals[i] = val; + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + Double.toHexString(expectedVals[i]), Double.toHexString(el.getValue())); + } + } + + private void check(Float32Element[] e, float[] expectedVals, int length) { + for (int i = 0; i < length; i++) { + Float32Element el = e[i]; + Assert.assertTrue( + String.format("corruption detect on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + expectedVals[i] == el.getValue()); + } + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/Float64ElementTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/Float64ElementTest.java new file mode 100644 index 000000000..8c47626ad --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/Float64ElementTest.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.elements; + +import java.util.Random; +import org.eclipse.osee.ote.message.data.HeaderData; +import org.eclipse.osee.ote.message.data.MemoryResource; +import org.eclipse.osee.ote.message.elements.Float64Element; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class Float64ElementTest { + + @Test + public void testFloat64() { + final HeaderData hd = new HeaderData("test_data", new MemoryResource(new byte[64], 0, 64)); + Float64Element[] e = new Float64Element[8]; + for (int i = 0; i < e.length; i++) { + e[i] = new Float64Element(null, "Float64@" + i, hd, i * 8, 0, 63); + } + Random r = new Random(System.currentTimeMillis()); + double[] vals = new double[e.length]; + for (int i = 0; i < 1000; i++) { + generateAscending(r, e, vals, e.length); + check(e, vals, e.length); + + generateDescending(r, e, vals, e.length); + check(e, vals, e.length); + + // zeroize test + for (int z = 0; z < e.length; z += 2) { + e[z].zeroize(); + vals[z] = 0; + } + + check(e, vals, e.length); + } + } + + private void generateAscending(Random r, Float64Element[] e, double[] expectedVals, int length) { + for (int i = 0; i < length; i++) { + double val = r.nextDouble(); + Float64Element el = e[i]; + el.setValue(val); + expectedVals[i] = val; + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + (Double) expectedVals[i], el.getValue()); + } + } + + private void generateDescending(Random r, Float64Element[] e, double[] expectedVals, int length) { + for (int i = length - 1; i >= 0; i--) { + double val = r.nextDouble(); + Float64Element el = e[i]; + el.setValue(val); + expectedVals[i] = val; + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + Double.toHexString(expectedVals[i]), Double.toHexString(el.getValue())); + } + } + + private void check(Float64Element[] e, double[] expectedVals, int length) { + for (int i = 0; i < length; i++) { + Float64Element el = e[i]; + Assert.assertEquals( + String.format("corruption detect on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + (Double) expectedVals[i], e[i].getValue()); + + String v = Double.toString(expectedVals[i]); + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), v, + el.valueOf()); + } + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/IntegerElementTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/IntegerElementTest.java new file mode 100644 index 000000000..f5a7b32bd --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/IntegerElementTest.java @@ -0,0 +1,270 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.elements; + +import java.util.Random; +import org.eclipse.osee.ote.message.data.HeaderData; +import org.eclipse.osee.ote.message.data.MemoryResource; +import org.eclipse.osee.ote.message.elements.IntegerElement; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class IntegerElementTest { + + @Test + public void test1BitInts() { + createTest(1); + } + + @Test + public void test2BitInts() { + createTest(2); + } + + @Test + public void test3BitInts() { + createTest(3); + } + + @Test + public void test4BitInts() { + createTest(4); + } + + @Test + public void test5BitInts() { + createTest(5); + } + + @Test + public void test6BitInts() { + createTest(6); + } + + @Test + public void test7BitInts() { + createTest(7); + } + + @Test + public void test8BitInts() { + createTest(8); + } + + @Test + public void test9BitInts() { + createTest(9); + } + + @Test + public void test10BitInts() { + createTest(10); + } + + @Test + public void test11BitInts() { + createTest(11); + } + + @Test + public void test12BitInts() { + createTest(12); + } + + @Test + public void test13BitInts() { + createTest(13); + } + + @Test + public void test14BitInts() { + createTest(14); + } + + @Test + public void test15BitInts() { + createTest(15); + } + + @Test + public void test16BitInts() { + createTest(16); + } + + @Test + public void test17BitInts() { + createTest(17); + } + + @Test + public void test18BitInts() { + createTest(18); + } + + @Test + public void test19BitInts() { + createTest(19); + } + + @Test + public void test20BitInts() { + createTest(20); + } + + @Test + public void test21BitInts() { + createTest(21); + } + + @Test + public void test22BitInts() { + createTest(22); + } + + @Test + public void test23BitInts() { + createTest(23); + } + + @Test + public void test24BitInts() { + createTest(24); + } + + @Test + public void test25BitInts() { + createTest(25); + } + + @Test + public void test26BitInts() { + createTest(26); + } + + @Test + public void test27BitInts() { + createTest(27); + } + + @Test + public void test28BitInts() { + createTest(28); + } + + @Test + public void test29BitInts() { + createTest(29); + } + + @Test + public void test30BitInts() { + createTest(30); + } + + @Test + public void test32BitInts() { + createTest(32); + } + + private void createTest(int width) { + final HeaderData hd = new HeaderData("test_data", new MemoryResource(new byte[64], 2, 64)); + IntegerElement[] e = new IntegerElement[32]; + for (int a = 0; a < 8; a++) { + for (int i = 0; i < width; i++) { + int count = 0; + int j; + // fill all bits before the first Integer Element with 1 bit elements + for (int k = 0; k < i; k++) { + e[count++] = new IntegerElement(null, "Element@" + k, hd, a, k, k); + } + for (j = i; j < 33 - width; j += width) { + e[count++] = new IntegerElement(null, "Element@" + j, hd, a, j, j + width - 1); + } + // fill remaining bits with 1 bit signals + for (int k = j; k < 32; k++) { + e[count++] = new IntegerElement(null, "Element@" + k, hd, a, k, k); + } + int[] expectedVals = new int[count]; + Random r = new Random(System.currentTimeMillis()); + + for (int l = 0; l <= 255; l++) { + /* + * perform sets going through the array. We do this so that we can catch sets that modified bits before + * the element + */ + generateAscending(r, e, expectedVals, count); + check(e, expectedVals, count); + + /* + * perform sets going backwards through the array. We do this so that we can catch sets that modified + * bits after the element + */ + generateDescending(r, e, expectedVals, count); + check(e, expectedVals, count); + + // zeroize test + for (int z = 0; z < count; z += 2) { + e[z].zeroize(); + expectedVals[z] = 0; + } + + check(e, expectedVals, count); + } + } + } + } + + private void generateAscending(Random r, IntegerElement[] e, int[] expectedVals, int length) { + for (int i = 0; i < length; i++) { + int val = r.nextInt(); + IntegerElement el = e[i]; + el.setValue(val); + int width = el.getLsb() - el.getMsb() + 1; + if (width < 32) { + expectedVals[i] = val & (1 << width) - 1; + } else { + expectedVals[i] = val; + } + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + (Integer) expectedVals[i], el.getValue()); + } + } + + private void generateDescending(Random r, IntegerElement[] e, int[] expectedVals, int length) { + for (int i = length - 1; i >= 0; i--) { + int val = r.nextInt(); + IntegerElement el = e[i]; + el.setValue(val); + int width = el.getLsb() - el.getMsb() + 1; + if (width < 32) { + expectedVals[i] = val & (1 << width) - 1; + } else { + expectedVals[i] = val; + } + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + Long.toHexString(expectedVals[i]), Long.toHexString(el.getValue())); + } + } + + private void check(IntegerElement[] e, int[] expectedVals, int length) { + for (int i = 0; i < length; i++) { + IntegerElement el = e[i]; + Assert.assertEquals( + String.format("corruption detect on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + (Integer) expectedVals[i], el.getValue()); + } + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/LongIntegerElementTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/LongIntegerElementTest.java new file mode 100644 index 000000000..6bcc5c5f3 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/LongIntegerElementTest.java @@ -0,0 +1,224 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.elements; + +import java.util.Random; +import org.eclipse.osee.ote.message.data.HeaderData; +import org.eclipse.osee.ote.message.data.MemoryResource; +import org.eclipse.osee.ote.message.elements.LongIntegerElement; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class LongIntegerElementTest { + @Test + public void test1BitInts() { + createLongIntTest(1); + } + + @Test + public void test2BitInts() { + createLongIntTest(2); + } + + @Test + public void test3BitInts() { + createLongIntTest(3); + } + + @Test + public void test4BitInts() { + createLongIntTest(4); + } + + @Test + public void test5BitInts() { + createLongIntTest(5); + } + + @Test + public void test8BitInts() { + createLongIntTest(8); + } + + @Test + public void test10BitInts() { + createLongIntTest(10); + } + + @Test + public void test12BitInts() { + createLongIntTest(12); + } + + @Test + public void test16BitInts() { + createLongIntTest(16); + } + + @Test + public void test20BitInts() { + createLongIntTest(20); + } + + @Test + public void test22BitInts() { + createLongIntTest(22); + } + + @Test + public void test24BitInts() { + createLongIntTest(24); + } + + @Test + public void test25BitInts() { + createLongIntTest(25); + } + + @Test + public void test26BitInts() { + createLongIntTest(26); + } + + @Test + public void test27BitInts() { + createLongIntTest(27); + } + + @Test + public void test30BitInts() { + createLongIntTest(30); + } + + @Test + public void test31BitInts() { + createLongIntTest(31); + } + + @Test + public void test32BitInts() { + createLongIntTest(32); + } + + @Test + public void test40BitInts() { + createLongIntTest(40); + } + + @Test + public void test48BitInts() { + createLongIntTest(48); + } + + @Test + public void test50BitInts() { + createLongIntTest(50); + } + + @Test + public void test64BitInts() { + createLongIntTest(64); + } + + private void createLongIntTest(int width) { + LongIntegerElement[] e = new LongIntegerElement[64]; + for (int a = 0; a < 4; a++) { + final HeaderData hd = new HeaderData("test_data", new MemoryResource(new byte[64], a, 64)); + for (int i = 0; i < width; i++) { + int count = 0; + int j; + for (int k = 0; k < i; k++) { + e[count++] = new LongIntegerElement(null, "Element@" + k, hd, 0, k, k); + } + for (j = i; j < 65 - width; j += width) { + e[count++] = new LongIntegerElement(null, "Element@" + j, hd, 0, j, j + width - 1); + } + // fill remaining bits with 1 bit signals + for (int k = j; k < 64; k++) { + e[count++] = new LongIntegerElement(null, "Element@" + k, hd, 0, k, k); + } + long[] expectedVals = new long[count]; + Random r = new Random(System.currentTimeMillis()); + + for (int l = 0; l <= 1024; l++) { + /* + * perform sets going through the array. We do this so that we can catch sets that modified bits before + * the element + */ + generateAscending(r, e, expectedVals, count); + check(e, expectedVals, count); + + /* + * perform sets going backwards through the array. We do this so that we can catch sets that modified + * bits after the element + */ + generateDescending(r, e, expectedVals, count); + check(e, expectedVals, count); + + // zeroize test + for (int z = 0; z < count; z += 2) { + e[z].zeroize(); + expectedVals[z] = 0; + } + + check(e, expectedVals, count); + } + } + } + } + + private void generateAscending(Random r, LongIntegerElement[] e, long[] expectedVals, int length) { + for (int i = 0; i < length; i++) { + long val = r.nextLong(); + LongIntegerElement el = e[i]; + el.setValue(val); + int width = el.getLsb() - el.getMsb() + 1; + if (width < 64) { + expectedVals[i] = val & (1L << width) - 1L; + } else { + expectedVals[i] = val; + } + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + Long.toHexString(expectedVals[i]), Long.toHexString(el.getValue())); + } + } + + private void generateDescending(Random r, LongIntegerElement[] e, long[] expectedVals, int length) { + for (int i = length - 1; i >= 0; i--) { + long val = r.nextLong(); + LongIntegerElement el = e[i]; + el.setValue(val); + int width = el.getLsb() - el.getMsb() + 1; + if (width < 64) { + expectedVals[i] = val & (1l << width) - 1l; + } else { + expectedVals[i] = val; + } + Assert.assertEquals( + String.format("set/get fail on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + (Long) expectedVals[i], el.getValue()); + } + } + + private void check(LongIntegerElement[] e, long[] expectedVals, int length) { + for (int i = 0; i < length; i++) { + LongIntegerElement el = e[i]; + Assert.assertEquals( + String.format("corruption detect on %s: msb=%d, lsb=%d", el.getName(), el.getMsb(), el.getLsb()), + (Long) expectedVals[i], e[i].getValue()); + } + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/RecordMapTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/RecordMapTest.java new file mode 100644 index 000000000..255927d76 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/RecordMapTest.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2010 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.elements; + +import org.eclipse.osee.ote.message.mock.TestMessage; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class RecordMapTest { + + @Test + public void testGetInt() { + TestMessage msg = new TestMessage(); + + // Testing below the boundary. + try { + msg.RECORD_MAP_1.get(1); + Assert.assertTrue(true); + } catch (IllegalArgumentException ex) { + Assert.fail("We shouldn't get an exception for this get!"); + } + + // Testing on the boundary. + try { + msg.RECORD_MAP_1.get(2); + Assert.fail("We should get an exception for this get on the boundary!"); + } catch (IllegalArgumentException ex) { + Assert.assertTrue("We should get an exception for this index", true); + } + // Testing above the boundary. + try { + msg.RECORD_MAP_1.get(3); + Assert.fail("We should get an exception for this get above the boundary!"); + } catch (IllegalArgumentException ex) { + Assert.assertTrue("We should get an exception for this index", true); + } + + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/SignedInteger16ElementTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/SignedInteger16ElementTest.java new file mode 100644 index 000000000..59ff96a9f --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/SignedInteger16ElementTest.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.elements; + +import org.eclipse.osee.ote.message.data.HeaderData; +import org.eclipse.osee.ote.message.data.MemoryResource; +import org.eclipse.osee.ote.message.elements.SignedInteger16Element; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class SignedInteger16ElementTest { + + @Test + public void test1() { + final HeaderData hd1 = new HeaderData("test_data 1", new MemoryResource(new byte[64], 0, 64)); + final HeaderData hd2 = new HeaderData("test_data 2", new MemoryResource(new byte[64], 2, 64)); + SignedInteger16Element element1 = new SignedInteger16Element(null, "Element1", hd1, 0, 0, 15); + SignedInteger16Element element2 = new SignedInteger16Element(null, "Element2", hd2, 0, 0, 15); + + int val1 = -1000; + int val2 = 2000; + element1.setValue(val1); + check(element1, val1); + element1.setValue(val2); + check(element1, val2); + + element2.setValue(val1); + check(element2, val1); + element2.setValue(val2); + check(element2, val2); + } + + @Test + public void test2() { + final HeaderData hd1 = new HeaderData("test_data 1", new MemoryResource(new byte[64], 0, 64)); + final HeaderData hd2 = new HeaderData("test_data 2", new MemoryResource(new byte[64], 2, 64)); + SignedInteger16Element element1 = new SignedInteger16Element(null, "Element1", hd1, 0, 16, 31); + SignedInteger16Element element2 = new SignedInteger16Element(null, "Element2", hd2, 0, 16, 31); + + int val1 = -10000; + int val2 = 2100; + element1.setValue(val1); + check(element1, val1); + element1.setValue(val2); + check(element1, val2); + + element2.setValue(val1); + check(element2, val1); + element2.setValue(val2); + check(element2, val2); + } + + private void check(SignedInteger16Element e, int expectedVals) { + Assert.assertEquals( + String.format("corruption detect on %s: msb=%d, lsb=%d", e.getName(), e.getMsb(), e.getLsb()), + (Integer) expectedVals, e.getValue()); + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/StringElementTest.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/StringElementTest.java new file mode 100644 index 000000000..1123f809e --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/elements/StringElementTest.java @@ -0,0 +1,190 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.elements; + +import static org.junit.Assert.assertEquals; +import java.util.Arrays; +import org.eclipse.osee.ote.message.data.HeaderData; +import org.eclipse.osee.ote.message.data.MemoryResource; +import org.eclipse.osee.ote.message.data.MessageData; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class StringElementTest { + @Test + public void testZeroize() { + for (int i = 0; i < 10; i++) { + final HeaderData hd = new HeaderData("test_data", new MemoryResource(new byte[128], i, 128)); + StringElement e1 = new StringElement(null, "test string1", hd, 0, 0, 8 * 10 - 1); + StringElement e2 = new StringElement(null, "test string2", hd, 10, 0, 8 * 10 - 1); + StringElement e3 = new StringElement(null, "test string3", hd, 20, 0, 8 * 10 - 1); + + String s1 = "aaaa bb c"; + String s2 = "zeroizing2"; + String s3 = "1234567890"; + + e1.setValue(s1); + e2.setValue(s2); + e3.setValue(s3); + + check(e1, s1); + check(e2, s2); + check(e3, s3); + + e2.zeroize(); + checkEmpty(e2); + + check(e1, s1); + check(e2, ""); + check(e3, s3); + + e2.setValue(s2); + e1.zeroize(); + checkEmpty(e1); + + check(e1, ""); + check(e2, s2); + check(e3, s3); + + e1.setValue(s1); + e3.zeroize(); + + check(e1, s1); + check(e2, s2); + check(e3, ""); + } + } + + @Test + public void testStringsTooBig() { + for (int i = 0; i < 10; i++) { + final HeaderData hd = new HeaderData("test_data", new MemoryResource(new byte[128], i, 128)); + + String s1 = "aaaa bb c"; + String s2 = "zeroizing2"; + String s3 = "1234567890"; + for (int j = 1; j < 10; j++) { + StringElement e1 = new StringElement(null, "test string1", hd, 0, 0, 8 * j - 1); + StringElement e2 = new StringElement(null, "test string2", hd, 10, 0, 8 * j - 1); + StringElement e3 = new StringElement(null, "test string3", hd, 20, 0, 8 * j - 1); + + e1.setValue(s1); + e2.setValue(s2); + e3.setValue(s3); + + check(e1, s1.substring(0, j)); + check(e2, s2.substring(0, j)); + check(e3, s3.substring(0, j)); + + e3.setValue(s3); + e2.setValue(s2); + e1.setValue(s1); + + check(e1, s1.substring(0, j)); + check(e2, s2.substring(0, j)); + check(e3, s3.substring(0, j)); + } + } + } + + @Test + public void testStringsTooSmall() { + for (int i = 0; i < 10; i++) { + final HeaderData hd = new HeaderData("test_data", new MemoryResource(new byte[128], i, 128)); + StringElement e1 = new StringElement(null, "test string1", hd, 0, 0, 8 * 10 - 1); + StringElement e2 = new StringElement(null, "test string2", hd, 10, 0, 8 * 10 - 1); + StringElement e3 = new StringElement(null, "test string3", hd, 20, 0, 8 * 10 - 1); + String ss1 = "aaaa bb c"; + String ss2 = "zeroizing2"; + String ss3 = "1234567890"; + for (int j = 1; j <= 10; j++) { + String s1 = ss1.substring(0, j); + String s2 = ss2.substring(0, j); + String s3 = ss3.substring(0, j); + e1.setValue(s1); + e2.setValue(s2); + e3.setValue(s3); + + check(e1, s1); + check(e2, s2); + check(e3, s3); + + e3.setValue(s3); + e2.setValue(s2); + e1.setValue(s1); + + check(e1, s1); + check(e2, s2); + check(e3, s3); + } + } + } + + @Test + public void testSetChars() { + final char[] testData = {'A', 'B', '\0', 'C', 'D'}; + final MessageData hd = new HeaderData("test_data", new MemoryResource(new byte[5], 0, 5)); + final StringElement sut = new StringElement(null, "test string element", hd, 0, 0, 8 * 5 - 1); + assertEquals("Empty string to start", "", sut.getValue()); + sut.setChars(testData); + assertEquals("New value is cut off by the null", "AB", sut.getValue()); + char[] result = new char[5]; + assertEquals("Five bytes in, five bytes back", 5, sut.getChars(result)); + assertEquals("Exact same array comes back with getChars()", Arrays.toString(testData), Arrays.toString(result)); + } + + @Test + public void testSetCharsOverflow() { + final char[] testData = {'A', 'B', '\0', 'C', 'D', 'E'}; + // buffer is big enough for test data + final MessageData hd = new HeaderData("test_data", new MemoryResource(new byte[6], 0, 6)); + // element is not big enough for test data + final StringElement sut = new StringElement(null, "test string element", hd, 0, 0, 8 * 5 - 1); + assertEquals("Empty string to start", "", sut.getValue()); + sut.setChars(testData); + assertEquals("New value is cut off by the null", "AB", sut.getValue()); + char[] result = new char[5]; + assertEquals("Five bytes in, five bytes back", 5, sut.getChars(result)); + final char[] expected = {'A', 'B', '\0', 'C', 'D'}; + assertEquals("Exact same array comes back with getChars()", Arrays.toString(expected), Arrays.toString(result)); + } + + @Test + public void testSetCharsUnderflow() { + final char[] testData = {'A', 'B', '\0', 'C'}; + // buffer is big enough for test data + final MessageData hd = new HeaderData("test_data", new MemoryResource(new byte[5], 0, 5)); + // element is not big enough for test data + final StringElement sut = new StringElement(null, "test string element", hd, 0, 0, 8 * 5 - 1); + assertEquals("Empty string to start", "", sut.getValue()); + sut.setChars(testData); + assertEquals("New value is cut off by the null", "AB", sut.getValue()); + char[] result = new char[5]; + assertEquals("Five bytes in, five bytes back", 5, sut.getChars(result)); + final char[] expected = {'A', 'B', '\0', 'C', '\0'}; + assertEquals("Exact same array comes back with getChars()", Arrays.toString(expected), Arrays.toString(result)); + } + + private void check(StringElement elem, String value) { + if (!elem.getValue().equals(value)) { + Assert.assertEquals(elem.getName(), value, elem.getValue()); + } + } + + private void checkEmpty(StringElement elem) { + if (!elem.isEmpty()) { + Assert.assertEquals(elem.getName(), "rmpty", "not empty"); + } + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/ISequenceHandle.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/ISequenceHandle.java new file mode 100644 index 000000000..6c5b425d4 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/ISequenceHandle.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import java.util.concurrent.TimeUnit; + +/** + * @author Ken J. Aguilar + */ +public interface ISequenceHandle { + boolean waitForEndSequence(long timoeut, TimeUnit timeUnit) throws InterruptedException; +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/SequenceHandle.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/SequenceHandle.java new file mode 100644 index 000000000..4b510086a --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/SequenceHandle.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @author Ken J. Aguilar + */ +class SequenceHandle implements ISequenceHandle { + + private final ReentrantLock lock = new ReentrantLock(); + private final Condition endSequenceCondition = lock.newCondition(); + private boolean endSequence = false; + + @Override + public boolean waitForEndSequence(long timeout, TimeUnit timeUnit) throws InterruptedException { + lock.lock(); + long nanos = timeUnit.toNanos(timeout); + try { + while (!endSequence) { + if (nanos > 0) { + nanos = endSequenceCondition.awaitNanos(nanos); + } else { + return false; + } + } + return true; + } finally { + lock.unlock(); + } + } + + void signalEndSequence() { + lock.lock(); + try { + endSequence = true; + endSequenceCondition.signalAll(); + } finally { + lock.unlock(); + } + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestEnum.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestEnum.java new file mode 100644 index 000000000..34b019223 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestEnum.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import org.eclipse.osee.ote.message.elements.IEnumValue; + +public enum TestEnum implements IEnumValue<TestEnum> { + VAL_0, + VAL_1, + VAL_2, + VAL_3, + VAL_4, + VAL_5, + VAL_6, + VAL_7, + VAL_8, + VAL_9, + VAL_10; + + @Override + public TestEnum getEnum(int value) { + if (value < 0 || value >= values().length) { + throw new IllegalArgumentException("no enum matching value of " + value); + } + return values()[value]; + } + + @Override + public int getIntValue() { + return ordinal(); + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMemType.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMemType.java new file mode 100644 index 000000000..8d7d94ee3 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMemType.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2010 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import org.eclipse.osee.ote.message.enums.DataType; + +/** + * @author Andrew M. Finkbeiner + */ +public enum TestMemType implements DataType { + ETHERNET, + SERIAL; + + @Override + public int getToolingBufferSize() { + return 0; + } + + @Override + public int getToolingDepth() { + return 0; + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMessage.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMessage.java new file mode 100644 index 000000000..7172e89b5 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMessage.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import java.util.Collection; +import org.eclipse.osee.ote.message.Message; +import org.eclipse.osee.ote.message.data.MessageData; +import org.eclipse.osee.ote.message.elements.EnumeratedElement; +import org.eclipse.osee.ote.message.elements.Float32Element; +import org.eclipse.osee.ote.message.elements.IRecordFactory; +import org.eclipse.osee.ote.message.elements.IntegerElement; +import org.eclipse.osee.ote.message.elements.RecordElement; +import org.eclipse.osee.ote.message.elements.RecordMap; +import org.eclipse.osee.ote.message.elements.StringElement; + +public class TestMessage extends Message<UnitTestAccessor, TestMessageData, TestMessage> { + + public final IntegerElement INT_ELEMENT_1; + public final StringElement STRING_ELEMENT_1; + public final EnumeratedElement<TestEnum> ENUM_ELEMENT_1; + public final Float32Element FLOAT32_ELEMENT_1; + public final RecordMap<TestRecordElement> RECORD_MAP_1; + + public TestMessage() { + super("TEST_MSG", 100, 0, true, 0, 50.0); + TestMessageData ethData = + new TestMessageData(this.getClass().getName(), getName(), getDefaultByteSize(), getDefaultOffset(), + TestMemType.ETHERNET); + new TestMessageData(this.getClass().getName(), getName(), getDefaultByteSize(), getDefaultOffset(), + TestMemType.SERIAL); + setDefaultMessageData(ethData); + INT_ELEMENT_1 = new IntegerElement(this, "INT_ELEMENT_1", ethData, 0, 0, 15); + STRING_ELEMENT_1 = new StringElement(this, "STRING_ELEMENT_1", ethData, 2, 0, 159); + ENUM_ELEMENT_1 = new EnumeratedElement<>(this, "ENUM_ELEMENT_1", TestEnum.class, ethData, 22, 0, 7); + FLOAT32_ELEMENT_1 = new Float32Element(this, "FLOAT32_ELEMENT_1", ethData, 23, 0, 31); + RECORD_MAP_1 = + new RecordMap<TestRecordElement>(this, ethData, "RECORD_MAP_1", 2, new RECORD_MAP_1_factory(ethData)); + addElements(INT_ELEMENT_1, STRING_ELEMENT_1, ENUM_ELEMENT_1, FLOAT32_ELEMENT_1, RECORD_MAP_1); + setMemSource(TestMemType.ETHERNET); + } + + @Override + public void switchElementAssociation(Collection<TestMessage> messages) { + + } + + public class RECORD_MAP_1_factory implements IRecordFactory { + private final MessageData data; + + public RECORD_MAP_1_factory(MessageData data) { + this.data = data; + } + + @Override + public RecordElement create(int i) { + TestRecordElement temp = + new TestRecordElement(TestMessage.this, "RECORD_MAP_1", i, getBitOffset() + 0, this.data); + temp.addPath(RECORD_MAP_1.getElementPath().toArray()); + return temp; + } + + @Override + public int getBitLength() { + return 512; + } + + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMessageData.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMessageData.java new file mode 100644 index 000000000..3bfdb01e0 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestMessageData.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import org.eclipse.osee.ote.message.IMessageHeader; +import org.eclipse.osee.ote.message.data.IMessageDataVisitor; +import org.eclipse.osee.ote.message.data.MessageData; +import org.eclipse.osee.ote.message.enums.DataType; + +public class TestMessageData extends MessageData { + + public TestMessageData(String typeName, String name, int dataByteSize, int offset, DataType memType) { + super(typeName, name, dataByteSize, offset, memType); + } + + @Override + public IMessageHeader getMsgHeader() { + return null; + } + + @Override + public void initializeDefaultHeaderValues() { + + } + + @Override + public void visit(IMessageDataVisitor visitor) { + + } + + @Override + public void zeroize() { + + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestRecordElement.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestRecordElement.java new file mode 100644 index 000000000..f545a342c --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/TestRecordElement.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import org.eclipse.osee.ote.message.Message; +import org.eclipse.osee.ote.message.data.MessageData; +import org.eclipse.osee.ote.message.elements.RecordElement; + +public class TestRecordElement extends RecordElement { + + public TestRecordElement(Message message, String name, int index, int bitOffset, MessageData messageData) { + super(message, name, index, messageData, bitOffset, 152); + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/UnitTestAccessor.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/UnitTestAccessor.java new file mode 100644 index 000000000..4969cd494 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/UnitTestAccessor.java @@ -0,0 +1,668 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import java.util.HashMap; +import java.util.ResourceBundle; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.logging.Filter; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import org.eclipse.osee.framework.jdk.core.persistence.Xmlizable; +import org.eclipse.osee.ote.core.MethodFormatter; +import org.eclipse.osee.ote.core.TestCase; +import org.eclipse.osee.ote.core.TestScript; +import org.eclipse.osee.ote.core.environment.EnvironmentTask; +import org.eclipse.osee.ote.core.environment.ReportDataControl; +import org.eclipse.osee.ote.core.environment.ScriptControl; +import org.eclipse.osee.ote.core.environment.TestEnvironment; +import org.eclipse.osee.ote.core.environment.command.CommandDescription; +import org.eclipse.osee.ote.core.environment.interfaces.BasicTimeout; +import org.eclipse.osee.ote.core.environment.interfaces.ICancelTimer; +import org.eclipse.osee.ote.core.environment.interfaces.IExecutionUnitManagement; +import org.eclipse.osee.ote.core.environment.interfaces.IReportData; +import org.eclipse.osee.ote.core.environment.interfaces.IScriptControl; +import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironmentAccessor; +import org.eclipse.osee.ote.core.environment.interfaces.ITestLogger; +import org.eclipse.osee.ote.core.environment.interfaces.ITestPoint; +import org.eclipse.osee.ote.core.environment.interfaces.ITestStation; +import org.eclipse.osee.ote.core.environment.interfaces.ITimeout; +import org.eclipse.osee.ote.core.environment.interfaces.ITimerControl; +import org.eclipse.osee.ote.core.environment.status.CommandEndedStatusEnum; +import org.eclipse.osee.ote.core.framework.IRunManager; +import org.eclipse.osee.ote.core.log.ITestPointTally; +import org.eclipse.osee.ote.core.log.TestLogger; +import org.eclipse.osee.ote.core.log.record.TestPointRecord; +import org.eclipse.osee.ote.core.log.record.TestRecord; +import org.eclipse.osee.ote.message.enums.DataType; +import org.eclipse.osee.ote.message.interfaces.IMessageManager; +import org.eclipse.osee.ote.message.interfaces.ITestAccessor; +import org.eclipse.osee.ote.message.interfaces.ITestEnvironmentMessageSystemAccessor; + +public class UnitTestAccessor implements ITestEnvironmentMessageSystemAccessor, ITestAccessor { + private final HashMap<EnvironmentTask, ScheduledFuture<?>> handleMap = + new HashMap<EnvironmentTask, ScheduledFuture<?>>(32); + private final ScheduledExecutorService executor = + Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()); + private final IScriptControl scriptCtrl = new ScriptControl(); + private final IReportData reportData = new ReportDataControl(); + private final ITestLogger testLogger = new TestLogger() { + + @Override + public void attention(ITestEnvironmentAccessor source, String message) { + + } + + @Override + public void debug(ITestEnvironmentAccessor source, String message, boolean timeStamp) { + + } + + @Override + public void debug(ITestEnvironmentAccessor source, String message) { + + } + + @Override + public void log(TestRecord record) { + + } + + @Override + public void methodCalled(ITestEnvironmentAccessor source, MethodFormatter arguments, int methodCount) { + + } + + @Override + public void methodCalled(ITestEnvironmentAccessor source, MethodFormatter arguments) { + + } + + @Override + public void methodCalled(ITestEnvironmentAccessor source) { + + } + + @Override + public void methodCalledOnObject(ITestEnvironmentAccessor source, String objectName, MethodFormatter arguments, int methodCount) { + + } + + @Override + public void methodCalledOnObject(ITestEnvironmentAccessor source, String objectName, MethodFormatter methodFormat, Xmlizable xmlObject) { + + } + + @Override + public void methodCalledOnObject(ITestEnvironmentAccessor source, String objectName, MethodFormatter methodFormat) { + + } + + @Override + public void methodCalledOnObject(ITestEnvironmentAccessor source, String objectName) { + + } + + @Override + public void methodEnded(ITestEnvironmentAccessor source) { + + } + + @Override + public void requirement(ITestEnvironmentAccessor source, String message) { + + } + + @Override + public void severe(ITestEnvironmentAccessor source, String message) { + + } + + @Override + public void severe(Object source, Throwable thrown) { + + } + + @Override + public void support(ITestEnvironmentAccessor source, String message) { + + } + + @Override + public void testCaseBegan(TestCase testCase) { + + } + + @Override + public void testpoint(ITestEnvironmentAccessor env, TestScript script, TestCase testCase, boolean passed, String testPointName, String exp, String act) { + + } + + @Override + public void testpoint(ITestEnvironmentAccessor env, TestScript script, TestCase testCase, ITestPoint testPoint) { + + } + + @Override + public void testpoint(TestPointRecord record) { + + } + + @Override + public void warning(ITestEnvironmentAccessor source, String message) { + + } + + @Override + public synchronized void addHandler(Handler handler) throws SecurityException { + + } + + @Override + public void config(String msg) { + + } + + @Override + public void entering(String sourceClass, String sourceMethod) { + + } + + @Override + public void entering(String sourceClass, String sourceMethod, Object param1) { + + } + + @Override + public void entering(String sourceClass, String sourceMethod, Object[] params) { + + } + + @Override + public void exiting(String sourceClass, String sourceMethod) { + + } + + @Override + public void exiting(String sourceClass, String sourceMethod, Object result) { + + } + + @Override + public void fine(String msg) { + + } + + @Override + public void finer(String msg) { + + } + + @Override + public void finest(String msg) { + + } + + @Override + public Filter getFilter() { + return super.getFilter(); + } + + @Override + public synchronized Handler[] getHandlers() { + return super.getHandlers(); + } + + @Override + public Level getLevel() { + return super.getLevel(); + } + + @Override + public String getName() { + return super.getName(); + } + + @Override + public Logger getParent() { + return super.getParent(); + } + + @Override + public ResourceBundle getResourceBundle() { + return super.getResourceBundle(); + } + + @Override + public String getResourceBundleName() { + return super.getResourceBundleName(); + } + + @Override + public synchronized boolean getUseParentHandlers() { + return super.getUseParentHandlers(); + } + + @Override + public void info(String msg) { + super.info(msg); + } + + @Override + public boolean isLoggable(Level level) { + return super.isLoggable(level); + } + + @Override + public void log(LogRecord record) { + super.log(record); + } + + @Override + public void log(Level level, String msg) { + super.log(level, msg); + } + + @Override + public void log(Level level, String msg, Object param1) { + super.log(level, msg, param1); + } + + @Override + public void log(Level level, String msg, Object[] params) { + super.log(level, msg, params); + } + + @Override + public void log(Level level, String msg, Throwable thrown) { + super.log(level, msg, thrown); + } + + @Override + public void logp(Level level, String sourceClass, String sourceMethod, String msg) { + super.logp(level, sourceClass, sourceMethod, msg); + } + + @Override + public void logp(Level level, String sourceClass, String sourceMethod, String msg, Object param1) { + super.logp(level, sourceClass, sourceMethod, msg, param1); + } + + @Override + public void logp(Level level, String sourceClass, String sourceMethod, String msg, Object[] params) { + super.logp(level, sourceClass, sourceMethod, msg, params); + } + + @Override + public void logp(Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown) { + super.logp(level, sourceClass, sourceMethod, msg, thrown); + } + + @Override + public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg) { + super.logrb(level, sourceClass, sourceMethod, bundleName, msg); + } + + @Override + public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Object param1) { + super.logrb(level, sourceClass, sourceMethod, bundleName, msg, param1); + } + + @Override + public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Object[] params) { + super.logrb(level, sourceClass, sourceMethod, bundleName, msg, params); + } + + @Override + public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Throwable thrown) { + super.logrb(level, sourceClass, sourceMethod, bundleName, msg, thrown); + } + + @Override + public synchronized void removeHandler(Handler handler) throws SecurityException { + super.removeHandler(handler); + } + + @Override + public void setFilter(Filter newFilter) throws SecurityException { + super.setFilter(newFilter); + } + + @Override + public void setLevel(Level newLevel) throws SecurityException { + super.setLevel(newLevel); + } + + @Override + public void setParent(Logger parent) { + super.setParent(parent); + } + + @Override + public synchronized void setUseParentHandlers(boolean useParentHandlers) { + super.setUseParentHandlers(useParentHandlers); + } + + @Override + public void severe(String msg) { + super.severe(msg); + } + + @Override + public void throwing(String sourceClass, String sourceMethod, Throwable thrown) { + super.throwing(sourceClass, sourceMethod, thrown); + } + + @Override + public void warning(String msg) { + super.warning(msg); + } + + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public String toString() { + return super.toString(); + } + + }; + + private final ITimerControl timerCtrl = new ITimerControl() { + + @Override + public void addTask(final EnvironmentTask task, TestEnvironment environment) { + final ScheduledFuture<?> handle = schedulePeriodicTask(new Runnable() { + @Override + public void run() { + + try { + if (task.isRunning()) { + task.runOneCycle(); + } + } catch (Throwable ex) { + ScheduledFuture<?> h = handleMap.get(task); + if (h != null) { + h.cancel(false); + } + ex.printStackTrace(System.err); + } + } + + }, 0, (long) Math.rint(1000.0 / task.getHzRate())); + handleMap.put(task, handle); + } + + @Override + public void cancelAllTasks() { + for (ScheduledFuture<?> handle : handleMap.values()) { + handle.cancel(false); + } + handleMap.clear(); + } + + @Override + public void cancelTimers() { + executor.shutdown(); + } + + @Override + public void dispose() { + + } + + @Override + public void envWait(ITimeout obj, int milliseconds) throws InterruptedException { + synchronized (obj) { + obj.wait(milliseconds); + } + } + + @Override + public void envWait(int milliseconds) throws InterruptedException { + envWait(new BasicTimeout(), milliseconds); + } + + @Override + public int getCycleCount() { + return (int) System.currentTimeMillis() / 20; + } + + @Override + public long getEnvTime() { + return System.currentTimeMillis(); + } + + @Override + public void incrementCycleCount() { + + } + + @Override + public void removeTask(final EnvironmentTask task) { + ScheduledFuture<?> handle = handleMap.remove(task); + if (handle != null) { + handle.cancel(false); + } + } + + @Override + public void setCycleCount(int cycle) { + + } + + @Override + public ICancelTimer setTimerFor(final ITimeout objToNotify, int milliseconds) { + objToNotify.setTimeout(false); + final ScheduledFuture<?> handle = scheduleOneShotTask(new Runnable() { + + @Override + public void run() { + synchronized (objToNotify) { + objToNotify.setTimeout(true); + objToNotify.notify(); + } + } + }, milliseconds); + + return new ICancelTimer() { + + @Override + public void cancelTimer() { + handle.cancel(false); + } + }; + } + + @Override + public void step() { + + } + + @Override + public long getTimeOfDay() { + return 0; + } + + @Override + public void setRunManager(IRunManager runManager) { + } + + @Override + public IRunManager getRunManager() { + return null; + } + + @Override + public boolean isRealtime() { + return false; + } + + }; + + public UnitTestAccessor() { + + } + + @Override + public IMessageManager getMsgManager() { + return null; + } + + @Override + public boolean isPhysicalTypeAvailable(DataType physicalType) { + return physicalType == TestMemType.ETHERNET; + } + + @Override + public void abortTestScript() { + + } + + @Override + public void abortTestScript(Throwable t) { + + } + + @Override + public boolean addTask(final EnvironmentTask task) { + timerCtrl.addTask(task, null); + return true; + } + + public void addRunnable(Runnable r) { + + } + + @Override + public void associateObject(Class<?> c, Object obj) { + + } + + @Override + public Object getAssociatedObject(Class<?> c) { + return null; + } + + @Override + public Set<Class<?>> getAssociatedObjects() { + + return null; + } + + public ITestPointTally getAttachedTestPointTally(TestScript script) { + + return null; + } + + @Override + public long getEnvTime() { + return timerCtrl.getEnvTime(); + } + + @Override + public IExecutionUnitManagement getExecutionUnitManagement() { + return null; + } + + @Override + public ITestLogger getLogger() { + return testLogger; + } + + @Override + public IScriptControl getScriptCtrl() { + return scriptCtrl; + } + + @Override + public TestScript getTestScript() { + return null; + } + + @Override + public ITestStation getTestStation() { + return null; + } + + @Override + public ITimerControl getTimerCtrl() { + return timerCtrl; + } + + @Override + public void onScriptComplete() throws InterruptedException { + + } + + @Override + public void onScriptSetup() { + + } + + public void setSequentialCmdFinished(CommandDescription description, CommandEndedStatusEnum status) throws Exception { + + } + + @Override + public ICancelTimer setTimerFor(ITimeout listener, int time) { + return timerCtrl.setTimerFor(listener, time); + } + + public ScheduledFuture<?> schedulePeriodicTask(Runnable task, long initialDelay, long period) { + return executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS); + } + + public ScheduledFuture<?> scheduleOneShotTask(Runnable task, long delay) { + return executor.schedule(task, delay, TimeUnit.MILLISECONDS); + } + + public void shutdown() { + timerCtrl.cancelAllTasks(); + timerCtrl.cancelTimers(); + } + + @Override + public TestCase getTestCase() { + + return null; + } + + @Override + public Set<DataType> getDataTypes() { + return null; + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/UnitTestSupport.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/UnitTestSupport.java new file mode 100644 index 000000000..70fe316d2 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/mock/UnitTestSupport.java @@ -0,0 +1,439 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.mock; + +import java.util.Random; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import org.junit.Assert; +import org.eclipse.osee.ote.core.TestException; +import org.eclipse.osee.ote.core.environment.EnvironmentTask; +import org.eclipse.osee.ote.core.testPoint.CheckGroup; +import org.eclipse.osee.ote.core.testPoint.CheckPoint; +import org.eclipse.osee.ote.core.testPoint.Operation; +import org.eclipse.osee.ote.message.MessageSystemException; +import org.eclipse.osee.ote.message.data.MessageData; +import org.eclipse.osee.ote.message.elements.DiscreteElement; +import org.eclipse.osee.ote.message.enums.DataType; +import org.eclipse.osee.ote.message.listener.IOSEEMessageListener; + +public class UnitTestSupport { + + private final UnitTestAccessor accessor = new UnitTestAccessor(); + private final Random rand = new Random(); + + public UnitTestSupport() { + + } + + public void activateMsg(final TestMessage msg) { + accessor.addTask(new EnvironmentTask(msg.getRate(), 0) { + + @Override + public void runOneCycle() throws InterruptedException, TestException { + TestMessageData data = msg.getActiveDataSource(); + data.incrementActivityCount(); + data.notifyListeners(); + + } + + }); + } + + public void cleanup() { + accessor.shutdown(); + } + + public <T extends Comparable<T>> void setAfter(final DiscreteElement<T> element, final T value, int millis) { + accessor.scheduleOneShotTask(new Runnable() { + + @Override + public void run() { + element.setValue(value); + } + + }, millis); + } + + public <T extends Comparable<T>> void maintain(final DiscreteElement<T> element, final T value, final T postValue, int millis) { + element.setValue(value); + accessor.scheduleOneShotTask(new Runnable() { + + @Override + public void run() { + element.setValue(postValue); + } + + }, millis); + } + + public <T extends Comparable<T>> ISequenceHandle setSequence(final DiscreteElement<T> element, final T[] sequence) throws InterruptedException { + final SequenceHandle seqHandle = new SequenceHandle(); + element.setValue(sequence[0]); + IOSEEMessageListener listener = new IOSEEMessageListener() { + int index = 1; + + @Override + public void onDataAvailable(MessageData data, DataType type) throws MessageSystemException { + if (index < sequence.length) { + element.setValue(sequence[index]); + System.out.println(element.getName() + " is now " + element.getValue()); + index++; + } else { + element.getMessage().removeListener(this); + seqHandle.signalEndSequence(); + } + } + + @Override + public void onInitListener() throws MessageSystemException { + + } + + }; + + // wait for a transmission so that the sequence begins on transimssion edges + element.getMessage().waitForTransmission(accessor); + element.getMessage().addListener(listener); + return seqHandle; + } + + public <T extends Comparable<T>> ScheduledFuture<?> maintainRandomizedList(final DiscreteElement<T> element, final T[] values, int millis) { + element.setValue(values[0]); + return accessor.schedulePeriodicTask(new Runnable() { + @Override + public void run() { + try { + element.setValue(selectRandom(values)); + } catch (Exception e) { + e.printStackTrace(System.err); + } + } + }, 0, millis); + } + + public <T extends Comparable<T>> void checkRange(final DiscreteElement<T> element, final T minValue, boolean minInclusive, T maxValue, boolean maxInclusive, int millis) throws InterruptedException { + Assert.assertTrue(element.getName() + ".checkRange()->failed", + element.checkRange(accessor, null, minValue, minInclusive, maxValue, maxInclusive, millis)); + } + + public <T extends Comparable<T>> void checkMaintainRange(final DiscreteElement<T> element, final T minValue, boolean minInclusive, T maxValue, boolean maxInclusive, int millis) throws InterruptedException { + CheckGroup chkGrp = new CheckGroup(Operation.AND, "checkMaintainRangeGrp"); + T val = element.checkMaintainRange(accessor, chkGrp, minValue, minInclusive, maxValue, maxInclusive, millis); + Assert.assertTrue(element.getName() + ".checkMaintainRange()->failed, value=" + val.toString(), chkGrp.isPass()); + } + + public <T extends Comparable<T>> void checkMaintainRangeFail(final DiscreteElement<T> element, final T minValue, boolean minInclusive, T maxValue, boolean maxInclusive, int millis) throws InterruptedException { + CheckGroup chkGrp = new CheckGroup(Operation.AND, "checkMaintainRangeFailGrp"); + T val = element.checkMaintainRange(accessor, chkGrp, minValue, minInclusive, maxValue, maxInclusive, millis); + Assert.assertFalse(element.getName() + ".checkMaintainRangeFail()->failed, value=" + val.toString(), + chkGrp.isPass()); + } + + public <T extends Comparable<T>> void checkMaintainNotRange(final DiscreteElement<T> element, final T minValue, boolean minInclusive, T maxValue, boolean maxInclusive, int millis) throws InterruptedException { + CheckGroup chkGrp = new CheckGroup(Operation.AND, "checkMaintainRangeGrp"); + T val = element.checkMaintainNotRange(accessor, chkGrp, minValue, minInclusive, maxValue, maxInclusive, millis); + Assert.assertTrue(element.getName() + ".checkMaintainNotRange()->failed, value=" + val.toString(), + chkGrp.isPass()); + } + + public <T extends Comparable<T>> void checkMaintainNotRangeFail(final DiscreteElement<T> element, final T minValue, boolean minInclusive, T maxValue, boolean maxInclusive, int millis) throws InterruptedException { + CheckGroup chkGrp = new CheckGroup(Operation.AND, "checkMaintainRangeGrp"); + T val = element.checkMaintainNotRange(accessor, chkGrp, minValue, minInclusive, maxValue, maxInclusive, millis); + Assert.assertFalse(element.getName() + ".checkMaintainNotRangeFail()->failed, value=" + val.toString(), + chkGrp.isPass()); + } + + public <T extends Comparable<T>> void checkRangeFail(final DiscreteElement<T> element, final T minValue, boolean minInclusive, T maxValue, boolean maxInclusive, int millis) throws InterruptedException { + Assert.assertFalse(element.getName() + ".checkRangeFail()->failed", + element.checkRange(accessor, null, minValue, minInclusive, maxValue, maxInclusive, millis)); + } + + public <T extends Comparable<T>> void checkMaintain(DiscreteElement<T> element, T value, int millis) throws InterruptedException { + T result = element.checkMaintain(accessor, null, value, millis); + Assert.assertEquals(element.getName() + ".checkMaintain()->failed", value, result); + } + + public <T extends Comparable<T>> void checkMaintainFail(DiscreteElement<T> element, T maintainValue, T discontinuity, int millis) { + try { + T result = element.checkMaintain(accessor, null, maintainValue, millis); + Assert.assertEquals(element.getName() + ".checkMaintainFail()->failed", discontinuity, result); + } catch (InterruptedException e) { + Assert.fail("Exception"); + } + } + + public <T extends Comparable<T>> void checkNot(DiscreteElement<T> element, T value, int millis) { + CheckGroup grp = new CheckGroup(Operation.AND, "checkNotCheckGroup"); + try { + boolean c = element.checkNot(accessor, grp, value, millis); + CheckPoint cp = (CheckPoint) grp.getTestPoints().get(0); + long elapsedTime = cp.getElpasedTime(); + Assert.assertTrue(element.getName() + String.format(".checkNot()->failed, elapsed time=%d", elapsedTime), c); + System.out.printf("checkNot->passed, actual %s, expected %s, elapsed=%d\n", cp.getActual(), cp.getExpected(), + cp.getElpasedTime()); + } catch (InterruptedException e) { + Assert.fail("Exception"); + } + } + + public <T extends Comparable<T>> void checkNotFail(DiscreteElement<T> element, T value, int millis) { + try { + Assert.assertFalse(element.getName() + " .checkNotFail()->failed", + element.checkNot(accessor, null, value, millis)); + } catch (InterruptedException e) { + Assert.fail("Exception"); + } + } + + public <T extends Comparable<T>> void check(DiscreteElement<T> element, T value) throws InterruptedException { + CheckGroup grp = new CheckGroup(Operation.AND, "checkCheckGrp"); + boolean c = element.check(accessor, grp, value); + long time = ((CheckPoint) grp.getTestPoints().get(0)).getElpasedTime(); + Assert.assertTrue( + element.getName() + String.format(".check()->failed, elapsed=%d, expect=<%s>, actual=<%s>", time, value, + element.getValue()), c); + } + + public <T extends Comparable<T>> void checkWaitForValue(DiscreteElement<T> element, T value, int millis) throws InterruptedException { + T result = element.waitForValue(accessor, value, millis); + Assert.assertEquals(element.getName() + " .checkWaitForValue()->failed", value, result); + } + + public <T extends Comparable<T>> void checkList(DiscreteElement<T> element, T[] values, int millis) throws InterruptedException { + Assert.assertTrue(element.getName() + " .checkList()->failed", element.checkInList(accessor, values, millis)); + } + + public <T extends Comparable<T>> void checkNotInList(DiscreteElement<T> element, T[] values, int millis) throws InterruptedException { + Assert.assertTrue(element.getName() + " .checkNotInList()->failed", + element.checkNotInList(accessor, values, millis)); + } + + public <T extends Comparable<T>> void checkNotInListFail(DiscreteElement<T> element, T[] values, int millis) throws InterruptedException { + CheckGroup chkGrp = new CheckGroup(Operation.AND, "checkNotInListFail"); + element.checkNotInList(accessor, chkGrp, values, millis); + Assert.assertFalse(element.getName() + " .checkNotInListFail()->failed", chkGrp.isPass()); + } + + public <T extends Comparable<T>> void checkListFail(DiscreteElement<T> element, T[] values, int millis) throws InterruptedException { + CheckGroup chkGrp = new CheckGroup(Operation.AND, "checkListFail"); + boolean b = element.checkInList(accessor, chkGrp, values, millis); + CheckPoint cp = (CheckPoint) ((CheckGroup) chkGrp.getTestPoints().get(0)).getTestPoints().get(0); + Assert.assertFalse( + element.getName() + String.format(" .checkListFail()->failed, elapsed time=%d", cp.getElpasedTime()), b); + } + + public <T extends Comparable<T>> void checkMaintainList(DiscreteElement<T> element, T[] values, int millis) throws InterruptedException { + CheckGroup chkGrp = new CheckGroup(Operation.AND, "checkMaintianList"); + T val = element.checkMaintainList(accessor, chkGrp, values, true, millis); + Assert.assertTrue(element.getName() + " .checkMaintainList()->failed, value=" + val.toString(), chkGrp.isPass()); + } + + public <T extends Comparable<T>> void checkMaintainListFail(DiscreteElement<T> element, T[] values, int millis) throws InterruptedException { + CheckGroup chkGrp = new CheckGroup(Operation.AND, "checkMaintainListFail"); + T val = element.checkMaintainList(accessor, chkGrp, values, true, millis); + Assert.assertFalse(element.getName() + " .checkMaintainListFail()->failed, value=" + val.toString(), + chkGrp.isPass()); + } + + public <T extends Comparable<T>> void checkWaitForValueFail(DiscreteElement<T> element, T value, int millis) throws InterruptedException { + T result = element.waitForValue(accessor, value, millis); + boolean b = value.equals(result); + Assert.assertFalse( + element.getName() + String.format(" .checkWaitForValueFail()->failed, expect=%s, actual=%s", value, result), b); + } + + public <T extends Comparable<T>> void checkPulse(DiscreteElement<T> element, T pulsedValue, T nonPulsedValue) throws InterruptedException { + Assert.assertTrue(element.getName() + " .checkPulse()->failed", + element.checkPulse(accessor, pulsedValue, nonPulsedValue)); + } + + public <T extends Comparable<T>> void checkPulseFail(DiscreteElement<T> element, T pulsedValue, T nonPulsedValue) throws InterruptedException { + Assert.assertFalse(element.getName() + " .checkPulseFail()->failed", + element.checkPulse(accessor, pulsedValue, nonPulsedValue)); + } + + public <T extends Comparable<T>> void genericTestCheckNot(DiscreteElement<T> element, T[] values) throws InterruptedException { + if (values.length < 2) { + throw new IllegalArgumentException("array needs atleast two values"); + } + System.out.println("genericTestCheckNot()"); + // check pass conditions + for (int i = values.length - 1; i >= 1; i--) { + T notValue = values[i]; + T goodValue = values[i - 1]; + System.out.format("\tgoodValue=%s, notValue=%s\n", goodValue, notValue); + element.setValue(notValue); + setAfter(element, goodValue, 100); + checkNot(element, notValue, 210); + check(element, goodValue); // make sure we did not pass until the goodValue was transmitted + } + + // check fail conditions + for (T value : values) { + element.setValue(value); + checkNotFail(element, value, 100); + } + } + + public <T extends Comparable<T>> void genericCheckMaintain(DiscreteElement<T> element, T[] values) throws InterruptedException { + if (values.length < 2) { + throw new IllegalArgumentException("array needs atleast two values"); + } + + // check pass conditions + for (T value : values) { + element.setValue(value); + checkMaintain(element, value, 200); + check(element, value); // make sure we pass for the right reasons e.q. no false positives + } + + // check fail conditions + for (int i = values.length - 1; i >= 1; i--) { + T valueToMaintain = values[i]; + T badValue = values[i - 1]; + element.setValue(valueToMaintain); + setAfter(element, badValue, 100); + checkMaintainFail(element, valueToMaintain, badValue, 200); // make sure we fail as expected, no false negatives + } + } + + public <T extends Comparable<T>> void genericTestCheckWaitForValue(DiscreteElement<T> element, T[] values, T valueToFInd) throws InterruptedException { + // check sequence + element.getMessage().waitForTransmission(accessor); + ISequenceHandle handle = setSequence(element, values); + for (T v : values) { + checkWaitForValue(element, v, 40); + } + handle.waitForEndSequence(100, TimeUnit.MILLISECONDS); + setSequence(element, values); + checkWaitForValueFail(element, valueToFInd, 200); + } + + public <T extends Comparable<T>> void genericTestCheckList(DiscreteElement<T> element, T[] goodValues, T[] badValues) throws InterruptedException { + // check finding of every item in the list + for (T v : goodValues) { + element.setValue(selectRandom(badValues)); // value will always not be in the list until after 40 ms + setAfter(element, v, 40); + checkList(element, goodValues, 100); + } + + // check pass + maintainRandomizedList(element, goodValues, 500); + checkList(element, goodValues, 500); + + // check failure + maintainRandomizedList(element, badValues, 500); + checkListFail(element, goodValues, 500); + } + + public <T extends Comparable<T>> void genericTestCheckNotList(DiscreteElement<T> element, T[] allowedValues, T[] excludeValues) throws InterruptedException { + // check transition from fail to pass within time period + for (T v : excludeValues) { + element.setValue(selectRandom(allowedValues)); // value will always not be in the list until after 40 ms + setAfter(element, v, 40); + checkNotInList(element, excludeValues, 100); + } + + // check pass + ScheduledFuture<?> handle = maintainRandomizedList(element, allowedValues, 10); + try { + checkNotInList(element, excludeValues, 10); + } finally { + try { + handle.cancel(false); + handle.get(); + } catch (CancellationException ex) { + // do nothing + } catch (ExecutionException ex) { + throw new RuntimeException("exception while waiting for randomizedList to finish", ex); + } + } + + // check failure + handle = maintainRandomizedList(element, excludeValues, 10); + try { + checkNotInListFail(element, excludeValues, 550); + } finally { + try { + handle.cancel(false); + handle.get(); + } catch (CancellationException ex) { + // do nothing + } catch (ExecutionException ex) { + throw new RuntimeException("exception while waiting for randomizedList to finish", ex); + } + } + } + + public <T extends Comparable<T>> void genericTestCheckMaintainList(DiscreteElement<T> element, T[] values, T badValue) throws InterruptedException { + // visit each possible value in list, make sure checkMaintainList does not fail + element.setValue(values[0]); + int timeStep = 40; + for (T val : values) { + setAfter(element, val, timeStep); + } + checkMaintainList(element, values, 1000); + + // random elements from list + ScheduledFuture<?> handle = maintainRandomizedList(element, values, 15); + try { + checkMaintainList(element, values, 2000); + } finally { + try { + handle.cancel(false); + handle.get(); + } catch (CancellationException ex) { + // do nothing + } catch (ExecutionException ex) { + throw new RuntimeException("exception while waiting for randomizedList to finish", ex); + } + } + + // check proper failure behavior + handle = maintainRandomizedList(element, values, 25); + try { + setAfter(element, badValue, 1500); + checkMaintainListFail(element, values, 2000); + checkWaitForValue(element, badValue, 0); + } finally { + try { + handle.cancel(false); + handle.get(); + } catch (CancellationException ex) { + // do nothing + } catch (ExecutionException ex) { + throw new RuntimeException("exception while waiting for randomizedList to finish", ex); + } + } + } + + public <T> T selectRandom(T[] list) { + return list[Math.abs(rand.nextInt(list.length))]; + + } + + public void checkForTransmission(TestMessage msg, int numXmits, int millis) throws InterruptedException { + Assert.assertTrue(msg.getName() + " failed to transmit " + numXmits + " times in " + millis + "ms", + msg.checkForTransmissions(accessor, numXmits, millis)); + } + + public void checkForTransmissionFail(TestMessage msg, int numXmits, int millis) throws InterruptedException { + Assert.assertFalse(msg.getName() + " had at least " + numXmits + " transmissions", + msg.checkForTransmissions(accessor, numXmits, millis)); + } + + public boolean inRange(int target, int tolerance, int actual) { + return Math.abs(actual - target) <= tolerance; + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestEnumElementOperations.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestEnumElementOperations.java new file mode 100644 index 000000000..74a657812 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestEnumElementOperations.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.other; + +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_0; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_1; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_10; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_2; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_3; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_4; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_5; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_6; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_7; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_8; +import static org.eclipse.osee.ote.message.mock.TestEnum.VAL_9; +import java.util.EnumSet; +import org.eclipse.osee.ote.message.mock.TestEnum; +import org.eclipse.osee.ote.message.mock.TestMessage; +import org.eclipse.osee.ote.message.mock.UnitTestSupport; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class TestEnumElementOperations { + + private UnitTestSupport support; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + support = new UnitTestSupport(); + } + + @After + public void tearDown() throws Exception { + support.cleanup(); + } + + @Test + public void testCheckWaitForValue() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + support.genericTestCheckWaitForValue(msg.ENUM_ELEMENT_1, EnumSet.of(VAL_0, VAL_9).toArray(new TestEnum[] {}), + VAL_10); + + support.genericTestCheckWaitForValue(msg.ENUM_ELEMENT_1, EnumSet.of(VAL_1, VAL_10).toArray(new TestEnum[] {}), + VAL_0); + } + + @Test + public void testCheckNot() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + support.genericTestCheckNot(msg.ENUM_ELEMENT_1, TestEnum.values()); + } + + @Test + public void testCheckMaintain() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + support.genericCheckMaintain(msg.ENUM_ELEMENT_1, TestEnum.values()); + } + + @Test + public void testCheckList() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + support.genericTestCheckList(msg.ENUM_ELEMENT_1, new TestEnum[] {VAL_0, VAL_2, VAL_4, VAL_6, VAL_8, VAL_10}, + new TestEnum[] {VAL_1, VAL_3, VAL_5, VAL_7, VAL_9}); + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestFloat32Operations.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestFloat32Operations.java new file mode 100644 index 000000000..f8aa274d5 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestFloat32Operations.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.other; + +import org.eclipse.osee.ote.message.mock.TestMessage; +import org.eclipse.osee.ote.message.mock.UnitTestSupport; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class TestFloat32Operations { + private UnitTestSupport support; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + support = new UnitTestSupport(); + } + + @After + public void tearDown() throws Exception { + support.cleanup(); + } + + @Test + public void testCheckWaitForValue() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + support.genericTestCheckWaitForValue(msg.FLOAT32_ELEMENT_1, new Double[] { + new Double(0.0f), + new Double(0.2f), + new Double(100.0f), + new Double(999.00075f)}, new Double(33.4001f)); + + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestIntegerOperations.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestIntegerOperations.java new file mode 100644 index 000000000..923d372b3 --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestIntegerOperations.java @@ -0,0 +1,333 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.other; + +import org.eclipse.osee.ote.message.elements.IntegerElement; +import org.eclipse.osee.ote.message.mock.TestMessage; +import org.eclipse.osee.ote.message.mock.UnitTestSupport; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class TestIntegerOperations { + + private UnitTestSupport support; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + support = new UnitTestSupport(); + } + + @After + public void tearDown() throws Exception { + support.cleanup(); + } + + /** + * Not sure how this is even supposed to work. + * + * @throws InterruptedException + */ + @Ignore + @Test + public void testCheckWaitForValue() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + int maxElementValue = getMaxIntValue(msg.INT_ELEMENT_1); + support.genericTestCheckWaitForValue(msg.INT_ELEMENT_1, new Integer[] { + 10, + 30, + 31, + 30, + 50, + 75, + maxElementValue, + maxElementValue, + 400, + 1, + maxElementValue, + 0, + maxElementValue, + 1, + 0, + 1, + 2}, 49); + } + + @Test + public void testCheckNot() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + int maxElementValue = getMaxIntValue(msg.INT_ELEMENT_1); + support.genericTestCheckNot(msg.INT_ELEMENT_1, new Integer[] {0, maxElementValue, 10, 100, 1000, 5}); + } + + @Test + public void testCheckMaintain() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + int maxElementValue = getMaxIntValue(msg.INT_ELEMENT_1); + support.genericCheckMaintain(msg.INT_ELEMENT_1, new Integer[] { + 0, + maxElementValue, + 0, + 0, + maxElementValue, + maxElementValue, + 1000, + 5, + 1, + maxElementValue - 1}); + } + + @Test + public void testCheckRange() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + msg.INT_ELEMENT_1.setValue(50); + support.checkRange(msg.INT_ELEMENT_1, 0, true, 100, true, 100); + + // test lower bound, inclusive + msg.INT_ELEMENT_1.setValue(0); + support.checkRangeFail(msg.INT_ELEMENT_1, 1, true, 100, true, 100); + support.setAfter(msg.INT_ELEMENT_1, 1, 50); + support.checkRange(msg.INT_ELEMENT_1, 1, true, 100, true, 100); + + // test upper bound, inclusive + msg.INT_ELEMENT_1.setValue(101); + support.checkRangeFail(msg.INT_ELEMENT_1, 1, true, 100, true, 100); + support.setAfter(msg.INT_ELEMENT_1, 100, 50); + support.checkRange(msg.INT_ELEMENT_1, 1, true, 100, true, 100); + + // test middle + msg.INT_ELEMENT_1.setValue(1000); + support.setAfter(msg.INT_ELEMENT_1, 50, 50); + support.checkRange(msg.INT_ELEMENT_1, 1, true, 100, true, 100); + + // test lower bound, exclusive + msg.INT_ELEMENT_1.setValue(1); + support.checkRangeFail(msg.INT_ELEMENT_1, 1, false, 100, false, 100); // make sure lower bound is exclusive + support.setAfter(msg.INT_ELEMENT_1, 2, 50); + support.checkRange(msg.INT_ELEMENT_1, 1, false, 100, false, 100); + + // test upper bound, exclusive + msg.INT_ELEMENT_1.setValue(100); + support.checkRangeFail(msg.INT_ELEMENT_1, 1, false, 100, false, 100); // make sure upper bound is exclusive + support.setAfter(msg.INT_ELEMENT_1, 99, 50); + support.checkRange(msg.INT_ELEMENT_1, 1, false, 100, false, 100); + + // check multiple out of range values + msg.INT_ELEMENT_1.setValue(10); + support.setAfter(msg.INT_ELEMENT_1, 100, 20); + support.setAfter(msg.INT_ELEMENT_1, 9, 40); + support.setAfter(msg.INT_ELEMENT_1, 101, 60); + support.checkRangeFail(msg.INT_ELEMENT_1, 10, false, 100, false, 160); + support.checkWaitForValue(msg.INT_ELEMENT_1, 101, 0); // make sure we did not fail before we should have + } + + @Test + public void testCheckMaintainRange() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + msg.INT_ELEMENT_1.setValue(50); + support.checkMaintainRange(msg.INT_ELEMENT_1, 0, true, 100, true, 100); + + msg.INT_ELEMENT_1.setValue(0); + support.checkMaintainRange(msg.INT_ELEMENT_1, 0, true, 100, true, 100); + + msg.INT_ELEMENT_1.setValue(100); + support.checkMaintainRange(msg.INT_ELEMENT_1, 0, true, 100, true, 100); + + // check lower bound, inclusive + msg.INT_ELEMENT_1.setValue(10); + support.setAfter(msg.INT_ELEMENT_1, 9, 50); + support.checkMaintainRangeFail(msg.INT_ELEMENT_1, 10, true, 100, true, 100); + + // check upper bound, inclusive + msg.INT_ELEMENT_1.setValue(100); + support.setAfter(msg.INT_ELEMENT_1, 101, 50); + support.checkMaintainRangeFail(msg.INT_ELEMENT_1, 10, true, 100, true, 100); + support.checkWaitForValue(msg.INT_ELEMENT_1, 101, 0); + + // check lower bound, exclusive + msg.INT_ELEMENT_1.setValue(1); + support.setAfter(msg.INT_ELEMENT_1, 0, 50); + support.checkMaintainRangeFail(msg.INT_ELEMENT_1, 0, false, 100, false, 100); + support.checkWaitForValue(msg.INT_ELEMENT_1, 0, 0); + + // check upper bound, exclusive + msg.INT_ELEMENT_1.setValue(99); + support.setAfter(msg.INT_ELEMENT_1, 100, 50); + support.checkMaintainRangeFail(msg.INT_ELEMENT_1, 0, false, 100, false, 100); + + // check bouncing between upper and lower inclusive + msg.INT_ELEMENT_1.setValue(0); + support.setAfter(msg.INT_ELEMENT_1, 100, 20); + support.setAfter(msg.INT_ELEMENT_1, 50, 40); + support.setAfter(msg.INT_ELEMENT_1, 1, 60); + support.checkMaintainRange(msg.INT_ELEMENT_1, 0, true, 100, true, 100); + support.checkWaitForValue(msg.INT_ELEMENT_1, 1, 0); + + // check approaching upper fail boundary + msg.INT_ELEMENT_1.setValue(97); + support.setAfter(msg.INT_ELEMENT_1, 98, 20); + support.setAfter(msg.INT_ELEMENT_1, 99, 40); + support.setAfter(msg.INT_ELEMENT_1, 100, 60); + support.checkMaintainRangeFail(msg.INT_ELEMENT_1, 0, false, 100, false, 100); + + // check approaching lower fail boundary + msg.INT_ELEMENT_1.setValue(3); + support.setAfter(msg.INT_ELEMENT_1, 2, 20); + support.setAfter(msg.INT_ELEMENT_1, 1, 40); + support.setAfter(msg.INT_ELEMENT_1, 0, 60); + support.checkMaintainRangeFail(msg.INT_ELEMENT_1, 0, false, 100, false, 100); + + // check mixed inclusive, exclusive + msg.INT_ELEMENT_1.setValue(1); + support.setAfter(msg.INT_ELEMENT_1, 100, 60); + support.checkMaintainRange(msg.INT_ELEMENT_1, 0, false, 100, true, 100); + + // check mixed inclusive, exclusive + msg.INT_ELEMENT_1.setValue(99); + support.setAfter(msg.INT_ELEMENT_1, 0, 60); + support.checkMaintainRange(msg.INT_ELEMENT_1, 0, true, 100, false, 100); + + // check bouncing between upper and lower exclusive + support.setSequence(msg.INT_ELEMENT_1, new Integer[] {1, 99, 50, 2, 2, 2, 1, 2, 1, 2, 44}); + support.checkMaintainRange(msg.INT_ELEMENT_1, 0, false, 100, false, 240); + support.checkWaitForValue(msg.INT_ELEMENT_1, 44, 0); + + // check fail fast behavior + msg.INT_ELEMENT_1.setValue(1); + support.setAfter(msg.INT_ELEMENT_1, 100, 30); + support.setAfter(msg.INT_ELEMENT_1, 50, 60); + support.checkMaintainRangeFail(msg.INT_ELEMENT_1, 0, false, 100, false, 100); + support.checkWaitForValue(msg.INT_ELEMENT_1, 100, 0); // should immediately check value since it should have caused the above to fail + support.checkWaitForValue(msg.INT_ELEMENT_1, 50, 100); + } + + @Test + public void testCheckMaintainNotRange() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + // bounce around boundaries, make sure it doesn't fail, inclusive + support.setSequence(msg.INT_ELEMENT_1, new Integer[] {0, 101, 9, 102, 8, 102, 101, 101, 101, 9, 9, 101, 9}); + support.checkMaintainNotRange(msg.INT_ELEMENT_1, 10, true, 100, true, 400); + + // bounce around boundaries, make sure it doesn't fail, exclusive + msg.INT_ELEMENT_1.setValue(0); + support.setAfter(msg.INT_ELEMENT_1, 100, 20); + support.setAfter(msg.INT_ELEMENT_1, 10, 40); + support.setAfter(msg.INT_ELEMENT_1, 101, 60); + support.setAfter(msg.INT_ELEMENT_1, 9, 80); + support.checkMaintainNotRange(msg.INT_ELEMENT_1, 10, false, 100, false, 200); + + // explore boundary then cause failure, exclusive + msg.INT_ELEMENT_1.setValue(10); + support.setAfter(msg.INT_ELEMENT_1, 100, 20); + support.setAfter(msg.INT_ELEMENT_1, 55, 80); + support.checkMaintainNotRangeFail(msg.INT_ELEMENT_1, 10, false, 100, false, 200); + support.checkWaitForValue(msg.INT_ELEMENT_1, 55, 0); // make sure our plan failure actually caused it to fail + + // explore boundary then cause failure, inclusive + support.setSequence(msg.INT_ELEMENT_1, new Integer[] {9, 9, 101, 101, 9, 101, 55}); + support.checkMaintainNotRangeFail(msg.INT_ELEMENT_1, 10, true, 100, true, 200); + support.checkWaitForValue(msg.INT_ELEMENT_1, 55, 0); // make sure our planned failure actually caused it to fail + + // check transition to upper boundary causes failure, inclusive + support.maintain(msg.INT_ELEMENT_1, 105, 100, 50); + support.checkMaintainNotRangeFail(msg.INT_ELEMENT_1, 10, true, 100, true, 200); + support.checkWaitForValue(msg.INT_ELEMENT_1, 100, 0); // make sure our planned failure actually caused it to fail + + // check transition to upper boundary causes failure, exclusive + support.maintain(msg.INT_ELEMENT_1, 100, 99, 50); + support.checkMaintainNotRangeFail(msg.INT_ELEMENT_1, 10, false, 100, false, 200); + support.checkWaitForValue(msg.INT_ELEMENT_1, 99, 0); // make sure our planned failure actually caused it to fail + + // check transition to lower boundary causes failure, inclusive + support.maintain(msg.INT_ELEMENT_1, 9, 10, 50); + support.checkMaintainNotRangeFail(msg.INT_ELEMENT_1, 10, true, 100, true, 200); + support.checkWaitForValue(msg.INT_ELEMENT_1, 10, 0); // make sure our planned failure actually caused it to fail + + // check transition to lower boundary causes failure, exclusive + support.maintain(msg.INT_ELEMENT_1, 10, 11, 50); + support.checkMaintainNotRangeFail(msg.INT_ELEMENT_1, 10, false, 100, false, 200); + support.checkWaitForValue(msg.INT_ELEMENT_1, 11, 0); // make sure our planned failure actually caused it to fail + } + + @Test + public void testCheckList() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + support.genericTestCheckList(msg.INT_ELEMENT_1, new Integer[] {20, 40, 50, 60, 80, 100}, new Integer[] { + 200, + 300, + 400, + 500, + 600}); + } + + @Test + public void testCheckNotInList() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + support.genericTestCheckNotList(msg.INT_ELEMENT_1, new Integer[] {20, 40, 50, 60, 80, 100}, new Integer[] { + 200, + 300, + 400, + 500, + 600}); + } + + @Test + public void testCheckMaintainList() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + Integer[] values = {20, 40, 50, 60, 80}; + support.genericTestCheckMaintainList(msg.INT_ELEMENT_1, values, 999); + } + + @Test + public void testCheckPulse() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + + support.setSequence(msg.INT_ELEMENT_1, new Integer[] {2, 100, 33, 100, 2, 99}); + support.checkPulse(msg.INT_ELEMENT_1, 100, 99); + + support.setSequence(msg.INT_ELEMENT_1, new Integer[] {100, 100, 99}); + support.checkPulse(msg.INT_ELEMENT_1, 100, 99); + } + + private int getMaxIntValue(IntegerElement e) { + return (1 << e.getBitLength()) - 1; + } +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestMessageOperations.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestMessageOperations.java new file mode 100644 index 000000000..c064a747b --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestMessageOperations.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.other; + +import org.eclipse.osee.framework.jdk.core.util.benchmark.Benchmark; +import org.eclipse.osee.ote.message.MessageSystemException; +import org.eclipse.osee.ote.message.data.MessageData; +import org.eclipse.osee.ote.message.enums.DataType; +import org.eclipse.osee.ote.message.listener.IOSEEMessageListener; +import org.eclipse.osee.ote.message.mock.TestMessage; +import org.eclipse.osee.ote.message.mock.UnitTestSupport; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class TestMessageOperations { + private UnitTestSupport support; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + support = new UnitTestSupport(); + } + + @After + public void tearDown() throws Exception { + support.cleanup(); + } + + /** + * We noticed this failing due to being overly picky about timing, needs to be evaluated to be fixed or removed. + * + * @throws InterruptedException + */ + @Ignore + @Test + public void testTransmissionRate() throws InterruptedException { + Benchmark.setBenchmarkingEnabled(true); + TestMessage msg = new TestMessage(); + final Benchmark bm = new Benchmark("transmission rate"); + msg.addListener(new IOSEEMessageListener() { + @Override + public void onDataAvailable(MessageData data, DataType type) throws MessageSystemException { + bm.samplePoint(); + } + + @Override + public void onInitListener() throws MessageSystemException { + + } + + }); + int time = 5000; // 5 seconds + support.activateMsg(msg); + int period = (int) Math.round(1000.0 / msg.getRate()); + int expectedXmits = (int) Math.round(msg.getRate()) * (time / 1000); + Thread.sleep(time + 10); + Assert.assertEquals(expectedXmits, bm.getTotalSamples()); + long avg = bm.getAverage() / 1000; + Assert.assertTrue("period is out of range:expected " + period + ", actual " + avg, + support.inRange(period, 1, (int) avg)); + } + + @Test + public void testCheckForTransmission() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + int time = 1000; + int expectedXmits = (int) Math.round(msg.getRate()) * (time / 1000); + support.checkForTransmission(msg, expectedXmits, time + 10); + + support.checkForTransmissionFail(msg, expectedXmits, time - 10); + } + +} diff --git a/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestStringOperations.java b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestStringOperations.java new file mode 100644 index 000000000..0cb060b9a --- /dev/null +++ b/org.eclipse.osee.ote.core.test/src/org/eclipse/osee/ote/message/other/TestStringOperations.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.ote.message.other; + +import org.eclipse.osee.ote.message.mock.TestMessage; +import org.eclipse.osee.ote.message.mock.UnitTestSupport; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class TestStringOperations { + + private UnitTestSupport support; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + support = new UnitTestSupport(); + } + + @After + public void tearDown() throws Exception { + support.cleanup(); + } + + @Test + public void testCheckWaitForValue() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + support.genericTestCheckWaitForValue(msg.STRING_ELEMENT_1, new String[] { + "Expected 1", + " Expected", + "Expected ", + "", + "expected", + "EXPECTED", + "Expected", + "Expected", + "01234", + "x"}, "abc"); + } + + @Test + public void testCheckNot() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + String values[] = new String[] {" ", "a", " ", "hi", "by", "123456789ABCDEF", "1"}; + support.genericTestCheckNot(msg.STRING_ELEMENT_1, values); + } + + @Test + public void testCheckList() throws InterruptedException { + TestMessage msg = new TestMessage(); + support.activateMsg(msg); + String[] good = new String[] {"a b c d e f g", "b", "_", "hello", "test"}; + String[] bad = new String[] {"a b c d e f g e", "c", " ", "\t", "hellO", "t\0est"}; + support.genericTestCheckList(msg.STRING_ELEMENT_1, good, bad); + } + + @Test + public void testStringEmpty() throws InterruptedException { + TestMessage msg = new TestMessage(); + msg.STRING_ELEMENT_1.zeroize(); + Assert.assertTrue("string is not empty", msg.STRING_ELEMENT_1.isEmpty()); + msg.STRING_ELEMENT_1.setValue("hi"); + Assert.assertFalse("string is empty", msg.STRING_ELEMENT_1.isEmpty()); + msg.STRING_ELEMENT_1.setValue("\0"); + Assert.assertTrue("string is not empty", msg.STRING_ELEMENT_1.isEmpty()); + msg.STRING_ELEMENT_1.setValue("0123456789"); + Assert.assertFalse("string is empty", msg.STRING_ELEMENT_1.isEmpty()); + + } + +} |