Skip to main content
summaryrefslogtreecommitdiffstats
blob: 545673c74995a0267dc45ab390d22947b30c17c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.objectivity;

import static org.junit.Assert.assertEquals;

import org.eclipse.emf.cdo.server.internal.objectivity.schema.ObjyArrayList;

import com.objy.as.app.Class_Object;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

/**
 * Class to be used for testing, all persistence related calls are overriden by local ones.
 * 
 * @author ibrahim
 */
class ObjyLocalArrayListLong extends ObjyArrayList<Long>
{
  public ObjyLocalArrayListLong(Class_Object classObject)
  {
    super(classObject);
  }

  ArrayList<Long> arrayList = new ArrayList<Long>();

  long arraySize = 0;

  @Override
  protected void setValue(long index, Long newValue)
  {
    arrayList.set((int)index, newValue);
  }

  @Override
  protected Long getValue(long index)
  {
    return arrayList.get((int)index);
  }

  @Override
  protected long cachedSize()
  {
    if (cacheSize == -1)
    {
      cacheSize = arraySize;
    }
    return cacheSize;
  }

  @Override
  protected void update()
  {
    // do nothing.
  }

  @Override
  protected long getVArraySize()
  {
    return arrayList.size();
  }

  @Override
  protected void saveSize()
  {
    arraySize = cacheSize;
    cacheSize = -1;
  }

  @Override
  protected void grow(int item)
  {
    for (int i = 0; i < Math.max(item + 10, 10); i++)
    {
      arrayList.add(new Long(0));
    }
  }

  @Override
  public long size()
  {
    return arraySize;
  }
}

public class ObjyArrayListTest
{
  ObjyLocalArrayListLong arrayListLong;

  int numItems = 10;

  @Before
  public void setUp()
  {
    arrayListLong = new ObjyLocalArrayListLong(null);
    // fill the array.
    for (int i = 0; i < numItems; i++)
    {
      arrayListLong.add(new Long(i));
    }
  }

  @Test
  public void moveItems()
  {
    // move elemnt 7 to spot 2.
    arrayListLong.move(2, 7);
    assertEquals(numItems, arrayListLong.size());
    assertEquals(new Long(7), arrayListLong.get(2));
  }

  @Test
  public void removeLastItem()
  {
    //
    arrayListLong.remove((int)arrayListLong.size());
    assertEquals((numItems - 1), arrayListLong.size());
  }

  @Test
  public void removeMiddleItem()
  {
    //
    arrayListLong.remove(5);
    assertEquals((numItems - 1), arrayListLong.size());
  }

  @Test(expected = IndexOutOfBoundsException.class)
  public void empty()
  {
    new ArrayList<Object>().get(0);
  }

  @After
  public void tearDown()
  {
  }
}

Back to the top