|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.library.banyandb.v1.client; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 26 | + |
| 27 | +/** |
| 28 | + * Holds the version lists to the rules the client relies on, whatever versions they name. |
| 29 | + */ |
| 30 | +public class BanyanDBAPIVersionTest { |
| 31 | + @Test |
| 32 | + public void everyVersionWorksWithServersOfItsOwnVersion() { |
| 33 | + for (final BanyanDBAPIVersion version : BanyanDBAPIVersion.values()) { |
| 34 | + assertTrue(version.isCompatibleWith(version.getVersion()), version.name()); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * A server the oldest version does not work with fails the boot, so no newer version may list it. |
| 40 | + */ |
| 41 | + @Test |
| 42 | + public void everyCompatibleServerIsOneTheOldestVersionWorksWith() { |
| 43 | + final BanyanDBAPIVersion oldest = BanyanDBAPIVersion.oldest(); |
| 44 | + for (final BanyanDBAPIVersion version : BanyanDBAPIVersion.values()) { |
| 45 | + for (final String server : version.getCompatibleServerVersions()) { |
| 46 | + assertTrue(oldest.isCompatibleWith(server), version.name() + " lists " + server); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void versionsAreDeclaredOldestFirst() { |
| 53 | + final BanyanDBAPIVersion[] versions = BanyanDBAPIVersion.values(); |
| 54 | + assertEquals(versions[0], BanyanDBAPIVersion.oldest()); |
| 55 | + for (int i = 1; i < versions.length; i++) { |
| 56 | + assertTrue( |
| 57 | + compare(versions[i - 1].getVersion(), versions[i].getVersion()) < 0, |
| 58 | + versions[i - 1].name() + " is declared before " + versions[i].name()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Code written for a version uses what that version added, which an older server does not have. |
| 64 | + */ |
| 65 | + @Test |
| 66 | + public void noVersionWorksWithAnOlderServer() { |
| 67 | + for (final BanyanDBAPIVersion version : BanyanDBAPIVersion.values()) { |
| 68 | + for (final String server : version.getCompatibleServerVersions()) { |
| 69 | + assertTrue(compare(server, version.getVersion()) >= 0, version.name() + " lists " + server); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void unknownServerVersionIsNotCompatible() { |
| 76 | + for (final BanyanDBAPIVersion version : BanyanDBAPIVersion.values()) { |
| 77 | + assertFalse(version.isCompatibleWith("0.0"), version.name()); |
| 78 | + assertFalse(version.isCompatibleWith(""), version.name()); |
| 79 | + assertFalse(version.isCompatibleWith(null), version.name()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static int compare(final String left, final String right) { |
| 84 | + final String[] l = left.split("\\."); |
| 85 | + final String[] r = right.split("\\."); |
| 86 | + final int major = Integer.compare(Integer.parseInt(l[0]), Integer.parseInt(r[0])); |
| 87 | + return major != 0 ? major : Integer.compare(Integer.parseInt(l[1]), Integer.parseInt(r[1])); |
| 88 | + } |
| 89 | +} |
0 commit comments