|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common.geo; |
| 21 | + |
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 24 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 25 | +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashType; |
| 26 | +import org.elasticsearch.test.ESTestCase; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +import static org.hamcrest.Matchers.containsString; |
| 31 | +import static org.hamcrest.Matchers.equalTo; |
| 32 | + |
| 33 | +public class GeoHashTypeTests extends ESTestCase { |
| 34 | + |
| 35 | + public void testValidOrdinals() { |
| 36 | + assertThat(GeoHashType.DEFAULT.ordinal(), equalTo(0)); |
| 37 | + assertThat(GeoHashType.GEOHASH.ordinal(), equalTo(0)); |
| 38 | + } |
| 39 | + |
| 40 | + public void testWriteTo() throws Exception { |
| 41 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 42 | + |
| 43 | + // FIXME: update version after backport |
| 44 | + |
| 45 | + final Version version = Version.V_7_0_0_alpha1; |
| 46 | + out.setVersion(version); |
| 47 | + GeoHashType.GEOHASH.writeTo(out); |
| 48 | + try (StreamInput in = out.bytes().streamInput()) { |
| 49 | + assertThat(in.readVInt(), equalTo(0)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 54 | + |
| 55 | + // FIXME: update version after backport to the last unsupported |
| 56 | + |
| 57 | + out.setVersion(Version.V_6_3_0); |
| 58 | + GeoHashType.GEOHASH.writeTo(out); |
| 59 | + // Should not have written anything |
| 60 | + assertThat(out.size(), equalTo(0)); |
| 61 | + } |
| 62 | + |
| 63 | + // ATTENTION: new hashing types should also assert that writeTo() throws |
| 64 | + // an error when values other than GEOHASH are written to older version streams |
| 65 | + } |
| 66 | + |
| 67 | + public void testReadFrom() throws Exception { |
| 68 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 69 | + out.writeVInt(0); |
| 70 | + try (StreamInput in = out.bytes().streamInput()) { |
| 71 | + |
| 72 | + // FIXME: update version after backport |
| 73 | + |
| 74 | + in.setVersion(Version.V_7_0_0_alpha1); |
| 75 | + assertThat(in.available(), equalTo(1)); |
| 76 | + assertThat(GeoHashType.readFromStream(in), equalTo(GeoHashType.GEOHASH)); |
| 77 | + assertThat(in.available(), equalTo(0)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 82 | + // keep the stream empty to cause an exception if a read is attempted |
| 83 | + try (StreamInput in = out.bytes().streamInput()) { |
| 84 | + |
| 85 | + // FIXME: update version after backport to the last unsupported |
| 86 | + |
| 87 | + in.setVersion(Version.V_6_3_0); |
| 88 | + assertThat(in.available(), equalTo(0)); |
| 89 | + assertThat(in.available(), equalTo(0)); |
| 90 | + assertThat(GeoHashType.readFromStream(in), equalTo(GeoHashType.GEOHASH)); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public void testInvalidReadFrom() throws Exception { |
| 96 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 97 | + out.writeVInt(randomIntBetween(1, Integer.MAX_VALUE)); |
| 98 | + try (StreamInput in = out.bytes().streamInput()) { |
| 99 | + |
| 100 | + // FIXME: update version after backport |
| 101 | + |
| 102 | + in.setVersion(Version.V_7_0_0_alpha1); |
| 103 | + GeoHashType.readFromStream(in); |
| 104 | + fail("Expected IOException"); |
| 105 | + } catch(IOException e) { |
| 106 | + assertThat(e.getMessage(), containsString("Unknown GeoHashType ordinal [")); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments