Author: julien_viet
Date: 2009-10-25 17:04:29 -0400 (Sun, 25 Oct 2009)
New Revision: 427
Added:
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/BufferingOutputStream.java
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestOutputStreamPrinter.java
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestPrinter.java
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestSafe.java
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestTextEncoder.java
Removed:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/OutputStreamPrinterTestCase.java
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/PrinterTestCase.java
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/SafeTestCase.java
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TextEncoderTestCase.java
Modified:
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/CharsetTextEncoder.java
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/OutputStreamPrinter.java
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/Printer.java
portal/branches/performance/component/scripting/src/main/java/org/exoplatform/commons/utils/PortalPrinter.java
portal/branches/performance/webui/portal/src/main/java/org/exoplatform/portal/application/PortalRequestContext.java
Log:
- avoid to use byte[] when not necessary during the conversion of char to byte
- maintains a byte[] buffer at the portal level instead of relying on the servlet output
stream buffer
- rename a few unit tests to Test* as the *TestCase is not recognized by the build
Added:
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/BufferingOutputStream.java
===================================================================
---
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/BufferingOutputStream.java
(rev 0)
+++
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/BufferingOutputStream.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2003-2007 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.commons.utils;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * A stream that maintains a buffer and flush it on a delegate output stream when it is
+ * filled. Unlike {@link java.io.BufferedOutputStream} this class is not synchronized.
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class BufferingOutputStream extends OutputStream
+{
+
+ /** . */
+ private final OutputStream out;
+
+ /** . */
+ private byte[] buffer;
+
+ /** . */
+ private boolean open;
+
+ /** . */
+ private int offset;
+
+ /** . */
+ private final int size;
+
+ public BufferingOutputStream(OutputStream out, int bufferSize)
+ {
+ if (out == null)
+ {
+ throw new NullPointerException("No null output stream");
+ }
+ if (bufferSize < 1)
+ {
+ throw new IllegalArgumentException("No buffer size under 1");
+ }
+ this.out = out;
+ this.buffer = new byte[bufferSize];
+ this.size = bufferSize;
+ this.open = true;
+ }
+
+ @Override
+ public void write(int b) throws IOException
+ {
+ if (!open)
+ {
+ throw new IOException("closed");
+ }
+ if (offset >= size)
+ {
+ out.write(buffer);
+ offset = 0;
+ }
+ buffer[offset++] = (byte)b;
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ if (!open)
+ {
+ throw new IOException("closed");
+ }
+
+ //
+ if (offset + len >= size)
+ {
+ // Clear the buffer
+ out.write(buffer, 0, offset);
+ offset = 0;
+
+ // While the data length is greater than the the buffer size
+ // write directly to the wire
+ while (len >= size)
+ {
+ out.write(b, off, size);
+ off += size;
+ len -= size;
+ }
+ }
+
+ //
+ System.arraycopy(b, off, buffer, offset, len);
+ offset += len;
+ }
+
+ @Override
+ public void flush() throws IOException
+ {
+ if (!open)
+ {
+ throw new IOException("closed");
+ }
+
+ //
+ if (offset > 0)
+ {
+ out.write(buffer, 0, offset);
+ offset = 0;
+ }
+
+ //
+ out.flush();
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ if (!open)
+ {
+ throw new IOException("closed");
+ }
+
+ //
+ if (offset > 0)
+ {
+ out.write(buffer, 0, offset);
+ offset = 0;
+ }
+
+ //
+ open = false;
+ out.close();
+ }
+}
Modified:
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/CharsetTextEncoder.java
===================================================================
---
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/CharsetTextEncoder.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/CharsetTextEncoder.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -29,7 +29,7 @@
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
-public class CharsetTextEncoder implements TextEncoder
+public final class CharsetTextEncoder implements TextEncoder
{
private static final CharsetTextEncoder UTF8 = new
CharsetTextEncoder(CharsetCharEncoder.getUTF8());
@@ -54,8 +54,33 @@
public void encode(char c, OutputStream out) throws IOException
{
- byte[] bytes = charEncoder.encode(c);
- out.write(bytes);
+ if (c > -1 && c < 128)
+ {
+ out.write((int)c);
+ }
+ else
+ {
+ byte[] bytes = charEncoder.encode(c);
+ switch (bytes.length)
+ {
+ case 0:
+ out.write(bytes[0]);
+ break;
+ case 1:
+ out.write(bytes[0]);
+ out.write(bytes[1]);
+ break;
+ case 2:
+ out.write(bytes[0]);
+ out.write(bytes[1]);
+ out.write(bytes[2]);
+ break;
+ case 3:
+ break;
+ default:
+ out.write(bytes);
+ }
+ }
}
public void encode(char[] chars, int off, int len, OutputStream out) throws
IOException
@@ -63,8 +88,7 @@
for (int i = off; i < len; i++)
{
char c = chars[i];
- byte[] bytes = charEncoder.encode(c);
- out.write(bytes);
+ encode(c, out);
}
}
@@ -73,8 +97,7 @@
for (int i = off; i < len; i++)
{
char c = str.charAt(i);
- byte[] bytes = charEncoder.encode(c);
- out.write(bytes);
+ encode(c, out);
}
}
}
Modified:
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/OutputStreamPrinter.java
===================================================================
---
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/OutputStreamPrinter.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/OutputStreamPrinter.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -42,6 +42,9 @@
*
* </p>
*
+ * <p>The class provides direct write access to the underlying output stream when
the client of the stream can provides
+ * bytes directly.</p>
+ *
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
@@ -71,10 +74,37 @@
*/
public OutputStreamPrinter(TextEncoder encoder, OutputStream out, boolean
flushOnClose) throws IllegalArgumentException
{
- this(encoder, out, IOFailureFlow.RETHROW, false, flushOnClose);
+ this(encoder, out, IOFailureFlow.RETHROW, false, flushOnClose, 0);
}
/**
+ * Builds an instance with the failureFlow being {@link IOFailureFlow#RETHROW} and
+ * a the ignoreOnFailure property set to false.
+ *
+ * @param encoder the encoder
+ * @param out the output
+ * @param flushOnClose flush when stream is closed
+ * @throws IllegalArgumentException if any argument is null
+ */
+ public OutputStreamPrinter(TextEncoder encoder, OutputStream out, boolean
flushOnClose, int bufferSize) throws IllegalArgumentException
+ {
+ this(encoder, out, IOFailureFlow.RETHROW, false, flushOnClose, bufferSize);
+ }
+
+ /**
+ * Builds an instance with the failureFlow being {@link IOFailureFlow#RETHROW} and
+ * a the ignoreOnFailure property set to false.
+ *
+ * @param encoder the encoder
+ * @param out the output
+ * @throws IllegalArgumentException if any argument is null
+ */
+ public OutputStreamPrinter(TextEncoder encoder, OutputStream out) throws
IllegalArgumentException
+ {
+ this(encoder, out, IOFailureFlow.RETHROW, false, false, 0);
+ }
+
+ /**
* Builds a new instance.
*
* @param encoder the encoder
@@ -89,7 +119,8 @@
OutputStream out,
IOFailureFlow failureFlow,
boolean ignoreOnFailure,
- boolean flushOnClose)
+ boolean flushOnClose,
+ int bufferSize)
throws IllegalArgumentException
{
if (encoder == null)
@@ -104,6 +135,18 @@
{
throw new IllegalArgumentException("No null control flow mode
accepted");
}
+ if (bufferSize < 0)
+ {
+ throw new IllegalArgumentException("Invalid negative max buffer size:
" + bufferSize);
+ }
+
+ //
+ if (bufferSize > 0)
+ {
+ out = new BufferingOutputStream(out, bufferSize);
+ }
+
+ //
this.encoder = encoder;
this.out = out;
this.failureFlow = failureFlow;
@@ -137,6 +180,55 @@
return failed;
}
+ // Bytes access interface
+
+ public void write(byte b) throws IOException
+ {
+ if (!failed)
+ {
+ try
+ {
+ out.write(b);
+ }
+ catch (IOException e)
+ {
+ handle(e);
+ }
+ }
+ }
+
+ public void write(byte[] bytes) throws IOException
+ {
+ if (!failed)
+ {
+ try
+ {
+ out.write(bytes);
+ }
+ catch (IOException e)
+ {
+ handle(e);
+ }
+ }
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ if (!failed)
+ {
+ try
+ {
+ out.write(b, off, len);
+ }
+ catch (IOException e)
+ {
+ handle(e);
+ }
+ }
+ }
+
+ //
+
@Override
public void write(int c) throws IOException
{
Modified:
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/Printer.java
===================================================================
---
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/Printer.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/component/common/src/main/java/org/exoplatform/commons/utils/Printer.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -54,17 +54,21 @@
{
try
{
- if (o instanceof Text)
+ if (o == null)
{
+ write("null");
+ }
+ else if (o instanceof Text)
+ {
((Text)o).writeTo(this);
}
else
{
- String s = String.valueOf(o);
+ String s = (o == null) ? "null" : o.toString();
write(s);
}
}
- catch (IOException ignore)
+ catch (IOException e)
{
}
}
Deleted:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/OutputStreamPrinterTestCase.java
===================================================================
---
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/OutputStreamPrinterTestCase.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/OutputStreamPrinterTestCase.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -1,312 +0,0 @@
-/**
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.exoplatform.commons.utils;
-
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.LinkedList;
-
-/**
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class OutputStreamPrinterTestCase extends TestCase
-{
-
- static final int NOOP = -1;
-
- static final int WRITE = 0;
-
- static final int FLUSH = 1;
-
- static final int CLOSE = 2;
-
- public void testIOExceptionBlocking()
- {
- internalTest(IOFailureFlow.RETHROW, true);
- }
-
- public void testUndeclaredIOExceptionBlocking()
- {
- internalTest(IOFailureFlow.THROW_UNDECLARED, true);
- }
-
- public void testIgnoreIOExceptionBlocking()
- {
- internalTest(IOFailureFlow.IGNORE, true);
- }
-
- public void testIOExceptionNoBlocking()
- {
- internalTest(IOFailureFlow.RETHROW, false);
- }
-
- public void testUndeclaredIOExceptionNoBlocking()
- {
- internalTest(IOFailureFlow.THROW_UNDECLARED, false);
- }
-
- public void testIgnoreIOExceptionNoBlocking()
- {
- internalTest(IOFailureFlow.IGNORE, false);
- }
-
- public void internalTest(IOFailureFlow mode, boolean blockOnFailure)
- {
- int writeAfterFailure = blockOnFailure ? NOOP : WRITE;
- int flushAfterFailure = blockOnFailure ? NOOP : FLUSH;
-
- //
- TestOutputStream out = new TestOutputStream(true, mode, blockOnFailure);
- out.write(mode);
- out.assertOperation(WRITE).assertEmpty().wantFailure = false;
- out.flush(IOFailureFlow.IGNORE);
- out.assertOperation(flushAfterFailure).assertEmpty();
- out.write(IOFailureFlow.IGNORE);
- out.assertOperation(writeAfterFailure).assertEmpty();
- out.close(IOFailureFlow.IGNORE);
- out.assertOperation(CLOSE).assertEmpty();
-
- //
- out = new TestOutputStream(false, mode, blockOnFailure);
- out.write(IOFailureFlow.IGNORE);
- out.assertOperation(WRITE).assertEmpty().wantFailure = true;
- out.flush(mode);
- out.assertOperation(FLUSH).assertEmpty().wantFailure = false;
- out.write(IOFailureFlow.IGNORE);
- out.assertOperation(writeAfterFailure).assertEmpty();
- out.close(IOFailureFlow.IGNORE);
- out.assertOperation(CLOSE).assertEmpty();
-
- //
- out = new TestOutputStream(false, mode, blockOnFailure);
- out.write(IOFailureFlow.IGNORE);
- out.assertOperation(WRITE).assertEmpty();
- out.flush(IOFailureFlow.IGNORE);
- out.assertOperation(FLUSH).assertEmpty().wantFailure = true;
- out.write(mode);
- out.assertOperation(WRITE).assertEmpty().wantFailure = false;
- out.close(IOFailureFlow.IGNORE);
- out.assertOperation(CLOSE).assertEmpty();
-
- //
- out = new TestOutputStream(false, mode, blockOnFailure);
- out.write(IOFailureFlow.IGNORE);
- out.assertOperation(WRITE).assertEmpty();
- out.flush(IOFailureFlow.IGNORE);
- out.assertOperation(FLUSH).assertEmpty();
- out.write(IOFailureFlow.IGNORE);
- out.assertOperation(WRITE).assertEmpty().wantFailure = true;
- out.close(mode);
- out.assertOperation(CLOSE).assertEmpty();
- }
-
- private static class TestOutputStream extends OutputStream
- {
-
- boolean wantFailure = false;
-
- final LinkedList<Integer> operations = new LinkedList<Integer>();
-
- OutputStreamPrinter osp;
-
- private TestOutputStream(boolean wantFailure, IOFailureFlow mode, boolean
blockOnFailure)
- {
- this.wantFailure = wantFailure;
- this.osp = new OutputStreamPrinter(CharsetTextEncoder.getUTF8(), this, mode,
blockOnFailure, false);
- }
-
- public void write(int b) throws IOException
- {
- operations.add(WRITE);
- if (wantFailure)
- {
- throw new IOException();
- }
- }
-
- @Override
- public void flush() throws IOException
- {
- operations.add(FLUSH);
- if (wantFailure)
- {
- throw new IOException();
- }
- }
-
- @Override
- public void close() throws IOException
- {
- operations.add(CLOSE);
- if (wantFailure)
- {
- throw new IOException();
- }
- }
-
- public void write(IOFailureFlow mode)
- {
- switch (mode)
- {
- case RETHROW :
- try
- {
- osp.write("a");
- fail();
- }
- catch (IOException ignore)
- {
- }
- break;
- case THROW_UNDECLARED :
- try
- {
- osp.write("a");
- fail();
- }
- catch (UndeclaredIOException ignore)
- {
- }
- catch (IOException expected)
- {
- fail();
- }
- break;
- case IGNORE :
- try
- {
- osp.write("a");
- }
- catch (UndeclaredIOException ignore)
- {
- fail();
- }
- catch (IOException expected)
- {
- fail();
- }
- break;
- }
- }
-
- public void flush(IOFailureFlow mode)
- {
- switch (mode)
- {
- case RETHROW :
- try
- {
- osp.flush();
- fail();
- }
- catch (IOException ignore)
- {
- }
- break;
- case THROW_UNDECLARED :
- try
- {
- osp.flush();
- fail();
- }
- catch (UndeclaredIOException ignore)
- {
- }
- catch (IOException e)
- {
- fail();
- }
- break;
- case IGNORE :
- try
- {
- osp.flush();
- }
- catch (IOException e)
- {
- fail();
- }
- break;
- }
- }
-
- public void close(IOFailureFlow mode)
- {
- switch (mode)
- {
- case RETHROW :
- try
- {
- osp.close();
- fail();
- }
- catch (IOException ignore)
- {
- }
- break;
- case THROW_UNDECLARED :
- try
- {
- osp.close();
- fail();
- }
- catch (UndeclaredIOException ignore)
- {
- }
- catch (IOException e)
- {
- fail();
- }
- break;
- case IGNORE :
- try
- {
- osp.close();
- }
- catch (IOException e)
- {
- fail();
- }
- break;
- }
- }
-
- public TestOutputStream assertEmpty()
- {
- Assert.assertTrue(operations.isEmpty());
- return this;
- }
-
- public TestOutputStream assertOperation(int operation)
- {
- if (operation != NOOP)
- {
- Assert.assertFalse(operations.isEmpty());
- int actual = operations.removeFirst();
- Assert.assertEquals(operation, actual);
- }
- return this;
- }
- }
-}
Deleted:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/PrinterTestCase.java
===================================================================
---
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/PrinterTestCase.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/PrinterTestCase.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -1,135 +0,0 @@
-/**
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.exoplatform.commons.utils;
-
-import junit.framework.TestCase;
-
-import java.io.IOException;
-
-/**
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class PrinterTestCase extends TestCase
-{
-
- private MyPrinter printer;
-
- @Override
- protected void setUp() throws Exception
- {
- printer = new MyPrinter();
- }
-
- @Override
- protected void tearDown() throws Exception
- {
- printer = null;
- }
-
- public void testPrintNull()
- {
- printer.print(null);
- assertEquals("null", printer.buffer.toString());
- }
-
- public void testPrint()
- {
- printer.print("foo");
- assertEquals("foo", printer.buffer.toString());
- }
-
- public void testPrintlnNull()
- {
- printer.println(null);
- assertEquals("null\n", printer.buffer.toString());
- }
-
- public void testPrintln()
- {
- printer.println("foo");
- assertEquals("foo\n", printer.buffer.toString());
- }
-
- public void testPrintln2()
- {
- printer.println();
- assertEquals("\n", printer.buffer.toString());
- }
-
- public void testWriteNull() throws IOException
- {
- try
- {
- printer.write((String)null);
- fail();
- }
- catch (Exception ignore)
- {
- assertEquals("", printer.buffer.toString());
- }
- try
- {
- printer.write((String)null, 0, 10);
- fail();
- }
- catch (Exception ignore)
- {
- assertEquals("", printer.buffer.toString());
- }
- try
- {
- printer.write((char[])null, 0, 10);
- fail();
- }
- catch (Exception ignore)
- {
- assertEquals("", printer.buffer.toString());
- }
- try
- {
- printer.write((char[])null);
- fail();
- }
- catch (Exception ignore)
- {
- assertEquals("", printer.buffer.toString());
- }
- }
-
- private static class MyPrinter extends Printer
- {
-
- private StringBuffer buffer = new StringBuffer();
-
- public void write(char[] cbuf, int off, int len) throws IOException
- {
- buffer.append(cbuf, off, len);
- }
-
- public void flush() throws IOException
- {
- }
-
- public void close() throws IOException
- {
- }
- }
-}
Deleted:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/SafeTestCase.java
===================================================================
---
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/SafeTestCase.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/SafeTestCase.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -1,142 +0,0 @@
-/**
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.exoplatform.commons.utils;
-
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-
-import java.io.ByteArrayOutputStream;
-import java.io.Closeable;
-import java.io.IOException;
-
-/**
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class SafeTestCase extends TestCase
-{
-
- public SafeTestCase()
- {
- }
-
- public SafeTestCase(String s)
- {
- super(s);
- }
-
- public void testClose()
- {
- assertFalse(Safe.close(null));
- assertTrue(Safe.close(new ByteArrayOutputStream()));
- assertFalse(Safe.close(new Closeable()
- {
- public void close() throws IOException
- {
- throw new IOException();
- }
- }));
- assertFalse(Safe.close(new Closeable()
- {
- public void close() throws IOException
- {
- throw new RuntimeException();
- }
- }));
- final Error expectedError = new Error();
- try
- {
- Safe.close(new Closeable()
- {
- public void close() throws IOException
- {
- throw expectedError;
- }
- });
- fail();
- }
- catch (Error error)
- {
- assertSame(expectedError, error);
- }
- }
-
- private static class Thrower
- {
-
- private final Throwable t;
-
- private Thrower(Throwable t)
- {
- this.t = t;
- }
-
- @Override
- public boolean equals(Object obj)
- {
- if (t instanceof Error)
- {
- throw ((Error)t);
- }
- if (t instanceof RuntimeException)
- {
- throw ((RuntimeException)t);
- }
- throw new AssertionFailedError();
- }
- }
-
- public void testEquals()
- {
- Object o = new Object();
- assertTrue(Safe.equals(o, o));
- assertTrue(Safe.equals(null, null));
- assertFalse(Safe.equals(new Object(), null));
- assertFalse(Safe.equals(null, new Object()));
- assertFalse(Safe.equals(new Object(), new Object()));
- assertFalse(Safe.equals(new Thrower(new RuntimeException()), null));
- assertFalse(Safe.equals(null, new Thrower(new RuntimeException())));
- assertFalse(Safe.equals(new Object(), new Thrower(new RuntimeException())));
- assertFalse(Safe.equals(new Thrower(new Error()), null));
- assertFalse(Safe.equals(null, new Thrower(new Error())));
- assertFalse(Safe.equals(new Object(), new Thrower(new Error())));
- RuntimeException re = new RuntimeException();
- Error er = new Error();
- try
- {
- Safe.equals(new Thrower(er), new Object());
- fail();
- }
- catch (Error e)
- {
- assertSame(er, e);
- }
- try
- {
- Safe.equals(new Thrower(re), new Object());
- fail();
- }
- catch (RuntimeException e)
- {
- assertSame(re, e);
- }
- }
-
-}
Copied:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestOutputStreamPrinter.java
(from rev 424,
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/OutputStreamPrinterTestCase.java)
===================================================================
---
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestOutputStreamPrinter.java
(rev 0)
+++
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestOutputStreamPrinter.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -0,0 +1,312 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.LinkedList;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestOutputStreamPrinter extends TestCase
+{
+
+ static final int NOOP = -1;
+
+ static final int WRITE = 0;
+
+ static final int FLUSH = 1;
+
+ static final int CLOSE = 2;
+
+ public void testIOExceptionBlocking()
+ {
+ internalTest(IOFailureFlow.RETHROW, true);
+ }
+
+ public void testUndeclaredIOExceptionBlocking()
+ {
+ internalTest(IOFailureFlow.THROW_UNDECLARED, true);
+ }
+
+ public void testIgnoreIOExceptionBlocking()
+ {
+ internalTest(IOFailureFlow.IGNORE, true);
+ }
+
+ public void testIOExceptionNoBlocking()
+ {
+ internalTest(IOFailureFlow.RETHROW, false);
+ }
+
+ public void testUndeclaredIOExceptionNoBlocking()
+ {
+ internalTest(IOFailureFlow.THROW_UNDECLARED, false);
+ }
+
+ public void testIgnoreIOExceptionNoBlocking()
+ {
+ internalTest(IOFailureFlow.IGNORE, false);
+ }
+
+ public void internalTest(IOFailureFlow mode, boolean blockOnFailure)
+ {
+ int writeAfterFailure = blockOnFailure ? NOOP : WRITE;
+ int flushAfterFailure = blockOnFailure ? NOOP : FLUSH;
+
+ //
+ TestOutputStream out = new TestOutputStream(true, mode, blockOnFailure);
+ out.write(mode);
+ out.assertOperation(WRITE).assertEmpty().wantFailure = false;
+ out.flush(IOFailureFlow.IGNORE);
+ out.assertOperation(flushAfterFailure).assertEmpty();
+ out.write(IOFailureFlow.IGNORE);
+ out.assertOperation(writeAfterFailure).assertEmpty();
+ out.close(IOFailureFlow.IGNORE);
+ out.assertOperation(CLOSE).assertEmpty();
+
+ //
+ out = new TestOutputStream(false, mode, blockOnFailure);
+ out.write(IOFailureFlow.IGNORE);
+ out.assertOperation(WRITE).assertEmpty().wantFailure = true;
+ out.flush(mode);
+ out.assertOperation(FLUSH).assertEmpty().wantFailure = false;
+ out.write(IOFailureFlow.IGNORE);
+ out.assertOperation(writeAfterFailure).assertEmpty();
+ out.close(IOFailureFlow.IGNORE);
+ out.assertOperation(CLOSE).assertEmpty();
+
+ //
+ out = new TestOutputStream(false, mode, blockOnFailure);
+ out.write(IOFailureFlow.IGNORE);
+ out.assertOperation(WRITE).assertEmpty();
+ out.flush(IOFailureFlow.IGNORE);
+ out.assertOperation(FLUSH).assertEmpty().wantFailure = true;
+ out.write(mode);
+ out.assertOperation(WRITE).assertEmpty().wantFailure = false;
+ out.close(IOFailureFlow.IGNORE);
+ out.assertOperation(CLOSE).assertEmpty();
+
+ //
+ out = new TestOutputStream(false, mode, blockOnFailure);
+ out.write(IOFailureFlow.IGNORE);
+ out.assertOperation(WRITE).assertEmpty();
+ out.flush(IOFailureFlow.IGNORE);
+ out.assertOperation(FLUSH).assertEmpty();
+ out.write(IOFailureFlow.IGNORE);
+ out.assertOperation(WRITE).assertEmpty().wantFailure = true;
+ out.close(mode);
+ out.assertOperation(CLOSE).assertEmpty();
+ }
+
+ private static class TestOutputStream extends OutputStream
+ {
+
+ boolean wantFailure = false;
+
+ final LinkedList<Integer> operations = new LinkedList<Integer>();
+
+ OutputStreamPrinter osp;
+
+ private TestOutputStream(boolean wantFailure, IOFailureFlow mode, boolean
blockOnFailure)
+ {
+ this.wantFailure = wantFailure;
+ this.osp = new OutputStreamPrinter(CharsetTextEncoder.getUTF8(), this, mode,
blockOnFailure, false, 0);
+ }
+
+ public void write(int b) throws IOException
+ {
+ operations.add(WRITE);
+ if (wantFailure)
+ {
+ throw new IOException();
+ }
+ }
+
+ @Override
+ public void flush() throws IOException
+ {
+ operations.add(FLUSH);
+ if (wantFailure)
+ {
+ throw new IOException();
+ }
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ operations.add(CLOSE);
+ if (wantFailure)
+ {
+ throw new IOException();
+ }
+ }
+
+ public void write(IOFailureFlow mode)
+ {
+ switch (mode)
+ {
+ case RETHROW :
+ try
+ {
+ osp.write("a");
+ fail();
+ }
+ catch (IOException ignore)
+ {
+ }
+ break;
+ case THROW_UNDECLARED :
+ try
+ {
+ osp.write("a");
+ fail();
+ }
+ catch (UndeclaredIOException ignore)
+ {
+ }
+ catch (IOException expected)
+ {
+ fail();
+ }
+ break;
+ case IGNORE :
+ try
+ {
+ osp.write("a");
+ }
+ catch (UndeclaredIOException ignore)
+ {
+ fail();
+ }
+ catch (IOException expected)
+ {
+ fail();
+ }
+ break;
+ }
+ }
+
+ public void flush(IOFailureFlow mode)
+ {
+ switch (mode)
+ {
+ case RETHROW :
+ try
+ {
+ osp.flush();
+ fail();
+ }
+ catch (IOException ignore)
+ {
+ }
+ break;
+ case THROW_UNDECLARED :
+ try
+ {
+ osp.flush();
+ fail();
+ }
+ catch (UndeclaredIOException ignore)
+ {
+ }
+ catch (IOException e)
+ {
+ fail();
+ }
+ break;
+ case IGNORE :
+ try
+ {
+ osp.flush();
+ }
+ catch (IOException e)
+ {
+ fail();
+ }
+ break;
+ }
+ }
+
+ public void close(IOFailureFlow mode)
+ {
+ switch (mode)
+ {
+ case RETHROW :
+ try
+ {
+ osp.close();
+ fail();
+ }
+ catch (IOException ignore)
+ {
+ }
+ break;
+ case THROW_UNDECLARED :
+ try
+ {
+ osp.close();
+ fail();
+ }
+ catch (UndeclaredIOException ignore)
+ {
+ }
+ catch (IOException e)
+ {
+ fail();
+ }
+ break;
+ case IGNORE :
+ try
+ {
+ osp.close();
+ }
+ catch (IOException e)
+ {
+ fail();
+ }
+ break;
+ }
+ }
+
+ public TestOutputStream assertEmpty()
+ {
+ Assert.assertTrue(operations.isEmpty());
+ return this;
+ }
+
+ public TestOutputStream assertOperation(int operation)
+ {
+ if (operation != NOOP)
+ {
+ Assert.assertFalse(operations.isEmpty());
+ int actual = operations.removeFirst();
+ Assert.assertEquals(operation, actual);
+ }
+ return this;
+ }
+ }
+}
Copied:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestPrinter.java
(from rev 413,
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/PrinterTestCase.java)
===================================================================
---
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestPrinter.java
(rev 0)
+++
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestPrinter.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -0,0 +1,135 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+import junit.framework.TestCase;
+
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestPrinter extends TestCase
+{
+
+ private MyPrinter printer;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ printer = new MyPrinter();
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ printer = null;
+ }
+
+ public void testPrintNull()
+ {
+ printer.print(null);
+ assertEquals("null", printer.buffer.toString());
+ }
+
+ public void testPrint()
+ {
+ printer.print("foo");
+ assertEquals("foo", printer.buffer.toString());
+ }
+
+ public void testPrintlnNull()
+ {
+ printer.println(null);
+ assertEquals("null\n", printer.buffer.toString());
+ }
+
+ public void testPrintln()
+ {
+ printer.println("foo");
+ assertEquals("foo\n", printer.buffer.toString());
+ }
+
+ public void testPrintln2()
+ {
+ printer.println();
+ assertEquals("\n", printer.buffer.toString());
+ }
+
+ public void testWriteNull() throws IOException
+ {
+ try
+ {
+ printer.write((String)null);
+ fail();
+ }
+ catch (Exception ignore)
+ {
+ assertEquals("", printer.buffer.toString());
+ }
+ try
+ {
+ printer.write((String)null, 0, 10);
+ fail();
+ }
+ catch (Exception ignore)
+ {
+ assertEquals("", printer.buffer.toString());
+ }
+ try
+ {
+ printer.write((char[])null, 0, 10);
+ fail();
+ }
+ catch (Exception ignore)
+ {
+ assertEquals("", printer.buffer.toString());
+ }
+ try
+ {
+ printer.write((char[])null);
+ fail();
+ }
+ catch (Exception ignore)
+ {
+ assertEquals("", printer.buffer.toString());
+ }
+ }
+
+ private static class MyPrinter extends Printer
+ {
+
+ private StringBuffer buffer = new StringBuffer();
+
+ public void write(char[] cbuf, int off, int len) throws IOException
+ {
+ buffer.append(cbuf, off, len);
+ }
+
+ public void flush() throws IOException
+ {
+ }
+
+ public void close() throws IOException
+ {
+ }
+ }
+}
Copied:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestSafe.java
(from rev 413,
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/SafeTestCase.java)
===================================================================
---
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestSafe.java
(rev 0)
+++
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestSafe.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -0,0 +1,142 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestSafe extends TestCase
+{
+
+ public TestSafe()
+ {
+ }
+
+ public TestSafe(String s)
+ {
+ super(s);
+ }
+
+ public void testClose()
+ {
+ assertFalse(Safe.close(null));
+ assertTrue(Safe.close(new ByteArrayOutputStream()));
+ assertFalse(Safe.close(new Closeable()
+ {
+ public void close() throws IOException
+ {
+ throw new IOException();
+ }
+ }));
+ assertFalse(Safe.close(new Closeable()
+ {
+ public void close() throws IOException
+ {
+ throw new RuntimeException();
+ }
+ }));
+ final Error expectedError = new Error();
+ try
+ {
+ Safe.close(new Closeable()
+ {
+ public void close() throws IOException
+ {
+ throw expectedError;
+ }
+ });
+ fail();
+ }
+ catch (Error error)
+ {
+ assertSame(expectedError, error);
+ }
+ }
+
+ private static class Thrower
+ {
+
+ private final Throwable t;
+
+ private Thrower(Throwable t)
+ {
+ this.t = t;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (t instanceof Error)
+ {
+ throw ((Error)t);
+ }
+ if (t instanceof RuntimeException)
+ {
+ throw ((RuntimeException)t);
+ }
+ throw new AssertionFailedError();
+ }
+ }
+
+ public void testEquals()
+ {
+ Object o = new Object();
+ assertTrue(Safe.equals(o, o));
+ assertTrue(Safe.equals(null, null));
+ assertFalse(Safe.equals(new Object(), null));
+ assertFalse(Safe.equals(null, new Object()));
+ assertFalse(Safe.equals(new Object(), new Object()));
+ assertFalse(Safe.equals(new Thrower(new RuntimeException()), null));
+ assertFalse(Safe.equals(null, new Thrower(new RuntimeException())));
+ assertFalse(Safe.equals(new Object(), new Thrower(new RuntimeException())));
+ assertFalse(Safe.equals(new Thrower(new Error()), null));
+ assertFalse(Safe.equals(null, new Thrower(new Error())));
+ assertFalse(Safe.equals(new Object(), new Thrower(new Error())));
+ RuntimeException re = new RuntimeException();
+ Error er = new Error();
+ try
+ {
+ Safe.equals(new Thrower(er), new Object());
+ fail();
+ }
+ catch (Error e)
+ {
+ assertSame(er, e);
+ }
+ try
+ {
+ Safe.equals(new Thrower(re), new Object());
+ fail();
+ }
+ catch (RuntimeException e)
+ {
+ assertSame(re, e);
+ }
+ }
+
+}
Copied:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestTextEncoder.java
(from rev 413,
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TextEncoderTestCase.java)
===================================================================
---
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestTextEncoder.java
(rev 0)
+++
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TestTextEncoder.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -0,0 +1,88 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.commons.utils;
+
+import junit.framework.TestCase;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestTextEncoder extends TestCase
+{
+
+ public void testA() throws IOException
+ {
+ assertOK("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
+ /*
+ assertOK("<>&\"\\=+");
+
+ // Chinese
+ assertOK(new String(new char[]{0x4EAC, 0x4EC5, 0x5C3D, 0x5F84, 0x60CA, 0x740E,
0x7579, 0x7D27, 0x7ECF, 0x8B66, 0x8C28, 0x9CB8}));
+
+ // Extended Roman
+ assertOK(new String(new char[]{0xEA, 0xFC, 0xE2, 0xCC}));
+
+ // Russian
+ assertOK(new String(new char[]{0x0433, 0x043E, 0x0432, 0x043E, 0x0440,
0x044E}));
+
+ // Greek
+ assertOK(new String(new char[]{0x0391, 0x03C5, 0x034, 0x03BF, 0x03C5}));
+ */
+ }
+
+ private void assertOK(String s) throws IOException
+ {
+ TextEncoder encoder = CharsetTextEncoder.getUTF8();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ encoder.encode(s, 0, s.length(), baos);
+ baos.flush();
+ byte[] b1 = baos.toByteArray();
+
+ //
+ baos.reset();
+ OutputStreamWriter osw = new OutputStreamWriter(baos);
+ osw.write(s);
+ osw.close();
+ byte[] b2 = baos.toByteArray();
+
+ //
+ List<Byte> expected = toList(b2);
+ List<Byte> actual = toList(b1);
+ assertEquals(expected, actual);
+ }
+
+ private List<Byte> toList(byte[] bytes)
+ {
+ List<Byte> tmp = new ArrayList<Byte>(bytes.length);
+ for (byte aByte : bytes)
+ {
+ tmp.add(aByte);
+ }
+ return tmp;
+ }
+
+}
Deleted:
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TextEncoderTestCase.java
===================================================================
---
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TextEncoderTestCase.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/component/common/src/test/java/org/exoplatform/commons/utils/TextEncoderTestCase.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -1,88 +0,0 @@
-/**
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.exoplatform.commons.utils;
-
-import junit.framework.TestCase;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class TextEncoderTestCase extends TestCase
-{
-
- public void testA() throws IOException
- {
- assertOK("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- /*
- assertOK("<>&\"\\=+");
-
- // Chinese
- assertOK(new String(new char[]{0x4EAC, 0x4EC5, 0x5C3D, 0x5F84, 0x60CA, 0x740E,
0x7579, 0x7D27, 0x7ECF, 0x8B66, 0x8C28, 0x9CB8}));
-
- // Extended Roman
- assertOK(new String(new char[]{0xEA, 0xFC, 0xE2, 0xCC}));
-
- // Russian
- assertOK(new String(new char[]{0x0433, 0x043E, 0x0432, 0x043E, 0x0440,
0x044E}));
-
- // Greek
- assertOK(new String(new char[]{0x0391, 0x03C5, 0x034, 0x03BF, 0x03C5}));
- */
- }
-
- private void assertOK(String s) throws IOException
- {
- TextEncoder encoder = CharsetTextEncoder.getUTF8();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- encoder.encode(s, 0, s.length(), baos);
- baos.flush();
- byte[] b1 = baos.toByteArray();
-
- //
- baos.reset();
- OutputStreamWriter osw = new OutputStreamWriter(baos);
- osw.write(s);
- osw.close();
- byte[] b2 = baos.toByteArray();
-
- //
- List<Byte> expected = toList(b2);
- List<Byte> actual = toList(b1);
- assertEquals(expected, actual);
- }
-
- private List<Byte> toList(byte[] bytes)
- {
- List<Byte> tmp = new ArrayList<Byte>(bytes.length);
- for (byte aByte : bytes)
- {
- tmp.add(aByte);
- }
- return tmp;
- }
-
-}
Modified:
portal/branches/performance/component/scripting/src/main/java/org/exoplatform/commons/utils/PortalPrinter.java
===================================================================
---
portal/branches/performance/component/scripting/src/main/java/org/exoplatform/commons/utils/PortalPrinter.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/component/scripting/src/main/java/org/exoplatform/commons/utils/PortalPrinter.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -19,52 +19,36 @@
package org.exoplatform.commons.utils;
-import java.io.IOException;
import java.io.OutputStream;
+import java.nio.charset.Charset;
/**
+ * The portal printer convert char to bytes based on a charset encoder.
+ *
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
public class PortalPrinter extends OutputStreamPrinter
{
- public PortalPrinter(TextEncoder encoder, OutputStream out, boolean flushOnClose)
throws IllegalArgumentException
- {
- super(encoder, out, flushOnClose);
- }
+ /** The optimized encoder. */
+ private static final TextEncoder encoder = new CharsetTextEncoder(new
TableCharEncoder(CharsetCharEncoder.getUTF8()));
- public void println()
+ /** The charset used. */
+ private static final Charset charset = Charset.forName("UTF8");
+
+ public PortalPrinter(OutputStream out, boolean flushOnClose, int bufferSize) throws
IllegalArgumentException
{
- try
- {
- write('\n');
- }
- catch (IOException e)
- {
- }
+ super(encoder, out, flushOnClose, bufferSize);
}
- public void print(Object o)
+ /**
+ * Returns the charset used for the conversion
+ *
+ * @return the charset used
+ */
+ public Charset getCharset()
{
- try
- {
- if (o == null)
- {
- write("null");
- }
- else if (o instanceof Text)
- {
- ((Text)o).writeTo(this);
- }
- else
- {
- String s = (o == null) ? "null" : o.toString();
- write(s);
- }
- }
- catch (IOException e)
- {
- }
+ return charset;
}
}
Modified:
portal/branches/performance/webui/portal/src/main/java/org/exoplatform/portal/application/PortalRequestContext.java
===================================================================
---
portal/branches/performance/webui/portal/src/main/java/org/exoplatform/portal/application/PortalRequestContext.java 2009-10-25
11:55:22 UTC (rev 426)
+++
portal/branches/performance/webui/portal/src/main/java/org/exoplatform/portal/application/PortalRequestContext.java 2009-10-25
21:04:29 UTC (rev 427)
@@ -265,22 +265,13 @@
return request_.isUserInRole(roleUser);
}
- /** The optimized encoder. */
- private static final TextEncoder encoder =
- new CharsetTextEncoder(new TableCharEncoder(CharsetCharEncoder.getUTF8()));
-
final public Writer getWriter() throws Exception
{
if (writer_ == null)
{
+ PortalPrinter printer = new PortalPrinter(response_.getOutputStream(), true,
30000);
- // Setup a buffer size of 20K
- response_.setBufferSize(30000);
-
//
- PortalPrinter printer = new PortalPrinter(encoder, response_.getOutputStream(),
true);
-
- //
if (HtmlValidator.DEBUG_MODE)
{
writer_ = new WriterPrinter(new HtmlValidator(printer));