| 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 com.android.annotations.NonNull; |
| 20 |
import com.android.ide.common.blame.parser.ParsingFailedException; |
| 21 |
import com.android.ide.common.blame.parser.PatternAwareOutputParser; |
| 22 |
import com.android.ide.common.blame.parser.ToolOutputParser; |
| 23 |
import com.android.ide.common.blame.parser.util.OutputLineReader; |
| 24 |
import com.android.ide.common.process.ProcessException; |
| 25 |
import com.android.ide.common.process.ProcessOutput; |
| 26 |
import com.android.ide.common.res2.RecordingLogger; |
| 27 |
import com.android.utils.ILogger; |
| 28 |
import com.google.common.base.Charsets; |
| 29 |
|
| 30 |
import org.junit.BeforeClass; |
| 31 |
import org.junit.Test; |
| 32 |
import org.mockito.Mockito; |
| 33 |
|
| 34 |
import java.io.File; |
| 35 |
import java.io.IOException; |
| 36 |
import java.util.List; |
| 37 |
|
| 38 |
public class ParsingProcessOutputHandlerTest { |
| 39 |
|
| 40 |
private static ParsingProcessOutputHandler sParsingProcessOutputHandler; |
| 41 |
|
| 42 |
private static RecordingLogger sLogger; |
| 43 |
|
| 44 |
private static PatternAwareOutputParser sFakePatternParser; |
| 45 |
|
| 46 |
private static MessageReceiver sMessageReceiver; |
| 47 |
|
| 48 |
@BeforeClass |
| 49 |
public static void setUp() throws Exception { |
| 50 |
sLogger = new RecordingLogger(); |
| 51 |
sFakePatternParser = new FakePatternAwareOutputParser(); |
| 52 |
|
| 53 |
sMessageReceiver = Mockito.mock(MessageReceiver.class); |
| 54 |
sParsingProcessOutputHandler = new ParsingProcessOutputHandler( |
| 55 |
new ToolOutputParser(sFakePatternParser, sLogger), |
| 56 |
sMessageReceiver); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void testRewriteMessages() throws IOException, ProcessException { |
| 61 |
|
| 62 |
String original = "error example\ntwo line error\nnext line\nsomething else"; |
| 63 |
|
| 64 |
ProcessOutput processOutput = processOutputFromErrorString(original); |
| 65 |
processOutput.close(); |
| 66 |
sParsingProcessOutputHandler.handleOutput(processOutput); |
| 67 |
|
| 68 |
Mockito.verify(sMessageReceiver).receiveMessage(new Message( |
| 69 |
Message.Kind.ERROR, |
| 70 |
"errorText", |
| 71 |
"original_text", |
| 72 |
null, |
| 73 |
new SourceFilePosition( |
| 74 |
new SourceFile(FakePatternAwareOutputParser.ERROR_EXAMPLE_FILE), |
| 75 |
new SourcePosition(1, 2, 3, 4, 5, 6) |
| 76 |
))); |
| 77 |
|
| 78 |
Mockito.verify(sMessageReceiver).receiveMessage(new Message( |
| 79 |
Message.Kind.WARNING, |
| 80 |
"two line warning", |
| 81 |
new SourceFilePosition( |
| 82 |
new SourceFile(FakePatternAwareOutputParser.TWO_LINE_ERROR_FILE), |
| 83 |
new SourcePosition(1, 2, -1) |
| 84 |
))); |
| 85 |
|
| 86 |
|
| 87 |
Mockito.verify(sMessageReceiver).receiveMessage(new Message( |
| 88 |
Message.Kind.SIMPLE, |
| 89 |
"something else", |
| 90 |
SourceFilePosition.UNKNOWN)); |
| 91 |
Mockito.verifyNoMoreInteractions(sMessageReceiver); |
| 92 |
|
| 93 |
String expected = "AGPBI: {" |
| 94 |
+ "\"kind\":\"error\"," |
| 95 |
+ "\"text\":\"errorText\"," |
| 96 |
+ "\"sources\":[{" |
| 97 |
+ "\"file\":\"" + FakePatternAwareOutputParser.ERROR_EXAMPLE_FILE.getAbsolutePath() |
| 98 |
+ "\",\"position\":{\"startLine\":1,\"startColumn\":2,\"startOffset\":3," |
| 99 |
+ "\"endLine\":4,\"endColumn\":5,\"endOffset\":6}}]," |
| 100 |
+ "\"original\":\"original_text\"}\n" |
| 101 |
+ "AGPBI: {\"kind\":\"warning\"," |
| 102 |
+ "\"text\":\"two line warning\"," |
| 103 |
+ "\"sources\":[{\"file\":\"" + |
| 104 |
FakePatternAwareOutputParser.TWO_LINE_ERROR_FILE.getAbsolutePath() + "\"," |
| 105 |
+ "\"position\":{\"startLine\":1,\"startColumn\":2}}]}\n" |
| 106 |
+ "AGPBI: {\"kind\":\"simple\"," |
| 107 |
+ "\"text\":\"something else\"," |
| 108 |
+ "\"sources\":[{}]}"; |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void parseException() throws IOException, ProcessException { |
| 113 |
String original = "two line error"; |
| 114 |
|
| 115 |
ProcessOutput processOutput = processOutputFromErrorString(original); |
| 116 |
processOutput.close(); |
| 117 |
sParsingProcessOutputHandler.handleOutput(processOutput); |
| 118 |
|
| 119 |
Mockito.verifyNoMoreInteractions(sMessageReceiver); |
| 120 |
} |
| 121 |
|
| 122 |
private static ProcessOutput processOutputFromErrorString(String original) throws IOException { |
| 123 |
ProcessOutput processOutput = sParsingProcessOutputHandler.createOutput(); |
| 124 |
processOutput.getErrorOutput().write(original.getBytes(Charsets.UTF_8)); |
| 125 |
return processOutput; |
| 126 |
} |
| 127 |
|
| 128 |
private static class FakePatternAwareOutputParser implements PatternAwareOutputParser { |
| 129 |
static final File ERROR_EXAMPLE_FILE = new File("error/source"); |
| 130 |
static final File TWO_LINE_ERROR_FILE = new File("error/source/2"); |
| 131 |
@Override |
| 132 |
public boolean parse(@NonNull String line, @NonNull OutputLineReader reader, |
| 133 |
@NonNull List<Message> messages, @NonNull ILogger logger) |
| 134 |
throws ParsingFailedException { |
| 135 |
if (line.equals("two line error")) { |
| 136 |
String nextLine = reader.readLine(); |
| 137 |
if ("next line".equals(nextLine)) { |
| 138 |
messages.add(new Message( |
| 139 |
Message.Kind.WARNING, |
| 140 |
"two line warning", |
| 141 |
"two line warning", |
| 142 |
null, |
| 143 |
new SourceFilePosition( |
| 144 |
TWO_LINE_ERROR_FILE, |
| 145 |
new SourcePosition(1, 2, -1)))); |
| 146 |
} else { |
| 147 |
throw new ParsingFailedException(); |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |
| 151 |
if (line.equals("error example")) { |
| 152 |
messages.add( |
| 153 |
new Message( |
| 154 |
Message.Kind.ERROR, |
| 155 |
"errorText", |
| 156 |
"original_text", |
| 157 |
null, |
| 158 |
new SourceFilePosition( |
| 159 |
ERROR_EXAMPLE_FILE, |
| 160 |
new SourcePosition(1, 2, 3, 4, 5, 6)))); |
| 161 |
return true; |
| 162 |
} |
| 163 |
return false; |
| 164 |
} |
| 165 |
} |
| 166 |
} |