View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.revwalk;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertSame;
50  import static org.junit.Assert.assertTrue;
51  
52  import java.util.Arrays;
53  import java.util.Iterator;
54  
55  import org.junit.Test;
56  
57  public class RevFlagSetTest extends RevWalkTestCase {
58  	@Test
59  	public void testEmpty() {
60  		final RevFlagSet set = new RevFlagSet();
61  		assertEquals(0, set.mask);
62  		assertEquals(0, set.size());
63  		assertNotNull(set.iterator());
64  		assertFalse(set.iterator().hasNext());
65  	}
66  
67  	@Test
68  	public void testAddOne() {
69  		final String flagName = "flag";
70  		final RevFlag flag = rw.newFlag(flagName);
71  		assertTrue(0 != flag.mask);
72  		assertSame(flagName, flag.name);
73  
74  		final RevFlagSet set = new RevFlagSet();
75  		assertTrue(set.add(flag));
76  		assertFalse(set.add(flag));
77  		assertEquals(flag.mask, set.mask);
78  		assertEquals(1, set.size());
79  		final Iterator<RevFlag> i = set.iterator();
80  		assertTrue(i.hasNext());
81  		assertSame(flag, i.next());
82  		assertFalse(i.hasNext());
83  	}
84  
85  	@Test
86  	public void testAddTwo() {
87  		final RevFlag flag1 = rw.newFlag("flag_1");
88  		final RevFlag flag2 = rw.newFlag("flag_2");
89  		assertEquals(0, (flag1.mask & flag2.mask));
90  
91  		final RevFlagSet set = new RevFlagSet();
92  		assertTrue(set.add(flag1));
93  		assertTrue(set.add(flag2));
94  		assertEquals(flag1.mask | flag2.mask, set.mask);
95  		assertEquals(2, set.size());
96  	}
97  
98  	@Test
99  	public void testContainsAll() {
100 		final RevFlag flag1 = rw.newFlag("flag_1");
101 		final RevFlag flag2 = rw.newFlag("flag_2");
102 		final RevFlagSet set1 = new RevFlagSet();
103 		assertTrue(set1.add(flag1));
104 		assertTrue(set1.add(flag2));
105 
106 		assertTrue(set1.containsAll(Arrays
107 				.asList(new RevFlag[] { flag1, flag2 })));
108 
109 		final RevFlagSet set2 = new RevFlagSet();
110 		set2.add(rw.newFlag("flag_3"));
111 		assertFalse(set1.containsAll(set2));
112 	}
113 
114 	@Test
115 	public void testEquals() {
116 		final RevFlag flag1 = rw.newFlag("flag_1");
117 		final RevFlag flag2 = rw.newFlag("flag_2");
118 		final RevFlagSet set = new RevFlagSet();
119 		assertTrue(set.add(flag1));
120 		assertTrue(set.add(flag2));
121 
122 		assertEquals(set, new RevFlagSet(set));
123 		assertTrue(new RevFlagSet(Arrays.asList(new RevFlag[] { flag1, flag2 }))
124 				.equals(set));
125 	}
126 
127 	@Test
128 	public void testRemove() {
129 		final RevFlag flag1 = rw.newFlag("flag_1");
130 		final RevFlag flag2 = rw.newFlag("flag_2");
131 		final RevFlagSet set = new RevFlagSet();
132 		assertTrue(set.add(flag1));
133 		assertTrue(set.add(flag2));
134 
135 		assertTrue(set.remove(flag1));
136 		assertFalse(set.remove(flag1));
137 		assertEquals(flag2.mask, set.mask);
138 		assertFalse(set.contains(flag1));
139 	}
140 
141 	@Test
142 	public void testContains() {
143 		final RevFlag flag1 = rw.newFlag("flag_1");
144 		final RevFlag flag2 = rw.newFlag("flag_2");
145 		final RevFlagSet set = new RevFlagSet();
146 		set.add(flag1);
147 		assertTrue(set.contains(flag1));
148 		assertFalse(set.contains(flag2));
149 	}
150 }