Commit 010c0afe authored by Björn Butzin's avatar Björn Butzin
Browse files

further documentation improvements

added test infrastructure
added first tests
parent 8ff17400
......@@ -3,5 +3,7 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/log4j-1.2.16.jar"/>
<classpathentry kind="lib" path="lib/junit-4.12.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -354,14 +354,8 @@ public class BasicCoapClientChannel extends BasicCoapChannel implements
*/
public CoapBlockOption getNextBlock() {
if (!context) {
blockNumber = this.payload.size() / blockSize.getSize(); // ignore
// the
// rest
// (no
// rest
// should
// be
// there)
// ignore the rest (no rest should be there)
blockNumber = this.payload.size() / blockSize.getSize();
return new CoapBlockOption(blockNumber, false, blockSize);
} else {
blockNumber++;
......
......@@ -50,13 +50,13 @@ public interface CoapChannelManager {
public void createServerListener(CoapServer serverListener, int localPort);
/**
* called by a client to create a connection TODO: allow client to bind to a
* special port
* called by a client to create a connection
* @param client
* @param addr
* @param port
* @return
*/
// TODO: (solved?) allow client to bind to a special port
public CoapClientChannel connect(CoapClient client, InetAddress addr,
int port);
......@@ -68,7 +68,7 @@ public interface CoapChannelManager {
public void setMessageId(int globalMessageId);
/**
*
* Initializes the message ID with a random value.
*/
public void initRandom();
}
......@@ -21,15 +21,15 @@ package org.ws4d.coap.interfaces;
public interface CoapClient extends CoapChannelListener {
/**
*
* @param channel
* @param response
* This is a callback method which allows to handle a response at the application layer.
* @param channel - The {@link CoapClientChannel} where the message arrived.
* @param response - The {@link CoapResponse} that arrived.
*/
public void onResponse(CoapClientChannel channel, CoapResponse response);
/**
*
* @param channel
* This is a callback method which allows to handle a connection failure at the application layer.
* @param channel - The {@link CoapClientChannel} where the connection failed.
* @param notReachable
* @param resetByServer
*/
......
......@@ -25,7 +25,7 @@ import org.ws4d.coap.messages.CoapPacketType;
*/
public interface CoapMessage {
/**
*
* The number of milliseconds before a timeout is indicated
*/
public static final int RESPONSE_TIMEOUT_MS = 2000;
......@@ -35,7 +35,7 @@ public interface CoapMessage {
public static final double RESPONSE_RANDOM_FACTOR = 1.5;
/**
*
* The maximum number of retransmits
*/
public static final int MAX_RETRANSMIT = 4;
......@@ -46,20 +46,18 @@ public interface CoapMessage {
public static final int ACK_RST_RETRANS_TIMEOUT_MS = 120000;
/**
* returns the value of the internal message code in case of an error this
* function returns -1
* @return Value of the internal message code.<br>-1, in case of an error.
*/
public int getMessageCodeValue();
/**
*
* @return
* @return The ID of the message.
*/
public int getMessageID();
/**
*
* @param msgID
* @param msgID - The ID of the Message to be set.
*/
public void setMessageID(int msgID);
......
/* Copyright 2015 University of Rostock
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
package org.ws4d.coap.test;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.ws4d.coap.messages.CoapMediaType;
import org.ws4d.coap.rest.BasicCoapResource;
import org.ws4d.coap.rest.CoapResource;
import org.ws4d.coap.rest.CoapResourceServer;
/**
* Tests for jCoAP.
*
* @author Bjrn Butzin <bjoern.butzin[at]uni-rostock.de>
*/
public class BasicResourceTest {
private static CoapResourceServer resourceServer;
// @Test //indicates a test method
// @Test(expected= IndexOutOfBoundsException.class) //indicates a test
// method expecting an exception
// @Test(timeout=1000) //indicates a test method that fails after 1000
// milliseconds execution time
// org.junit.Assert.* // compare result & expectation
/*
* ########################################################################
* General Test preparations
* ########################################################################
*/
@BeforeClass
public static void setUpClass() {
// set up server
if (resourceServer != null) {
resourceServer.stop();
}
resourceServer = new CoapResourceServer();
try {
resourceServer.start();
} catch (Exception e) {
System.err.println(e.getLocalizedMessage());
System.exit(-1);
}
}
@AfterClass
public static void tearDownClass() {
// tear down server
if (resourceServer != null) {
resourceServer.stop();
resourceServer = null;
}
}
@Before
public void setUp() {
// intentionally left blank
}
@After
public void tearDown() {
// reset server
for (String path : resourceServer.getResources().keySet()) {
resourceServer.deleteResource(path);
}
}
/*
* Tests
*/
@Test
public void thisAlwaysPasses() {
// validate test fixtures
}
/*
* ########################################################################
* invalid resources
* ########################################################################
*/
@Test(expected = Exception.class)
public void EmptyPathResource() {
CoapResource res = new BasicCoapResource("", "".getBytes(),
CoapMediaType.text_plain);
resourceServer.createResource(res);
}
@Test(expected = Exception.class)
public void WrongSlashResource() {
CoapResource res = new BasicCoapResource("\\", "".getBytes(),
CoapMediaType.text_plain);
resourceServer.createResource(res);
}
@Test(expected = Exception.class)
public void LongNameResource() {
CoapResource res = new BasicCoapResource("/shouldbetoolong",
"".getBytes(), CoapMediaType.text_plain);
resourceServer.createResource(res);
}
@Test(expected = Exception.class)
public void LongPathResource() {
CoapResource res = new BasicCoapResource(
"/1/2/3/4/5/6/7/8/9/0/1/2/3/4/5/6/7/8/9/0", "".getBytes(),
CoapMediaType.text_plain);
resourceServer.createResource(res);
}
/*
* ########################################################################
* valid resources
* ########################################################################
*/
@Test
public void ShortestValidPathResources() {
CoapResource res = new BasicCoapResource("/", "".getBytes(),
CoapMediaType.text_plain);
resourceServer.createResource(res);
}
@Test
public void longestValidNameResources() {
CoapResource res = new BasicCoapResource("/12345678", "".getBytes(),
CoapMediaType.text_plain);
resourceServer.createResource(res);
}
@Test
public void longestValidPathResources() {
CoapResource res = new BasicCoapResource("/1/2/3/4/5/6/7/8",
"".getBytes(), CoapMediaType.text_plain);
resourceServer.createResource(res);
}
/*
* ########################################################################
* CoreResource (/.well-known/core)
* ########################################################################
*/
}
\ No newline at end of file
/* Copyright 2015 University of Rostock
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
package org.ws4d.coap.test;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.ws4d.coap.Constants;
import org.ws4d.coap.connection.BasicCoapChannelManager;
import org.ws4d.coap.interfaces.CoapChannelManager;
import org.ws4d.coap.interfaces.CoapClient;
import org.ws4d.coap.interfaces.CoapClientChannel;
import org.ws4d.coap.interfaces.CoapResponse;
import org.ws4d.coap.messages.CoapMediaType;
import org.ws4d.coap.rest.BasicCoapResource;
import org.ws4d.coap.rest.CoapResourceServer;
/**
* Tests for jCoAP.
*
* @author Bjrn Butzin <bjoern.butzin[at]uni-rostock.de>
*/
public class InterfaceTest {
private static CoapResourceServer resourceServer;
private static CoapClientChannel clientChannel;
private static CoapChannelManager channelManager;
private static ClientDummy clientDummy;
private static InetAddress inetAddress;
private class ClientDummy implements CoapClient {
@Override
public void onResponse(CoapClientChannel channel, CoapResponse response) {
// This is intended to be empty
}
@Override
public void onConnectionFailed(CoapClientChannel channel,
boolean notReachable, boolean resetByServer) {
// This is intended to be empty
}
}
// @Test //indicates a test method
// @Test(expected= IndexOutOfBoundsException.class) //indicates a test
// method expecting an exception
// @Test(timeout=1000) //indicates a test method that fails after 1000
// milliseconds execution time
// org.junit.Assert.* // compare result & expectation
/*
* ########################################################################
* General Test preparations
* ########################################################################
*/
@BeforeClass
public static void setUpClass() {
try {
inetAddress = InetAddress.getByName("127.0.0.1");
} catch (UnknownHostException e) {
System.err.println(e.getLocalizedMessage());
}
// set up server
if (resourceServer != null) {
resourceServer.stop();
}
resourceServer = new CoapResourceServer();
try {
resourceServer.start();
} catch (Exception e) {
System.err.println(e.getLocalizedMessage());
System.exit(-1);
}
}
@AfterClass
public static void tearDownClass() {
// tear down server
if (resourceServer != null) {
resourceServer.stop();
resourceServer = null;
}
}
@Before
public void setUp() {
// set up client
channelManager = BasicCoapChannelManager.getInstance();
clientDummy = new ClientDummy();
clientChannel = channelManager.connect(clientDummy, inetAddress,
Constants.COAP_DEFAULT_PORT);
if (clientChannel == null) {
System.err.println("Connect failed.");
System.exit(-1);
}
}
@After
public void tearDown() {
// tear down client
if (clientChannel != null) {
clientChannel.close();
channelManager = null;
}
clientDummy = null;
// reset server
for (String path : resourceServer.getResources().keySet()) {
resourceServer.deleteResource(path);
}
}
/*
* ########################################################################
* Tests
* ########################################################################
*
* Considerations:
* (/.well-known/core)
* (GET / OBSERVE, POST, PUT, DELETE)
* Blockwise Transfer
*/
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment