1 /*
2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import javax.tools.Diagnostic;
25 
26 import jdk.jshell.Diag;
27 import static org.testng.Assert.assertEquals;
28 
29 public class ExpectedDiagnostic {
30 
31     private final String code;
32     private final long startPosition;
33     private final long endPosition;
34     private final long position;
35     private final long lineNumber;
36     private final long columnNumber;
37     private final Diagnostic.Kind kind;
38 
ExpectedDiagnostic( String code, long startPosition, long endPosition, long position, long lineNumber, long columnNumber, Diagnostic.Kind kind)39     public ExpectedDiagnostic(
40             String code, long startPosition, long endPosition,
41             long position, long lineNumber, long columnNumber,
42             Diagnostic.Kind kind) {
43         this.code = code;
44         this.startPosition = startPosition;
45         this.endPosition = endPosition;
46         this.position = position;
47         this.lineNumber = lineNumber;
48         this.columnNumber = columnNumber;
49         this.kind = kind;
50     }
51 
getStartPosition()52     public long getStartPosition() {
53         return startPosition;
54     }
55 
getCode()56     public String getCode() {
57         return code;
58     }
59 
getEndPosition()60     public long getEndPosition() {
61         return endPosition;
62     }
63 
getPosition()64     public long getPosition() {
65         return position;
66     }
67 
getLineNumber()68     public long getLineNumber() {
69         return lineNumber;
70     }
71 
getColumnNumber()72     public long getColumnNumber() {
73         return columnNumber;
74     }
75 
getKind()76     public Diagnostic.Kind getKind() {
77         return kind;
78     }
79 
assertDiagnostic(Diag diagnostic)80     public void assertDiagnostic(Diag diagnostic) {
81         String code = diagnostic.getCode();
82         assertEquals(code, this.code, "Expected error: " + this.code + ", got: " + code);
83         assertEquals(diagnostic.isError(), kind == Diagnostic.Kind.ERROR);
84         if (startPosition != -1) {
85             assertEquals(diagnostic.getStartPosition(), startPosition, "Start position");
86         }
87         if (endPosition != -1) {
88             assertEquals(diagnostic.getEndPosition(), endPosition, "End position");
89         }
90         if (position != -1) {
91             assertEquals(diagnostic.getPosition(), position, "Position");
92         }
93     }
94 }
95