|
| 1 | +/* |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 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 | +package feign.codec; |
| 17 | + |
| 18 | +import dagger.ObjectGraph; |
| 19 | +import dagger.Provides; |
| 20 | +import org.testng.annotations.BeforeClass; |
| 21 | +import org.testng.annotations.Test; |
| 22 | +import org.xml.sax.helpers.DefaultHandler; |
| 23 | + |
| 24 | +import javax.inject.Inject; |
| 25 | +import javax.inject.Provider; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.StringReader; |
| 28 | +import java.text.ParseException; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +import static dagger.Provides.Type.SET; |
| 32 | +import static org.testng.Assert.assertEquals; |
| 33 | + |
| 34 | +// unbound wildcards are not currently injectable in dagger. |
| 35 | +@SuppressWarnings("rawtypes") |
| 36 | +public class SAXDecoderTest { |
| 37 | + |
| 38 | + @dagger.Module(injects = SAXDecoderTest.class) |
| 39 | + static class Module { |
| 40 | + @Provides(type = SET) Decoder saxDecoder(Provider<NetworkStatusHandler> networkStatus, // |
| 41 | + Provider<NetworkStatusStringHandler> networkStatusAsString) { |
| 42 | + return SAXDecoder.builder() // |
| 43 | + .addContentHandler(networkStatus) // |
| 44 | + .addContentHandler(networkStatusAsString) // |
| 45 | + .build(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Inject Set<Decoder> decoders; |
| 50 | + |
| 51 | + @BeforeClass void inject() { |
| 52 | + ObjectGraph.create(new Module()).inject(this); |
| 53 | + } |
| 54 | + |
| 55 | + @Test public void parsesConfiguredTypes() throws ParseException, IOException { |
| 56 | + Decoder decoder = decoders.iterator().next(); |
| 57 | + assertEquals(decoder.decode(new StringReader(statusFailed), NetworkStatus.class), NetworkStatus.FAILED); |
| 58 | + assertEquals(decoder.decode(new StringReader(statusFailed), String.class), "Failed"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = |
| 62 | + "type int not in configured handlers \\[class .*NetworkStatus, class java.lang.String\\]") |
| 63 | + public void niceErrorOnUnconfiguredType() throws ParseException, IOException { |
| 64 | + Decoder decoder = decoders.iterator().next(); |
| 65 | + decoder.decode(new StringReader(statusFailed), int.class); |
| 66 | + } |
| 67 | + |
| 68 | + static String statusFailed = ""// |
| 69 | + + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"// |
| 70 | + + " <soap:Body>\n"// |
| 71 | + + " <ns1:getNeustarNetworkStatusResponse xmlns:ns1=\"http://webservice.api.ultra.neustar.com/v01/\">\n"// |
| 72 | + + " <NeustarNetworkStatus xmlns:ns2=\"http://schema.ultraservice.neustar.com/v01/\">Failed</NeustarNetworkStatus>\n"// |
| 73 | + + " </ns1:getNeustarNetworkStatusResponse>\n"// |
| 74 | + + " </soap:Body>\n"// |
| 75 | + + "</soap:Envelope>"; |
| 76 | + |
| 77 | + static enum NetworkStatus { |
| 78 | + GOOD, FAILED; |
| 79 | + } |
| 80 | + |
| 81 | + static class NetworkStatusStringHandler extends DefaultHandler implements |
| 82 | + SAXDecoder.ContentHandlerWithResult<String> { |
| 83 | + @Inject NetworkStatusStringHandler() { |
| 84 | + } |
| 85 | + |
| 86 | + private StringBuilder currentText = new StringBuilder(); |
| 87 | + |
| 88 | + private String status; |
| 89 | + |
| 90 | + @Override |
| 91 | + public String result() { |
| 92 | + return status; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void endElement(String uri, String name, String qName) { |
| 97 | + if (qName.equals("NeustarNetworkStatus")) { |
| 98 | + this.status = currentText.toString().trim(); |
| 99 | + } |
| 100 | + currentText = new StringBuilder(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void characters(char ch[], int start, int length) { |
| 105 | + currentText.append(ch, start, length); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + static class NetworkStatusHandler extends DefaultHandler implements |
| 110 | + SAXDecoder.ContentHandlerWithResult<NetworkStatus> { |
| 111 | + @Inject NetworkStatusHandler() { |
| 112 | + } |
| 113 | + |
| 114 | + private StringBuilder currentText = new StringBuilder(); |
| 115 | + |
| 116 | + private NetworkStatus status; |
| 117 | + |
| 118 | + @Override |
| 119 | + public NetworkStatus result() { |
| 120 | + return status; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void endElement(String uri, String name, String qName) { |
| 125 | + if (qName.equals("NeustarNetworkStatus")) { |
| 126 | + this.status = NetworkStatus.valueOf(currentText.toString().trim().toUpperCase()); |
| 127 | + } |
| 128 | + currentText = new StringBuilder(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void characters(char ch[], int start, int length) { |
| 133 | + currentText.append(ch, start, length); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments