1 /*
2  * Copyright (C) 2019 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.protolog.tool
18 
19 import com.android.internal.protolog.common.LogLevel
20 import com.github.javaparser.StaticJavaParser
21 import com.github.javaparser.ast.expr.BinaryExpr
22 import com.github.javaparser.ast.expr.StringLiteralExpr
23 import org.junit.Assert.assertEquals
24 import org.junit.Assert.assertFalse
25 import org.junit.Assert.assertTrue
26 import org.junit.Test
27 
28 class CodeUtilsTest {
29     @Test
hashnull30     fun hash() {
31         assertEquals(3883826472308915399, CodeUtils.hash("Test.java:50", "test",
32                 LogLevel.DEBUG, LogGroup("test", true, true, "TAG")))
33     }
34 
35     @Test
hash_changeLocationnull36     fun hash_changeLocation() {
37         assertEquals(4125273133972468649, CodeUtils.hash("Test.java:10", "test2",
38                 LogLevel.DEBUG, LogGroup("test", true, true, "TAG")))
39     }
40 
41     @Test
hash_changeLevelnull42     fun hash_changeLevel() {
43         assertEquals(2618535069521361990, CodeUtils.hash("Test.java:50", "test",
44                 LogLevel.ERROR, LogGroup("test", true, true, "TAG")))
45     }
46 
47     @Test
hash_changeMessagenull48     fun hash_changeMessage() {
49         assertEquals(8907822592109789043, CodeUtils.hash("Test.java:50", "test2",
50                 LogLevel.DEBUG, LogGroup("test", true, true, "TAG")))
51     }
52 
53     @Test
hash_changeGroupnull54     fun hash_changeGroup() {
55         assertEquals(-1299517016176640015, CodeUtils.hash("Test.java:50", "test2",
56                 LogLevel.DEBUG, LogGroup("test2", true, true, "TAG")))
57     }
58 
59     @Test(expected = IllegalImportException::class)
checkWildcardStaticImported_truenull60     fun checkWildcardStaticImported_true() {
61         val code = """package org.example.test;
62             import static org.example.Test.*;
63         """
64         CodeUtils.checkWildcardStaticImported(
65                 StaticJavaParser.parse(code), "org.example.Test", "")
66     }
67 
68     @Test
checkWildcardStaticImported_notStaticnull69     fun checkWildcardStaticImported_notStatic() {
70         val code = """package org.example.test;
71             import org.example.Test.*;
72         """
73         CodeUtils.checkWildcardStaticImported(
74                 StaticJavaParser.parse(code), "org.example.Test", "")
75     }
76 
77     @Test
checkWildcardStaticImported_differentClassnull78     fun checkWildcardStaticImported_differentClass() {
79         val code = """package org.example.test;
80             import static org.example.Test2.*;
81         """
82         CodeUtils.checkWildcardStaticImported(
83                 StaticJavaParser.parse(code), "org.example.Test", "")
84     }
85 
86     @Test
checkWildcardStaticImported_notWildcardnull87     fun checkWildcardStaticImported_notWildcard() {
88         val code = """package org.example.test;
89             import org.example.Test.test;
90         """
91         CodeUtils.checkWildcardStaticImported(
92                 StaticJavaParser.parse(code), "org.example.Test", "")
93     }
94 
95     @Test
isClassImportedOrSamePackage_importednull96     fun isClassImportedOrSamePackage_imported() {
97         val code = """package org.example.test;
98             import org.example.Test;
99         """
100         assertTrue(CodeUtils.isClassImportedOrSamePackage(
101                 StaticJavaParser.parse(code), "org.example.Test"))
102     }
103 
104     @Test
isClassImportedOrSamePackage_samePackagenull105     fun isClassImportedOrSamePackage_samePackage() {
106         val code = """package org.example.test;
107         """
108         assertTrue(CodeUtils.isClassImportedOrSamePackage(
109                 StaticJavaParser.parse(code), "org.example.test.Test"))
110     }
111 
112     @Test
isClassImportedOrSamePackage_falsenull113     fun isClassImportedOrSamePackage_false() {
114         val code = """package org.example.test;
115             import org.example.Test;
116         """
117         assertFalse(CodeUtils.isClassImportedOrSamePackage(
118                 StaticJavaParser.parse(code), "org.example.Test2"))
119     }
120 
121     @Test
staticallyImportedMethods_abnull122     fun staticallyImportedMethods_ab() {
123         val code = """
124             import static org.example.Test.a;
125             import static org.example.Test.b;
126         """
127         val imported = CodeUtils.staticallyImportedMethods(StaticJavaParser.parse(code),
128                 "org.example.Test")
129         assertTrue(imported.containsAll(listOf("a", "b")))
130         assertEquals(2, imported.size)
131     }
132 
133     @Test
staticallyImportedMethods_differentClassnull134     fun staticallyImportedMethods_differentClass() {
135         val code = """
136             import static org.example.Test.a;
137             import static org.example.Test2.b;
138         """
139         val imported = CodeUtils.staticallyImportedMethods(StaticJavaParser.parse(code),
140                 "org.example.Test")
141         assertTrue(imported.containsAll(listOf("a")))
142         assertEquals(1, imported.size)
143     }
144 
145     @Test
staticallyImportedMethods_notStaticnull146     fun staticallyImportedMethods_notStatic() {
147         val code = """
148             import static org.example.Test.a;
149             import org.example.Test.b;
150         """
151         val imported = CodeUtils.staticallyImportedMethods(StaticJavaParser.parse(code),
152                 "org.example.Test")
153         assertTrue(imported.containsAll(listOf("a")))
154         assertEquals(1, imported.size)
155     }
156 
157     @Test
concatMultilineString_singlenull158     fun concatMultilineString_single() {
159         val str = StringLiteralExpr("test")
160         val out = CodeUtils.concatMultilineString(str, ParsingContext())
161         assertEquals("test", out)
162     }
163 
164     @Test
concatMultilineString_doublenull165     fun concatMultilineString_double() {
166         val str = """
167             "test" + "abc"
168         """
169         val code = StaticJavaParser.parseExpression<BinaryExpr>(str)
170         val out = CodeUtils.concatMultilineString(code, ParsingContext())
171         assertEquals("testabc", out)
172     }
173 
174     @Test
concatMultilineString_multiplenull175     fun concatMultilineString_multiple() {
176         val str = """
177             "test" + "abc" + "1234" + "test"
178         """
179         val code = StaticJavaParser.parseExpression<BinaryExpr>(str)
180         val out = CodeUtils.concatMultilineString(code, ParsingContext())
181         assertEquals("testabc1234test", out)
182     }
183 }
184