diff options
Diffstat (limited to 'tests_mgrapp/src')
10 files changed, 985 insertions, 0 deletions
diff --git a/tests_mgrapp/src/com/google/codereview/TrashTestCase.java b/tests_mgrapp/src/com/google/codereview/TrashTestCase.java new file mode 100644 index 0000000000..c374b1ab49 --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/TrashTestCase.java @@ -0,0 +1,59 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview; + +import junit.framework.TestCase; + +import java.io.File; + +/** + * JUnit TestCase with support for creating a temporary directory. + */ +public abstract class TrashTestCase extends TestCase { + protected File tempRoot; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + tempRoot = File.createTempFile("codereview", "test"); + tempRoot.delete(); + tempRoot.mkdir(); + } + + @Override + protected void tearDown() throws Exception { + if (tempRoot != null) { + rm(tempRoot); + } + super.tearDown(); + } + + protected static void rm(final File dir) { + final File[] list = dir.listFiles(); + for (int i = 0; list != null && i < list.length; i++) { + final File f = list[i]; + if (f.getName().equals(".") || f.getName().equals("..")) { + continue; + } + if (f.isDirectory()) { + rm(f); + } else { + f.delete(); + } + } + dir.delete(); + } +} diff --git a/tests_mgrapp/src/com/google/codereview/manager/RepositoryCacheTest.java b/tests_mgrapp/src/com/google/codereview/manager/RepositoryCacheTest.java new file mode 100644 index 0000000000..8bf541affb --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/manager/RepositoryCacheTest.java @@ -0,0 +1,84 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.manager; + +import com.google.codereview.TrashTestCase; + +import org.spearce.jgit.lib.Repository; + +import java.io.File; + +public class RepositoryCacheTest extends TrashTestCase { + public void testCreateCache() { + final RepositoryCache rc = new RepositoryCache(tempRoot); + } + + public void testLookupInvalidNames() { + final RepositoryCache rc = new RepositoryCache(tempRoot); + assertInvalidRepository(rc, ""); + assertInvalidRepository(rc, "^"); + assertInvalidRepository(rc, "."); + assertInvalidRepository(rc, ".."); + assertInvalidRepository(rc, "../foo"); + assertInvalidRepository(rc, "/foo"); + assertInvalidRepository(rc, "/foo/bar"); + assertInvalidRepository(rc, "bar/../foo"); + assertInvalidRepository(rc, "bar\\..\\foo"); + } + + private void assertInvalidRepository(final RepositoryCache rc, final String n) { + try { + rc.get(n); + fail("Cache accepted name " + n); + } catch (InvalidRepositoryException err) { + assertEquals(n, err.getMessage()); + } + } + + public void testLookupNotCreatedRepository() { + final String name = "test.git"; + final RepositoryCache rc = new RepositoryCache(tempRoot); + try { + rc.get(name); + } catch (InvalidRepositoryException err) { + assertEquals(name, err.getMessage()); + } + } + + public void testLookupExistingEmptyRepository() throws Exception { + final RepositoryCache rc = new RepositoryCache(tempRoot); + + // Create after the cache is built, to test creation-on-the-fly. + // + final String[] names = {"test.git", "foo/bar/test.git"}; + final File[] gitdir = new File[names.length]; + for (int i = 0; i < names.length; i++) { + gitdir[i] = new File(tempRoot, names[i]); + final Repository r = new Repository(gitdir[i]); + r.create(); + assertTrue(gitdir[i].isDirectory()); + } + + final Repository[] cached = new Repository[names.length]; + for (int i = 0; i < names.length; i++) { + cached[i] = rc.get(names[i]); + assertNotNull(cached[i]); + assertEquals(gitdir[i], cached[i].getDirectory()); + } + for (int i = 0; i < names.length; i++) { + assertSame(cached[i], rc.get(names[i])); + } + } +} diff --git a/tests_mgrapp/src/com/google/codereview/manager/unpack/DiffReaderTest.java b/tests_mgrapp/src/com/google/codereview/manager/unpack/DiffReaderTest.java new file mode 100644 index 0000000000..43f9141e48 --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/manager/unpack/DiffReaderTest.java @@ -0,0 +1,193 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.manager.unpack; + +import com.google.codereview.TrashTestCase; +import com.google.codereview.internal.UploadPatchsetFile.UploadPatchsetFileRequest.StatusType; + +import org.spearce.jgit.lib.Commit; +import org.spearce.jgit.lib.ObjectId; +import org.spearce.jgit.lib.ObjectWriter; +import org.spearce.jgit.lib.PersonIdent; +import org.spearce.jgit.lib.Repository; +import org.spearce.jgit.lib.Tree; +import org.spearce.jgit.revwalk.RevCommit; +import org.spearce.jgit.revwalk.RevWalk; + +import java.io.File; +import java.io.IOException; + +public class DiffReaderTest extends TrashTestCase { + private Repository db; + private ObjectWriter writer; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + db = new Repository(new File(tempRoot, ".git")); + db.create(); + writer = new ObjectWriter(db); + } + + public void testAddOneTextFile_RootCommit() throws Exception { + final Tree top = new Tree(db); + final ObjectId blobId = blob("a\nb\n"); + top.addFile("foo").setId(blobId); + final RevCommit c = commit(top); + final DiffReader dr = new DiffReader(db, c); + + final FileDiff foo = dr.next(); + assertNotNull(foo); + assertEquals("foo", foo.getFilename()); + assertSame(StatusType.ADD, foo.getStatus()); + assertEquals(ObjectId.zeroId(), foo.getBaseId()); + assertEquals("diff --git a/foo b/foo\n" + "new file mode 100644\n" + + "index " + ObjectId.zeroId().name() + ".." + blobId.name() + "\n" + + "--- /dev/null\n" + "+++ b/foo\n" + "@@ -0,0 +1,2 @@\n" + "+a\n" + + "+b\n", foo.getPatch()); + + assertNull(dr.next()); + dr.close(); + } + + public void testAddOneTextFile_ExistingTree() throws Exception { + final Tree top = new Tree(db); + final RevCommit parent = commit(top); + final ObjectId blobId = blob("a\nb\n"); + top.addFile("foo").setId(blobId); + final RevCommit c = commit(top, parent); + final DiffReader dr = new DiffReader(db, c); + + final FileDiff foo = dr.next(); + assertNotNull(foo); + assertEquals("foo", foo.getFilename()); + assertSame(StatusType.ADD, foo.getStatus()); + assertEquals(ObjectId.zeroId(), foo.getBaseId()); + assertEquals("diff --git a/foo b/foo\n" + "new file mode 100644\n" + + "index " + ObjectId.zeroId().name() + ".." + blobId.name() + "\n" + + "--- /dev/null\n" + "+++ b/foo\n" + "@@ -0,0 +1,2 @@\n" + "+a\n" + + "+b\n", foo.getPatch()); + + assertNull(dr.next()); + dr.close(); + } + + public void testDeleteOneTextFile() throws Exception { + final Tree top = new Tree(db); + final ObjectId blobId = blob("a\nb\n"); + top.addFile("foo").setId(blobId); + final RevCommit parent = commit(top); + top.findBlobMember("foo").delete(); + final RevCommit c = commit(top, parent); + final DiffReader dr = new DiffReader(db, c); + + final FileDiff foo = dr.next(); + assertNotNull(foo); + assertEquals("foo", foo.getFilename()); + assertSame(StatusType.DELETE, foo.getStatus()); + assertEquals(blobId, foo.getBaseId()); + assertEquals("diff --git a/foo b/foo\n" + "deleted file mode 100644\n" + + "index " + blobId.name() + ".." + ObjectId.zeroId().name() + "\n" + + "--- a/foo\n" + "+++ /dev/null\n" + "@@ -1,2 +0,0 @@\n" + "-a\n" + + "-b\n", foo.getPatch()); + + assertNull(dr.next()); + dr.close(); + } + + public void testModifyTwoTextFiles() throws Exception { + final Tree top = new Tree(db); + final ObjectId barBaseId = blob("a\nc\n"); + top.addFile("bar").setId(barBaseId); + final ObjectId fooBaseId = blob("a\nb\n"); + top.addFile("foo").setId(fooBaseId); + + final RevCommit parent = commit(top); + final ObjectId barNewId = blob("a\nd\nc\n"); + top.findBlobMember("bar").setId(barNewId); + final ObjectId fooNewId = blob("a\nc\nb\n"); + top.findBlobMember("foo").setId(fooNewId); + + final RevCommit c = commit(top, parent); + final DiffReader dr = new DiffReader(db, c); + + final FileDiff bar = dr.next(); + assertNotNull(bar); + assertEquals("bar", bar.getFilename()); + assertSame(StatusType.MODIFY, bar.getStatus()); + assertEquals(barBaseId, bar.getBaseId()); + assertEquals("diff --git a/bar b/bar\n" + "index " + barBaseId.name() + + ".." + barNewId.name() + " 100644\n" + "--- a/bar\n" + "+++ b/bar\n" + + "@@ -1,2 +1,3 @@\n" + " a\n" + "+d\n" + " c\n", bar.getPatch()); + + final FileDiff foo = dr.next(); + assertNotNull(foo); + assertEquals("foo", foo.getFilename()); + assertSame(StatusType.MODIFY, foo.getStatus()); + assertEquals(fooBaseId, foo.getBaseId()); + assertEquals("diff --git a/foo b/foo\n" + "index " + fooBaseId.name() + + ".." + fooNewId.name() + " 100644\n" + "--- a/foo\n" + "+++ b/foo\n" + + "@@ -1,2 +1,3 @@\n" + " a\n" + "+c\n" + " b\n", foo.getPatch()); + + assertNull(dr.next()); + dr.close(); + } + + public void testAddBinaryFile() throws Exception { + final Tree top = new Tree(db); + final ObjectId blobId = blob("\0\1\2\0\1\2\0\1\2"); + top.addFile("foo").setId(blobId); + final RevCommit c = commit(top); + final DiffReader dr = new DiffReader(db, c); + + final FileDiff foo = dr.next(); + assertNotNull(foo); + assertEquals("foo", foo.getFilename()); + assertSame(StatusType.ADD, foo.getStatus()); + assertEquals(ObjectId.zeroId(), foo.getBaseId()); + assertEquals("diff --git a/foo b/foo\n" + "new file mode 100644\n" + + "index " + ObjectId.zeroId().name() + ".." + blobId.name() + "\n" + + "Binary files /dev/null and b/foo differ\n", foo.getPatch()); + + assertNull(dr.next()); + dr.close(); + } + + private ObjectId blob(final String content) throws IOException { + return writer.writeBlob(content.getBytes("UTF-8")); + } + + private RevCommit commit(final Tree top, final RevCommit... parents) + throws IOException { + final Commit c = new Commit(db); + c.setTreeId(writer.writeTree(top)); + final ObjectId parentIds[] = new ObjectId[parents.length]; + for (int i = 0; i < parents.length; i++) { + parentIds[i] = parents[i].getId(); + } + c.setParentIds(parentIds); + c.setAuthor(new PersonIdent("A U Thor <a@example.com> 1 +0000")); + c.setCommitter(c.getAuthor()); + c.setMessage(""); + c.setCommitId(writer.writeCommit(c)); + final RevWalk rw = new RevWalk(db); + final RevCommit r = rw.parseCommit(c.getCommitId()); + for (final RevCommit p : r.getParents()) { + rw.parse(p); + } + return r; + } +} diff --git a/tests_mgrapp/src/com/google/codereview/manager/unpack/FileDiffTest.java b/tests_mgrapp/src/com/google/codereview/manager/unpack/FileDiffTest.java new file mode 100644 index 0000000000..6e866eafd7 --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/manager/unpack/FileDiffTest.java @@ -0,0 +1,88 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.manager.unpack; + +import com.google.codereview.internal.UploadPatchsetFile.UploadPatchsetFileRequest.StatusType; + +import junit.framework.TestCase; + +import org.spearce.jgit.lib.ObjectId; + +import java.io.UnsupportedEncodingException; + +public class FileDiffTest extends TestCase { + public void testConstructor() { + final FileDiff fd = new FileDiff(); + assertNull(fd.getBaseId()); + assertNull(fd.getFilename()); + assertSame(StatusType.MODIFY, fd.getStatus()); + assertEquals("", fd.getPatch()); + } + + public void testBaseId() { + final ObjectId id1 = + ObjectId.fromString("fc5ac44497e0548c32506b9c584248fc49bb9f97"); + final ObjectId id2 = + ObjectId.fromString("8abf2492d8c5228192a3cba5528e47b3a4bb87e0"); + final FileDiff fd = new FileDiff(); + fd.setBaseId(id1); + assertSame(id1, fd.getBaseId()); + fd.setBaseId(id2); + assertSame(id2, fd.getBaseId()); + } + + public void testFilename() { + final FileDiff fd = new FileDiff(); + final String name1 = "foo"; + final String name2 = "foo/bar/baz"; + fd.setFilename(name1); + assertSame(name1, fd.getFilename()); + fd.setFilename(name2); + assertSame(name2, fd.getFilename()); + } + + public void testStatus() { + final FileDiff fd = new FileDiff(); + fd.setStatus(StatusType.ADD); + assertSame(StatusType.ADD, fd.getStatus()); + fd.setStatus(StatusType.MODIFY); + assertSame(StatusType.MODIFY, fd.getStatus()); + fd.setStatus(StatusType.DELETE); + assertSame(StatusType.DELETE, fd.getStatus()); + } + + public void testPatchBody() { + final FileDiff fd = new FileDiff(); + final String n1 = "diff --git a/foo b/foo"; + final String n2 = "--- a/foo"; + final String n3 = "+++ b/foo"; + final String n4 = "@@ -20,7 + 20,7 @@"; + + fd.appendLine(toBytes(n1)); + fd.appendLine(toBytes(n2)); + fd.appendLine(toBytes(n3)); + fd.appendLine(toBytes(n4)); + assertEquals(n1 + "\n" + n2 + "\n" + n3 + "\n" + n4 + "\n", fd.getPatch()); + } + + private static byte[] toBytes(final String s) { + final String enc = "UTF-8"; + try { + return s.getBytes(enc); + } catch (UnsupportedEncodingException uee) { + throw new RuntimeException("No " + enc + " support?", uee); + } + } +} diff --git a/tests_mgrapp/src/com/google/codereview/manager/unpack/ReceivedBundleUnpackerTest.java b/tests_mgrapp/src/com/google/codereview/manager/unpack/ReceivedBundleUnpackerTest.java new file mode 100644 index 0000000000..cad463e7f9 --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/manager/unpack/ReceivedBundleUnpackerTest.java @@ -0,0 +1,205 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.manager.unpack; + +import com.google.codereview.TrashTestCase; +import com.google.codereview.internal.NextReceivedBundle.NextReceivedBundleRequest; +import com.google.codereview.internal.NextReceivedBundle.NextReceivedBundleResponse; +import com.google.codereview.internal.UpdateReceivedBundle.UpdateReceivedBundleRequest; +import com.google.codereview.internal.UpdateReceivedBundle.UpdateReceivedBundleResponse; +import com.google.codereview.manager.Backend; +import com.google.codereview.manager.RepositoryCache; +import com.google.codereview.rpc.MockRpcChannel; +import com.google.protobuf.ByteString; +import com.google.protobuf.Message; +import com.google.protobuf.RpcCallback; +import com.google.protobuf.RpcChannel; +import com.google.protobuf.RpcController; +import com.google.protobuf.Descriptors.MethodDescriptor; + +import org.spearce.jgit.lib.Repository; + +import java.io.File; + +public class ReceivedBundleUnpackerTest extends TrashTestCase { + private MockRpcChannel rpc; + private RepositoryCache repoCache; + private Backend server; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + rpc = new MockRpcChannel(); + repoCache = new RepositoryCache(tempRoot); + server = new Backend(repoCache, rpc, null, null); + new Repository(new File(tempRoot, "foo.git")).create(); + } + + public void testNextReceivedBundle_EmptyQueue() throws Exception { + final ReceivedBundleUnpacker rbu = newRBU(); + rpc.add(new RpcChannel() { + public void callMethod(final MethodDescriptor method, + final RpcController controller, final Message request, + final Message responsePrototype, final RpcCallback<Message> done) { + assertEquals("NextReceivedBundle", method.getName()); + assertSame(NextReceivedBundleRequest.getDefaultInstance(), request); + + final NextReceivedBundleResponse.Builder r = + NextReceivedBundleResponse.newBuilder(); + r.setStatusCode(NextReceivedBundleResponse.CodeType.QUEUE_EMPTY); + done.run(r.build()); + } + }); + rbu.run(); + rpc.assertAllCallsMade(); + } + + public void testNextReceivedBundle_GetBundle() throws Exception { + final String bundleKey = "bundle-key-abcd123efg"; + final NextReceivedBundleResponse nrb[] = new NextReceivedBundleResponse[1]; + final UpdateReceivedBundleRequest urbr[] = + new UpdateReceivedBundleRequest[1]; + + final ReceivedBundleUnpacker rbu = + new ReceivedBundleUnpacker(server) { + @Override + protected UpdateReceivedBundleRequest unpackImpl( + final NextReceivedBundleResponse rsp) { + assertNotNull("unpackImpl only after bundle available", nrb[0]); + assertSame(nrb[0], rsp); + + final UpdateReceivedBundleRequest.Builder r = + UpdateReceivedBundleRequest.newBuilder(); + r.setBundleKey(bundleKey); + r.setStatusCode(UpdateReceivedBundleRequest.CodeType.UNPACKED_OK); + urbr[0] = r.build(); + return urbr[0]; + } + }; + + rpc.add(new RpcChannel() { + public void callMethod(final MethodDescriptor method, + final RpcController controller, final Message request, + final Message responsePrototype, final RpcCallback<Message> done) { + assertEquals("NextReceivedBundle", method.getName()); + assertSame(NextReceivedBundleRequest.getDefaultInstance(), request); + + final NextReceivedBundleResponse.Builder r = + NextReceivedBundleResponse.newBuilder(); + r.setStatusCode(NextReceivedBundleResponse.CodeType.BUNDLE_AVAILABLE); + r.setBundleKey(bundleKey); + r.setBundleData(ByteString.EMPTY); + r.setDestProject("foo.git"); + r.setDestProjectKey("project:foo.git"); + r.setDestBranchKey("branch:refs/heads/master"); + r.setOwner("author@example.com"); + nrb[0] = r.build(); + done.run(nrb[0]); + } + }); + + rpc.add(new RpcChannel() { + public void callMethod(final MethodDescriptor method, + final RpcController controller, final Message request, + final Message responsePrototype, final RpcCallback<Message> done) { + assertEquals("UpdateReceivedBundle", method.getName()); + assertSame(urbr[0], request); + + final UpdateReceivedBundleResponse.Builder r = + UpdateReceivedBundleResponse.newBuilder(); + r.setStatusCode(UpdateReceivedBundleResponse.CodeType.UPDATED); + done.run(r.build()); + } + }); + + rpc.add(new RpcChannel() { + public void callMethod(final MethodDescriptor method, + final RpcController controller, final Message request, + final Message responsePrototype, final RpcCallback<Message> done) { + assertEquals("NextReceivedBundle", method.getName()); + assertSame(NextReceivedBundleRequest.getDefaultInstance(), request); + + final NextReceivedBundleResponse.Builder r = + NextReceivedBundleResponse.newBuilder(); + r.setStatusCode(NextReceivedBundleResponse.CodeType.QUEUE_EMPTY); + done.run(r.build()); + } + }); + + rbu.run(); + assertNotNull(nrb[0]); + assertNotNull(urbr[0]); + rpc.assertAllCallsMade(); + } + + public void testNextReceivedBundle_RpcFailure() throws Exception { + final ReceivedBundleUnpacker rbu = newRBU(); + rpc.add(new RpcChannel() { + public void callMethod(final MethodDescriptor method, + final RpcController controller, final Message request, + final Message responsePrototype, final RpcCallback<Message> done) { + assertEquals("NextReceivedBundle", method.getName()); + controller.setFailed("mock failure"); + } + }); + rbu.run(); + rpc.assertAllCallsMade(); + } + + public void testNextReceivedBundle_RuntimeException() throws Exception { + final String msg = "test-a-message-win-a-prize"; + final ReceivedBundleUnpacker rbu = newRBU(); + rpc.add(new RpcChannel() { + public void callMethod(final MethodDescriptor method, + final RpcController controller, final Message request, + final Message responsePrototype, final RpcCallback<Message> done) { + assertEquals("NextReceivedBundle", method.getName()); + throw new RuntimeException(msg); + } + }); + try { + rbu.run(); + fail("Unpacker did not rethrow an unexpected runtime exception"); + } catch (RuntimeException re) { + assertEquals(msg, re.getMessage()); + } + rpc.assertAllCallsMade(); + } + + public void testNextReceivedBundle_RuntimeError() throws Exception { + final String msg = "test-a-message-win-a-prize"; + final ReceivedBundleUnpacker rbu = newRBU(); + rpc.add(new RpcChannel() { + public void callMethod(final MethodDescriptor method, + final RpcController controller, final Message request, + final Message responsePrototype, final RpcCallback<Message> done) { + assertEquals("NextReceivedBundle", method.getName()); + throw new OutOfMemoryError(msg); + } + }); + try { + rbu.run(); + fail("Unpacker did not rethrow an unexpected OutOfMemoryError"); + } catch (OutOfMemoryError re) { + assertEquals(msg, re.getMessage()); + } + rpc.assertAllCallsMade(); + } + + private ReceivedBundleUnpacker newRBU() { + return new ReceivedBundleUnpacker(server); + } +} diff --git a/tests_mgrapp/src/com/google/codereview/manager/unpack/RecordInputStreamTest.java b/tests_mgrapp/src/com/google/codereview/manager/unpack/RecordInputStreamTest.java new file mode 100644 index 0000000000..c848af2df4 --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/manager/unpack/RecordInputStreamTest.java @@ -0,0 +1,135 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.manager.unpack; + +import junit.framework.TestCase; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Arrays; + +public class RecordInputStreamTest extends TestCase { + public void testEmptyStream() throws IOException { + final RecordInputStream in = b(""); + assertEquals(-1, in.read()); + assertEquals(-1, in.read()); + assertEquals(-1, in.read(new byte[8], 0, 8)); + assertNull(in.readRecord('\n')); + in.close(); + } + + public void testReadOneByte() throws IOException { + final String exp = "abc"; + final RecordInputStream in = b(exp); + assertEquals(exp.charAt(0), in.read()); + assertEquals(exp.charAt(1), in.read()); + assertEquals(exp.charAt(2), in.read()); + assertEquals(-1, in.read()); + in.close(); + } + + public void testReadBlockOffset0() throws IOException { + final String exp = "abc"; + final RecordInputStream in = b(exp); + final byte[] act = new byte[exp.length()]; + assertEquals(act.length, in.read(act, 0, act.length)); + assertEquals(exp.charAt(0), act[0]); + assertEquals(exp.charAt(1), act[1]); + assertEquals(exp.charAt(2), act[2]); + assertEquals(-1, in.read(act, 0, act.length)); + in.close(); + } + + public void testReadBlockOffset1() throws IOException { + final String exp = "abc"; + final RecordInputStream in = b(exp); + assertEquals(exp.charAt(0), in.read()); + final byte[] act = new byte[exp.length() - 1]; + assertEquals(act.length, in.read(act, 0, act.length)); + assertEquals(exp.charAt(1), act[0]); + assertEquals(exp.charAt(2), act[1]); + assertEquals(-1, in.read(act, 0, act.length)); + in.close(); + } + + public void testReadRecord_SeparatorOnly() throws IOException { + final String rec1 = "foo"; + final String rec2 = "bar"; + final char sep = '\0'; + final RecordInputStream in = b(rec1 + sep + rec2); + assertTrue(Arrays.equals(toBytes(rec1), in.readRecord(sep))); + assertTrue(Arrays.equals(toBytes(rec2), in.readRecord(sep))); + assertNull(in.readRecord(sep)); + in.close(); + } + + public void testReadRecord_Terminated() throws IOException { + final String rec1 = "foo"; + final String rec2 = "bar"; + final char sep = '\0'; + final RecordInputStream in = b(rec1 + sep + rec2 + sep); + assertTrue(Arrays.equals(toBytes(rec1), in.readRecord(sep))); + assertTrue(Arrays.equals(toBytes(rec2), in.readRecord(sep))); + assertNull(in.readRecord(sep)); + in.close(); + } + + public void testReadRecord_EmptyRecords() throws IOException { + final char sep = '\0'; + final RecordInputStream in = b("" + sep + "" + sep); + assertTrue(Arrays.equals(new byte[0], in.readRecord(sep))); + assertTrue(Arrays.equals(new byte[0], in.readRecord(sep))); + assertNull(in.readRecord(sep)); + in.close(); + } + + public void testReadRecord_PartialRecordInBuffer() throws IOException { + final int huge = 16 * 1024; + final StringBuilder temp = new StringBuilder(huge); + for (int i = 0; i < huge; i++) { + temp.append('x'); + } + final char sep = '\n'; + final String ts = temp.toString(); + final RecordInputStream in = b(ts + "1" + sep + ts + "2"); + assertEquals(ts + "1", toString(in.readRecord(sep))); + assertEquals(ts + "2", toString(in.readRecord(sep))); + assertNull(in.readRecord(sep)); + in.close(); + } + + private static RecordInputStream b(final String s) { + return new RecordInputStream(new ByteArrayInputStream(toBytes(s))); + } + + private static byte[] toBytes(final String s) { + final String enc = "UTF-8"; + try { + return s.getBytes(enc); + } catch (UnsupportedEncodingException uee) { + throw new RuntimeException("No " + enc + " support?", uee); + } + } + + private static String toString(final byte[] b) { + final String enc = "UTF-8"; + try { + return new String(b, 0, b.length, enc); + } catch (UnsupportedEncodingException uee) { + throw new RuntimeException("No " + enc + " support?", uee); + } + } +} diff --git a/tests_mgrapp/src/com/google/codereview/rpc/MessageRequestEntityTest.java b/tests_mgrapp/src/com/google/codereview/rpc/MessageRequestEntityTest.java new file mode 100644 index 0000000000..adf5995dc3 --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/rpc/MessageRequestEntityTest.java @@ -0,0 +1,61 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.rpc; + +import com.google.codereview.internal.SubmitChange.SubmitChangeResponse; +import com.google.protobuf.Message; + +import junit.framework.TestCase; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.zip.DeflaterOutputStream; + +public class MessageRequestEntityTest extends TestCase { + public void testCompressedEntity() throws Exception { + final Message msg = buildMessage(); + final byte[] bin = compress(msg); + final String contentType = + "application/x-google-protobuf" + + "; name=codereview.internal.SubmitChangeResponse" + + "; compress=deflate"; + + final MessageRequestEntity mre = new MessageRequestEntity(msg); + assertTrue(mre.isRepeatable()); + assertEquals(bin.length, mre.getContentLength()); + assertEquals(contentType, mre.getContentType()); + + for (int i = 0; i < 5; i++) { + final ByteArrayOutputStream compressed = new ByteArrayOutputStream(); + mre.writeRequest(compressed); + assertTrue(Arrays.equals(bin, compressed.toByteArray())); + } + } + + private static Message buildMessage() { + final SubmitChangeResponse.Builder r = SubmitChangeResponse.newBuilder(); + r.setStatusCode(SubmitChangeResponse.CodeType.PATCHSET_EXISTS); + return r.build(); + } + + private static byte[] compress(final Message m) throws IOException { + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + final DeflaterOutputStream dos = new DeflaterOutputStream(out); + m.writeTo(dos); + dos.close(); + return out.toByteArray(); + } +} diff --git a/tests_mgrapp/src/com/google/codereview/rpc/MockRpcChannel.java b/tests_mgrapp/src/com/google/codereview/rpc/MockRpcChannel.java new file mode 100644 index 0000000000..4a1f1a49ff --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/rpc/MockRpcChannel.java @@ -0,0 +1,48 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.rpc; + +import com.google.protobuf.Message; +import com.google.protobuf.RpcCallback; +import com.google.protobuf.RpcChannel; +import com.google.protobuf.RpcController; +import com.google.protobuf.Descriptors.MethodDescriptor; + +import junit.framework.TestCase; + +import java.util.LinkedList; + +public class MockRpcChannel implements RpcChannel { + private LinkedList<RpcChannel> calls = new LinkedList<RpcChannel>(); + + public void add(final RpcChannel c) { + calls.add(c); + } + + public void callMethod(final MethodDescriptor method, + final RpcController controller, final Message request, + final Message responsePrototype, final RpcCallback<Message> done) { + if (calls.isEmpty()) { + TestCase.fail("Incorrect call for " + method.getFullName()); + } + + final RpcChannel c = calls.removeFirst(); + c.callMethod(method, controller, request, responsePrototype, done); + } + + public void assertAllCallsMade() { + TestCase.assertTrue(calls.isEmpty()); + } +} diff --git a/tests_mgrapp/src/com/google/codereview/rpc/SimpleControllerTest.java b/tests_mgrapp/src/com/google/codereview/rpc/SimpleControllerTest.java new file mode 100644 index 0000000000..9496f45738 --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/rpc/SimpleControllerTest.java @@ -0,0 +1,68 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.rpc; + +import com.google.protobuf.RpcCallback; + +import junit.framework.TestCase; + +public class SimpleControllerTest extends TestCase { + public void testDefaultConstructor() { + final SimpleController sc = new SimpleController(); + assertNull(sc.errorText()); + assertFalse(sc.failed()); + assertFalse(sc.isCanceled()); + } + + public void testNotifyCancelUnsupported() { + try { + new SimpleController().notifyOnCancel(new RpcCallback<Object>() { + public void run(Object parameter) { + fail("Callback invoked during notifyOnCancel setup"); + } + }); + fail("notifyOnCancel accepted a callback"); + } catch (UnsupportedOperationException e) { + // Pass + } + } + + public void testStartCancelUnsupported() { + try { + new SimpleController().startCancel(); + fail("startCancel did not fail"); + } catch (UnsupportedOperationException e) { + // Pass + } + } + + public void testSetFailed() { + final String reason = "we failed, yes we did"; + final SimpleController sc = new SimpleController(); + sc.setFailed(reason); + assertTrue(sc.failed()); + assertSame(reason, sc.errorText()); + } + + public void testResetClearedFailure() { + final String reason = "we failed, yes we did"; + final SimpleController sc = new SimpleController(); + sc.setFailed(reason); + sc.reset(); + assertNull(sc.errorText()); + assertFalse(sc.failed()); + assertFalse(sc.isCanceled()); + } +} diff --git a/tests_mgrapp/src/com/google/codereview/util/MutableBooleanTest.java b/tests_mgrapp/src/com/google/codereview/util/MutableBooleanTest.java new file mode 100644 index 0000000000..7d4d6469f9 --- /dev/null +++ b/tests_mgrapp/src/com/google/codereview/util/MutableBooleanTest.java @@ -0,0 +1,44 @@ +// Copyright 2008 Google Inc. +// +// Licensed 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 com.google.codereview.util; + +import junit.framework.TestCase; + + +public class MutableBooleanTest extends TestCase { + public void testCreateDefault() { + final MutableBoolean b = new MutableBoolean(); + assertFalse(b.value); + } + + public void testCreateTrue() { + final MutableBoolean b = new MutableBoolean(true); + assertTrue(b.value); + } + + public void testCreateFalse() { + final MutableBoolean b = new MutableBoolean(false); + assertFalse(b.value); + } + + public void testMutable() { + final MutableBoolean b = new MutableBoolean(); + assertFalse(b.value); + b.value = true; + assertTrue(b.value); + b.value = false; + assertFalse(b.value); + } +} |