Skip to content

Commit

Permalink
Null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kasemir committed Jun 6, 2016
1 parent ee649fe commit 7ac952e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .classpath
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
Expand Down
13 changes: 7 additions & 6 deletions src/etherip/protocol/CIPReadDataProtocol.java
Expand Up @@ -7,9 +7,9 @@
*******************************************************************************/
package etherip.protocol;

import java.nio.ByteBuffer;

import etherip.types.CIPData;
import java.nio.ByteBuffer;

import etherip.types.CIPData;
import etherip.types.CNService;

/** Protocol body for {@link CNService#CIP_ReadData}
Expand All @@ -19,7 +19,7 @@
public class CIPReadDataProtocol extends ProtocolAdapter
{
private CIPData data;

@Override
public int getRequestSize()
{
Expand All @@ -40,7 +40,8 @@ public void decode(final ByteBuffer buf, final int available, final StringBuilde
if (available <= 0)
{
data = null;
log.append("USINT type, data : - nothing-\n");
if (log != null)
log.append("USINT type, data : - nothing-\n");
return;
}
final CIPData.Type type = CIPData.Type.forCode(buf.getShort());
Expand All @@ -50,7 +51,7 @@ public void decode(final ByteBuffer buf, final int available, final StringBuilde
if (log != null)
log.append("USINT type, data : ").append(data).append("\n");
}

final public CIPData getData()
{
return data;
Expand Down
29 changes: 18 additions & 11 deletions test/etherip/EtherIPDemo.java
Expand Up @@ -7,6 +7,11 @@
*******************************************************************************/
package etherip;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -16,15 +21,12 @@
import etherip.types.CIPData;
import etherip.types.CIPData.Type;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

/** @author Kay Kasemir */
public class EtherIPDemo
{
@Before
public void setup()
{
{
TestSettings.logAll();
}

Expand All @@ -38,22 +40,24 @@ public void testEtherIP() throws Exception
)
{
plc.connect();

System.out.println("\n*\n* Connected:\n*\n");
System.out.println(plc);

System.out.println("\n*\n* Individual read/write:\n*\n");
CIPData value = plc.readTag(TestSettings.get("tag1"));
System.out.println(value);
assertThat(value, not(nullValue()));
value.set(0, 47);
plc.writeTag(TestSettings.get("tag1"), value);

value = plc.readTag(TestSettings.get("tag1"));
assertThat(value, not(nullValue()));
System.out.println(value);

value.set(0, 3.1416);
plc.writeTag(TestSettings.get("tag1"), value);

System.out.println("\n*\n* Multi read:\n*\n");
plc.readTags(TestSettings.get("tag1"), TestSettings.get("tag2"));
}
Expand All @@ -69,28 +73,31 @@ public void testBool() throws Exception
)
{
plc.connect();

final String tag = TestSettings.get("bool_tag");
CIPData value = plc.readTag(tag);
System.out.println("Original Value: " + value);

value = new CIPData(Type.BOOL, 1);
value.set(0, 255);
plc.writeTags(new String[] { tag }, new CIPData[] { value });
value = plc.readTag(tag);
System.out.println("Wrote 255: " + value);
System.out.println("Wrote 255: " + value);
assertThat(value, not(nullValue()));
assertThat(value.getNumber(0).intValue(), not(equalTo(0)));

value.set(0, 1);
plc.writeTag(tag, value);
plc.writeTag(tag, value);
value = plc.readTag(tag);
System.out.println("Wrote 1: " + value);
assertThat(value, not(nullValue()));
assertThat(value.getNumber(0).intValue(), not(equalTo(0)));

value.set(0, 0);
plc.writeTag(tag, value);
value = plc.readTag(tag);
System.out.println("Wrote 0: " + value);
assertThat(value, not(nullValue()));
assertThat(value.getNumber(0).intValue(), equalTo(0));
}
}
Expand Down

0 comments on commit 7ac952e

Please sign in to comment.