| 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.gson.Gson; |
| 22 |
import com.google.gson.GsonBuilder; |
| 23 |
|
| 24 |
import org.junit.AfterClass; |
| 25 |
import org.junit.BeforeClass; |
| 26 |
import org.junit.Test; |
| 27 |
import org.junit.experimental.runners.Enclosed; |
| 28 |
import org.junit.runner.RunWith; |
| 29 |
import org.junit.runners.Parameterized; |
| 30 |
|
| 31 |
import java.io.File; |
| 32 |
import java.util.Arrays; |
| 33 |
import java.util.Collection; |
| 34 |
|
| 35 |
@RunWith(Enclosed.class) |
| 36 |
public class SourceFilePositionJsonSerializerTest { |
| 37 |
|
| 38 |
private static GsonBuilder sGsonBuilder = new GsonBuilder(); |
| 39 |
|
| 40 |
static { |
| 41 |
MessageJsonSerializer.registerTypeAdapters(sGsonBuilder); |
| 42 |
} |
| 43 |
|
| 44 |
private static Object[][] allPairings(Object[] x, Object[] y) { |
| 45 |
int totalSize = x.length * y.length; |
| 46 |
|
| 47 |
final Object[][] matrix = new Object[totalSize][]; |
| 48 |
for (int i = 0; i < totalSize; i++) { |
| 49 |
matrix[i] = new Object[2]; |
| 50 |
matrix[i][0] = x[i / y.length]; |
| 51 |
matrix[i][1] = y[i % y.length]; |
| 52 |
} |
| 53 |
|
| 54 |
return matrix; |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
@RunWith(Parameterized.class) |
| 59 |
public static class RoundTripTest { |
| 60 |
|
| 61 |
private static Gson sGson; |
| 62 |
|
| 63 |
@Parameterized.Parameter |
| 64 |
public SourceFile mSourceFile; |
| 65 |
|
| 66 |
@Parameterized.Parameter(value = 1) |
| 67 |
public SourcePosition mSourcePosition; |
| 68 |
|
| 69 |
@Parameterized.Parameters(name = "SourceFilePosition({0}, {1})") |
| 70 |
public static Collection<Object[]> data() { |
| 71 |
|
| 72 |
return Arrays.asList(allPairings(new SourceFile[]{ |
| 73 |
new SourceFile(new File("/path/to/a/file.java")), |
| 74 |
new SourceFile(new File("/path/to/a/file.java"), "Description"), |
| 75 |
new SourceFile("Description"), |
| 76 |
SourceFile.UNKNOWN}, |
| 77 |
SourcePositionJsonTypeAdapterTest.mExamples)); |
| 78 |
} |
| 79 |
|
| 80 |
@BeforeClass |
| 81 |
public static void initGson() { |
| 82 |
sGson = sGsonBuilder.create(); |
| 83 |
} |
| 84 |
|
| 85 |
@AfterClass |
| 86 |
public static void removeGson() { |
| 87 |
sGson = null; |
| 88 |
} |
| 89 |
|
| 90 |
@Test |
| 91 |
public void test() { |
| 92 |
SourceFilePosition item = new SourceFilePosition(mSourceFile, mSourcePosition); |
| 93 |
assertEquals(item, sGson.fromJson(sGson.toJson(item), SourceFilePosition.class)); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@RunWith(Parameterized.class) |
| 98 |
public static class DeserializeTest { |
| 99 |
|
| 100 |
private static Gson sGson; |
| 101 |
|
| 102 |
@Parameterized.Parameter(value = 0) |
| 103 |
public SourceFilePosition mSourceFilePosition; |
| 104 |
|
| 105 |
@Parameterized.Parameter(value = 1) |
| 106 |
public String jsonString; |
| 107 |
|
| 108 |
@Parameterized.Parameters(name = "fromJson( {1} ) → {0}") |
| 109 |
public static Collection<Object[]> data() { |
| 110 |
return Arrays.asList(new Object[][]{ |
| 111 |
{new SourceFilePosition( |
| 112 |
new SourceFile(new File("/path/file.java")), |
| 113 |
SourcePosition.UNKNOWN), |
| 114 |
"{\"file\":\"/path/file.java\"}"}, |
| 115 |
{new SourceFilePosition( |
| 116 |
new SourceFile(new File("/path/file.java"), "Description"), |
| 117 |
new SourcePosition(1, 3, 4)), |
| 118 |
"{file:{\"description\":\"Description\",\"path\":\"/path/file.java\"}," |
| 119 |
+ "position:{startLine:1, startColumn:3,startOffset:4}}"}, |
| 120 |
{new SourceFilePosition( |
| 121 |
new SourceFile("Description"), |
| 122 |
new SourcePosition(11, 22, 33, 66, 77, 88)), |
| 123 |
"{\"file\":{\"description\":\"Description\"}, " + "position: {" |
| 124 |
+ "\"startLine\":11,\"startColumn\":22,\"startOffset\":33," |
| 125 |
+ "\"endLine\":66,\"endColumn\":77,\"endOffset\":88" + "}}"}, |
| 126 |
{new SourceFilePosition( |
| 127 |
SourceFile.UNKNOWN, |
| 128 |
new SourcePosition(11, 22, 33, 66, 77, 88)), |
| 129 |
"{ position: {\"startLine\":11,\"startColumn\":22,\"startOffset\":33," |
| 130 |
+ "\"endLine\":66,\"endColumn\":77,\"endOffset\":88" + "}, " |
| 131 |
+ "\"invalid_something\":[\"ignored tree\"]}"}, |
| 132 |
{new SourceFilePosition(SourceFile.UNKNOWN, SourcePosition.UNKNOWN), "{}"},}); |
| 133 |
} |
| 134 |
|
| 135 |
@BeforeClass |
| 136 |
public static void initGson() { |
| 137 |
sGson = sGsonBuilder.create(); |
| 138 |
} |
| 139 |
|
| 140 |
@AfterClass |
| 141 |
public static void removeGson() { |
| 142 |
sGson = null; |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void test() { |
| 147 |
assertEquals(mSourceFilePosition, sGson.fromJson(jsonString, SourceFilePosition.class)); |
| 148 |
} |
| 149 |
} |
| 150 |
} |