001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.server.rpc;
017:
018: import com.google.gwt.user.client.rpc.SerializationException;
019:
020: import junit.framework.TestCase;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.UnsupportedEncodingException;
026: import java.text.ParseException;
027:
028: /**
029: * Test the {@link SerializationPolicyLoader} class.
030: */
031: public class SerializationPolicyLoaderTest extends TestCase {
032: // allowed by the policy
033: static class A {
034: }
035:
036: // not allowed by the policy
037: static class B {
038: }
039:
040: // missing the instantiable attribute
041: private static String POLICY_FILE_MISSING_FIELD = A.class.getName();
042:
043: private static String POLICY_FILE_TRIGGERS_CLASSNOTFOUND = "C,false";
044:
045: private static String VALID_POLICY_FILE_CONTENTS = A.class
046: .getName()
047: + ", true";
048:
049: public static InputStream getInputStreamFromString(String content)
050: throws UnsupportedEncodingException {
051: return new ByteArrayInputStream(
052: content
053: .getBytes(SerializationPolicyLoader.SERIALIZATION_POLICY_FILE_ENCODING));
054: }
055:
056: public void testPolicyFileMissingField() throws IOException,
057: ClassNotFoundException {
058: InputStream is = getInputStreamFromString(POLICY_FILE_MISSING_FIELD);
059: try {
060: SerializationPolicyLoader.loadFromStream(is);
061: fail("Expected ParseException");
062: } catch (ParseException e) {
063: // expected to get here
064: }
065: }
066:
067: public void testPolicyFileTriggersClassNotFound()
068: throws IOException, ParseException {
069: InputStream is = getInputStreamFromString(POLICY_FILE_TRIGGERS_CLASSNOTFOUND);
070: try {
071: SerializationPolicyLoader.loadFromStream(is);
072: fail("Expected ClassNotFoundException");
073: } catch (ClassNotFoundException e) {
074: // expected to get here
075: }
076: }
077:
078: /**
079: * Test that a valid policy file will allow the types in the policy to be used
080: * and reject those that are not.
081: *
082: * @throws ClassNotFoundException
083: * @throws ParseException
084: */
085: public void testValidSerializationPolicy() throws IOException,
086: SerializationException, ParseException,
087: ClassNotFoundException {
088:
089: InputStream is = getInputStreamFromString(VALID_POLICY_FILE_CONTENTS);
090: SerializationPolicy sp = SerializationPolicyLoader
091: .loadFromStream(is);
092: assertTrue(sp.shouldDeserializeFields(A.class));
093: assertTrue(sp.shouldSerializeFields(A.class));
094:
095: assertFalse(sp.shouldDeserializeFields(B.class));
096: assertFalse(sp.shouldSerializeFields(B.class));
097:
098: sp.validateDeserialize(A.class);
099: sp.validateSerialize(A.class);
100:
101: try {
102: sp.validateDeserialize(B.class);
103: fail("Expected SerializationException");
104: } catch (SerializationException ex) {
105: // should get here
106: }
107:
108: try {
109: sp.validateSerialize(B.class);
110: fail("Expected SerializationException");
111: } catch (SerializationException ex) {
112: // should get here
113: }
114: }
115: }
|