1 /* 2 * Copyright 2014 Google LLC 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 package com.google.auto.value.gwt; 17 18 import static org.mockito.Mockito.verify; 19 import static org.mockito.Mockito.verifyNoMoreInteractions; 20 21 import com.google.auto.value.AutoValue; 22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.collect.ImmutableList; 24 import com.google.common.collect.ImmutableMap; 25 import com.google.gwt.user.client.rpc.SerializationException; 26 import com.google.gwt.user.client.rpc.SerializationStreamWriter; 27 import java.io.Serializable; 28 import java.util.Collections; 29 import java.util.List; 30 import java.util.Map; 31 import javax.annotation.Nullable; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.junit.runners.JUnit4; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 39 /** 40 * Tests that the generated GWT serializer for GwtValueType serializes fields in the expected way. 41 * 42 * @author emcmanus@google.com (Éamonn McManus) 43 */ 44 @RunWith(JUnit4.class) 45 public class CustomFieldSerializerTest { 46 @Before setUp()47 public void setUp() throws Exception { 48 MockitoAnnotations.initMocks(this); 49 } 50 51 @AutoValue 52 @GwtCompatible(serializable = true) 53 abstract static class ValueType implements Serializable { string()54 abstract String string(); 55 integer()56 abstract int integer(); 57 58 @Nullable other()59 abstract ValueType other(); 60 others()61 abstract List<ValueType> others(); 62 create(String string, int integer, @Nullable ValueType other)63 static ValueType create(String string, int integer, @Nullable ValueType other) { 64 return create(string, integer, other, Collections.<ValueType>emptyList()); 65 } 66 create( String string, int integer, @Nullable ValueType other, List<ValueType> others)67 static ValueType create( 68 String string, int integer, @Nullable ValueType other, List<ValueType> others) { 69 return new AutoValue_CustomFieldSerializerTest_ValueType(string, integer, other, others); 70 } 71 } 72 73 private static final ValueType SIMPLE = ValueType.create("anotherstring", 1729, null); 74 private static final ValueType CONS = ValueType.create("whatever", 1296, SIMPLE); 75 private static final ValueType WITH_LIST = 76 ValueType.create("blim", 11881376, SIMPLE, ImmutableList.of(SIMPLE, CONS)); 77 78 @Mock SerializationStreamWriter streamWriter; 79 80 @Test testCustomFieldSerializer()81 public void testCustomFieldSerializer() throws SerializationException { 82 AutoValue_CustomFieldSerializerTest_ValueType withList = 83 (AutoValue_CustomFieldSerializerTest_ValueType) WITH_LIST; 84 AutoValue_CustomFieldSerializerTest_ValueType_CustomFieldSerializer.serialize( 85 streamWriter, withList); 86 verify(streamWriter).writeString("blim"); 87 verify(streamWriter).writeInt(11881376); 88 verify(streamWriter).writeObject(SIMPLE); 89 verify(streamWriter).writeObject(ImmutableList.of(SIMPLE, CONS)); 90 verifyNoMoreInteractions(streamWriter); 91 } 92 93 @AutoValue 94 @GwtCompatible(serializable = true) 95 abstract static class ValueTypeWithGetters implements Serializable { getPackage()96 abstract String getPackage(); 97 isDefault()98 abstract boolean isDefault(); 99 create(String pkg, boolean dflt)100 static ValueTypeWithGetters create(String pkg, boolean dflt) { 101 return new AutoValue_CustomFieldSerializerTest_ValueTypeWithGetters(pkg, dflt); 102 } 103 } 104 105 @Test testCustomFieldSerializerWithGetters()106 public void testCustomFieldSerializerWithGetters() throws SerializationException { 107 AutoValue_CustomFieldSerializerTest_ValueTypeWithGetters instance = 108 (AutoValue_CustomFieldSerializerTest_ValueTypeWithGetters) 109 ValueTypeWithGetters.create("package", true); 110 AutoValue_CustomFieldSerializerTest_ValueTypeWithGetters_CustomFieldSerializer.serialize( 111 streamWriter, instance); 112 verify(streamWriter).writeString("package"); 113 verify(streamWriter).writeBoolean(true); 114 verifyNoMoreInteractions(streamWriter); 115 } 116 117 @AutoValue 118 @GwtCompatible(serializable = true) 119 abstract static class GenericValueType<K extends Comparable<K>, V extends K> 120 implements Serializable { map()121 abstract Map<K, V> map(); 122 create(Map<K, V> map)123 static <K extends Comparable<K>, V extends K> GenericValueType<K, V> create(Map<K, V> map) { 124 return new AutoValue_CustomFieldSerializerTest_GenericValueType<K, V>(map); 125 } 126 } 127 128 @Test testCustomFieldSerializerGeneric()129 public void testCustomFieldSerializerGeneric() throws SerializationException { 130 Map<Integer, Integer> map = ImmutableMap.of(2, 2); 131 AutoValue_CustomFieldSerializerTest_GenericValueType<Integer, Integer> instance = 132 (AutoValue_CustomFieldSerializerTest_GenericValueType<Integer, Integer>) 133 GenericValueType.create(map); 134 AutoValue_CustomFieldSerializerTest_GenericValueType_CustomFieldSerializer.serialize( 135 streamWriter, instance); 136 verify(streamWriter).writeObject(map); 137 verifyNoMoreInteractions(streamWriter); 138 } 139 140 @AutoValue 141 @GwtCompatible(serializable = true) 142 abstract static class ValueTypeWithBuilder implements Serializable { string()143 abstract String string(); 144 strings()145 abstract ImmutableList<String> strings(); 146 builder()147 static Builder builder() { 148 return new AutoValue_CustomFieldSerializerTest_ValueTypeWithBuilder.Builder(); 149 } 150 151 @AutoValue.Builder 152 interface Builder { string(String x)153 Builder string(String x); 154 strings(ImmutableList<String> x)155 Builder strings(ImmutableList<String> x); 156 build()157 ValueTypeWithBuilder build(); 158 } 159 } 160 161 @Test testCustomFieldSerializerWithBuilder()162 public void testCustomFieldSerializerWithBuilder() throws SerializationException { 163 AutoValue_CustomFieldSerializerTest_ValueTypeWithBuilder instance = 164 (AutoValue_CustomFieldSerializerTest_ValueTypeWithBuilder) 165 ValueTypeWithBuilder.builder().string("s").strings(ImmutableList.of("a", "b")).build(); 166 AutoValue_CustomFieldSerializerTest_ValueTypeWithBuilder_CustomFieldSerializer.serialize( 167 streamWriter, instance); 168 verify(streamWriter).writeString("s"); 169 verify(streamWriter).writeObject(ImmutableList.of("a", "b")); 170 verifyNoMoreInteractions(streamWriter); 171 } 172 173 @AutoValue 174 @GwtCompatible(serializable = true) 175 abstract static class ValueTypeWithBuilderAndGetters implements Serializable { getPackage()176 abstract String getPackage(); 177 isDefault()178 abstract boolean isDefault(); 179 builder()180 static Builder builder() { 181 return new AutoValue_CustomFieldSerializerTest_ValueTypeWithBuilderAndGetters.Builder(); 182 } 183 184 @AutoValue.Builder 185 interface Builder { setPackage(String x)186 Builder setPackage(String x); 187 setDefault(boolean x)188 Builder setDefault(boolean x); 189 build()190 ValueTypeWithBuilderAndGetters build(); 191 } 192 } 193 194 @Test testCustomFieldSerializerWithBuilderAndGetters()195 public void testCustomFieldSerializerWithBuilderAndGetters() throws SerializationException { 196 AutoValue_CustomFieldSerializerTest_ValueTypeWithBuilderAndGetters instance = 197 (AutoValue_CustomFieldSerializerTest_ValueTypeWithBuilderAndGetters) 198 ValueTypeWithBuilderAndGetters.builder().setPackage("s").setDefault(false).build(); 199 AutoValue_CustomFieldSerializerTest_ValueTypeWithBuilderAndGetters_CustomFieldSerializer 200 .serialize(streamWriter, instance); 201 verify(streamWriter).writeString("s"); 202 verify(streamWriter).writeBoolean(false); 203 verifyNoMoreInteractions(streamWriter); 204 } 205 206 @AutoValue 207 @GwtCompatible(serializable = true) 208 abstract static class GenericValueTypeWithBuilder<K extends Comparable<K>, V extends K> 209 implements Serializable { map()210 abstract Map<K, V> map(); 211 builder()212 static <K extends Comparable<K>, V extends K> Builder<K, V> builder() { 213 return new AutoValue_CustomFieldSerializerTest_GenericValueTypeWithBuilder.Builder<K, V>(); 214 } 215 216 @AutoValue.Builder 217 interface Builder<K extends Comparable<K>, V extends K> { map(Map<K, V> map)218 Builder<K, V> map(Map<K, V> map); 219 build()220 GenericValueTypeWithBuilder<K, V> build(); 221 } 222 } 223 224 @Test testCustomFieldSerializerGenericWithBuilder()225 public void testCustomFieldSerializerGenericWithBuilder() throws SerializationException { 226 Map<Integer, Integer> map = ImmutableMap.of(2, 2); 227 AutoValue_CustomFieldSerializerTest_GenericValueTypeWithBuilder<Integer, Integer> instance = 228 (AutoValue_CustomFieldSerializerTest_GenericValueTypeWithBuilder<Integer, Integer>) 229 GenericValueTypeWithBuilder.<Integer, Integer>builder().map(map).build(); 230 AutoValue_CustomFieldSerializerTest_GenericValueTypeWithBuilder_CustomFieldSerializer.serialize( 231 streamWriter, instance); 232 verify(streamWriter).writeObject(map); 233 verifyNoMoreInteractions(streamWriter); 234 } 235 } 236