-
Notifications
You must be signed in to change notification settings - Fork 15
/
FaultTest.java
131 lines (115 loc) · 5.11 KB
/
FaultTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package soap;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import jakarta.xml.soap.*;
import javax.xml.transform.stream.*;
import javax.xml.namespace.QName;
import junit.framework.TestCase;
public class FaultTest extends TestCase {
public FaultTest(String name) {
super(name);
}
public void testGetDetail() throws Exception {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
SOAPFault fault = body.addFault();
Detail detail = fault.addDetail();
String detailEntryLocalName = "name";
Name detailEntryName = envelope.createName(detailEntryLocalName, "prefix", "uri");
detail.addDetailEntry(detailEntryName);
SOAPFault extractedFault = body.getFault();
assertTrue(extractedFault != null);
Detail extractedDetail = extractedFault.getDetail();
assertTrue(extractedDetail != null);
Iterator<DetailEntry> eachDetailEntry = extractedDetail.getDetailEntries();
assertTrue(eachDetailEntry.hasNext());
DetailEntry extractedEntry = eachDetailEntry.next();
assertEquals(detailEntryLocalName, extractedEntry.getLocalName());
assertFalse(eachDetailEntry.hasNext());
extractedFault.setFaultActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
Detail d = extractedFault.getDetail();
d.detachNode();
d = extractedFault.getDetail();
assertTrue(d == null);
}
public void testFaultWithAmp() throws Exception {
String testDoc =
"<?xml version='1.0' encoding='UTF-8'?>"
+ "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body>"
+ "<soap:Fault>"
+ "<soap:faultcode>"
+ "code>code"
+ "</soap:faultcode>"
+ "<soap:faultstring>"
+ "string>string"
+ "</soap:faultstring>"
+ "<soap:faultactor>"
+ "actor>actor"
+ "</soap:faultactor>"
+ "<soap:detail />"
+ "</soap:Fault>"
+ "</soap:Body>"
+ "</soap:Envelope>";
byte[] testDocBytes = testDoc.getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream bais = new ByteArrayInputStream(testDocBytes);
StreamSource strSource = new StreamSource(bais);
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
soapPart.setContent(strSource);
message.saveChanges();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPFault fault = body.getFault();
assertTrue(fault.getFaultCode().equals("code>code"));
assertTrue(fault.getFaultString().equals("string>string"));
assertTrue(fault.getFaultActor().equals("actor>actor"));
}
/**
* Test case for CR ID 6212709
*/
public void testSetFaultCodeWithNameArg() throws Exception {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPBody body = message.getSOAPBody();
SOAPFault fault = body.addFault();
Name faultCodeName = SOAPFactory.newInstance().createName("Client", "env", null);
try {
fault.setFaultCode(faultCodeName);
} catch (SOAPException se) {
return;
}
fail("Invalid fault code allowed to be set");
}
public void testSetFaultCodeWithPrefixEmpty() throws Exception {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPBody body = message.getSOAPBody();
body.addFault(new QName("some-uri", "code"), "Some string");
}
public void testSetFaultStringAndLocale() throws Exception {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPBody body = message.getSOAPBody();
body.addFault(new QName("some-uri", "code"), "Some string");
SOAPFault fault = body.getFault();
fault.setFaultString("EN faultString" , java.util.Locale.ENGLISH);
//System.out.println(fault.getFaultString() + " : " + fault.getFaultStringLocale());
fault.setFaultString("No Locale faultString");
//System.out.println(fault.getFaultString() + " : " + fault.getFaultStringLocale());
assertTrue(fault.getFaultStringLocale() == null);
}
}