Java tutorial
/* * The MIT License * * Copyright 2014 CloudBees. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.cloudbees.mtslaves.client; import java.io.PrintWriter; import java.io.StringWriter; import net.sf.json.JSONObject; import org.apache.commons.io.IOUtils; import org.junit.Test; import static org.junit.Assert.*; public class ServerExceptionTest { @Test public void stackTraces() throws Exception { Exception x1; try { serverside(); throw new AssertionError(); } catch (Exception _x) { x1 = _x; } Exception x2; try { clientside(x1); throw new AssertionError(); } catch (Exception _x) { x2 = _x; } StringWriter w = new StringWriter(); x2.printStackTrace(new PrintWriter(w)); assertEquals(IOUtils.toString(ServerExceptionTest.class.getResource("expectedStackTrace.txt"), "UTF-8"), w.toString().replace(ServerExceptionTest.class.getName(), "SET") .replace(ServerException.class.getName(), "SE") .replaceAll("[.][.][.] \\d+ more", "... more") .replaceAll("[(]ServerExceptionTest.java:\\d+[)]", "") .replaceAll("((\n\tat SET[.].+)+)(\n\tat .+)*", "$1\n\tat ")); } private Void serverside() throws Exception { throw new IllegalStateException("failed"); } private Void clientside(Exception x1) throws Exception { try { clientside2(x1); throw new AssertionError(); } catch (ServerException x) { throw new Exception("wrapped", x); } } private Void clientside2(Exception x1) throws ServerException { StringWriter w = new StringWriter(); x1.printStackTrace(new PrintWriter(w)); JSONObject json = new JSONObject().accumulate("somekey", "someval").accumulate("cause", w.toString()); throw new ServerException("basic message", json, 0, null, null); } }