| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 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 com.android.ide.common.blame; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertEquals; |
| 20 |
|
| 21 |
import com.google.common.base.Optional; |
| 22 |
import com.google.gson.Gson; |
| 23 |
import com.google.gson.GsonBuilder; |
| 24 |
|
| 25 |
import org.junit.AfterClass; |
| 26 |
import org.junit.BeforeClass; |
| 27 |
import org.junit.Test; |
| 28 |
import org.junit.experimental.runners.Enclosed; |
| 29 |
import org.junit.runner.RunWith; |
| 30 |
import org.junit.runners.Parameterized; |
| 31 |
|
| 32 |
import java.io.File; |
| 33 |
import java.util.Arrays; |
| 34 |
import java.util.Collection; |
| 35 |
|
| 36 |
@RunWith(Enclosed.class) |
| 37 |
public class MessageJsonSerializerTest { |
| 38 |
|
| 39 |
private static GsonBuilder sGsonBuilder = new GsonBuilder(); |
| 40 |
|
| 41 |
static { |
| 42 |
MessageJsonSerializer.registerTypeAdapters(sGsonBuilder); |
| 43 |
} |
| 44 |
|
| 45 |
@RunWith(Parameterized.class) |
| 46 |
public static class DeserializeTest { |
| 47 |
|
| 48 |
private static Gson sGson; |
| 49 |
|
| 50 |
@Parameterized.Parameter(value = 0) |
| 51 |
public Message message; |
| 52 |
|
| 53 |
@Parameterized.Parameter(value = 1) |
| 54 |
public String serializedMessage; |
| 55 |
|
| 56 |
@Parameterized.Parameters(name = "fromJson(\"{1}\") should give {0}") |
| 57 |
public static Collection<Object[]> data() { |
| 58 |
return Arrays.asList(new Object[][]{ |
| 59 |
{new Message( |
| 60 |
Message.Kind.ERROR, |
| 61 |
"some error text", |
| 62 |
new SourceFilePosition( |
| 63 |
new SourceFile(new File("/path/file.java")), |
| 64 |
new SourcePosition(1, 3, 5))), |
| 65 |
"{" |
| 66 |
+ "\"kind\":\"Error\", " |
| 67 |
+ "\"text\":\"some error text\"," |
| 68 |
+ "\"sources\": {" |
| 69 |
+ "\"file\":\"/path/file.java\"," |
| 70 |
+ "\"position\":{" |
| 71 |
+ "\"startLine\":1," |
| 72 |
+ "\"startColumn\":3," |
| 73 |
+ "\"startOffset\":5" |
| 74 |
+ "}" |
| 75 |
+ "}}" |
| 76 |
}, { |
| 77 |
new Message( |
| 78 |
Message.Kind.ERROR, |
| 79 |
"errorText", |
| 80 |
"", |
| 81 |
null, |
| 82 |
new SourceFilePosition( |
| 83 |
new File("error/source"), |
| 84 |
new SourcePosition(1,2,3,4,5,6)) |
| 85 |
), |
| 86 |
"{\"kind\":\"ERROR\",\"text\":\"errorText\",\"sourcePath\":\"error/source\"," |
| 87 |
+ "\"position\":{\"startLine\":1,\"startColumn\":2,\"startOffset\":3," |
| 88 |
+ "\"endLine\":4,\"endColumn\":5,\"endOffset\":6},\"original\":\"\"}\n" |
| 89 |
}, {new Message(Message.Kind.SIMPLE, "something else", new SourceFilePosition(SourceFile.UNKNOWN, SourcePosition.UNKNOWN)), |
| 90 |
"{\"kind\":\"SIMPLE\"," |
| 91 |
+ "\"text\":\"something else\",\"position\":{},\"original\":\"something else\"}" |
| 92 |
}, { |
| 93 |
new Message( |
| 94 |
Message.Kind.SIMPLE, |
| 95 |
"Warning: AndroidManifest.xml already defines debuggable (in http://" |
| 96 |
+ "schemas.android.com/apk/res/android); using existing value " |
| 97 |
+ "in manifest.", |
| 98 |
SourceFilePosition.UNKNOWN), |
| 99 |
"{\"kind\":\"simple\",\"text\":\"Warning: AndroidManifest.xml already defines " |
| 100 |
+ "debuggable (in http://schemas.android.com/apk/res/android); using " |
| 101 |
+ "existing value in manifest.\",\"sources\":\"\"}" |
| 102 |
}, { |
| 103 |
new Message( |
| 104 |
Message.Kind.UNKNOWN, |
| 105 |
"Text.", |
| 106 |
SourceFilePosition.UNKNOWN), |
| 107 |
"{\"text\":\"Text.\"}", |
| 108 |
|
| 109 |
}}); |
| 110 |
} |
| 111 |
|
| 112 |
@BeforeClass |
| 113 |
public static void initGson() { |
| 114 |
sGson = sGsonBuilder.create(); |
| 115 |
} |
| 116 |
|
| 117 |
@AfterClass |
| 118 |
public static void removeGson() { |
| 119 |
sGson = null; |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void check() { |
| 124 |
assertEquals(message, sGson.fromJson(serializedMessage, Message.class)); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
@RunWith(Parameterized.class) |
| 131 |
public static class RoundTripTest { |
| 132 |
|
| 133 |
private static Gson sGson; |
| 134 |
|
| 135 |
@Parameterized.Parameter |
| 136 |
public Message message; |
| 137 |
|
| 138 |
|
| 139 |
@Parameterized.Parameters(name = "{0}") |
| 140 |
public static Collection<Object[]> data() { |
| 141 |
return Arrays.asList(new Object[][]{{ |
| 142 |
new Message( |
| 143 |
Message.Kind.ERROR, |
| 144 |
"some error text", |
| 145 |
"original error text", |
| 146 |
"tool name", |
| 147 |
new SourceFilePosition( |
| 148 |
new SourceFile(new File("/path/file.java")), |
| 149 |
new SourcePosition(1, 3, 5))) |
| 150 |
}, { |
| 151 |
new Message( |
| 152 |
Message.Kind.SIMPLE, |
| 153 |
"something else", |
| 154 |
new SourceFilePosition(SourceFile.UNKNOWN, SourcePosition.UNKNOWN)) |
| 155 |
}, { |
| 156 |
new Message( |
| 157 |
Message.Kind.SIMPLE, |
| 158 |
"Warning: AndroidManifest.xml already defines debuggable (in http://" |
| 159 |
+ "schemas.android.com/apk/res/android); using existing value " |
| 160 |
+ "in manifest.", |
| 161 |
SourceFilePosition.UNKNOWN) |
| 162 |
}}); |
| 163 |
} |
| 164 |
|
| 165 |
@BeforeClass |
| 166 |
public static void initGson() { |
| 167 |
sGson = sGsonBuilder.create(); |
| 168 |
} |
| 169 |
|
| 170 |
@AfterClass |
| 171 |
public static void removeGson() { |
| 172 |
sGson = null; |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void check() { |
| 177 |
assertEquals(message, sGson.fromJson(sGson.toJson(message), Message.class)); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
} |
| 182 |
} |