Repository: incubator-wave Updated Branches: refs/heads/master 19783bca9 -> d35211be5
http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/d35211be/wave/src/test/java/org/waveprotocol/wave/model/testing/RandomProviderImpl.java ---------------------------------------------------------------------- diff --git a/wave/src/test/java/org/waveprotocol/wave/model/testing/RandomProviderImpl.java b/wave/src/test/java/org/waveprotocol/wave/model/testing/RandomProviderImpl.java new file mode 100644 index 0000000..d6a9c17 --- /dev/null +++ b/wave/src/test/java/org/waveprotocol/wave/model/testing/RandomProviderImpl.java @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.waveprotocol.wave.model.testing; + +import org.waveprotocol.wave.model.testing.RandomDocOpGenerator.RandomProvider; +import org.waveprotocol.wave.model.util.Preconditions; + +/** + * Implementation of RandomProvider for use in classes intended for use in + * GWT, which doesn't support java.util.Random. + * + * The implementation is simplistic and not well tested, so don't use it for + * any usage that depends on the quality of the generated pseudorandom numbers. + * + * The implementation is based on the recommendations in + * Knuth: The Art of Computer Programming, Volume 2, Section 3.6. + * + */ +public class RandomProviderImpl implements RandomProvider { + + public static RandomProviderImpl ofSeed(int seed) { + return new RandomProviderImpl(seed); + } + + private int next32; + + public RandomProviderImpl(int seed) { + next32 = seed; + } + + @Override + public int nextInt(int upperBound) { + Preconditions.checkArgument(upperBound > 0, "upperBound must be positive"); + + // 0x77DD9E95 is a random number from http://www.fourmilab.ch/hotbits/ + // satisfying 0x77DD9E95 % 8 == 5 + // TODO: check if this multiplier passes the spectral test and other tests in Knuth's book + next32 = (int) (0x77DD9E95L * (long) next32 + 1L); + // NOTE(2010/06/08): the casts above were necessary to work around a Gwt miscompilation + // problem, in Java a simpler expression works: next32 = 0x77DD9E95L * next32 + 1; + + // convert the signed 32 bit content into a floating point number + // between 0 (inclusive) and 1 (exclusive) + double d = (((double) next32) + 2147483648.0) / 4294967296.0; + + // truncate the multiplum of d and upperBound to get an integer + // between 0 (inclusive) and upperBound (exclusive) + return (int) (d * (double) upperBound); + } + + @Override + public boolean nextBoolean() { + return nextInt(2) != 0; + } +} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/d35211be/wave/src/test/java/org/waveprotocol/wave/model/testing/Response.java ---------------------------------------------------------------------- diff --git a/wave/src/test/java/org/waveprotocol/wave/model/testing/Response.java b/wave/src/test/java/org/waveprotocol/wave/model/testing/Response.java new file mode 100644 index 0000000..5efb790 --- /dev/null +++ b/wave/src/test/java/org/waveprotocol/wave/model/testing/Response.java @@ -0,0 +1,107 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.waveprotocol.wave.model.testing; + +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.util.Arrays; +import java.util.List; + +/** + * A simple answer for providing type-safe generic containers as return + * values to Mockito stubbing calls. + * + * Java doesn't resolve the wildcard generic here: + * <pre> + * interface Container { + * Collection<? extends Foo> getFoos(); + * } + * + * public void testFooContainer() { + * Container c = mock(Container.class); + * when(c.getFoos()).thenReturn(Arrays.asList(a, b)); + * } + * </pre> + * + * Instead, try: + * <pre> + * public void testFooContainer() { + * Container c = mock(Container.class); + * when(c.getFoos()).thenAnswer(Response.of(Arrays.asList(a, b))); + * } + * </pre> + * or + * <pre> + * public void testFooContainer() { + * Container c = mock(Container.class); + * when(c.getFoos()).thenAnswer(Response.ofList(a, b)); + * } + * </pre> + * + * Note that {@code Mockito.doReturn()} does work, but is unnatural. + * <pre> + * public void testFooContainer() { + * Container c = mock(Container.class); + * doReturn(Arrays.asList(a, b)).when(c.getFoos()); + * } + * </pre> + * + * @author ano...@google.com (Alex North) + */ +public final class Response { + /** + * Creates a response which returns a value. + * + * @param response the value to return + */ + public static <T> ResponseAnswer<T> of(T response) { + return new ResponseAnswer<T>(response); + } + + /** + * Creates a response which returns a list of values. + */ + @SuppressWarnings("unchecked") + public static <T> ResponseAnswer<List<T>> ofList(T... responses) { + return new ResponseAnswer<List<T>>(Arrays.asList(responses)); + } + + /** + * An answer which simply returns a response value. + * + * @param <T> type of the response + */ + public static final class ResponseAnswer<T> implements Answer<T> { + private final T response; + + ResponseAnswer(T response) { + this.response = response; + } + + @Override + public T answer(InvocationOnMock invocation) throws Throwable { + return response; + } + } + + private Response() { + } +} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/d35211be/wave/src/test/java/org/waveprotocol/wave/model/testing/TestOperations.java ---------------------------------------------------------------------- diff --git a/wave/src/test/java/org/waveprotocol/wave/model/testing/TestOperations.java b/wave/src/test/java/org/waveprotocol/wave/model/testing/TestOperations.java new file mode 100644 index 0000000..8e2bf29 --- /dev/null +++ b/wave/src/test/java/org/waveprotocol/wave/model/testing/TestOperations.java @@ -0,0 +1,87 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.waveprotocol.wave.model.testing; + +import org.waveprotocol.wave.model.document.operation.Attributes; +import org.waveprotocol.wave.model.document.operation.DocOp; +import org.waveprotocol.wave.model.document.operation.impl.AnnotationBoundaryMapImpl; +import org.waveprotocol.wave.model.document.operation.impl.AttributesImpl; +import org.waveprotocol.wave.model.document.operation.impl.AttributesUpdateImpl; +import org.waveprotocol.wave.model.document.operation.impl.DocOpBuffer; + +public class TestOperations { + + // The test case for the message-based implementation should also use this. + public static DocOp getBasicTestOp() { + DocOpBuffer b = new DocOpBuffer(); + + // The operation starts with characters/deleteCharacters of various lengths + // and case, mixed with some retains and nested element start/end with + // different mixes of attributes. + b.characters("hello"); + b.characters("z"); + b.retain(1); + b.deleteCharacters("ab"); + b.characters("world"); + b.retain(2); + b.deleteCharacters("cd"); + b.elementStart("a", Attributes.EMPTY_MAP); + b.characters("hEllo"); + b.elementStart("b", new AttributesImpl("a", "1")); + b.characters("world"); + b.elementStart("B", new AttributesImpl("A", "1", "b", "abc12")); + b.elementEnd(); + // A non-ASCII Unicode character. + b.characters("\u2603"); + b.elementEnd(); + b.elementEnd(); + b.deleteElementStart("a", new AttributesImpl("a", "2", "c", "")); + b.deleteCharacters("asdf"); + b.deleteElementEnd(); + + // Now some replaceAttributes with different size and case. + b.replaceAttributes(new AttributesImpl("a", "b"), new AttributesImpl("b", "c", "c", "d")); + b.replaceAttributes(Attributes.EMPTY_MAP, new AttributesImpl("Aa", "aA")); + b.replaceAttributes(new AttributesImpl("B", "A"), new AttributesImpl()); + // Try both a fresh empty AttributesImpl() instance and the preallocated + // EMPTY_MAP. + b.replaceAttributes(new AttributesImpl(), Attributes.EMPTY_MAP); + // Now we add similar cases for annotation boundaries. Since consecutive annotation + // boundaries would make the operation ill-formed, we interleave them with further + // updateAttributes tests. + b.annotationBoundary(AnnotationBoundaryMapImpl.builder().build()); + b.updateAttributes(new AttributesUpdateImpl()); + b.annotationBoundary(AnnotationBoundaryMapImpl.builder() + .updateValues("b", "XZ", "yz", "f-", null, null, + "g-", "a", null, "k-", "b", "", "r", "", "2") + .build()); + b.updateAttributes(new AttributesUpdateImpl("a", null, "1")); + b.annotationBoundary(AnnotationBoundaryMapImpl.builder() + .initializationEnd("b", "g-", "k-", "r") + .updateValues("e", "166", null, "f-", null, null) + .build()); + b.updateAttributes(new AttributesUpdateImpl("P", null, "", ":wq", "ZZ", null)); + b.annotationBoundary(AnnotationBoundaryMapImpl.builder() + .initializationEnd("e", "f-") + .build()); + + return b.finish(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/d35211be/wave/src/test/java/org/waveprotocol/wave/model/testing/WaveletDataFactory.java ---------------------------------------------------------------------- diff --git a/wave/src/test/java/org/waveprotocol/wave/model/testing/WaveletDataFactory.java b/wave/src/test/java/org/waveprotocol/wave/model/testing/WaveletDataFactory.java new file mode 100644 index 0000000..bf8ff93 --- /dev/null +++ b/wave/src/test/java/org/waveprotocol/wave/model/testing/WaveletDataFactory.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.waveprotocol.wave.model.testing; + + +import org.waveprotocol.wave.model.id.IdGenerator; +import org.waveprotocol.wave.model.id.WaveId; +import org.waveprotocol.wave.model.id.WaveletId; +import org.waveprotocol.wave.model.version.HashedVersion; +import org.waveprotocol.wave.model.wave.ParticipantId; +import org.waveprotocol.wave.model.wave.data.ObservableWaveletData; +import org.waveprotocol.wave.model.wave.data.WaveletData; +import org.waveprotocol.wave.model.wave.data.impl.EmptyWaveletSnapshot; + +/** + * Exposes any {@link ObservableWaveletData.Factory} as a {@link Factory}, by + * injecting suitable dependencies for testing. + * + */ +public final class WaveletDataFactory<T extends WaveletData> implements Factory<T> { + private final static WaveId WAVE_ID; + private final static WaveletId WAVELET_ID; + private static final ParticipantId PARTICIPANT_ID = new ParticipantId("f...@example.com"); + + static { + IdGenerator gen = FakeIdGenerator.create(); + WAVE_ID = gen.newWaveId(); + WAVELET_ID = gen.newConversationWaveletId(); + } + + private final WaveletData.Factory<T> factory; + + private WaveletDataFactory(WaveletData.Factory<T> factory) { + this.factory = factory; + } + + public static <T extends WaveletData> Factory<T> of(WaveletData.Factory<T> factory) { + return new WaveletDataFactory<T>(factory); + } + + @Override + public T create() { + return factory.create(new EmptyWaveletSnapshot(WAVE_ID, WAVELET_ID, PARTICIPANT_ID, + HashedVersion.unsigned(0), 0)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/d35211be/wave/src/test/resources/org/waveprotocol/wave/client/wavepanel/tests.gwt.xml ---------------------------------------------------------------------- diff --git a/wave/src/test/resources/org/waveprotocol/wave/client/wavepanel/tests.gwt.xml b/wave/src/test/resources/org/waveprotocol/wave/client/wavepanel/tests.gwt.xml index 866f890..a9bbc59 100644 --- a/wave/src/test/resources/org/waveprotocol/wave/client/wavepanel/tests.gwt.xml +++ b/wave/src/test/resources/org/waveprotocol/wave/client/wavepanel/tests.gwt.xml @@ -22,7 +22,11 @@ <module> <inherits name="com.google.gwt.user.User"/> - + <inherits name="org.waveprotocol.wave.model.util.Util"/> + <inherits name="org.waveprotocol.wave.client.common.safehtml.SafeHtml"/> + <inherits name="org.waveprotocol.wave.client.uibuilder.UiBuilder"/> + <inherits name="org.waveprotocol.box.stat.Stat"/> + <source path=""/> </module>