Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
[Tradfri] Added better version check (#4930)
Browse files Browse the repository at this point in the history
* Added better version check

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored and maggu2810 committed Jan 22, 2018
1 parent b5dd480 commit 223eaa4
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 10 deletions.
Expand Up @@ -12,23 +12,16 @@
<groupId>org.eclipse.smarthome.archetype</groupId>
<artifactId>org.eclipse.smarthome.binding.tradfri.test</artifactId>
<version>0.10.0-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>

<name>TRÅDFRI Binding Tests</name>

<properties>
<bundle.symbolicName>org.eclipse.smarthome.binding.tradfri.test</bundle.symbolicName>
<bundle.namespace>org.eclipse.smarthome.binding.tradfri</bundle.namespace>
</properties>

<packaging>eclipse-test-plugin</packaging>

<build>
<plugins>
<plugin>
<groupId>${tycho-groupid}</groupId>
<artifactId>target-platform-configuration</artifactId>
<configuration>
<environments combine.self="override"></environments>
<dependency-resolution>
<extraRequirements>
<requirement>
Expand Down
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.binding.tradfri.internal.model;

import static org.junit.Assert.*;

import java.util.Arrays;

import org.junit.Test;

/**
* Tests for {@link TradfriVersion}.
*
* @author Christoph Weitkamp - Initial contribution
*/
public class TradfriVersionTest {

private static final int LESS_THAN = -1;
private static final int EQUAL_TO = 0;
private static final int GREATER_THAN = 1;

private static final String VERSION_STRING = "1.2.42";
private static final TradfriVersion VERSION = new TradfriVersion(VERSION_STRING);

@Test(expected = IllegalArgumentException.class)
public void testIllegalArgumentException() throws IllegalArgumentException {
new TradfriVersion("FAILURE");
}

@Test
public void testParts() {
assertEquals(Arrays.asList(1, 2, 42), VERSION.parts);
}

@Test
public void testCompareToEqualTo() {
assertEquals(EQUAL_TO, VERSION.compareTo(VERSION));
assertEquals(EQUAL_TO, VERSION.compareTo(new TradfriVersion(VERSION_STRING)));
}

@Test
public void testCompareToLessThan() {
assertEquals(LESS_THAN, VERSION.compareTo(new TradfriVersion("2")));
assertEquals(LESS_THAN, VERSION.compareTo(new TradfriVersion("1.3")));
assertEquals(LESS_THAN, VERSION.compareTo(new TradfriVersion("1.2.50")));
assertEquals(LESS_THAN, VERSION.compareTo(new TradfriVersion("1.2.42.5")));
}

@Test
public void testCompareToGreaterThan() {
assertEquals(GREATER_THAN, VERSION.compareTo(new TradfriVersion("1")));
assertEquals(GREATER_THAN, VERSION.compareTo(new TradfriVersion("1.1")));
assertEquals(GREATER_THAN, VERSION.compareTo(new TradfriVersion("1.2.30")));
}

@SuppressWarnings("unlikely-arg-type")
@Test
public void testEquals() {
assertTrue(VERSION.equals(VERSION));
assertTrue(VERSION.equals(new TradfriVersion(VERSION_STRING)));

assertFalse(VERSION.equals((TradfriVersion) null));
assertFalse(VERSION.equals(new Integer("1")));
assertFalse(VERSION.equals(new TradfriVersion("1.2.5")));
}

@Test
public void testToString() {
assertEquals(VERSION_STRING, VERSION.toString());
}
}
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapEndpoint;
import org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapHandler;
import org.eclipse.smarthome.binding.tradfri.internal.config.TradfriGatewayConfig;
import org.eclipse.smarthome.binding.tradfri.internal.model.TradfriVersion;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
Expand Down Expand Up @@ -68,7 +69,7 @@ public class TradfriGatewayHandler extends BaseBridgeHandler implements CoapCall

protected final Logger logger = LoggerFactory.getLogger(getClass());

private static final String MIN_SUPPORTED_VERSION = "1.2.42";
private static final TradfriVersion MIN_SUPPORTED_VERSION = new TradfriVersion("1.2.42");

private TradfriCoapClient deviceClient;
private String gatewayURI;
Expand Down Expand Up @@ -376,7 +377,7 @@ private boolean isNullOrEmpty(String string) {
*/
private boolean isOldFirmware() {
String currentFirmware = thing.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION);
return currentFirmware == null || MIN_SUPPORTED_VERSION.compareTo(currentFirmware) > 0;
return currentFirmware == null || MIN_SUPPORTED_VERSION.compareTo(new TradfriVersion(currentFirmware)) > 0;
}

@Override
Expand Down
@@ -0,0 +1,90 @@
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.binding.tradfri.internal.model;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* The {@link TradfriVersion} class is a default implementation for comparing TRÅDFRI versions.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public class TradfriVersion implements Comparable<TradfriVersion> {
private static final String VERSION_PATTERN = "[0-9]+(\\.[0-9]+)*";
private static final String VERSION_DELIMITER = "\\.";
final List<Integer> parts;

/**
* Create a new instance.
*
* @param version the version string
*/
public TradfriVersion(final String version) {
if (!version.matches(VERSION_PATTERN)) {
throw new IllegalArgumentException("TradfriVersion cannot be created as version has invalid format.");
}
parts = Arrays.stream(version.split(VERSION_DELIMITER)).map(part -> Integer.parseInt(part))
.collect(Collectors.toList());
}

@Override
public int compareTo(final TradfriVersion other) {
int minSize = Math.min(parts.size(), other.parts.size());
for (int i = 0; i < minSize; ++i) {
int diff = parts.get(i) - other.parts.get(i);
if (diff == 0) {
continue;
} else if (diff < 0) {
return -1;
} else {
return 1;
}
}
for (int i = minSize; i < parts.size(); ++i) {
if (parts.get(i) != 0) {
return 1;
}
}
for (int i = minSize; i < other.parts.size(); ++i) {
if (other.parts.get(i) != 0) {
return -1;
}
}
return 0;
}

@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
return compareTo((TradfriVersion) obj) == 0;
}

@Override
public String toString() {
return parts.stream().map(String::valueOf).collect(Collectors.joining("."));
}
}

0 comments on commit 223eaa4

Please sign in to comment.