2121
2222package com .microsoft .applicationinsights .telemetry ;
2323
24- import java .io .BufferedWriter ;
24+ import com .google .gson .Gson ;
25+ import com .google .gson .reflect .TypeToken ;
26+ import org .junit .Test ;
27+
2528import java .io .IOException ;
2629import java .io .Serializable ;
2730import java .io .StringWriter ;
3033import java .util .List ;
3134import java .util .Map ;
3235
33- import com .google .gson .Gson ;
34- import org .junit .Test ;
35-
3636import static org .junit .Assert .assertEquals ;
37+ import static org .junit .Assert .assertNotEquals ;
3738
3839public class JsonTelemetryDataSerializerTest {
39- private final static class TestClassWithStrings implements JsonSerializable , Serializable {
40+ private final static class TestClassWithStrings implements JsonSerializable , Serializable {
4041 private String s1 ;
4142 private String s2 ;
4243
4344 @ Override
4445 public void serialize (JsonTelemetryDataSerializer serializer ) throws IOException {
45- serializer .write ("s1" , s1 );
46- serializer .write ("s2" , s2 );
46+ serializer .write ("s1" , s1 , 100 , true );
47+ serializer .write ("s2" , s2 , 15 , false );
4748 }
4849
4950 public String getS1 () {
@@ -155,10 +156,10 @@ public void setList1(List<String> list1) {
155156 public void serialize (JsonTelemetryDataSerializer serializer ) throws IOException {
156157 serializer .write ("i1" , i1 );
157158 serializer .write ("i2" , i2 );
158- serializer .write ("s1" , s1 );
159+ serializer .write ("s1" , s1 , 10 , true );
159160 serializer .write ("l1" , l1 );
160161 serializer .write ("l2" , l2 );
161- serializer .write ("s2" , s2 );
162+ serializer .write ("s2" , s2 , 15 , false );
162163 serializer .write ("m1" , m1 );
163164 serializer .write ("list1" , list1 );
164165 }
@@ -189,7 +190,6 @@ public void testStrings() throws IOException {
189190 testClassWithStrings .setS2 ("s2" );
190191
191192 StringWriter stringWriter = new StringWriter ();
192- BufferedWriter bufferedWriter = new BufferedWriter (stringWriter );
193193 JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer (stringWriter );
194194 testClassWithStrings .serialize (tested );
195195 tested .close ();
@@ -198,6 +198,42 @@ public void testStrings() throws IOException {
198198 assertEquals (bac , testClassWithStrings );
199199 }
200200
201+ //This is to test if the write method with name parameters work
202+ @ Test
203+ public void testLengthOfStrings () throws IOException {
204+ TestClassWithStrings testClassWithStrings = new TestClassWithStrings ();
205+ String s1 = TelemetryTestsUtils .createString (110 );
206+ testClassWithStrings .setS1 (s1 );
207+ testClassWithStrings .setS2 ("abc" );
208+ StringWriter stringWriter = new StringWriter ();
209+ JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer (stringWriter );
210+ testClassWithStrings .serialize (tested );
211+ tested .close ();
212+ String str = stringWriter .toString ();
213+ TestClassWithStrings bac = new Gson ().fromJson (str , TestClassWithStrings .class );
214+ Map <String , String > recoveryMap = new Gson ().fromJson (str , new TypeToken <HashMap <String , String >>() {}.getType ());
215+ assertNotEquals ((recoveryMap .get ("s1" )).length (), s1 .length ());
216+ assertNotEquals (bac , testClassWithStrings );
217+ }
218+
219+
220+ @ Test
221+ public void testSanitization () throws IOException {
222+ TestClassWithStrings testClassWithStrings = new TestClassWithStrings ();
223+ String s1 = "\\ '\\ f\\ b\\ f\\ n\\ r\\ t/\\ " ;
224+ String s2 = "0x0021\t " ;
225+ testClassWithStrings .setS1 (s1 );
226+ testClassWithStrings .setS2 (s2 );
227+ StringWriter stringWriter = new StringWriter ();
228+ JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer (stringWriter );
229+ testClassWithStrings .serialize (tested );
230+ tested .close ();
231+ String str = stringWriter .toString ();
232+ Map <String , String > recoveryMap = new Gson ().fromJson (str , new TypeToken <HashMap <String , String >>() {}.getType ());
233+ assertEquals (recoveryMap .get ("s1" ), "\\ '\\ f\\ b\\ f\\ n\\ r\\ t/\\ " );
234+ assertEquals (recoveryMap .get ("s2" ), "0x0021\t " );
235+
236+ }
201237 @ Test
202238 public void test () throws IOException {
203239 StubClass stubClass = new StubClass ();
@@ -210,15 +246,21 @@ public void test() throws IOException {
210246 stubClass .getM1 ().put ("key1" , 5 );
211247 stubClass .getM1 ().put ("key2" , 6 );
212248 stubClass .getM1 ().put ("key3" , 7 );
213-
214249 StringWriter stringWriter = new StringWriter ();
215- BufferedWriter bufferedWriter = new BufferedWriter (stringWriter );
216250 JsonTelemetryDataSerializer tested = new JsonTelemetryDataSerializer (stringWriter );
217251 stubClass .serialize (tested );
218252 tested .close ();
219253 String str = stringWriter .toString ();
220254 StubClass bac = new Gson ().fromJson (str , StubClass .class );
255+ assertEquals (bac .i1 , stubClass .i1 );
256+ assertEquals (bac .i2 , stubClass .i2 );
257+ assertEquals (bac .l1 , stubClass .l1 );
258+ assertEquals (bac .l2 , stubClass .l2 );
259+ assertEquals (bac .list1 , stubClass .list1 );
260+ assertEquals (bac .m1 , stubClass .m1 );
221261// System.out.println(str);
222262 }
223263
264+
265+
224266}
0 commit comments