1 /* 2 * Copyright 2015 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertTrue; 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.util.HashSet; 27 import java.util.Set; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 /** 33 * Tests for {@link DecompressorRegistry}. 34 */ 35 @RunWith(JUnit4.class) 36 public class DecompressorRegistryTest { 37 38 private final Dummy dummyDecompressor = new Dummy(); 39 private DecompressorRegistry registry = DecompressorRegistry.emptyInstance(); 40 41 @Test lookupDecompressor_checkDefaultMessageEncodingsExist()42 public void lookupDecompressor_checkDefaultMessageEncodingsExist() { 43 // Explicitly put the names in, rather than link against MessageEncoding 44 assertNotNull("Expected identity to be registered", 45 DecompressorRegistry.getDefaultInstance().lookupDecompressor("identity")); 46 assertNotNull("Expected gzip to be registered", 47 DecompressorRegistry.getDefaultInstance().lookupDecompressor("gzip")); 48 } 49 50 @Test getKnownMessageEncodings_checkDefaultMessageEncodingsExist()51 public void getKnownMessageEncodings_checkDefaultMessageEncodingsExist() { 52 Set<String> knownEncodings = new HashSet<>(); 53 knownEncodings.add("identity"); 54 knownEncodings.add("gzip"); 55 56 assertEquals(knownEncodings, 57 DecompressorRegistry.getDefaultInstance().getKnownMessageEncodings()); 58 } 59 60 /* 61 * This test will likely change once encoders are advertised 62 */ 63 @Test getAdvertisedMessageEncodings_noEncodingsAdvertised()64 public void getAdvertisedMessageEncodings_noEncodingsAdvertised() { 65 assertTrue(registry.getAdvertisedMessageEncodings().isEmpty()); 66 } 67 68 @Test registerDecompressor_advertisedDecompressor()69 public void registerDecompressor_advertisedDecompressor() { 70 registry = registry.with(dummyDecompressor, true); 71 72 assertTrue(registry.getAdvertisedMessageEncodings() 73 .contains(dummyDecompressor.getMessageEncoding())); 74 } 75 76 @Test registerDecompressor_nonadvertisedDecompressor()77 public void registerDecompressor_nonadvertisedDecompressor() { 78 registry = registry.with(dummyDecompressor, false); 79 80 assertFalse(registry.getAdvertisedMessageEncodings() 81 .contains(dummyDecompressor.getMessageEncoding())); 82 } 83 84 private static final class Dummy implements Decompressor { 85 @Override getMessageEncoding()86 public String getMessageEncoding() { 87 return "dummy"; 88 } 89 90 @Override decompress(InputStream is)91 public InputStream decompress(InputStream is) throws IOException { 92 return is; 93 } 94 } 95 } 96 97