diff options
author | Alex Clayton <alex.clayton@isode.com> | 2016-02-19 15:10:29 (GMT) |
---|---|---|
committer | Alex Clayton <alex.clayton@isode.com> | 2016-02-29 11:09:07 (GMT) |
commit | 2de569d23468c94fdcf1adc336a580b053423fd7 (patch) | |
tree | f34fff273c65be6006c9de34c6903ad8db553118 /test/com/isode | |
parent | 3bfe54c141dd3fa20e391312a0a84c75731e2b2a (diff) | |
download | stroke-2de569d23468c94fdcf1adc336a580b053423fd7.zip stroke-2de569d23468c94fdcf1adc336a580b053423fd7.tar.bz2 |
Add sort method for ServiceQuery and add Tests
Add the sortResult static method to the DomainNameServiceQuery class. This
required adding a few equivalances for C++ std library methods to the class. And
add a test for the new method too.
Test-information:
All unit tests pass ok.
Change-Id: Idee0888f7ea140d35a971414fc2fd3cbcdfc337f
Diffstat (limited to 'test/com/isode')
-rw-r--r-- | test/com/isode/stroke/network/DomainNameServiceQueryTest.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/test/com/isode/stroke/network/DomainNameServiceQueryTest.java b/test/com/isode/stroke/network/DomainNameServiceQueryTest.java new file mode 100644 index 0000000..3ad212a --- /dev/null +++ b/test/com/isode/stroke/network/DomainNameServiceQueryTest.java @@ -0,0 +1,89 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.network; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import com.isode.stroke.base.RandomGenerator; +import com.isode.stroke.network.DomainNameServiceQuery.Result; + +/** + * Test for {@link DomainNameServiceQuery} + */ +public class DomainNameServiceQueryTest { + + private static class RandomGenerator1 implements RandomGenerator { + + @Override + public int generateRandomInteger(int max) { + return 0; + } + + } + + public static class RandomGenerator2 implements RandomGenerator { + + @Override + public int generateRandomInteger(int max) { + return max; + } + + } + + @Test + public void testSortResults_Random1() { + List<Result> results = new ArrayList<Result>(6); + results.add(new Result("server1.com", 5222, 5, 1)); + results.add(new Result("server2.com", 5222, 3, 10)); + results.add(new Result("server3.com", 5222, 6, 1)); + results.add(new Result("server4.com", 5222, 3, 20)); + results.add(new Result("server5.com", 5222, 2, 1)); + results.add(new Result("server6.com", 5222, 3, 10)); + + RandomGenerator generator = new RandomGenerator1(); + DomainNameServiceQuery.sortResults(results, generator); + + assertEquals("server5.com",results.get(0).hostname); + assertEquals("server2.com",results.get(1).hostname); + assertEquals("server4.com",results.get(2).hostname); + assertEquals("server6.com",results.get(3).hostname); + assertEquals("server1.com",results.get(4).hostname); + assertEquals("server3.com",results.get(5).hostname); + } + + @Test + public void testSortResults_Random2() { + List<Result> results = new ArrayList<Result>(7); + results.add(new Result("server1.com", 5222, 5, 1)); + results.add(new Result("server2.com", 5222, 3, 10)); + results.add(new Result("server3.com", 5222, 6, 1)); + results.add(new Result("server4.com", 5222, 3, 20)); + results.add(new Result("server5.com", 5222, 2, 1)); + results.add(new Result("server6.com", 5222, 3, 10)); + results.add(new Result("server7.com", 5222, 3, 40)); + + RandomGenerator generator = new RandomGenerator2(); + DomainNameServiceQuery.sortResults(results, generator); + + assertEquals("server5.com",results.get(0).hostname); + assertEquals("server7.com",results.get(1).hostname); + assertEquals("server2.com",results.get(2).hostname); + assertEquals("server4.com",results.get(3).hostname); + assertEquals("server6.com",results.get(4).hostname); + assertEquals("server1.com",results.get(5).hostname); + assertEquals("server3.com",results.get(6).hostname); + } + +} |