| 1 |
<%@ jet package="org.eclipse.emf.test.common.templates" imports="java.util.* org.eclipse.emf.common.util.*" class="DiagnosticTestGen"%> |
| 2 |
<% |
| 3 |
/** |
| 4 |
* Copyright (c) 2006-2007 IBM Corporation and others. |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* http://www.eclipse.org/legal/epl-v20.html |
| 9 |
* |
| 10 |
* Contributors: |
| 11 |
* IBM - Initial API and implementation |
| 12 |
*/ |
| 13 |
%> |
| 14 |
<% |
| 15 |
String name = (String)((Object[])argument)[0]; |
| 16 |
Diagnostic rootDiagnostic = (Diagnostic)((Object[])argument)[1]; |
| 17 |
TreeIterator<Diagnostic> diagnosticIterator = new AbstractTreeIterator<Diagnostic>(rootDiagnostic) |
| 18 |
{ |
| 19 |
private static final long serialVersionUID = 1L; |
| 20 |
@Override |
| 21 |
protected Iterator<? extends Diagnostic> getChildren(Object object) |
| 22 |
{ |
| 23 |
return ((Diagnostic)object).getChildren().iterator(); |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
class Helper |
| 28 |
{ |
| 29 |
public String toString(Throwable throwable) |
| 30 |
{ |
| 31 |
StringBuilder sb = new StringBuilder(); |
| 32 |
sb.append(throwable.getClass().getName()); |
| 33 |
sb.append("#").append(throwable.getMessage()); |
| 34 |
|
| 35 |
Throwable cause = throwable.getCause(); |
| 36 |
if (cause != null && cause != throwable) |
| 37 |
{ |
| 38 |
sb.append("--").append(toString(cause)); |
| 39 |
} |
| 40 |
return sb.toString(); |
| 41 |
} |
| 42 |
|
| 43 |
protected String removeObjectHashCode(String string) |
| 44 |
{ |
| 45 |
return string.replaceAll("@\\w+", ""); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
public String toCodeString(String string) |
| 50 |
{ |
| 51 |
return "\"" + string.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\\"", "\\\\\"") + "\""; |
| 52 |
} |
| 53 |
|
| 54 |
public String getDiagnosticSeverity(int value) |
| 55 |
{ |
| 56 |
switch(value) |
| 57 |
{ |
| 58 |
case Diagnostic.CANCEL: |
| 59 |
return "Diagnostic.CANCEL"; |
| 60 |
case Diagnostic.OK: |
| 61 |
return "Diagnostic.OK"; |
| 62 |
case Diagnostic.INFO: |
| 63 |
return "Diagnostic.INFO"; |
| 64 |
case Diagnostic.ERROR: |
| 65 |
return "Diagnostic.ERROR"; |
| 66 |
case Diagnostic.WARNING: |
| 67 |
return "Diagnostic.WARNING"; |
| 68 |
default: |
| 69 |
return Integer.toString(value); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
Helper helper = new Helper(); |
| 74 |
%> |
| 75 |
|
| 76 |
import java.util.Iterator; |
| 77 |
|
| 78 |
import org.eclipse.emf.common.util.AbstractTreeIterator; |
| 79 |
import org.eclipse.emf.common.util.Diagnostic; |
| 80 |
import org.eclipse.emf.common.util.TreeIterator; |
| 81 |
import org.eclipse.emf.common.util.URI; |
| 82 |
import org.eclipse.emf.ecore.EPackage; |
| 83 |
import org.eclipse.emf.ecore.resource.Resource; |
| 84 |
import org.eclipse.emf.ecore.util.Diagnostician; |
| 85 |
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl; |
| 86 |
import org.eclipse.emf.test.common.TestUtil; |
| 87 |
|
| 88 |
import static org.junit.Assert.*; |
| 89 |
import org.junit.Before; |
| 90 |
import org.junit.Test; |
| 91 |
|
| 92 |
public class <%=name%>DiagnosticTest |
| 93 |
{ |
| 94 |
protected Diagnostic diagnostic; |
| 95 |
|
| 96 |
@Before |
| 97 |
public void setUp() throws Exception |
| 98 |
{ |
| 99 |
//TODO:Instantiates the diagnostic here |
| 100 |
|
| 101 |
assertNotNull(diagnostic); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void testDiagnostic() throws Exception |
| 106 |
{ |
| 107 |
TreeIterator<Diagnostic> diagnosticIterator = new AbstractTreeIterator<Diagnostic>(diagnostic) |
| 108 |
{ |
| 109 |
private static final long serialVersionUID = 1L; |
| 110 |
@Override |
| 111 |
protected Iterator<? extends Diagnostic> getChildren(Object object) |
| 112 |
{ |
| 113 |
return ((Diagnostic)object).getChildren().iterator(); |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
<%int count=0; while(diagnosticIterator.hasNext()){Diagnostic diagnostic = diagnosticIterator.next(); count++;%> |
| 118 |
Diagnostic diagnostic<%=count%> = diagnosticIterator.next(); |
| 119 |
assertEquals(<%=helper.getDiagnosticSeverity(diagnostic.getSeverity())%>, diagnostic<%=count%>.getSeverity()); |
| 120 |
assertEquals(<%=helper.toCodeString(diagnostic.getSource())%>, diagnostic<%=count%>.getSource()); |
| 121 |
assertEquals(<%=helper.removeObjectHashCode(helper.toCodeString(diagnostic.getMessage()))%>, removeObjectHashCode(diagnostic<%=count%>.getMessage())); |
| 122 |
assertEquals(<%=diagnostic.getCode()%>, diagnostic<%=count%>.getCode()); |
| 123 |
assertEquals(<%=diagnostic.getChildren().size()%>, diagnostic<%=count%>.getChildren().size()); |
| 124 |
assertEquals(<%=diagnostic.getData().size()%>, diagnostic<%=count%>.getData().size()); |
| 125 |
<%Throwable throwable = diagnostic.getException(); if (throwable != null) {%>assertEquals(<%=helper.toCodeString(helper.toString(diagnostic.getException()))%>, toString(diagnostic<%=count%>.getException())); |
| 126 |
<%} else {%>assertNull(diagnostic<%=count%>.getException());<%} if (diagnosticIterator.hasNext()) {stringBuffer.append(NL);}}%> |
| 127 |
|
| 128 |
assertFalse(diagnosticIterator.hasNext()); |
| 129 |
} |
| 130 |
|
| 131 |
protected String toString(Throwable throwable) |
| 132 |
{ |
| 133 |
StringBuilder sb = new StringBuilder(); |
| 134 |
sb.append(throwable.getClass().getName()); |
| 135 |
sb.append("#").append(throwable.getMessage()); |
| 136 |
|
| 137 |
Throwable cause = throwable.getCause(); |
| 138 |
if (cause != null && cause != throwable) |
| 139 |
{ |
| 140 |
sb.append("--").append(toString(cause)); |
| 141 |
} |
| 142 |
return sb.toString(); |
| 143 |
} |
| 144 |
|
| 145 |
protected String removeObjectHashCode(String string) |
| 146 |
{ |
| 147 |
return string.replaceAll("@\\w+", ""); |
| 148 |
} |
| 149 |
} |